Develop Python Command Line (CLI) Application With Click

Dec 22, 2018

Install click

pip install click

Why? Personally I like declaring command and parameters using decorators/annotations.

NOTE: Alternatives include argparse (native python support), Docopt, Clint, etc.

A sample click cli application which support 2 commands, with required and optional parameters.

import cli():    pass@cli.command()@click.option('--name', required=True)# @click.option('--name', prompt='Your name', help='The person to greet.')# @click.argument('name')@click.option('--count', type=int, default=1, help='Number of greetings.')def hello(name, count):    click.echo('Hello {}, No {}'.format(name, count))@cli.command()def test():    click.echo('test')# cli.add_command(hello)# cli.add_command(test)if __name__ == '__main__':    cli()

Sample usage

python testpython hello --name Desmondpython hello --name Desmond --count 9

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.