It works perfectly fine for me with the cmd
module in Python 2.6.5. Here is the sample code I was using to test this:
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()
When I type "level2" on the command line and then press Tab, the line gets expanded to level2_subcommand_
as this is the common prefix to all the completion proposals. When I press Tab again without typing anything, the next line correctly shows level2_subcommand_1
and level2_subcommand_2
. Is this what you are looking for?
Another variant for the case of sub-commands is to create a sub-interpreter for them:
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
The above variant gives you level1
, level2
and level3
in your "main" interpreter. When you invoke level2
in your main interpreter, it constructs the sub-interpreter and calls its command loops. The sub-interpreter has a different prompt from the main interpreter, so you can always tell which interpreter you are in. The sub-interpreter then gives you subcommand_1
, subcommand_2
, subcommand_3
and quit
. quit
takes you back to the main interpreter, and so does the EOF character.