def do_help(self, arg): " Modify the original do_help to pull docstrings from actions " if arg: # XXX check arg syntax try: func = getattr(self, 'help_' + arg) except (AttributeError, TypeError): try: doc=getattr(self, 'do_' + arg).__doc__ if doc: self.stdout.write("%s\n" % str(doc)) return except (AttributeError, TypeError): pass try: _action = COMMANDMAP[arg.lower()] if _action.needs_parm: doc = '%s [parm <idx>|<name>]\n%s' else: doc = '%s\n%s' doc = doc % (_fmt_usage(Usages[arg.lower()]), _action.__doc__) if doc: self.stdout.write("%s\n"%str(doc)) return except (AttributeError, KeyError): pass self.stdout.write("%s\n" % str(self.nohelp % (arg,))) return func() else: names = self.get_names() cmds_doc = [] cmds_undoc = [] help = {} for name in names: if name[:5] == 'help_': help[name[5:]]=1 names.sort() # There can be duplicates if routines overridden prevname = '' for name in names: if name[:3] == 'do_': if name == prevname: continue prevname = name cmd=name[3:] if cmd in help: cmds_doc.append(cmd) del help[cmd] elif getattr(self, name).__doc__: cmds_doc.append(cmd) elif cmd.lower() in COMMANDMAP and cmd.lower() in Usages: cmds_doc.append(cmd) else: cmds_undoc.append(cmd) self.stdout.write("%s\n"%str(self.doc_leader)) self.print_topics(self.doc_header, cmds_doc, 15,80) self.print_topics(self.misc_header, help.keys(),15,80) self.print_topics(self.undoc_header, cmds_undoc, 15,80)
def do_help(self, arg): " Modify the original do_help to pull docstrings from actions " if arg: # XXX check arg syntax try: func = getattr(self, 'help_' + arg) except (AttributeError, TypeError): try: doc = getattr(self, 'do_' + arg).__doc__ if doc: self.stdout.write("%s\n" % str(doc)) return except (AttributeError, TypeError): pass try: _action = COMMANDMAP[arg.lower()] if _action.needs_parm: doc = '%s [parm <idx>|<name>]\n%s' else: doc = '%s\n%s' doc = doc % (_fmt_usage( Usages[arg.lower()]), _action.__doc__) if doc: self.stdout.write("%s\n" % str(doc)) return except (AttributeError, KeyError): pass self.stdout.write("%s\n" % str(self.nohelp % (arg, ))) return func() else: names = self.get_names() cmds_doc = [] cmds_undoc = [] help = {} for name in names: if name[:5] == 'help_': help[name[5:]] = 1 names.sort() # There can be duplicates if routines overridden prevname = '' for name in names: if name[:3] == 'do_': if name == prevname: continue prevname = name cmd = name[3:] if cmd in help: cmds_doc.append(cmd) del help[cmd] elif getattr(self, name).__doc__: cmds_doc.append(cmd) elif cmd.lower() in COMMANDMAP and cmd.lower() in Usages: cmds_doc.append(cmd) else: cmds_undoc.append(cmd) self.stdout.write("%s\n" % str(self.doc_leader)) self.print_topics(self.doc_header, cmds_doc, 15, 80) self.print_topics(self.misc_header, help.keys(), 15, 80) self.print_topics(self.undoc_header, cmds_undoc, 15, 80)
def parseline(self, line): cmd, arg, line = super().parseline(line) if cmd: cmd = cmd.lower() if self._is_invalid_command(cmd, arg): return f'Invalid command: {line}', '', line return cmd, arg, line
def complete(self, text, state): """Return the next possible completion for 'text'.""" if state == 0: origline = readline.get_line_buffer() begidx = readline.get_begidx() endidx = readline.get_endidx() if begidx > 0: cmd, args, foo = self.parseline(origline) if cmd == '': compfunc = self.completedefault else: try: compfunc = getattr(self, 'complete_' + cmd.lower()) except AttributeError: try: compfunc = self.ctx.lookup_compfunction(cmd) except AttributeError: compfunc = self.completedefault else: compfunc = self.completenames arglist = [item.strip() for item in origline.strip().split()] comp_state = self.get_compstate(text, arglist) self.completion_matches = compfunc(text, origline, arglist, comp_state, begidx, endidx) try: return self.completion_matches[state] except: return None
def parseline(self, line): # Make the command part lowercase cmd, arg, line = super().parseline(line) if cmd == None: return cmd, arg, line else: return cmd.lower(), arg, line
def _do_command(self, command): """Execute a command This method routes the command to the appropriate methods for execution. command[in] Command to execute Returns bool True - exit utility, False - do not exit """ # do variable replacement command = self._replace_variables(command.strip(' ')) if self.options.get('verbosity', False): print "\nExecuting command:", command # process simple commands if command.lower().startswith('set '): self._add_variable(command[4:]) if not self.quiet: print elif command[0:14].lower() == 'show variables': self.variables.show_variables() elif self.custom_commands and \ command[0:12].lower() == 'show options': self.show_custom_options() else: cmd, parameters = self._get_util_parameters(command) cmd = cmd.strip() parameters = parameters.strip() if cmd.lower() == 'help': self.show_help(parameters) self.cmd_line.clear() self.tab_count = 0 elif cmd == '': print elif cmd.lower() in ['exit', 'quit']: print return True elif self.custom_commands: if not self.is_valid_custom_command(cmd): print "\n\nUnknown command: %s %s\n" % (cmd, parameters) else: try: self.execute_custom_command(cmd, parameters) print except UtilError, err: print err.errmsg
def run_console(self, lines=[]): """Run the console. This method is the main loop for executing commands. For all subclassed classes, the user need only call this method to execute an interactive shell or execute commands and exit. It can be used in three modes: 1) it can process commands passed via lines list 2) it can process commands passed via a pipe to the python exec 3) it can prompt for commands and execute them as entered Modes (1) and (2) execute all commands then exit. lines[in] If not empty, execute the list of commands. """ # If we have commands issued by the command line, execute and exit. if self.commands is not None: command_list = self.commands.split(';') for command in command_list: command = command.strip('\n').strip(' ') if os.name == 'nt': command = command.strip('"') if self._do_command(command.strip('"')): break # If we have piped input, read the input by line and execute elif not os.isatty(sys.stdin.fileno()) or len(lines) > 0: for command in sys.stdin.readlines(): command_list = command.split(';') for cmd in command_list: cmd = cmd.strip('\n').strip(' ') if os.name == 'nt': cmd = cmd.strip('"') if self._do_command(cmd.strip('"')): break # Otherwise, we are in an interactive mode where we get a command # from the user and execute else: cmd = '' if not self.quiet: print self.options.get('welcome', 'Welcome to the console!\n') while cmd.lower() not in ['exit', 'quit']: command = self.get_user_command() self.history.add(command) if self._do_command(command): break if not self.quiet: print self.options.get('goodbye', 'Thanks for using the console.\n')
def onecmd(self, line): """Run a single command. Exceptions should be caught by the caller""" cmd, arg, line = self.parseline(line) if not line: return self.emptyline() if cmd is None: return self.default(line) self.lastcmd = line if cmd == '': return self.default(line) else: try: # retrieve the command execution function, which will be # self.do_<command> func = getattr(self, 'do_' + cmd.lower()) except AttributeError: return self.default(line) return func(arg)
def parseline(self, line): """Override command line parsing for case insensitive commands lookup.""" cmd, arg, line = super().parseline(line) if cmd != 'EOF': cmd = cmd.lower() return cmd, arg, line