[英]python command line interface with shell prompt
I am researching into a python CLI application with a shell prompt. 我正在研究带有shell提示符的python CLI应用程序。 To clarify, I am looking for something which supports 2 functionalities. 为了澄清,我正在寻找支持2种功能的东西。
1) provides a shell prompt & auto complete on the commands like how "cmd" module supports. 1)提供shell提示和自动完成命令,例如“ cmd”模块如何支持。 for eg: the "do_" prefixed commands. 例如:“ do_”前缀命令。 ( do_xyz: do_abc. ) (do_xyz:do_abc。)
2) provides support for multiple sub-commands and options. 2)提供对多个子命令和选项的支持。 for eg: 例如:
$ python my_prog_prompt.py
(my_prog_prompt)> cmd1 subcmd1 subsubcmd1
(my_prog_prompt)> cmd1 subcmd1 subsubcmd2
(my_prog_prompt)> cmd1 subcmd2 subsubcmd1 -x -y
(my_prog_prompt)> cmd2 subcmd1
(my_prog_prompt)> cmd2 subcmd1
(my_prog_prompt)> exit
$
I found the "cmd" module above to be overly simplistic. 我发现上面的“ cmd”模块过于简单。 Since it does not supports multiple sub-commands. 由于它不支持多个子命令。 With Cement CLI i could see it supports the multiple commands/subcommands and options very well, but does not have a shell/prompt interface. 使用Cement CLI,我可以看到它很好地支持多个命令/子命令和选项,但是没有shell /提示界面。 Can the "cmd" module be extended to support the above ? 是否可以扩展“ cmd”模块以支持上述功能? Or is there another available framework or module i can use, which will give me the above support. 还是我可以使用其他可用的框架或模块,这将为我提供以上支持。
Much thanks. 非常感谢。
Posibly late for an answer but I was looking for a similar option and this is what I've ended up implementing: 可能迟到了答案,但我一直在寻找类似的选择,而这正是我最终实现的目标:
def parseargs( f ):
def wrapper(s, arg ):
args={ 0: f.__name__ }
i=0
for a in arg.split():
i+=1
if i>0 and '=' in a:
args[i] = a.split('=', 1)
else:
args[i] = a
return f(s, args)
wrapper.__doc__ = f.__doc__
return wrapper
@parseargs
def do_setvar_for_group(self, args):
'setvar_for_group <group> <var1=value1> : Sets a variable for a group'
command = args[0] # command = 'setvar_for_group'
group = args[1] # group = 'example_group'
var_key = arg[2][0] # var_key = 'myvar'
var_value = arg[2][1] # var_value = 'example'
#
# You would get this values by executing:
# prompt> setvar_for_group example_group myvar=example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.