I have a command line interface build with click which implements multiple commands.
Now I want to pass unspecified named options into one command which is here named command1
e.g. the number of options and their names should be able to vary flexibly.
import click
@click.group(chain=True)
@click.pass_context
def cli(ctx, **kwargs):
return True
@cli.command()
@click.option('--command1-option1', type=str)
@click.option('--command1-option2', type=str)
@click.pass_context
def command1(ctx, **kwargs):
"""Add command1."""
ctx.obj['command1_args'] = {}
for k, v in kwargs.items():
ctx.obj['command1_args'][k] = v
return True
@cli.command()
@click.argument('command2-argument1', type=str)
@click.pass_context
def command2(ctx, **kwargs):
"""Add command2."""
print(ctx.obj)
print(kwargs)
return True
if __name__ == '__main__':
cli(obj={})
I have already looked into forwarding unknown options like in this question but the problem is that I have to call (chanin) other commands after the first one which have to be asserted e.g. this call has to work but with arbitrary options for command1
:
$python cli.py command1 --command1-option1 foo --command1-option2 bar command2 'hello'
So how can I add unspecified named options to a single command and call (chain) another one at the same time (after it)?