Skip to content Skip to sidebar Skip to footer

Arbitrary Command Line Keywords With Python's Click Library

I have a command line tool built with Python's click library. I want to extend this tool to use user-defined keywords like the following: $ my-cli --foo 10 --bar 20 Normally I wo

Solution 1:

I think this should work:

import click


@click.command(context_settings=dict(ignore_unknown_options=True,))
@click.option('-v', '--verbose', is_flag=True, help='Enables verbose mode')
@click.argument('extra_args', nargs=-1, type=click.UNPROCESSED)
def cli(verbose, extra_args):
    """A wrapper around Python's timeit."""
    print(verbose, extra_args)


if __name__ == "__main__":
    cli()

Test:

$ python test.py --foo 12
False ('--foo', '12')

$ python test.py --foo 12 -v
True ('--foo', '12')

From: http://click.pocoo.org/5/advanced/#forwarding-unknown-options


Post a Comment for "Arbitrary Command Line Keywords With Python's Click Library"