Select 点击解析器的目标变量

Select destination variable for click parser

我想知道如何覆盖 click.option (Click lib) 的目标变量。例如在这样的一段代码中

import click

@click.command()
@click.option('--output', default='data')
def generate_data(output_folder):
    print(output_folder)

所以我想使用 --output 标志,但将其值传递给 output_folder 参数,有点像这样:@click.option('--output', default='data', dest='output_folder')? click有这种能力吗?我知道 argparse 允许这样的行为。

是的,请参阅点击文档中有关 parameter names 的部分,其中涵盖了选项和参数。

If a parameter is not given a name without dashes, a name is generated automatically by taking the longest argument and converting all dashes to underscores. For an option with ('-f', '--foo-bar'), the parameter name is foo_bar. For an option with ('-x',), the parameter is x. For an option with ('-f', '--filename', 'dest'), the parameter is called dest.

这是你的例子:

from __future__ import print_function
import click

@click.command()
@click.option('--output', 'data')
def generate_data(data):
    print(data)

if __name__ == '__main__':
    generate_data()

运行它:

$ python2.7 stack_overflow.py --output some_output
some_output