import logging
import click
DEFAULT_FORMATTER = '%(asctime)s[%(filename)s:%(lineno)d][%(levelname)s]:%(message)s'
logging.basicConfig(format=DEFAULT_FORMATTER, level=logging.INFO)
@click.group(invoke_without_command=True)
@click.option('--limit', type=int, default=5)
@click.option('--chunk-size', type=int, default=5)
@click.option('-vvv', is_flag=True)
@click.option('--debug', envvar='DEBUG', default=False, help='debug mode')
@click.pass_context
def cli(ctx, **kwargs):
if kwargs.get('debug'):
logging.basicConfig(format=DEFAULT_FORMATTER, level=logging.DEBUG)
ctx.obj = kwargs
if ctx.invoked_subcommand is None:
ctx.invoke('index')
@cli.command()
@click.option('-a')
@click.pass_context
def index(ctx, **kwargs):
kwargs.update(ctx.obj)
print(kwargs)
@cli.command()
@click.option('-b')
@click.pass_context
def test(ctx, **kwargs):
kwargs.update(ctx.obj)
print(kwargs)
if __name__ == '__main__':
cli()
python3 test.py -vvv test 这样是可以的
python3 test.py test -vvv
这样提示找不到选项
怎样支持这种命令
1
ipwx 2020-06-14 15:40:54 +08:00
写个自定义的 decorator,包装一下你的 child command,把传进来的通用参数都处理好,再丢给子命令。
|