python - 选择点击解析器的目标变量

标签 python command-line command-line-interface command-line-arguments python-click

我想知道如何覆盖 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

关于python - 选择点击解析器的目标变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37628508/

相关文章:

python - Docker 从守护进程获取事件时出错 : EOF

python - 使用 2 列匹配多列的值

python - 继承时需要初始化父类吗?

python - subprocess.Popen 带有 unicode 路径

c++ - 简单的选择功能似乎不起作用(C++)

python - 考虑开放阅读框将 CDS 的 FASTA 文件翻译为蛋白质

从命令行执行 Java 比在 IntelliJ 中慢

c# - 退出命令行程序的首选方式是什么?

php - 如何在 UwAmp CLI 中启用 'openssl'?

amazon-web-services - AWS S3存储桶不一致