使用argparse轻松定义命令行和子命令。
easycli的Python项目详细描述
easycli
argparse上python应用程序的命令行接口 包括子分析器。
安装
pip install easycli
快速启动
quickstart.py
fromeasycliimportRoot,SubCommand,Argument__version__='0.1.0'DEFAULT_TCP_PORT=8585DEFAULT_HOST='WPP.local'classSubCommand1(SubCommand):__command__='sub-command1'__aliases__=['s1','sc1']__arguments__=[Argument('-V','--version',action='store_true',help='Show programmer\'s version'),Argument('-p','--port',type=int,default=DEFAULT_TCP_PORT,help=f'TCP port, default: {DEFAULT_TCP_PORT}'),Argument('-H','--host',default=DEFAULT_HOST,help=f'Hostname, default: {DEFAULT_HOST}')]def__call__(self,args):print('Sub command 1, args:',args)classExample(Root):__help__='easycli example'__completion__=True__arguments__=[Argument('-V','--version',action='store_true',help='Show version'),SubCommand1,]def__call__(self,args):ifargs.version:print(__version__)returnreturnsuper().__call__(args)if__name__=='__main__':Example().main()
$ python quickstart.py usage: quickstart.py [-h][-V]{sub-command1,completion} ... easycli example optional arguments: -h, --help show this help message and exit -V, --version Show version Sub commands: {sub-command1,completion} sub-command1 completion Bash auto completion using argcomplete python package.
bash自动完成
$ python quickstart.py completion usage: quickstart.py completion [-h]{install,uninstall} ... optional arguments: -h, --help show this help message and exit Sub commands: {install,uninstall} install Enables the autocompletion. uninstall Disables the autocompletion.
进度条
fromeasycliimportProgressBarsteps=100withProgressBar(steps)aspb:foriinrange(steps):# Do what you want herepb.increment()