它对我来说非常适合cmd
Python 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()
当我在命令行上键入“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
上面的变体给你level1
,level2
和level3
在你的“主要”解释器中。当您level2
在主解释器中调用时,它会构造子解释器并调用其命令循环。副口译员的提示与主口译员不同,因此您始终可以分辨出您所在的口译员。然后副口译员会为您提供subcommand_1
、subcommand_2
和。带您回到主解释器,EOF 字符也是如此。subcommand_3
quit
quit