Python Click Library Rename Argument

淺唱寂寞╮ 提交于 2019-12-01 17:38:41

问题


I am using the Click library but I can't seem to find a behavior similar to dest from argparse.

For example, I have

@click.option('--format', type=click.Choice(['t', 'j']))
def plug(format):
  pass

Notice that I am using a flag with --format that gets translated into a built-in Python construct format which is not ideal.

Is there a way to change the argument passed into the click function for options?


回答1:


While Click doesn't have dest-equivalent of argparse, it has certain argument-naming behavior which can be exploited. Specifically, for parameters with multiple possible names, it will prefer non-dashed to dashed names, and as secondary preference will prioritize longer names over shorter names.

URL: http://click.pocoo.org/dev/parameters/#parameter-names

So if you declare your option as...

@click.option('--format', 'not-format', type=click.Choice(['t', 'j']))

...then Click will prioritize non-dashed variant ('not-format') and call your function with not_format=... argument.

Of course it also means that this alternative spelling can also be used in command line. If that is not desired, then I guess you could add a decorator to rename keyword arguments:

import functools

def rename_kwargs(**replacements):
    def actual_decorator(func):
        @functools.wraps(func)
        def decorated_func(*args, **kwargs):
            for internal_arg, external_arg in replacements.iteritems():
                if external_arg in kwargs:
                    kwargs[internal_arg] = kwargs.pop(external_arg)
            return func(*args, **kwargs)
        return decorated_func
    return actual_decorator

Testing code:

if __name__ == '__main__':

    @rename_kwargs(different_arg='format')
    def tester(different_arg):
        print different_arg

    tester(format='test value')

Test output:

$ python test_decor.py
test value

In your case, it would look like:

@click.option('--format', type=click.Choice(['t', 'j']))
@replace_kwargs(not_format='format')
def plug(not_format):
    pass



回答2:


If you decorate your method with

@click.option('--format', '-f', 'format_arg_name')
def plug(format_arg_name):
    print(format_arg_name)

then it will map the option to the format_arg_name parameter.

format_arg_name will not be available on the command line.



来源:https://stackoverflow.com/questions/35567753/python-click-library-rename-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!