if __name__ == '__main__': arg.add_argument("--name", "-n", default="China", type=str, help="the name of your country.") arg.add_argument("--age", "-a", default=25, type=int, help="your age.") args = arg.parse_args() main(args)
查看帮助,
1 2 3 4 5 6 7 8
# 输出 $ python test.py --help usage: This is a test! [-h] [--name NAME] [--age AGE] optional arguments: -h, --help show this help message and exit --name NAME, -n NAME the name of your country. --age AGE, -a AGE your age.
可以输入对应的参数,
1 2 3
$ python test.py --name Chinese --age 26 Chinese 26
$ python test.py second --name hh # 输出 hello world this is second: hh
一个简单的click例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import click
@click.command() @click.option('--count', default=1, help='Number of greetings.') @click.option('--name', prompt='Your name', help='The person to greet.') @click.option('--life', prompt='life leave', help='how many your life.') defhello(count, name, life): """Simple program that greets NAME for a total of COUNT times.""" for x inrange(count): click.echo('Hello %s!' % name) click.echo('life %s!' % life)