Python Cli 编写指南
python实现cli
环境:
- python 3.8
库
- python自带argparse
指南
简略示例 : cli.py
import argparsedef cli(): parser = argparse.ArgumentParser(description='CLI形容') subparsers = parser.add_subparsers(metavar='子命令') # 增加子命令,演示没有参数 one_parser = subparsers.add_parser('one', help='第一个命令') one_parser.set_defaults(handle=handle_one) # 解析命令 args = parser.parse_args() # 1.第一个命令会解析成handle,应用args.handle()就可能调用 if hasattr(args, 'handle'): args.handle(args) # 2.如果没有handle属性,则示意未输出子命令,则打印帮忙信息 else: parser.print_help()def handle_one(args): print('handle_one')if __name__ == '__main__': cli()
调用:
$ python cli.pyusage: cli_1.py [-h] 子命令 ...CLI形容positional arguments: 子命令 one 第一个命令optional arguments: -h, --help show this help message and exit$ python cli.py onehandle_one
减少第二个命令
import argparsedef cli(): parser = argparse.ArgumentParser(description='CLI形容') subparsers = parser.add_subparsers(metavar='子命令') # 增加子命令,演示没有参数 one_parser = subparsers.add_parser('one', help='第一个命令') one_parser.set_defaults(handle=handle_one) # 增加子命令,演示有参数 two_parser = subparsers.add_parser('two', help='第二个命令') # 参数(简写,全称,类型,是否必填,帮忙阐明) two_parser.add_argument('-s', '--str', type=str, required=True, help='一个字符串类型参数') # 参数(简写,全称,类型,默认值,帮忙阐明) two_parser.add_argument('-d', '--default', type=str, default='默认值', help='这个命令有默认值') # 参数(简写,全称,类型,帮忙阐明) two_parser.add_argument('-ts', '--the-str', type=str, help='当全称有横线时,属性名转换为下划线,即 the_str') two_parser.set_defaults(handle=handle_two) # 解析命令 args = parser.parse_args() # 1.第一个命令会解析成handle,应用args.handle()就可能调用 if hasattr(args, 'handle'): # 1.1.其余参数会被解析成args的属性,以命令全称为属性名 args.handle(args) # 2.如果没有handle属性,则示意未输出子命令 else: parser.print_help()def handle_one(args): # 无参数 print('handle_one')def handle_two(args): print('handle_two') print(f'str:{args.str}') print(f'default:{args.default}') print(f'the-str:{args.the_str}')if __name__ == '__main__': cli()
调用:
$ python cli.pyusage: cli.py [-h] 子命令 ...CLI形容positional arguments:子命令 one 第一个命令 two 第二个命令optional arguments:-h, --help show this help message and exit
调用one不受影响
$ python cli.py onehandle_one
调用two会显示须要必填项-s
$ python cli.py twousage: cli.py two [-h] -s STR [-d DEFAULT] [-ts THE_STR]cli.py two: error: the following arguments are required: -s/--str
输出-h查看帮忙
$ python cli.py two -husage: cli.py two [-h] -s STR [-d DEFAULT] [-ts THE_STR]optional arguments:-h, --help show this help message and exit-s STR, --str STR 一个字符串类型参数-d DEFAULT, --default DEFAULT 这个命令有默认值-ts THE_STR, --the-str THE_STR 当全称有横线时,属性名转换为下划线,即 the_str
输出-s参数(或全称--str)
- 能够看到加了默认值的会显示默认值
- 没默认值且非必填项值为None
$ python cli.py two -s 哈哈handle_twostr:哈哈default:默认值the-str:None
输出有默认值的参数-d (或全称--default)
- 输出的值会笼罩默认值
$ python cli.py two -s 哈哈 --default 改了handle_twostr:哈哈default:改了the-str:None
输出所有参数
$ python cli.py two -s 哈哈 --default 改了 -ts 赋值了handle_twostr:哈哈default:改了the-str:赋值了
其余代码应用
在"setup.py"文件中
import setuptoolssetuptools.setup( # 省略其余参数 entry_points={ 'console_scripts': [ 'my-cli = my.cli:cli' ] })
'my-cli = my.cli:cli'就是替换内容
'my-cli'
- 上传pypi后,拉取这个库,就可能应用'my-cli'来调用cli
- 如'my-cli one' / 'my-cli two -h'
'my.cli:cli'
- '残缺类名:办法名'
- 目录构造如下方构造
- cli命令须要放在模块下
- 不能放在根目录下,否则会报错找不到模块
文件目录构造:
root
my
- \_\_init\_\_.py
- cli.py
参考资料:
https://zhuanlan.zhihu.com/p/...