Imitate the click package
模仿click
包,写一个超简易自定义包myclick
,主要为了了解click
的原理。
pip install -e .
以学生成绩录入系统(enter_score.py
)为例:
# coding:utf-8
import myclick
@myclick.command()
@myclick.option('-n', '--name', 'username', default='Alice', help='input your name')
@myclick.option('-s', '--sex', default='M')
@myclick.option('--age', default='10')
@myclick.option('--verbose', is_flag=True)
@myclick.argument('chinese')
@myclick.argument('math')
@myclick.argument('englist')
def cli(username, sex, age, verbose, chinese, math, englist):
"""
input student score.
"""
print(f'name: {username}, sex: {sex}, age: {age}')
if verbose:
print(f' Chinese: {chinese}')
print(f' Math: {math}')
print(f' Englist: {englist}')
cli()
1.查看help信息
python enter_score.py --help
结果:
age: enter_score.py [OPTIONS] ENGLIST MATH CHINESE
Options:
-n, --name input your name
-s, --sex
--age
--verbose
--help Show this message and exit.``
2.录入成绩
python enter_score.py -n xiaowang -s M --age 8 --verbose 100 90 80
结果:
name: xiaowang, sex: M, age: 8
Chinese: 80
Math: 90
Englist: 100