python click sub command global option

Viewed 174
@click.group(invoke_without_command=True)
@click.option('--limit', type=int, default=5)
@click.option('--chunk-size', type=int, default=5)
@click.pass_context
def cli(ctx, **kwargs):
    ctx.obj = kwargs


@cli.command()
@click.option('--url')
@click.pass_context
def index(ctx, **kwargs):
    kwargs.update(ctx.obj)
    print(kwargs)

How do I need to use the --chunk-size and --limit option in the index subcommand? The effect I want python test.py index --limit 1

1 Answers

Pass the global options before the command:

./cli.py --limit 1 index --url foo
{'url': 'foo', 'limit': 1, 'chunk_size': 5}
Related