diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-28 13:53:27 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-28 13:53:27 -0400 |
commit | 4b4b3ef1819dde1c6d1ea7a70bf67e805d23470d (patch) | |
tree | 1f39e0d9f1dd4327a3d54fc381901ba7b83ac6cd /cmd2/cmd2.py | |
parent | 35e99b267c61ba38023fadfa6687785357c74aaf (diff) | |
parent | ed33f32b5924be3866c147bf8eaf088e65cd101d (diff) | |
download | cmd2-git-4b4b3ef1819dde1c6d1ea7a70bf67e805d23470d.tar.gz |
Merge branch 'macro' into argparse_conversion
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 61 |
1 files changed, 27 insertions, 34 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 2256558a..a2c2a46f 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1486,10 +1486,10 @@ class Cmd(cmd.Cmd): if compfunc is None: # There's no completer function, next see if the command uses argparser - cmd_func = self._cmd_func(command) - if cmd_func and hasattr(cmd_func, 'argparser'): + func = self.cmd_func(command) + if func and hasattr(func, 'argparser'): compfunc = functools.partial(self._autocomplete_default, - argparser=getattr(cmd_func, 'argparser')) + argparser=getattr(func, 'argparser')) else: compfunc = self.completedefault @@ -1970,17 +1970,16 @@ class Cmd(cmd.Cmd): self.redirecting = False - def _cmd_func(self, command: str) -> Optional[Callable]: + def cmd_func(self, command: str) -> Optional[Callable]: """ Get the function for a command :param command: the name of the command """ - func_name = self._cmd_func_name(command) + func_name = self.cmd_func_name(command) if func_name: return getattr(self, func_name) - return None - def _cmd_func_name(self, command: str) -> str: + def cmd_func_name(self, command: str) -> str: """Get the method name associated with a given command. :param command: command to look up method name which implements it @@ -2006,9 +2005,9 @@ class Cmd(cmd.Cmd): if statement.command in self.macros: stop = self._run_macro(statement) else: - cmd_func = self._cmd_func(statement.command) - if cmd_func: - stop = cmd_func(statement) + func = self.cmd_func(statement.command) + if func: + stop = func(statement) # Since we have a valid command store it in the history if statement.command not in self.exclude_from_history: @@ -2588,10 +2587,10 @@ class Cmd(cmd.Cmd): matches = [] # Check if this is a command with an argparse function - cmd_func = self._cmd_func(command) - if cmd_func and hasattr(cmd_func, 'argparser'): - completer = AutoCompleter(getattr(cmd_func, 'argparser'), cmd2_app=self) - matches = completer.complete_command_help(tokens[cmd_index:], text, line, begidx, endidx) + func = self.cmd_func(command) + if func and hasattr(func, 'argparser'): + completer = AutoCompleter(getattr(func, 'argparser'), cmd2_app=self) + matches = completer.complete_command_help(tokens[cmd_index:], text, line, begidx, endidx) return matches @@ -2611,21 +2610,15 @@ class Cmd(cmd.Cmd): if not args.command or args.verbose: self._help_menu(args.verbose) - elif args.command: + else: # Getting help for a specific command - cmd_func = self._cmd_func(args.command) - if cmd_func: - # Check to see if this function was decorated with an argparse ArgumentParser - if hasattr(cmd_func, 'argparser'): - completer = AutoCompleter(getattr(cmd_func, 'argparser'), cmd2_app=self) - tokens = [args.command] - tokens.extend(args.subcommand) - self.poutput(completer.format_help(tokens)) - else: - # No special behavior needed, delegate to cmd base class do_help() - super().do_help(args.command) + func = self.cmd_func(args.command) + if func and hasattr(func, 'argparser'): + completer = AutoCompleter(getattr(func, 'argparser'), cmd2_app=self) + tokens = [args.command] + args.subcommand + self.poutput(completer.format_help(tokens)) else: - # This could be a help topic + # No special behavior needed, delegate to cmd base class do_help() super().do_help(args.command) def _help_menu(self, verbose: bool=False) -> None: @@ -2642,12 +2635,12 @@ class Cmd(cmd.Cmd): cmds_cats = {} for command in visible_commands: - cmd_func = self._cmd_func(command) - if command in help_topics or cmd_func.__doc__: + func = self.cmd_func(command) + if command in help_topics or func.__doc__: if command in help_topics: help_topics.remove(command) - if hasattr(cmd_func, HELP_CATEGORY): - category = getattr(cmd_func, HELP_CATEGORY) + if hasattr(func, HELP_CATEGORY): + category = getattr(func, HELP_CATEGORY) cmds_cats.setdefault(category, []) cmds_cats[category].append(command) else: @@ -2700,13 +2693,13 @@ class Cmd(cmd.Cmd): func = getattr(self, 'help_' + command) except AttributeError: # Couldn't find a help function - cmd_func = self._cmd_func(command) + func = self.cmd_func(command) try: # Now see if help_summary has been set - doc = cmd_func.help_summary + doc = func.help_summary except AttributeError: # Last, try to directly access the function's doc-string - doc = cmd_func.__doc__ + doc = func.__doc__ else: # we found the help function result = io.StringIO() |