declic(面向decorator的cli creator)是一个用于创建命令行界面的python 3小包
declic的Python项目详细描述
declic(面向decorator的cli creator)是一个用于 使用decorators创建命令行接口。它的灵感来自 这个click包基于argparse。
安装
>;来自PYPI:
pip install declic
或来自github:
pip install git+https://github.com/Septaris/declic.git
用法
下面是declic用法的示例:
fromdeclicimportgroup,argument,command# on_before callbacks are executed if:# - the group itself is called# - if any of the child of the group is calleddefbefore_bar():print('before bar')defbefore_sub(tata):print('before sub: %s'%tata)# define the root command (a group)@group(description='my description',on_before=before_bar)@argument('--version',action='version',version='<the version>')@argument('--foo',type=int,default=1)defbar():print('bar')# define a sub-group@bar.group(invokable=True,on_before=before_sub)@argument('--toto',type=int,default=2)@argument('--tata',type=str,default='aaa')defsub(toto,tata):print('toto: %s'%toto)print('tata: %s'%tata)# define a sub-command of the sub-group# chain option allows to execute each parent group (if they are invokable) before the command call# each on_before functions will be executed anyway@sub.command(chain=True)defmop(toto,**kwargs):print('kwargs: %s'%kwargs)print('toto: %s'%toto)# define a sub-command of the root group@bar.command()@argument('-x',type=int,default=1)@argument('y',type=float)deffoo(x,y):print(x,y)if__name__=='__main__':importsysbar(sys.argv[1:])# or bar()
运行cli:
$ python my_file.py --help usage: bar [-h] [--foo FOO] [--version] {sub_group,foo} ... my description positional arguments: {sub,foo} optional arguments: -h, --help show this help message and exit --foo FOO --version show program's version number and exit