-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Comments
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. |
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. |
It would be interesting to see how this can be combined with #107. Just to summarise the idea for myself:
|
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:
The text was updated successfully, but these errors were encountered: