diff options
author | Eric Lin <anselor@gmail.com> | 2018-04-23 10:19:55 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-04-23 10:19:55 -0400 |
commit | cbb94bff0cfed8c0ccbf8e0c2dd4c61f59881573 (patch) | |
tree | 40945f85aeff71f639bffe1fa7c87ef23a8c17dd /cmd2 | |
parent | 102fc6741b5dfdbb02f28ee720214c70d1260cc0 (diff) | |
download | cmd2-git-cbb94bff0cfed8c0ccbf8e0c2dd4c61f59881573.tar.gz |
Addressed PR comments. Simplified some of the implementation per PR comments.
Diffstat (limited to 'cmd2')
-rwxr-xr-x | cmd2/argparse_completer.py | 2 | ||||
-rwxr-xr-x | cmd2/cmd2.py | 35 |
2 files changed, 17 insertions, 20 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index 6e53a518..e87e9c04 100755 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -415,7 +415,7 @@ class AutoCompleter(object): return completion_results def complete_command_help(self, tokens: List[str], text: str, line: str, begidx: int, endidx: int) -> List[str]: - """Supports the completion of sub-commands for commands thhrough the cmd2 help command.""" + """Supports the completion of sub-commands for commands through the cmd2 help command.""" for idx, token in enumerate(tokens): if idx >= self._token_start_index: if self._positional_completers: diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 10f790c0..288a506b 100755 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -267,8 +267,7 @@ def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser) -> Calla cmd_wrapper.__doc__ = argparser.format_help() - # Mark this function as having an argparse ArgumentParser (used by do_help) - cmd_wrapper.__dict__['has_parser'] = True + # Mark this function as having an argparse ArgumentParser setattr(cmd_wrapper, 'argparser', argparser) return cmd_wrapper @@ -305,8 +304,7 @@ def with_argparser(argparser: argparse.ArgumentParser) -> Callable: cmd_wrapper.__doc__ = argparser.format_help() - # Mark this function as having an argparse ArgumentParser (used by do_help) - cmd_wrapper.__dict__['has_parser'] = True + # Mark this function as having an argparse ArgumentParser setattr(cmd_wrapper, 'argparser', argparser) return cmd_wrapper @@ -1724,14 +1722,12 @@ class Cmd(cmd.Cmd): compfunc = getattr(self, 'complete_' + command) except AttributeError: # There's no completer function, next see if the command uses argparser - cmd_func = getattr(self, 'do_' + command) - if hasattr(cmd_func, 'has_parser') and hasattr(cmd_func, 'argparser') and \ - getattr(cmd_func, 'has_parser'): - # Command uses argparser, switch to the default argparse completer + try: + cmd_func = getattr(self, 'do_' + command) argparser = getattr(cmd_func, 'argparser') - compfunc = functools.partial(self._autocomplete_default, - argparser=argparser) - else: + # Command uses argparser, switch to the default argparse completer + compfunc = functools.partial(self._autocomplete_default, argparser=argparser) + except AttributeError: compfunc = self.completedefault # A valid command was not entered @@ -1904,13 +1900,14 @@ class Cmd(cmd.Cmd): matches = self.basic_complete(text, line, begidx, endidx, strs_to_match) # check if the command uses argparser - elif index >= subcmd_index and hasattr(self, 'do_' + tokens[cmd_index]) and\ - hasattr(getattr(self, 'do_' + tokens[cmd_index]), 'has_parser'): - command = tokens[cmd_index] - cmd_func = getattr(self, 'do_' + command) - parser = getattr(cmd_func, 'argparser') - completer = AutoCompleter(parser) - matches = completer.complete_command_help(tokens[1:], text, line, begidx, endidx) + elif index >= subcmd_index: + try: + cmd_func = getattr(self, 'do_' + tokens[cmd_index]) + parser = getattr(cmd_func, 'argparser') + completer = AutoCompleter(parser) + matches = completer.complete_command_help(tokens[1:], text, line, begidx, endidx) + except AttributeError: + pass return matches @@ -2561,7 +2558,7 @@ Usage: Usage: unalias [-a] name [name ...] if funcname: # Check to see if this function was decorated with an argparse ArgumentParser func = getattr(self, funcname) - if func.__dict__.get('has_parser', False): + if hasattr(func, 'argparser'): # Function has an argparser, so get help based on all the arguments in case there are sub-commands new_arglist = arglist[1:] new_arglist.append('-h') |