click
?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”
import click
@click.command()
@click.option('--count', default=1, help='Count for Hello world')
def hello(count):
"""Simple program that print "Hello World!" COUNT times."""
for x in range(count):
click.echo('Hello World!')
if __name__ == '__main__':
hello()
$ python hello.py --count=3
Hello World!
Hello World!
Hello World!
$ python hello.py --help
Usage: hello.py [OPTIONS]
Simple program that print "Hello World!" COUNT times.
Options:
--count INTEGER Count for Hello world
--help Show this message and exit.
@click.group()
@click.option('--debug/--no-debug', default=False)
def cli(debug):
click.echo('Debug mode is %s' % ('on' if debug else 'off'))
@cli.command(help="Check packages in venv")
@click.argument("venv_type", type=click.Choice(["code", "layer"]))
def checkpkgs(venv_type):
...
@cli.command(help="Install packages in venv (default: in layer)",)
@click.argument("venv_type", type=click.Choice(["code", "layer"]), default="layer")
def installpkgs(venv_type):
...
$ layer_manage.py
Usage: layer_manage.py [OPTIONS] COMMAND [ARGS]...
Options:
--debug / --no-debug
--help Show this message and exit.
Commands:
checkpkgs
installpkgs
help
페이지에 subcommands를 정리해서 보여준다.@click.group()
으로 decorate된 method는 subcommand가 호출될 때에만 method 내부가 실행된다.