Check argument passed via python cmd module -


this 2 parts question, please see below:

  • i need create sort of console used testers run commands manually! is cmd module way go?

below code have far using cmd module, trying learn , have 2 questions far:

  • why auto-completion feature not working? if double <tab> nothing , cursor moves forward. isn't auto-complete feature provided default?

  • do have handle faulty number of arguments each method? methods-'help' text show automatically if methods called faulty number of arguments or when should called argument , not.

.

class interactiveconsole(cmd.cmd):     """ interactive command line """      def __init__(self):         cmd.cmd.__init__(self)         self.prompt = "=>> "         self.intro = "welcome irt console!"      def do_hist(self, args):         """print list of commands have been entered"""         print self._hist      def do_exit(self, args):         """exits console"""         return -1      def do_help(self, args):         """get on commands            'help' or '?' no arguments prints list of commands available            'help <command>' or '? <command>' gives on <command>         """         # # reason define method text in doc string         cmd.cmd.do_help(self, args)      # # override methods in cmd object ##     def preloop(self):         """initialization before prompting user commands.            despite claims in cmd documentaion, cmd.preloop() not stub.         """         cmd.cmd.preloop(self)  # # sets command completion         self._hist = []  # # no history yet         self._locals = {}  # # initialize execution namespace user         self._globals = {}      def postloop(self):         """take care of unfinished business.            despite claims in cmd documentaion, cmd.postloop() not stub.         """         cmd.cmd.postloop(self)  # # clean command completion         print "exiting..."      def precmd(self, line):         """ method called after line has been input before             has been interpreted. if want modify input line             before execution (for example, variable substitution) here.         """         if line != '':             self._hist += [ line.strip() ]         return line      def postcmd(self, stop, line):         """if want stop console, return evaluates true.            if want post command processing, here.         """         return stop      def default(self, line):         """called on input line when command prefix not recognized.            in case execute line python code.         """         try:             exec(line) in self._locals, self._globals         except exception, e:             print e.__class__, ":", e      def emptyline(self):         """do nothing on empty input line"""         pass          def do_install(self, pathtobuild):         """install [pathtobuild]         install using specified file"""         if pathtobuild:             print "installing %s" % pathtobuild         else:             print "<error> must specify absolute path file should used!"       def do_configure(self, pathtoconfiguration):         """configure [pathtoconfiguration]         configure using specified file"""         if pathtoconfiguration:             print "configuring %s" % pathtoconfiguration         else:             print "<error> must specify absolute path file should used!" 

from cmd documentation:

the optional argument completekey readline name of completion key; defaults tab. if completekey not none , readline available, command completion done automatically.

you need have readline available tab completion work.

command methods ever take one argument , need parsing of argument in command method itself. can, of course, call self.do_help() or self.help_<cmd>() methods if necessary.


Comments