def main(self, args=sys.argv[1:]): """ Run this command. @type args: list of str's @param args: command line arguments """ if type(args) == str: args = parse_tokens(args) try: self.setup_parser() self.opts, args = self.parser.parse_args(args) if not args: self.parser.error(_('No command given; please see --help')) command = self.extract_command(args) # process command and action options before setup_credentials # to catch errors before accessing Kerberos command_args = args[1:] command.process_options(command_args) self.setup_server() action = command.extract_action(command_args) if not action or action.require_credentials(): self.setup_credentials() return command.main(command_args) except OptionParserExitError, opee: return opee.args[0]
def completeparams(self, text, line, *ignored): parts = parse_tokens(line) cmdName = parts[0] actionName = parts[1] action = self.admin_cli.get_command(cmdName).get_action(actionName) params = action.create_parser().get_long_options() return [a for a in params if a.startswith(text)]
def __parse_line(self, line): line_parts = parse_tokens(line) if line.endswith(" ") or not len(line): last_word = "" cmd = self.__get_command(line_parts) else: last_word = line_parts[-1] cmd = self.__get_command(line_parts[:-1]) return (last_word, cmd)
def main(self, args, command_name=None, parent_usage=None): if type(args) == str: args = parse_tokens(args) parser = self.create_parser(command_name, parent_usage) self.process_options(parser, args) self.run() subcommand = self._extract_command(parser, self.args) return subcommand.main(self.args[1:], self.args[0], self._get_usage_line(command_name, parent_usage))
def main(self, args, command_name=None, parent_usage=None): if type(args) == str: args = parse_tokens(args) parser = self.create_parser(command_name, parent_usage) self.process_options(parser, args) self.run() subcommand = self._extract_command(parser, self.args) return subcommand.main( self.args[1:], self.args[0], self._get_usage_line(command_name, parent_usage))
def complete(self, text, state): """ Return the next possible completion for 'text'. """ if state == 0: line = readline.get_line_buffer().lstrip() line_parts = parse_tokens(line) if len(line_parts) <= 1: self.completion_matches = self.__complete(text, line_parts, with_params=False) else: self.completion_matches = self.__complete(text, line_parts, with_params=True) try: return self.completion_matches[state] except IndexError: return None
def main(self, args): """ Main execution of a katello cli command This method parses options sent to the command itself, looks up the corresponding action, and calls that action's main() @warning: this method should only be overridden with care @type args: list of str's @param args: command line arguments to parse """ if type(args) == str: args = parse_tokens(args) try: self.process_options(args) action = self.extract_action(args) return action.main(args[1:]) except OptionParserExitError, opee: return opee.args[0]
def precmd(self, line): # remove leading/trailing whitespace line = re.sub("^\s+|\s+$", "", line) # don't do anything on empty lines if line == "": return "" # terminate the shell if re.match("quit|exit|eof", line, re.I): return "quit" line = line.strip() parts = parse_tokens(line) if len(parts): command = parts[0] else: return "" if len(parts[1:]): args = " ".join(parts[1:]) else: args = "" # print the help message if the user passes '--help' line_parts = line.split('"') for i in range(0, len(line_parts), 2): if re.search("--help", line_parts[i]) or re.search("-h", line_parts[i]): return "help %s" % line # should we look for an item in the history? if command[0] != "!" or len(command) < 2: return line # remove the '!*' line from the history self.remove_last_history_item() history_match = False if command[1] == "!": # repeat the last command line = readline.get_history_item(readline.get_current_history_length()) if line: history_match = True else: logging.warning("%s: event not found" % command) return "" # attempt to find a numbered history item if not history_match: try: number = int(command[1:]) line = readline.get_history_item(number) if line: history_match = True except IndexError: pass except ValueError: pass # attempt to match the beginning of the string with a history item if not history_match: history_range = range(1, readline.get_current_history_length()) history_range.reverse() for i in history_range: item = readline.get_history_item(i) if re.match(command[1:], item): line = item history_match = True break # append the arguments to the substituted command if history_match: # terminate the shell if re.match("quit|exit|eof", line, re.I): print line return "quit" line += " %s" % args readline.add_history(line) print line return line else: logging.warning("%s: event not found" % command) return ""