Example #1
0
 def __overwrite_child_attr(self, name, attr):
     # FIXME: when setting string preference nodes to lists, it casts
     # the list to a string!
     if VERBOSE_PREF:
         print('[pref.__overwrite_child_attr]: %s.%s = %r' %
               (self._intern.name, name, attr))
     # get child node to "overwrite"
     row = self._tree.child_names.index(name)
     child = self._tree.child_list[row]
     if isinstance(attr, Pref):
         # Do not break pointers when overwriting a Preference
         if issubclass(attr._intern.value, PrefNode):
             # Main Branch Logic
             for (key, val) in six.iteritems(attr):
                 child.__setattr__(key, val)
         else:
             self.__overwrite_child_attr(name, attr.value())
     else:  # Main Leaf Logic:
         #assert(not issubclass(child._intern.type, PrefNode), #(self.full_name() + ' Must be a leaf'))
         # Keep user-readonly map up to date with internals
         if isinstance(child._intern.value, PrefChoice):
             child.change_combo_val(attr)
         else:
             child_type = child._intern.get_type()
             if isinstance(attr, six.string_types) and issubclass(
                     child_type, six.string_types):
                 #import utool as ut
                 #ut.embed()
                 attr = child_type(attr)
             attr_type = type(attr)
             if attr is not None and child_type is not attr_type:
                 if util_arg.VERBOSE:
                     print('[pref] WARNING TYPE DIFFERENCE!')
                     print('[pref] * expected child_type = %r' %
                           (child_type, ))
                     print('[pref] * got attr_type = %r' % (attr_type, ))
                     print('[pref] * name = %r' % (name, ))
                     print('[pref] * attr = %r' % (attr, ))
                 attr = util_type.try_cast(attr, child_type, attr)
             child._intern.value = attr
         self.__dict__[name] = child.value()
Example #2
0
 def __overwrite_child_attr(self, name, attr):
     # FIXME: when setting string preference nodes to lists, it casts
     # the list to a string!
     if VERBOSE_PREF:
         print('[pref.__overwrite_child_attr]: %s.%s = %r' % (self._intern.name, name, attr))
     # get child node to "overwrite"
     row = self._tree.child_names.index(name)
     child = self._tree.child_list[row]
     if isinstance(attr, Pref):
         # Do not break pointers when overwriting a Preference
         if issubclass(attr._intern.value, PrefNode):
             # Main Branch Logic
             for (key, val) in six.iteritems(attr):
                 child.__setattr__(key, val)
         else:
             self.__overwrite_child_attr(name, attr.value())
     else:  # Main Leaf Logic:
         #assert(not issubclass(child._intern.type, PrefNode), #(self.full_name() + ' Must be a leaf'))
         # Keep user-readonly map up to date with internals
         if isinstance(child._intern.value, PrefChoice):
             child.change_combo_val(attr)
         else:
             child_type = child._intern.get_type()
             if isinstance(attr, six.string_types) and issubclass(child_type, six.string_types):
                 #import utool as ut
                 #ut.embed()
                 attr = child_type(attr)
             attr_type  = type(attr)
             if attr is not None and child_type is not attr_type:
                 if util_arg.VERBOSE:
                     print('[pref] WARNING TYPE DIFFERENCE!')
                     print('[pref] * expected child_type = %r' % (child_type,))
                     print('[pref] * got attr_type = %r' % (attr_type,))
                     print('[pref] * name = %r' % (name,))
                     print('[pref] * attr = %r' % (attr,))
                 attr = util_type.try_cast(attr, child_type, attr)
             child._intern.value = attr
         self.__dict__[name] = child.value()
Example #3
0
def get_argval(argstr_, type_=None, default=None, help_=None, smartcast=True,
               return_specified=None, argv=None, verbose=None,
               debug=None, return_was_specified=False):
    r""" Returns a value of an argument specified on the command line after some flag

    Args:
        argstr_ (str or tuple): string or tuple of strings denoting the command line values to parse
        type_ (None): type of the variable to parse (default = None)
        default (None): (default = None)
        help_ (None): help for this argument (not fully integrated) (default = None)
        smartcast (bool): tries to be smart about casting the parsed strings (default = True)
        return_specified (bool): (default = False)
        argv (None): override sys.argv with custom command line vector (default = None)

    TODO:
        depricate return_was_specified

    CommandLine:
        python -m utool.util_arg --test-get_argval
        python -m utool.util_arg --exec-get_argval:0
        python -m utool.util_arg --exec-get_argval:1
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest="holy grail"
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest="42"
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest=42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest 42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest', float)]])" --quest 42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval(('--nAssign'), int)]])" --nAssign 42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval(('--test'), str)]])" --test
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval(('--test'), str)]])" --test "foobar is good" --youbar ok

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_arg import *  # NOQA
        >>> import utool as ut
        >>> import sys
        >>> argv = ['--spam', 'eggs', '--quest=holy grail', '--ans=42', '--the-val=1,2,3']
        >>> # specify a list of args and kwargs to get_argval
        >>> argstr_kwargs_list = [
        >>>     ('--spam',                    dict(type_=str, default=None, argv=argv)),
        >>>     ('--quest',                   dict(type_=str, default=None, argv=argv)),
        >>>     (('--ans', '--foo'),          dict(type_=int, default=None, argv=argv)),
        >>>     (('--not-there', '--absent'), dict(argv=argv)),
        >>>     ('--the_val',                 dict(type_=list, argv=argv)),
        >>>     ('--the-val',                 dict(type_=list, argv=argv)),
        >>> ]
        >>> # Execute the command with for each of the test cases
        >>> res_list = []
        >>> argstr_list = ut.get_list_column(argstr_kwargs_list, 0)
        >>> for argstr_, kwargs in argstr_kwargs_list:
        >>>     res = get_argval(argstr_, **kwargs)
        >>>     res_list.append(res)
        >>> result = ut.dict_str(ut.odict(zip(argstr_list, res_list)))
        >>> result = result.replace('u\'', '\'')  # hack
        >>> print(result)
        {
            '--spam': 'eggs',
            '--quest': 'holy grail',
            ('--ans', '--foo'): 42,
            ('--not-there', '--absent'): None,
            '--the_val': [1, 2, 3],
            '--the-val': [1, 2, 3],
        }

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_arg import *  # NOQA
        >>> import utool as ut
        >>> import sys
        >>> argv = ['--slice1', '::', '--slice2=4:', '--slice3=::4', '--slice4', '[1,2,3,4]', '--slice5=3']
        >>> # specify a list of args and kwargs to get_argval
        >>> argstr_kwargs_list = [
        >>>     ('--slice1',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>>     ('--slice2',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>>     ('--slice3',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>>     ('--slice4',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>>     ('--slice5',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>> ]
        >>> # Execute the command with for each of the test cases
        >>> res_list = []
        >>> argstr_list = ut.get_list_column(argstr_kwargs_list, 0)
        >>> list1 = [1, 3, 5, 7, 9]
        >>> import numpy as np
        >>> list2 = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 1]])
        >>> for argstr_, kwargs in argstr_kwargs_list:
        >>>     res = get_argval(argstr_, **kwargs)
        >>>     print('---')
        >>>     print('res = %r' % (res,))
        >>>     print('list1[%r=%r] = %r' % (argstr_, res, ut.take(list1, res),))
        >>>     print('list2[%r=%r] = %r' % (argstr_, res, list2[res].tolist(),))
        >>>     res_list.append(res)
        >>> result = ut.dict_str(ut.odict(zip(argstr_list, res_list)))
        >>> result = result.replace('u\'', '\'')  # hack
        >>> print(result)

    """
    if verbose is None:
        pass
        # verbose = VERYVERBOSE

    if debug is None:
        debug = DEBUG
        # debug = VERYVERBOSE

    if argv is None:
        argv = sys.argv

    #verbose = 1

    if verbose:
        print('[get_argval] Searching Commandline for argstr_=%r' % (argstr_,))
        #print('[get_argval]  * type_ = %r' % (type_,))
        #print('[get_argval]  * default = %r' % (default,))
        #print('[get_argval]  * help_ = %r' % (help_,))
        #print('[get_argval]  * smartcast = %r' % (smartcast,))

    if return_specified is None:
        return_specified = return_was_specified

    #print(argstr_)
    was_specified = False
    arg_after = default
    if type_ is bool:
        arg_after = False if default is None else default
    try:
        # New for loop way (accounts for =)
        argstr_list = meta_util_iter.ensure_iterable(argstr_)
        # arg registration
        _register_arg(argstr_list, type_, default, help_)

        # expand out hypens
        EXPAND_HYPENS = True
        if EXPAND_HYPENS:
            argstr_list2 = []
            seen_ = set([])
            for argstr in argstr_list:
                if argstr not in seen_:
                    argstr_list2.append(argstr)
                    seen_.add(argstr)
                if argstr.startswith('--'):
                    num = 2
                elif argstr.startswith('-'):
                    num = 1
                else:
                    continue
                argstr2_0 = argstr[0:num] + argstr[num:].replace('_', '-')
                argstr2_1 = argstr[0:num] + argstr[num:].replace('-', '_')
                if argstr2_0 not  in seen_:
                    argstr_list2.append(argstr2_0)
                    seen_.add(argstr2_0)
                if argstr2_1 not  in seen_:
                    argstr_list2.append(argstr2_1)
                    seen_.add(argstr2_1)
            argstr_list = argstr_list2

        # Check environment variables for default as well as argv
        import os
        """
        set UTOOL_NOCNN=True
        export UTOOL_NOCNN True
        """
        #argv_orig = argv[:]
        for key, val in os.environ.items():
            key = key.upper()
            sentinal = 'UTOOL_'
            if key.startswith(sentinal):
                key = '--' + key[len(sentinal):].lower()
                new_argv = [key, val]
                if val.upper() in ['TRUE', 'FALSE', 'ON', 'OFF']:
                    # handled by argflag
                    continue
                argv = argv[:] + new_argv
                if debug:
                    print('argv.extend(new_argv=%r)' % (new_argv,))

        for argx, item in enumerate(argv):
            for argstr in argstr_list:
                if item == argstr:
                    if type_ is bool:
                        if debug:
                            print('[get_argval] ... argstr=%r' % (argstr,))
                            print('[get_argval] ... Found bool argx=%r' % (argx,))
                        arg_after = True
                        was_specified = True
                        break
                    if argx < len(argv):
                        if type_ is list:
                            # HACK FOR LIST. TODO INTEGRATE
                            if debug:
                                print('[get_argval] ... argstr=%r' % (argstr,))
                                print('[get_argval] ... Found noequal list argx=%r' % (argx,))
                            arg_after = parse_arglist_hack(argx, argv=argv)
                            if debug:
                                print('[get_argval] ... arg_after=%r' % (arg_after,))
                                print('argv=%r' % (argv,))
                            if smartcast:
                                arg_after = list(map(util_type.smart_cast2, arg_after))
                                if debug:
                                    print('[get_argval] ... smartcast arg_after=%r' % (arg_after,))
                        else:
                            if debug:
                                print('[get_argval] ... argstr=%r' % (argstr,))
                                print('[get_argval] ... Found type_=%r argx=%r' % (type_, argx,))
                            if type_ is None:
                                #arg_after = util_type.try_cast(argv[argx + 1], type_)
                                arg_after = util_type.smart_cast2(argv[argx + 1])
                            else:
                                arg_after = util_type.try_cast(argv[argx + 1], type_)
                        if was_specified:
                            print('WARNING: argstr=%r already specified' % (argstr,))
                        was_specified = True
                        break
                elif item.startswith(argstr + '='):
                    val_after = ''.join(item.split('=')[1:])
                    if type_ is list:
                        # HACK FOR LIST. TODO INTEGRATE
                        if verbose:
                            print('[get_argval] ... Found equal list')

                        #import IPython
                        #IPython.embed()
                        val_after_ = val_after.rstrip(']').lstrip('[')
                        if True:
                            # Hacker way to be less hacky about parsing lists
                            from utool import util_gridsearch
                            blocks = util_gridsearch.parse_nestings(val_after_)
                            sentinal = '##COM&&'
                            changed = [(block[0], block[1].replace(',', sentinal))
                                       if block[0] == 'nonNested' else block
                                       for block in blocks]
                            val_after2 = util_gridsearch.recombine_nestings(changed)
                            arg_after = val_after2.split(sentinal)
                        else:
                            arg_after = val_after_.split(',')
                        if smartcast:
                            arg_after = list(map(util_type.smart_cast2, arg_after))
                    else:
                        if type_ is None:
                            arg_after = util_type.smart_cast2(val_after)
                        else:
                            arg_after = util_type.try_cast(val_after, type_)
                            if not isinstance(type_, six.string_types) and issubclass(type_, six.string_types):
                                if arg_after == 'None':
                                    # hack
                                    arg_after = None
                    if was_specified:
                        print('WARNING: argstr=%r already specified' % (argstr,))
                    was_specified = True
                    break
    except Exception as ex:
        import utool as ut
        ut.printex(ex, 'problem in arg_val', keys=['type_'])
        if ut.SUPER_STRICT:
            raise
        pass
    if verbose:
        print('[get_argval] ... Parsed arg_after=%r, was_specified=%r' % (arg_after, was_specified))
    if return_specified:
        return arg_after, was_specified
    else:
        return arg_after
Example #4
0
def get_argval(argstr_, type_=None, default=None, help_=None, smartcast=True, return_was_specified=False):
    """ Returns a value of an argument specified on the command line after some flag

    Examples:
        >>> from utool.util_arg import *  # NOQA
        >>> import sys
        >>> sys.argv.extend(['--spam', 'eggs', '--quest=holy grail', '--ans=42'])
        >>> res1 = get_argval('--spam', type_=str, default=None)
        >>> res2 = get_argval('--quest', type_=str, default=None)
        >>> res3 = get_argval('--ans', type_=int, default=None)
        >>> result = ', '.join(map(str, (res1, res2, res3)))
        >>> print(result)
        eggs, holy grail, 42

    CommandLine:
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest="holy grail"
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest="42"
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest=42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest 42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest', float)]])" --quest 42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval(('--nAssign'), int)]])" --nAssign 42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval(('--test'), str)]])" --test
    """
    #print(argstr_)
    was_specified = False
    arg_after = default
    if type_ is bool:
        arg_after = False if default is None else default
    try:
        # New for loop way (accounts for =)
        argstr_list = meta_util_iter.ensure_iterable(argstr_)
        # arg registration
        _register_arg(argstr_list, type_, default, help_)

        for argx, item in enumerate(sys.argv):
            for argstr in argstr_list:
                if item == argstr:
                    if type_ is bool:
                        arg_after = True
                        was_specified = True
                        break
                    if argx < len(sys.argv):
                        if type_ is list:
                            # HACK FOR LIST. TODO INTEGRATE
                            arg_after = parse_arglist_hack(argx)
                            if smartcast:
                                arg_after = list(map(util_type.smart_cast2, arg_after))
                        else:
                            if type_ is None:
                                #arg_after = util_type.try_cast(sys.argv[argx + 1], type_)
                                arg_after = util_type.smart_cast2(sys.argv[argx + 1])
                            else:
                                arg_after = util_type.try_cast(sys.argv[argx + 1], type_)
                        was_specified = True
                        break
                elif item.startswith(argstr + '='):
                    val_after = ''.join(item.split('=')[1:])
                    if type_ is list:
                        #import utool as ut
                        #ut.embed()
                        # HACK FOR LIST. TODO INTEGRATE
                        val_after_ = val_after.rstrip(']').lstrip('[')
                        arg_after = val_after_.split(',')
                        if smartcast:
                            arg_after = list(map(util_type.smart_cast2, arg_after))
                    else:
                        if type_ is None:
                            arg_after = util_type.smart_cast2(val_after)
                        else:
                            arg_after = util_type.try_cast(val_after, type_)
                    was_specified = True
                    break
    except Exception as ex:
        import utool as ut
        ut.printex(ex, 'problem in arg_val')
        pass
    if return_was_specified:
        return arg_after, was_specified
    else:
        return arg_after
Example #5
0
def get_argval(argstr_,
               type_=None,
               default=None,
               help_=None,
               smartcast=True,
               return_specified=None,
               argv=None,
               verbose=None,
               debug=None,
               return_was_specified=False):
    r""" Returns a value of an argument specified on the command line after some flag

    Args:
        argstr_ (str or tuple): string or tuple of strings denoting the command line values to parse
        type_ (None): type of the variable to parse (default = None)
        default (None): (default = None)
        help_ (None): help for this argument (not fully integrated) (default = None)
        smartcast (bool): tries to be smart about casting the parsed strings (default = True)
        return_specified (bool): (default = False)
        argv (None): override sys.argv with custom command line vector (default = None)

    TODO:
        depricate return_was_specified

    CommandLine:
        python -m utool.util_arg --test-get_argval
        python -m utool.util_arg --exec-get_argval:0
        python -m utool.util_arg --exec-get_argval:1
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest="holy grail"
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest="42"
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest=42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest')]])" --quest 42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval('--quest', float)]])" --quest 42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval(('--nAssign'), int)]])" --nAssign 42
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval(('--test'), str)]])" --test
        python -c "import utool; print([(type(x), x) for x in [utool.get_argval(('--test'), str)]])" --test "foobar is good" --youbar ok

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_arg import *  # NOQA
        >>> import utool as ut
        >>> import sys
        >>> argv = ['--spam', 'eggs', '--quest=holy grail', '--ans=42', '--the-val=1,2,3']
        >>> # specify a list of args and kwargs to get_argval
        >>> argstr_kwargs_list = [
        >>>     ('--spam',                    dict(type_=str, default=None, argv=argv)),
        >>>     ('--quest',                   dict(type_=str, default=None, argv=argv)),
        >>>     (('--ans', '--foo'),          dict(type_=int, default=None, argv=argv)),
        >>>     (('--not-there', '--absent'), dict(argv=argv)),
        >>>     ('--the_val',                 dict(type_=list, argv=argv)),
        >>>     ('--the-val',                 dict(type_=list, argv=argv)),
        >>> ]
        >>> # Execute the command with for each of the test cases
        >>> res_list = []
        >>> argstr_list = ut.get_list_column(argstr_kwargs_list, 0)
        >>> for argstr_, kwargs in argstr_kwargs_list:
        >>>     res = get_argval(argstr_, **kwargs)
        >>>     res_list.append(res)
        >>> result = ut.dict_str(ut.odict(zip(argstr_list, res_list)))
        >>> result = result.replace('u\'', '\'')  # hack
        >>> print(result)
        {
            '--spam': 'eggs',
            '--quest': 'holy grail',
            ('--ans', '--foo'): 42,
            ('--not-there', '--absent'): None,
            '--the_val': [1, 2, 3],
            '--the-val': [1, 2, 3],
        }

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_arg import *  # NOQA
        >>> import utool as ut
        >>> import sys
        >>> argv = ['--slice1', '::', '--slice2=4:', '--slice3=::4', '--slice4', '[1,2,3,4]', '--slice5=3']
        >>> # specify a list of args and kwargs to get_argval
        >>> argstr_kwargs_list = [
        >>>     ('--slice1',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>>     ('--slice2',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>>     ('--slice3',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>>     ('--slice4',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>>     ('--slice5',            dict(type_='fuzzy_subset', default=None, argv=argv)),
        >>> ]
        >>> # Execute the command with for each of the test cases
        >>> res_list = []
        >>> argstr_list = ut.get_list_column(argstr_kwargs_list, 0)
        >>> list1 = [1, 3, 5, 7, 9]
        >>> import numpy as np
        >>> list2 = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 1]])
        >>> for argstr_, kwargs in argstr_kwargs_list:
        >>>     res = get_argval(argstr_, **kwargs)
        >>>     print('---')
        >>>     print('res = %r' % (res,))
        >>>     print('list1[%r=%r] = %r' % (argstr_, res, ut.take(list1, res),))
        >>>     print('list2[%r=%r] = %r' % (argstr_, res, list2[res].tolist(),))
        >>>     res_list.append(res)
        >>> result = ut.dict_str(ut.odict(zip(argstr_list, res_list)))
        >>> result = result.replace('u\'', '\'')  # hack
        >>> print(result)

    """
    if verbose is None:
        pass
        # verbose = VERYVERBOSE

    if debug is None:
        debug = DEBUG
        # debug = VERYVERBOSE

    if argv is None:
        argv = sys.argv

    #verbose = 1

    if verbose:
        print('[get_argval] Searching Commandline for argstr_=%r' %
              (argstr_, ))
        #print('[get_argval]  * type_ = %r' % (type_,))
        #print('[get_argval]  * default = %r' % (default,))
        #print('[get_argval]  * help_ = %r' % (help_,))
        #print('[get_argval]  * smartcast = %r' % (smartcast,))

    if return_specified is None:
        return_specified = return_was_specified

    #print(argstr_)
    was_specified = False
    arg_after = default
    if type_ is bool:
        arg_after = False if default is None else default
    try:
        # New for loop way (accounts for =)
        argstr_list = meta_util_iter.ensure_iterable(argstr_)
        # arg registration
        _register_arg(argstr_list, type_, default, help_)

        # expand out hypens
        EXPAND_HYPENS = True
        if EXPAND_HYPENS:
            argstr_list2 = []
            seen_ = set([])
            for argstr in argstr_list:
                if argstr not in seen_:
                    argstr_list2.append(argstr)
                    seen_.add(argstr)
                if argstr.startswith('--'):
                    num = 2
                elif argstr.startswith('-'):
                    num = 1
                else:
                    continue
                argstr2_0 = argstr[0:num] + argstr[num:].replace('_', '-')
                argstr2_1 = argstr[0:num] + argstr[num:].replace('-', '_')
                if argstr2_0 not in seen_:
                    argstr_list2.append(argstr2_0)
                    seen_.add(argstr2_0)
                if argstr2_1 not in seen_:
                    argstr_list2.append(argstr2_1)
                    seen_.add(argstr2_1)
            argstr_list = argstr_list2

        # Check environment variables for default as well as argv
        import os
        """
        set UTOOL_NOCNN=True
        export UTOOL_NOCNN True
        """
        #argv_orig = argv[:]
        for key, val in os.environ.items():
            key = key.upper()
            sentinal = 'UTOOL_'
            if key.startswith(sentinal):
                key = '--' + key[len(sentinal):].lower()
                new_argv = [key, val]
                if val.upper() in ['TRUE', 'FALSE', 'ON', 'OFF']:
                    # handled by argflag
                    continue
                argv = argv[:] + new_argv
                if debug:
                    print('argv.extend(new_argv=%r)' % (new_argv, ))

        for argx, item in enumerate(argv):
            for argstr in argstr_list:
                if item == argstr:
                    if type_ is bool:
                        if debug:
                            print('[get_argval] ... argstr=%r' % (argstr, ))
                            print('[get_argval] ... Found bool argx=%r' %
                                  (argx, ))
                        arg_after = True
                        was_specified = True
                        break
                    if argx < len(argv):
                        if type_ is list:
                            # HACK FOR LIST. TODO INTEGRATE
                            if debug:
                                print('[get_argval] ... argstr=%r' %
                                      (argstr, ))
                                print(
                                    '[get_argval] ... Found noequal list argx=%r'
                                    % (argx, ))
                            arg_after = parse_arglist_hack(argx, argv=argv)
                            if debug:
                                print('[get_argval] ... arg_after=%r' %
                                      (arg_after, ))
                                print('argv=%r' % (argv, ))
                            if smartcast:
                                arg_after = list(
                                    map(util_type.smart_cast2, arg_after))
                                if debug:
                                    print(
                                        '[get_argval] ... smartcast arg_after=%r'
                                        % (arg_after, ))
                        else:
                            if debug:
                                print('[get_argval] ... argstr=%r' %
                                      (argstr, ))
                                print(
                                    '[get_argval] ... Found type_=%r argx=%r' %
                                    (
                                        type_,
                                        argx,
                                    ))
                            if type_ is None:
                                #arg_after = util_type.try_cast(argv[argx + 1], type_)
                                arg_after = util_type.smart_cast2(argv[argx +
                                                                       1])
                            else:
                                arg_after = util_type.try_cast(
                                    argv[argx + 1], type_)
                        if was_specified:
                            print('WARNING: argstr=%r already specified' %
                                  (argstr, ))
                        was_specified = True
                        break
                elif item.startswith(argstr + '='):
                    val_after = ''.join(item.split('=')[1:])
                    if type_ is list:
                        # HACK FOR LIST. TODO INTEGRATE
                        if verbose:
                            print('[get_argval] ... Found equal list')

                        #import IPython
                        #IPython.embed()
                        val_after_ = val_after.rstrip(']').lstrip('[')
                        if True:
                            # Hacker way to be less hacky about parsing lists
                            from utool import util_gridsearch
                            blocks = util_gridsearch.parse_nestings(val_after_)
                            sentinal = '##COM&&'
                            changed = [(block[0],
                                        block[1].replace(',', sentinal))
                                       if block[0] == 'nonNested' else block
                                       for block in blocks]
                            val_after2 = util_gridsearch.recombine_nestings(
                                changed)
                            arg_after = val_after2.split(sentinal)
                        else:
                            arg_after = val_after_.split(',')
                        if smartcast:
                            arg_after = list(
                                map(util_type.smart_cast2, arg_after))
                    else:
                        if type_ is None:
                            arg_after = util_type.smart_cast2(val_after)
                        else:
                            arg_after = util_type.try_cast(val_after, type_)
                            if not isinstance(type_,
                                              six.string_types) and issubclass(
                                                  type_, six.string_types):
                                if arg_after == 'None':
                                    # hack
                                    arg_after = None
                    if was_specified:
                        print('WARNING: argstr=%r already specified' %
                              (argstr, ))
                    was_specified = True
                    break
    except Exception as ex:
        import utool as ut
        ut.printex(ex, 'problem in arg_val', keys=['type_'])
        if ut.SUPER_STRICT:
            raise
        pass
    if verbose:
        print('[get_argval] ... Parsed arg_after=%r, was_specified=%r' %
              (arg_after, was_specified))
    if return_specified:
        return arg_after, was_specified
    else:
        return arg_after