人性化的cli生成器
manage.p的Python项目详细描述
人性化的cli生成器。
安装
pip install manage.py
快速启动
cat manage.py
frommanagerimportManagermanager=Manager()@manager.commanddefecho(text,capitalyze=False):"""print the given <name>"""ifcapitalyze:text=text.upper()returntextif__name__=='__main__':manager.main()
manage --help:
usage: manage [<namespace>.]<command> [<args>] positional arguments: command the command to run optional arguments: -h, --help show this help message and exit available commands: echo print the given <name>
manage echo --help:
$ manage echo --help usage: manage [-h] [--capitalyze] text print the given <name> positional arguments: text no description optional arguments: -h, --help show this help message and exit --capitalyze no description
经理
管理者可以通过合并使用在一起
fromthird_partyimportmanagermy_app_manager.merge(manager)# Merge within a new namespace:my_app_manager.merge(manager,namespace='third_party')
命令
可以在名称空间中组织命令
@manager.command(namespace='config')defset(key,value):# ...
参数
当前,框架将假定具有默认值的参数是 键值参数(--arg value),而所需参数是位置参数 一个。
换句话说,这个定义:
@manager.commanddefcreate(user):pass
会有类似的调用:
$ manage create foobar
而create()中的user将取值'foobar'。
另一方面,这个:
@manager.commanddefcreate(user=''):pass
会有类似的调用:
$ manage create --user foobar
缺点显然是目前不可能有一个 非位置参数。
参数定义可以重写
@manager.arg('first_arg',help='this is help for first arg')@manager.commanddefmy_command(first_arg):# ...
可以提示参数
@manager.prompt('password',hidden=True,confirm=True)@manager.commanddefconnect(username,password):# ...
环境
环境变量可以作为key=value对从.env文件中获取。
可以使用Manager.envdecorator强制环境变量
@manager.env('MY_ENV_VAR')@manager.commanddefmy_command():returnos.environ['MY_ENV_VAR']