dan*_*ier 6 python command-line-interface tab-completion
我正在尝试使用CLI编写,最好用Python编写.我需要一个多级CLI,我想要标签完成.
我查看了cmd模块(来自Python标准库)和readline以及"完整"功能(用于标签完成).
他们都缺乏某些东西,即我还没弄清楚如何处理多个级别,例如:
level1
level2
level2_subcommand_1
level2_subcommand_2
level3
level3_subcommand_1
Run Code Online (Sandbox Code Playgroud)
示例:如果我输入:
cmd> level2
Run Code Online (Sandbox Code Playgroud)
,我想看到当我按Tab键时出现level2_subcommand_1和level2_subcommand_2,但是没有level1而没有level3.
我无法使用cmd lib以及readline这样做.
Tam*_*más 13
使用cmdPython 2.6.5中的模块,它对我来说非常好.这是我用来测试这个的示例代码:
import cmd
class MyInterpreter(cmd.Cmd):
def do_level1(self, args):
pass
def do_level2_subcommand_1(self, args):
pass
def do_level2_subcommand_2(self, args):
pass
def do_level3_subcommand_1(self, args):
pass
MyInterpreter().cmdloop()
Run Code Online (Sandbox Code Playgroud)
当我在命令行上键入"level2"然后按Tab键时,该行会扩展为,level2_subcommand_因为这是所有完成提案的公共前缀.当我再次按Tab而不键入任何内容时,下一行正确显示level2_subcommand_1和level2_subcommand_2.这是你想要的?
子命令的另一种变体是为它们创建一个子解释器:
class SubInterpreter(cmd.Cmd):
prompt = "(level2) "
def do_subcommand_1(self, args):
pass
def do_subcommand_2(self, args):
pass
def do_quit(self, args):
return True
do_EOF = do_quit
class MyInterpreter(cmd.Cmd):
def do_level1(self, args):
pass
def do_level2(self, args):
sub_cmd = SubInterpreter()
sub_cmd.cmdloop()
def do_level3(self, args):
pass
Run Code Online (Sandbox Code Playgroud)
上述变异给你level1,level2并level3在你的"主"的解释.level2在主解释器中调用时,它构造子解释器并调用其命令循环.子解释器与主解释不同的提示,让您可以随时告诉哪个解释你在,然后将子解释给你subcommand_1,subcommand_2,subcommand_3和quit.quit带你回到主翻译,EOF角色也是如此.
| 归档时间: |
|
| 查看次数: |
4045 次 |
| 最近记录: |