ec-一个用于python模块的cli接口。
ec的Python项目详细描述
意图
EC-打算减少通过命令行公开功能所需的学习曲线。有了像argparse和docopt这样的可用选项,可能需要几个小时才能使它们正确。还有一个问题是需要额外的编码来设置和处理参数。请参见下面的示例,了解ec如何解决该问题。
一个简单的例子
fromec.ecimporttask@taskdefsay_hello():print'Hello, world!'
可以从命令行访问,例如:
$ python hello.py say_hello Hello, world!
功能
- 类型化参数。
- 子命令(如git)。
- 自定义类型。
- shell模式(类似于python的交互模式)。
- 命名/位置参数。
更完整的示例
发件人:simple.py
fromec.ecimporttask,arg,group@task# define a task@arg(type=int,desc='Value for arg1')# add an argument with a type and a description@arg(type=int)deftask1(arg1,arg2=1):printarg1,arg2@group(desc='A group with some tasks')# define a groupclassgroup1:@taskdeftask1(arg1):# define a task inside the groupprintarg1+arg1
执行任务:<;分派模式>;
从命令行中输入
$ python simple.py task1 arg1=1arg2=212 $ python simple.py group1/task1 arg1=12 $ python simple.py group1/task1 1# positional arguments too are supported 2
交互执行任务:<;外壳模式>;
从命令行中输入
$ python simple.py # this will enter into ec-shell >task1 Value for arg1: 1 arg2 (1): 212 >group1/task1 # execute task1 under group1 arg1: 111 >task1 arg1=1# arguments can be given while calling the task, the missing arguments will be collected from the user arg2 (1): 212 >^Z # exit the shell
利用stdin:
从命令行中输入
$ echo12| xargs python simple.py task1 # Dispatch a command with args passed through STDIN and converted to args using xargs. $ echo -e "1\n2"| python simple.py -p task1 # Dispatch a command and with partial args and pass the rest through STDIN. $ echo task1 12| python simple.py # Feeding the shell with complete commands through stdin.
详细的文档可以在PyDocs找到。
有关更多示例,请查看github页。