我想知道如何覆盖 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 isfoo_bar
. For an option with('-x',)
, the parameter isx
. For an option with('-f', '--filename', 'dest')
, the parameter is calleddest
.
这是您的示例:
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
关于python - 选择点击解析器的目标变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37628508/