def option(flag, short_flag, type, help):
"""
Automagically detect whether a function has a default value for an
option, and add an appropriately defaulted or required option
decorator.
This can only be used if you have wrapped the function
in `clickutil.call.call`, since normally decorators strip out the
information we need here.
Note that if you want to provide default values for arguments only
when calling from the command line, but not from python, then you
must use `default_option` instead.
For the sake of flexibility, we do not check the default value's
type against the declared type.
The `type` entry must be one of either:
- a valid click type
- a dict with a 'type' entry and an optional 'multiple' entry
For example `type` could be:
- `click.Choice(['choice_a', 'choice_b'])`
- `{'multiple': True, 'type': str}`
"""
varname = flag.strip('-').replace('-', '_')
def decorator(f):
has_default, default = get_arg_default(f, varname)
if has_default:
return default_option(flag, short_flag, type, default, help)(f)
else:
return required_option(flag, short_flag, type, help)(f)
return decorator
评论列表
文章目录