def argparse_dict(default_dict_, lbl=None, verbose=None,
                  only_specified=False, force_keys={}, type_hint=None,
                  alias_dict={}):
    r"""
    Gets values for a dict based on the command line

    Args:
        default_dict_ (?):
        only_specified (bool): if True only returns keys that are specified on commandline. no defaults.

    Returns:
        dict_: dict_ -  a dictionary

    CommandLine:
        python -m utool.util_arg --test-argparse_dict
        python -m utool.util_arg --test-argparse_dict --foo=3
        python -m utool.util_arg --test-argparse_dict --flag1
        python -m utool.util_arg --test-argparse_dict --flag2
        python -m utool.util_arg --test-argparse_dict --noflag2
        python -m utool.util_arg --test-argparse_dict --thresh=43
        python -m utool.util_arg --test-argparse_dict --bins=-10
        python -m utool.util_arg --test-argparse_dict --bins=-10 --only-specified --helpx

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_arg import *  # NOQA
        >>> import utool as ut
        >>> # build test data
        >>> default_dict_ = {
        ...    'bins': 8,
        ...    'foo': None,
        ...    'flag1': False,
        ...    'flag2': True,
        ...    'max': 0.2,
        ...    'neg': -5,
        ...    'thresh': -5.333,
        ... }
        >>> # execute function
        >>> only_specified = ut.get_argflag('--only-specified')
        >>> dict_ = argparse_dict(default_dict_, only_specified=only_specified)
        >>> # verify results
        >>> result = ut.repr4(dict_, sorted_=True)
        >>> print(result)
    """
    if verbose is None:
        verbose = VERBOSE_ARGPARSE
    def make_argstrs(key, prefix_list):
        for prefix in prefix_list:
            yield prefix + key
            yield prefix + key.replace('-', '_')
            yield prefix + key.replace('_', '-')

    def get_dictkey_cmdline_val(key, default, type_hint):
        # see if the user gave a commandline value for this dict key
        defaulttype_ = None if default is None else type(default)
        if type_hint is None:
            type_ = defaulttype_
        elif isinstance(type_hint, dict):
            type_ = type_hint.get(key, defaulttype_)
        elif isinstance(type_hint, type):
            type_ = type_hint
        else:
            raise NotImplementedError('Unknown type of type_hint=%r' % (type_hint,))
        was_specified = False
        if isinstance(default, bool):
            val = default
            if default is True:
                falsekeys = list(set(make_argstrs(key, ['--no', '--no-'])))
                notval, was_specified = get_argflag(falsekeys, return_specified=True)
                val = not notval
                if not was_specified:
                    truekeys = list(set(make_argstrs(key, ['--'])))
                    val_, was_specified = get_argflag(truekeys, return_specified=True)
                    if was_specified:
                        val = val_
            elif default is False:
                truekeys = list(set(make_argstrs(key, ['--'])))
                val, was_specified = get_argflag(truekeys, return_specified=True)
        else:
            argtup = list(set(make_argstrs(key, ['--'])))
            #if key == 'species':
            #    import utool as ut
            #    ut.embed()
            val, was_specified = get_argval(argtup, type_=type_,
                                            default=default,
                                            return_specified=True)
        return val, was_specified

    dict_  = {}
    num_specified = 0
    for key, default in six.iteritems(default_dict_):
        val, was_specified = get_dictkey_cmdline_val(key, default, type_hint)
        if not was_specified:
            alias_keys = meta_util_iter.ensure_iterable(alias_dict.get(key, []))
            for alias_key in alias_keys:
                val, was_specified = get_dictkey_cmdline_val(alias_key, default,
                                                             type_hint)
                if was_specified:
                    break
        if VERBOSE_ARGPARSE:
            if was_specified:
                num_specified += 1
                print('[argparse_dict] Specified key=%r, val=%r' % (key, val))
        #if key == 'foo':
        #    import utool as ut
        #    ut.embed()
        if not only_specified or was_specified or key in force_keys:
            dict_[key] = val
    if VERBOSE_ARGPARSE:
        print('[argparse_dict] num_specified = %r' % (num_specified,))
        print('[argparse_dict] force_keys = %r' % (force_keys,))
    #dict_ = {key: get_dictkey_cmdline_val(key, default) for key, default in
    #six.iteritems(default_dict_)}

    if verbose:
        for key in dict_:
            if dict_[key] != default_dict_[key]:
                print('[argparse_dict] GOT ARGUMENT: cfgdict[%r] = %r' % (key, dict_[key]))

    do_helpx = get_argflag('--helpx',
                           help_='Specifies that argparse_dict should print help and quit')

    if get_argflag(('--help', '--help2')) or do_helpx:
        import utool as ut
        print('COMMAND LINE IS ACCEPTING THESE PARAMS WITH DEFAULTS:')
        if lbl is not None:
            print(lbl)
        #print(ut.align(ut.repr4(dict_, sorted_=True), ':'))
        print(ut.align(ut.repr4(default_dict_, sorted_=True), ':'))
        if do_helpx:
            sys.exit(1)
    return dict_
def argparse_dict ( default_dict_ , lbl = None , verbose = None , only_specified = False , force_keys = { } , type_hint = None , alias_dict = { } ) : if verbose is None : verbose = VERBOSE_ARGPARSE def make_argstrs ( key , prefix_list ) : for prefix in prefix_list : yield prefix + key yield prefix + key . replace ( '-' , '_' ) yield prefix + key . replace ( '_' , '-' ) def get_dictkey_cmdline_val ( key , default , type_hint ) : defaulttype_ = None if default is None else type ( default ) if type_hint is None : type_ = defaulttype_ elif isinstance ( type_hint , dict ) : type_ = type_hint . get ( key , defaulttype_ ) elif isinstance ( type_hint , type ) : type_ = type_hint else : raise NotImplementedError ( 'Unknown type of type_hint=%r' % ( type_hint , ) ) was_specified = False if isinstance ( default , bool ) : val = default if default is True : falsekeys = list ( set ( make_argstrs ( key , [ '--no' , '--no-' ] ) ) ) notval , was_specified = get_argflag ( falsekeys , return_specified = True ) val = not notval if not was_specified : truekeys = list ( set ( make_argstrs ( key , [ '--' ] ) ) ) val_ , was_specified = get_argflag ( truekeys , return_specified = True ) if was_specified : val = val_ elif default is False : truekeys = list ( set ( make_argstrs ( key , [ '--' ] ) ) ) val , was_specified = get_argflag ( truekeys , return_specified = True ) else : argtup = list ( set ( make_argstrs ( key , [ '--' ] ) ) ) val , was_specified = get_argval ( argtup , type_ = type_ , default = default , return_specified = True ) return val , was_specified dict_ = { } num_specified = 0 for key , default in six . iteritems ( default_dict_ ) : val , was_specified = get_dictkey_cmdline_val ( key , default , type_hint ) if not was_specified : alias_keys = meta_util_iter . ensure_iterable ( alias_dict . get ( key , [ ] ) ) for alias_key in alias_keys : val , was_specified = get_dictkey_cmdline_val ( alias_key , default , type_hint ) if was_specified : break if VERBOSE_ARGPARSE : if was_specified : num_specified += 1 print ( '[argparse_dict] Specified key=%r, val=%r' % ( key , val ) ) if not only_specified or was_specified or key in force_keys : dict_ [ key ] = val if VERBOSE_ARGPARSE : print ( '[argparse_dict] num_specified = %r' % ( num_specified , ) ) print ( '[argparse_dict] force_keys = %r' % ( force_keys , ) ) if verbose : for key in dict_ : if dict_ [ key ] != default_dict_ [ key ] : print ( '[argparse_dict] GOT ARGUMENT: cfgdict[%r] = %r' % ( key , dict_ [ key ] ) ) do_helpx = get_argflag ( '--helpx' , help_ = 'Specifies that argparse_dict should print help and quit' ) if get_argflag ( ( '--help' , '--help2' ) ) or do_helpx : import utool as ut print ( 'COMMAND LINE IS ACCEPTING THESE PARAMS WITH DEFAULTS:' ) if lbl is not None : print ( lbl ) print ( ut . align ( ut . repr4 ( default_dict_ , sorted_ = True ) , ':' ) ) if do_helpx : sys . exit ( 1 ) return dict_
r"""
    Gets values for a dict based on the command line

    Args:
        default_dict_ (?):
        only_specified (bool): if True only returns keys that are specified on commandline. no defaults.

    Returns:
        dict_: dict_ -  a dictionary

    CommandLine:
        python -m utool.util_arg --test-argparse_dict
        python -m utool.util_arg --test-argparse_dict --foo=3
        python -m utool.util_arg --test-argparse_dict --flag1
        python -m utool.util_arg --test-argparse_dict --flag2
        python -m utool.util_arg --test-argparse_dict --noflag2
        python -m utool.util_arg --test-argparse_dict --thresh=43
        python -m utool.util_arg --test-argparse_dict --bins=-10
        python -m utool.util_arg --test-argparse_dict --bins=-10 --only-specified --helpx

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_arg import *  # NOQA
        >>> import utool as ut
        >>> # build test data
        >>> default_dict_ = {
        ...    'bins': 8,
        ...    'foo': None,
        ...    'flag1': False,
        ...    'flag2': True,
        ...    'max': 0.2,
        ...    'neg': -5,
        ...    'thresh': -5.333,
        ... }
        >>> # execute function
        >>> only_specified = ut.get_argflag('--only-specified')
        >>> dict_ = argparse_dict(default_dict_, only_specified=only_specified)
        >>> # verify results
        >>> result = ut.repr4(dict_, sorted_=True)
        >>> print(result)