Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customisable guess logic #99

Open
martinpengellyphillips opened this issue Jan 6, 2016 · 3 comments
Open

Customisable guess logic #99

martinpengellyphillips opened this issue Jan 6, 2016 · 3 comments

Comments

@martinpengellyphillips
Copy link

Would be useful to have an easy way to customise the assembling._guess logic so that it can be changed in one place for a program rather than having to duplicate argh decorators.

For example, would prefer boolean arguments to be treated as explicitly set truthy / falsey values rather than switches. At present have to do the following for each related boolean argument and feel like this could be centralised somehow:

@argh.decorators.arg('--with-some-flag', action='store', type=explicit_bool_type)
def foo(with_some_flag=True):
    ...
@neithere
Copy link
Owner

Thank you for the idea but I'm not sure I understand what you are trying to achieve. Perhaps you could draft some unit tests so we could see if it's possible to make them pass?

In general, Argh is indeed customizable via arguments to its functions and decorators that set attributes on command functions, but it's not configurable e.g. on app level. I could imagine some wrappers around it that handle configuration, or even some deeper changes, but there should be a clear vision and a set of well-defined use cases.

@martinpengellyphillips
Copy link
Author

Sure - will try and work up some unit tests.

In the meantime I'll expand on the example to help clarify requirements:

Given a custom type

def bool_type(string):
    '''Return a boolean value depending on string value'''
    affirmatives = ('y', 'yes', 'true', 'on', '1')
    negatives = ('n', 'no', 'false', 'off', '0')

    if string.lower() in affirmatives:
        return True

    elif string.lower() in negatives:
        return False

    raise argparse.ArgumentTypeError(
        '{0!r} is not a valid boolean value. '
        'Use one of: {1}'.format(string, ', '.join(affirmatives + negatives))
    )

It would be nice to not have to repeatedly reference this custom type everywhere that a bool value might be encounted (especially as that can introduce a lot of bolierplate code):

@argh.decorators.arg(
    '--with-option-a', action='store',
    type=custom_types.bool_type
)
@argh.decorators.arg(
    '--with-option-b', action='store',
    type=custom_types.bool_type
)
@argh.decorators.arg(
    '--with-option-c', action='store',
    type=custom_types.bool_type
)
def foo(with_option_a=True, with_option_b=False, with_option_c=True):
    pass

Looking at https://github.com/neithere/argh/blob/master/argh/assembling.py#L134 it seems the concept for inferring type from argument default value is already present, but private and hard to extend. So this is a request for being able to officially extend the guess logic to deal with type inferrence in a custom way at the app level, with the goal of reducing boilerplate.

@neithere
Copy link
Owner

neithere commented Feb 8, 2023

It would be interesting to see how this can be combined with #107.

Just to summarise the idea for myself:

  • user passes --with-foo yes, --with-foo y, --with-foo no etc.
  • a custom str to bool converter function preprocesses the value
    • it's hooked as arg(..., action='store', type=str_to_bool)
  • the function gets with_foo=True or with_foo=False

@neithere neithere added this to the 1.1 milestone Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants