Did I find the right examples for you? yes no

All Samples(14)  |  Call(14)  |  Derive(0)  |  Import(0)
Adds types, actions, etc. to given argument specification.
For example, ``default=3`` implies ``type=int``.

:param arg: a :class:`argh.utils.Arg` instance

        def _guess(kwargs):
    """
    Adds types, actions, etc. to given argument specification.
    For example, ``default=3`` implies ``type=int``.

    :param arg: a :class:`argh.utils.Arg` instance
    """
    guessed = {}

    TYPE_AWARE_ACTIONS = 'store', 'append'
    "Parser actions that accept argument 'type'."

    # guess type/action from default value
    value = kwargs.get('default')
    if value is not None:
        if isinstance(value, bool):
            if kwargs.get('action') is None:
                # infer action from default value
                guessed['action'] = 'store_false' if value else 'store_true'
        elif kwargs.get('type') is None:
            # infer type from default value
            # (make sure that action handler supports this keyword)
            if kwargs.get('action', 'store') in TYPE_AWARE_ACTIONS:
                guessed['type'] = type(value)

    # guess type from choices (first item)
    if kwargs.get('choices') and 'type' not in list(guessed) + list(kwargs):
        guessed['type'] = type(kwargs['choices'][0])

    return dict(kwargs, **guessed)
        


src/a/r/argh-0.24.1/test/test_assembling.py   argh(Download)
def test_guess_type_from_choices():
    old = dict(option_strings=('foo',), choices=[1,2])
    new = dict(option_strings=('foo',), choices=[1,2], type=int)
    assert new == argh.assembling._guess(old)
 
    # ensure no overrides
    same = dict(option_strings=('foo',), choices=[1,2], type='NO_MATTER_WHAT')
    assert same == argh.assembling._guess(same)
def test_guess_type_from_default():
    old = dict(option_strings=('foo',), default=1)
    new = dict(option_strings=('foo',), default=1, type=int)
    assert new == argh.assembling._guess(old)
 
    # ensure no overrides
    same = dict(option_strings=('foo',), default=1, type='NO_MATTER_WHAT')
    assert same == argh.assembling._guess(same)
def test_guess_action_from_default():
    # True → store_false
    old = dict(option_strings=('foo',), default=False)
    new = dict(option_strings=('foo',), default=False, action='store_true')
    assert new == argh.assembling._guess(old)

src/a/r/argh-HEAD/test/test_assembling.py   argh(Download)
def test_guess_type_from_choices():
    old = dict(option_strings=('foo',), choices=[1,2])
    new = dict(option_strings=('foo',), choices=[1,2], type=int)
    assert new == argh.assembling._guess(old)
 
    # ensure no overrides
    same = dict(option_strings=('foo',), choices=[1,2], type='NO_MATTER_WHAT')
    assert same == argh.assembling._guess(same)
def test_guess_type_from_default():
    old = dict(option_strings=('foo',), default=1)
    new = dict(option_strings=('foo',), default=1, type=int)
    assert new == argh.assembling._guess(old)
 
    # ensure no overrides
    same = dict(option_strings=('foo',), default=1, type='NO_MATTER_WHAT')
    assert same == argh.assembling._guess(same)
def test_guess_action_from_default():
    # True → store_false
    old = dict(option_strings=('foo',), default=False)
    new = dict(option_strings=('foo',), default=False, action='store_true')
    assert new == argh.assembling._guess(old)