![image.png](https://cdn.nlark.com/yuque/0/2020/png/149045/1600742078923-a901915d-8587-4def-a2be-3bf67ec65feb.png#align=left&display=inline&height=100&margin=%5Bobject%20Object%5D&name=image.png&originHeight=199&originWidth=503&size=11401&status=done&style=shadow&width=252)
basic
# test.py
import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name', help='The person to greet.')
def hello(count, name):
"""Simple program that greets NAME for a total of COUNT times."""
for x in range(count):
click.echo('Hello %s!' % name)
if __name__ == '__main__':
hello()
group+pass_context
import click
@click.group()
@click.pass_context
@click.option('--url', help='the url of wiki api', default=DEFAULT_URL)
@click.option('-u', '--username', help='the username to login', default='suqingdong')
@click.option('-p', '--password', help='the password to login', hide_input=True, prompt=True)
def cli(ctx, url, username, password):
ctx.ensure_object(dict)
api = WikiAPI(url, username, password)
ctx.obj['api'] = api
@cli.command()
@click.pass_context
@click.argument('filepath')
@click.option('-n', '--name', help='the name of file')
def upload(ctx, filepath, name):
api = ctx.obj['api']
api.upload(filepath, name)
@cli.command()
@click.pass_context
@click.argument('filepath')
def import_xml(ctx, filepath):
api = ctx.obj['api']
api.import_xml(filepath)
if __name__ == '__main__':
cli(obj={})
一些配置
-h帮助, 默认显示帮助
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(no_args_is_help=True, context_settings=CONTEXT_SETTINGS)