Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create both short and long options for one option in click (python package)?

How do I specify both a short option and long option for the same option? e.g., for the following, I also want to use -c for --count:

import click

@click.command()
@click.option('--count', default=1, help='count of something')
def my_command(count):
    click.echo('count=[%s]' % count)

if __name__ == '__main__':
    my_command()

e.g.,

$ python my_command.py --count=2
count=[2]
$ python my_command.py -c 3
count=[3]

References:
click documentation in a single pdf
click sourcecode on github
click website
click PyPI page

like image 781
Rob Bednark Avatar asked Mar 06 '17 01:03

Rob Bednark


People also ask

How do you click multiple options?

For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.

What is Click option in Python?

Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It's the “Command Line Interface Creation Kit”. It's highly configurable but comes with sensible defaults out of the box.

How do you add a click in Python?

Open your Linux terminal or shell. Type “ pip install click ” (without quotes), hit Enter. If it doesn't work, try "pip3 install click" or “ python -m pip install click “. Wait for the installation to terminate successfully.

What is click Pass_context?

When a Click command callback is executed, it's passed all the non-hidden parameters as keyword arguments. Notably absent is the context. However, a callback can opt into being passed to the context object by marking itself with pass_context() .


1 Answers

This is not well documented, but is quite straight forward:

@click.option('--count', '-c', default=1, help='count of something')

Test Code:

@click.command()
@click.option('--count', '-c', default=1, help='count of something')
def my_command(count):
    click.echo('count=[%s]' % count)

if __name__ == '__main__':
    my_command(['-c', '3'])

Result:

count=[3]
like image 97
Stephen Rauch Avatar answered Oct 21 '22 16:10

Stephen Rauch