-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo_click.py
61 lines (44 loc) · 1.74 KB
/
demo_click.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding:utf-8 -*-
import click
from progressbar import *
def get_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo('Version 1.0')
ctx.exit()
@click.group()
@click.option('-v', '--version', is_flag=True, callback=get_version, expose_value=False, is_eager=True)
def main():
pass
@main.command()
@click.option('-u', '--user', required=True, type=str, help="用户名")
@click.option('-p', '--password', required=True, type=str, help="密码")
@click.option('-t', '--type', required=True, default="phone", type=str, help="账户类型", show_default=True)
def add_user(user, password, type):
click.echo(f"user:{user} password:{password} type:{type}")
@main.command()
@click.option('-t', '--type', required=True, type=click.Choice(['user', 'admin']))
@click.option('-p', '--password', prompt=True, hide_input=True, confirmation_prompt=True)
def set_password(type, password):
click.echo(f"Your type:{type} password:{password}")
@main.command()
@click.confirmation_option(prompt='Are you sure you want to drop the db?')
def confirm_delete():
click.echo("Dropped all tables!")
@main.command()
def color_text():
click.secho('Hello World!', fg='green')
click.secho('Some more text', bg='blue', fg='white')
click.secho('ATTENTION', blink=True, bold=True)
all_process = {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
@main.command()
def progress_bar():
widgets = ['Progress: ', Percentage(), ' ', Bar('#'), ' ', Timer()]
process_bar = ProgressBar(widgets=widgets, maxval=len(all_process)).start()
# with click.progressbar(all_process) as bar:
for key, val in all_process.items():
time.sleep(key)
process_bar.update(key)
print()
if __name__ == '__main__':
main()