1.安装 click
[root@mysql1 site-packages]# pip3 install click
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting click
Using cached https://files.pythonhosted.org/packages/4a/a8/0b2ced25639fb20cc1c9784de90a8c25f9504a7f18cd8b5397bd61696d7d/click-8.0.4-py3-none-any.whl
Collecting importlib-metadata; python_version < "3.8" (from click)
Using cached https://files.pythonhosted.org/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl
Collecting zipp>=0.5 (from importlib-metadata; python_version < "3.8"->click)
Using cached https://files.pythonhosted.org/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl
Collecting typing-extensions>=3.6.4; python_version < "3.8" (from importlib-metadata; python_version < "3.8"->click)
Using cached https://files.pythonhosted.org/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl
Installing collected packages: zipp, typing-extensions, importlib-metadata, click
Successfully installed click-8.0.4 importlib-metadata-4.8.3 typing-extensions-4.1.1 zipp-3.6.0
--下载get-pip.py 脚本。
--curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
click 是一个第三方库,click对 argparse 的主要改进在易用性:
使用 click 有两个步骤:
(1)使用@click.command() 装饰一个函数,使之称为命令行接口。
(2)使用 @click.option() 装饰一个函数,为期添加命令行选项。
vi click_test.py
import click
@click.command()
@click.option('--count',default=1,help='Number of greetings.')
@click.option('--name',prompt='Your name',help='The perso to greet.')
@click.option('--address',prompt='Your Address',help='The Address your home.')
@click.option('--age',prompt='Your Age',help='How old are you.')
@click.option('--sex',prompt='Your sex',help='What is your sex')
def hello(count,name,address,age,sex):
"""Simple program that greets NAME for a total of COUNT times"""
for x in range(count):
click.echo('Hello %s' %name)
click.echo('Hello %s' %address)
click.echo(f'Hello {age}' )
click.echo('Hello %s' %sex)
if __name__=='__main__':
hello()
--执行。
[root@mysql1 python]# python3 click_test.py --count=1
Your name: xueshuangqi
Your Address: chengdu
Your Age: 32
Your sex: male
Hello xueshuangqi
Hello chengdu
Hello 32
Hello male
--或者直接传入参数。
[root@mysql1 python]# python3 click_test.py --count=1 --name=xueshuangqi --address=chengdu --age=32 --sex=male
Hello xueshuangqi
Hello chengdu
Hello 32
Hello male
--查看帮助信息。
[root@mysql1 python]# python3 click_test.py --help
Usage: click_test.py [OPTIONS]
Simple program that greets NAME for a total of COUNT times
Options:
--count INTEGER Number of greetings.
--name TEXT The perso to greet.
--address TEXT The Address your home.
--age TEXT How old are you.
--sex TEXT What is your sex
--help Show this message and exit.
[root@mysql1 python]#
command :使得函数 hello 成为命令行接口。
option:增加命令行选项。
echo :输出结果。
option 常用的命令行参数的默认值:
default:设置参数默认值。
help:参数说明
type:参数类型:可以是 int,string,float等。
prompt:在当前命令行中没有输入相应的参数时,根据 prompt提示输入。
nargs:指定参数结束的值的个数。
vi click2.py
import click
@click.command()
@click.option('--pos',nargs=2,type=float)
def findme(pos):
click.echo('%s / %s' % pos)
--该脚本没有输出任何内容。
--模拟输入密码
vi click3.py
import click
@click.command()
@click.option('--password',prompt=True,hide_input=True,confirmation_prompt=True)
def encrypt(password):
click.echo('Encryption password to %s' % password)
encrypt()
[root@mysql1 python]# python3 click3.py
Password:
Repeat for confirmation:
Error: The two entered values do not match.
Password:
Repeat for confirmation:
Encryption password to rootroot
linux:
fc + enter 查看上次执行的命令。
python 提供 fc的功能。
vi fc.py
import click
message=click.edit()
print(message)
--直接进入编辑器模式。
[root@mysql1 python]# python3 fc.py
--编辑数据。
1 henhao
2 taihao
3.buhao
--退出时打印输入的值。
[root@mysql1 python]# python3 fc.py
1 henhao
2 taihao
3.buhao
click 的官方地址是:http://click.pocoo.org/5
2. prompt_toolkit 开源工具
prompt_toolkit 是一个处理交互式场景的开源库,用来取代开源的 readline,curses;
prompt_toolkit 特性包括:
语法高亮
支持多行编辑
支持代码补全
支持自动提示
可以使用鼠标移动光标
支持emacs 与 vi 风格的快捷键。
支持查询历史
对Unicode 支持友好
使用python语言开发,跨平台。
--安装 prompt-toolkit
[root@mysql1 python]# pip3 install prompt_toolkit
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting prompt_toolkit
Using cached https://files.pythonhosted.org/packages/eb/37/791f1a6edd13c61cac85282368aa68cb0f3f164440fdf60032f2cc6ca34e/prompt_toolkit-3.0.36-py3-none-any.whl
Collecting wcwidth (from prompt_toolkit)
Using cached https://files.pythonhosted.org/packages/fd/84/fd2ba7aafacbad3c4201d395674fc6348826569da3c0937e75505ead3528/wcwidth-0.2.13-py2.py3-none-any.whl
Installing collected packages: wcwidth, prompt-toolkit
Successfully installed prompt-toolkit-3.0.36 wcwidth-0.2.13
vi prompt.py
from prompt_toolkit import prompt
while True:
user_input=prompt('>')
print(user_input)
[root@mysql1 python]# python3 prompt.py
>nihao
nihao
>buhao
buhao
>exit
exit
>tuichhu
tuichhu
--vi prompt2.py
--from prompt_toolkit import prompt
--while True:
-- user_input=raw_input('>')
-- print(user_input)
--将历史输出保存到文件中:
vi prompt3.py
from __future__ import unicode_literals
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
while True:
user_input=prompt('#',history=FileHistory('history.txt'))
print(user_input)
--可以输入并打印,同时可以上下翻页。
[root@mysql1 python]# python3 prompt3.py
#hello
hello
#wo shi xueshuangqii
wo shi xueshuangqii
#ni shi hsui
ni shi hsui
#ni shi hsui
Ctrl+A
Ctrl+E
↓↑
四个键都可以使用。
vi prompt4.py
from __future__ import unicode_literals
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)
--当输入相同命令的开头时会自动提示。
--类似redis-cli 的提示命令。
[root@mysql1 python]# python3 prompt4.py
#1 zhangsan
1 zhangsan
#2 wangwu
2 wangwu
#liliu
liliu
#3 sige
3 sige
#2 wangwu
2 wangwu
#1 zhangsan
--自动补全功能,忽略大小写。
vi prompt5.py
from __future__ import unicode_literals
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('SQL>',history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
completer=SQLCompleter)
print(user_input)
--输入关键词时会自动提示。
[root@mysql1 python]# python3 prompt5.py
SQL>selct * from
select
from
insert
update
delete
drop
SQL>delete from tab where
delete from tab where
SQL>
SQL>insert into tab1 values('1');
insert into tab1 values('1');