Python Click - 从配置文件中提供参数和选项

时间:2017-09-22 07:07:56

标签: python click python-click

鉴于以下计划:

#!/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--config_file?)并接受内容作为值参数和选项?

例如,我希望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函数看起来很有用,但我找不到修改兄弟参数/选项到同一函数的方法。

1 个答案:

答案 0 :(得分:24)

这可以通过覆盖click.Command.invoke()方法来完成,例如:

自定义类:

def CommandWithConfigFile(config_file_param_name):

    class CustomCommandClass(click.Command):

        def invoke(self, ctx):
            config_file = ctx.params[config_file_param_name]
            if config_file is not None:
                with open(config_file) as f:
                    config_data = yaml.load(f)
                    for param, value in ctx.params.items():
                        if value is None and param in config_data:
                            ctx.params[param] = config_data[param]

            return super(CustomCommandClass, self).invoke(ctx)

    return CustomCommandClass

使用自定义类:

然后使用自定义类,将其作为cls参数传递给命令装饰器,如:

@click.command(cls=CommandWithConfigFile('config_file'))
@click.argument("arg")
@click.option("--opt")
@click.option("--config_file", type=click.Path())
def main(arg, opt, config_file):

测试代码:

# !/usr/bin/env python
import click
import yaml

@click.command(cls=CommandWithConfigFile('config_file'))
@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))


main('my_arg --config_file config_file'.split())

测试结果:

arg: my_arg
opt: my_opt
config_file: config_file