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()
❯ python click_hello.py --count=3
Your name: kyle
Hello kyle!
Hello kyle!
Hello kyle!
❯ python click_hello.py --help
Usage: click_hello.py [OPTIONS]
Simple program that greets NAME for a total of COUNT times.
Options:
--count INTEGER Number of greetings.
--name TEXT The person to greet.
--help Show this message and exit.
prompt 为 true,交互式输入密码
hide_input 为 True,可以隐藏命令行输入
confirmation_prompt 为True,密码二次验证
import click
@click.command()
@click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True)
def encrypt(password):
click.echo('Encrypting password to %s' % password.encode('rot13'))
if __name__ == '__main__':
encrypt()
from prompt_toolkit import prompt
while True:
user_input = prompt('>')
print(user_input)
历史命令输入支持
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
while True:
user_input = prompt('>', history=FileHistory('history.txt'))
print(user_input)
❯ cat history.txt
# 2021-03-11 12:06:56.871280
+dfsgsg
# 2021-03-11 12:06:58.126798
+sldbfnvljdfs
# 2021-03-11 12:06:59.568565
+avriuiue
# 2021-03-11 12:07:00.651740
+aoinsbnv
用户输入时自动提示
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
while True:
user_input = prompt('>', history=FileHistory(
'history.txt'), auto_suggest=AutoSuggestFromHistory())
print(user_input)
自动补全
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import WordCompleter
SQLCompleter = WordCompleter(
['select', 'from', 'insert', 'update', 'delete', 'drop'], ignore_case=True)
while True:
user_input = prompt('>', history=FileHistory(
'history.txt'), auto_suggest=AutoSuggestFromHistory(), completer=SQLCompleter)
print(user_input)