Python单击配置fi中的Supply arguments和options

2024-04-26 02:17:42 发布

您现在位置:Python中文网/ 问答频道 /正文

给定以下程序:

#!/usr/bin/env python
import click

@click.command()
@click.argument("arg")
@click.option("--opt")
@click.option("--config_file", type=click.Path())
def main(arg, opt, config_file):
    print("arg: {}".format(arg))
    print("opt: {}".format(opt))
    print("config_file: {}".format(config_file))
    return

if __name__ == "__main__":
    main()

我可以用命令行提供的参数和选项运行它。

$ ./click_test.py my_arg --config_file my_config_file
arg: my_arg
opt: None
config_file: my_config_file

如何提供配置文件(在ini中?yamlpyjson?)并接受内容作为参数和选项的值?

例如,我希望my_config_file包含

opt: my_opt

程序输出显示:

$ ./click_test.py my_arg --config_file my_config_file
arg: my_arg
opt: my_opt
config_file: my_config_file

我找到了callback函数,它看起来很有用,但我找不到方法将同级参数/选项修改为同一个函数。


Tags: pytest程序configformat参数mainmy