简体   繁体   English

带有命令行提示符的python命令行界面

[英]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: 可能迟到了答案,但我一直在寻找类似的选择,而这正是我最终实现的目标:

  1. Create this wrapper 创建这个包装器
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
  1. Use it like this inside your class: 在您的班级中像这样使用它:
@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.

相关问题 代码可在Python Shell中运行,但不能在命令提示符下运行 - Code works in Python shell but not with command prompt 如何从bash shell提示符中将sentiance作为命令行参数运行python程序 - How to run python program with a sentance as a command line arguments from bash shell prompt python 模块的命令行界面 - Command line interface for python module 如何响应 python 中的命令行提示 - How to respond to a command line prompt in python 如何从 python 调用中获取命令 docker shell 提示 - How to get command docker shell prompt from python call 从python评估“带有shell变量的Shell命令行”或将python字符串评估为shell命令行 - Evaluate "Shell command line with shell variables" from python OR evaluate python string as shell command line 在命令提示符中运行python脚本的问题(特别是使用命令行参数)? - Issues running python scripts in Command Prompt (Specifically with command line arguments)? 避免在命令行中使用命令行参数使用shell = True - Avoid shell=True with command line arguments in python 在shell脚本中运行python命令行参数 - run python command line arguments in shell script 使用Python和Click创建shell命令行应用程序 - Creating a shell command line application with Python and Click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM