args.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:clickutil 作者: stroxler 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号