'Accept arbitrary arguments and options with Click

I'm writing a Python wrapper around another program. I want the user to be able to specify a few options for the wrapper and then pass the rest of the command-line through to the wrapped program. Something like this:

@click.command()
@click.option("--port", type=int)
@click.argument("args", nargs=-1)
def main(port, args):
    call_the_wrapped_program(port=port, args=args)

But this dies with Error: no such option: -k because it treats any command-line switch as something it should parse rather than an argument that can be added to args.

Is this possible?



Solution 1:[1]

Assuming you invoke the program with something akin to a cli command, have you tried simply calling it like this?

cli --port 8080 -- -k arg1 arg2 -r etc

If I print args with that invocation, I get all the arguments out, albeit as a string, but I'm hoping that whatever third party you want to delegate to might be able to run its own parsing over that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 afterburner