I use click for my Python script command line handling. I'd like to achieve behaviour similar to SSH or Sudo, i.e. parse some known arguments and take everything else as is without any processing.
For example, consider this command line:
ssh -v myhost echo -n -e foo bar
Notice that -v will be processed by SSH, but echo and everything after it will not be processed as options.
Here is my current implementation:
@click.command()
@click.option('-v', '--verbose', is_flag=True)
@click.argument('target')
@click.argument('command', nargs=-1)
def my_command(verbose, target, command):
print('verbose:', verbose)
print('target:', target)
print('command:', command)
It does not work as expected:
$ python test.py -v hostname echo -e foo
Usage: test.py [OPTIONS] TARGET [COMMAND]...
Try "test.py --help" for help.
Error: no such option: -e
I can add -- delimiter to force the expected behavior:
$ python /tmp/test.py -v hostname -- echo -e foo
verbose: True
target: hostname
command: ('echo', '-e', 'foo')
But it is not what I want.
I can also add ignore_unknown_options=True:
@click.command(context_settings=dict(ignore_unknown_options=True))
...
$ python /tmp/test.py -v hostname echo -e foo
verbose: True
target: hostname
command: ('echo', '-e', 'foo')
But it won't work with known options, like -v in this case.
So the question is: how to instruct click to stop handling any options after certain argument is encountered?