The following solution allows teams with an alias to share one help message. This allows you to save all your aliases in one convenient place for editing, making documentation much easier. It checks user input for an alias dictionary with function values and redefines it as default() (see laziness> and brian ) and do_help() .
Here I made aliases 'c' and 'con' execute do_constraint() , 'q' invoke do_quit() and 'h' invoke do_help() . The bonus of this solution is that 'h q' and 'help quit' print the same message. Documentation for multiple teams with an alias can be maintained in the same docstron.
import cmd class prompt(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) self.aliases = { 'c' : self.do_constraint , 'con' : self.do_constraint , 'q' : self.do_quit , 'h' : self.do_help } def do_constraint(self, arg): '''Constrain things.''' print('Constraint complete.') def do_quit(self, arg): '''Exit the program.''' return True def do_help(self, arg): '''List available commands.''' if arg in self.aliases: arg = self.aliases[arg].__name__[3:] cmd.Cmd.do_help(self, arg) def default(self, line): cmd, arg, line = self.parseline(line) if cmd in self.aliases: self.aliases[cmd](arg) else: print("*** Unknown syntax: %s" % line)
source share