
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)