I'm using the cmd module and have a command called server (method do_server()) which has autocomplete out of the box (I'm on macOS). That's all fine and works like expected. But if i want to additionally also use a sub-command on the server command, things don't work out like i need it.
I would need the following:
- sub-commands should also be integrated with the help command (shouldn't be shown when entering help since they are not first level commands but should be shown when entering help server)
- sub-commands should also integrate with auto-complete
Currently i don't see a way to define sub-commands out-of-the-box. I need to implement them as arguments for a command which isn't ideal.
My question would be, how can i achieve automatic documentation of the sub-commands with help and have autocomplete so that it integrates as good as possible with cmd?
For this example, i'd like to better integrate connect as a sub-command:
from cmd import Cmd
class Tansanit(Cmd):
def do_server(self, args):
""" Show server info """
print("Some server details")
if args and args == "connect":
print("Connect to the server")
def do_quit(self, args):
""" Quit CLI """
raise SystemExit
if __name__ == '__main__':
t = Tansanit()
t.prompt = "> "
t.cmdloop()
I would prefer to have something like this:
from cmd import Cmd
class Tansanit(Cmd):
def do_server(self, args):
""" Show server info """
print("Some server details")
def do_server_connect(self, args):
""" Connect to server """
print("Connect to the server")
def do_quit(self, args):
""" Quit CLI """
raise SystemExit
if __name__ == '__main__':
t = Tansanit()
t.prompt = "> "
t.cmdloop()
Unfortunately that's not possible.
Relavant infos can be found here
Autocomplete for sub-commands
helpfor sub-commandsThis isn't perfect since the help will be categorized as
undocumented commandsbut it's for an argument. But maybe still better then nothing.