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)
        pos (int): if specified the argument can also be found in position `pos` of the command line varargs

    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.repr2(ut.odict(zip(argstr_list, res_list)), nl=1)
        >>> 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.repr4(ut.odict(zip(argstr_list, res_list)))
        >>> result = result.replace('u\'', '\'')  # hack
        >>> print(result)

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, pos=None):
    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)
        pos (int): if specified the argument can also be found in position `pos` of the command line varargs

    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.repr2(ut.odict(zip(argstr_list, res_list)), nl=1)
        >>> 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.repr4(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 get_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,))
                            arg_after = argv[argx + 1]
                            if type_ is not None:
                                arg_after = util_type.try_cast(arg_after, type_)
                            elif smartcast:
                                arg_after = util_type.smart_cast2(arg_after)
                        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')
                        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:
                            if smartcast:
                                arg_after = util_type.smart_cast2(val_after)
                            else:
                                arg_after = 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 not was_specified and pos is not None:
        varargs = get_cmdline_varargs(argv)
        if len(varargs) > pos:
            arg_after = varargs[pos]
            assert type_ is not list, 'list not handled yet'
            if type_ is not None:
                arg_after = util_type.try_cast(arg_after, type_)
            elif smartcast:
                arg_after = util_type.smart_cast2(arg_after)

    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