diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-05 13:32:04 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-05 13:32:04 -0400 |
commit | 734796b895bb85d67c651d9e8e8bf71e7d60b7e3 (patch) | |
tree | 5879270409a4992622da2933275ccaae30525ee6 /cmd2 | |
parent | 49cbec9969b4b53248d6097d8f395d92c74f7228 (diff) | |
download | cmd2-git-734796b895bb85d67c651d9e8e8bf71e7d60b7e3.tar.gz |
Using sub-command instead of subcommand where possible to be consistent with argparse
Diffstat (limited to 'cmd2')
-rwxr-xr-x | cmd2/argparse_completer.py | 7 | ||||
-rw-r--r-- | cmd2/cmd2.py | 24 | ||||
-rw-r--r-- | cmd2/pyscript_bridge.py | 2 |
3 files changed, 20 insertions, 13 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index ad2c520b..c900a780 100755 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -917,6 +917,13 @@ class ACArgumentParser(argparse.ArgumentParser): self._custom_error_message = custom_message # End cmd2 customization + def add_subparsers(self, **kwargs): + """Custom override. Sets a default title if one was not given.""" + if 'title' not in kwargs: + kwargs['title'] = 'sub-commands' + + return super().add_subparsers(**kwargs) + def error(self, message: str) -> None: """Custom error override. Allows application to control the error being displayed by argparse""" if len(self._custom_error_message) > 0: diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 7aaacdb1..c95fbd51 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -2204,7 +2204,7 @@ class Cmd(cmd.Cmd): return stop - # ----- Alias subcommand functions ----- + # ----- Alias sub-command functions ----- def alias_create(self, args: argparse.Namespace): """Create or overwrite an alias""" @@ -2267,8 +2267,8 @@ class Cmd(cmd.Cmd): " macro") alias_parser = ACArgumentParser(description=alias_description, epilog=alias_epilog, prog='alias') - # Add subcommands to alias - alias_subparsers = alias_parser.add_subparsers(title='sub-commands') + # Add sub-commands to alias + alias_subparsers = alias_parser.add_subparsers() # alias -> create alias_create_help = "create or overwrite an alias" @@ -2326,13 +2326,13 @@ class Cmd(cmd.Cmd): """Manage aliases""" func = getattr(args, 'func', None) if func is not None: - # Call whatever subcommand function was selected + # Call whatever sub-command function was selected func(self, args) else: - # No subcommand was provided, so call help + # No sub-command was provided, so call help self.do_help('alias') - # ----- Macro subcommand functions ----- + # ----- Macro sub-command functions ----- def macro_create(self, args: argparse.Namespace): """Create or overwrite a macro""" @@ -2445,8 +2445,8 @@ class Cmd(cmd.Cmd): " alias") macro_parser = ACArgumentParser(description=macro_description, epilog=macro_epilog, prog='macro') - # Add subcommands to macro - macro_subparsers = macro_parser.add_subparsers(title='sub-commands') + # Add sub-commands to macro + macro_subparsers = macro_parser.add_subparsers() # macro -> create macro_create_help = "create or overwrite a macro" @@ -2527,10 +2527,10 @@ class Cmd(cmd.Cmd): """Manage macros""" func = getattr(args, 'func', None) if func is not None: - # Call whatever subcommand function was selected + # Call whatever sub-command function was selected func(self, args) else: - # No subcommand was provided, so call help + # No sub-command was provided, so call help self.do_help('macro') def complete_help_command(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: @@ -2551,7 +2551,7 @@ class Cmd(cmd.Cmd): if not tokens: return [] - # Must have at least 3 args for 'help command subcommand' + # Must have at least 3 args for 'help command sub-command' if len(tokens) < 3: return [] @@ -2580,7 +2580,7 @@ class Cmd(cmd.Cmd): setattr(help_parser.add_argument('command', help="command to retrieve help for", nargs="?"), ACTION_ARG_CHOICES, ('complete_help_command',)) - setattr(help_parser.add_argument('subcommand', help="subcommand to retrieve help for", + setattr(help_parser.add_argument('subcommand', help="sub-command to retrieve help for", nargs=argparse.REMAINDER), ACTION_ARG_CHOICES, ('complete_help_subcommand',)) help_parser.add_argument('-v', '--verbose', action='store_true', diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py index a70a7ae6..2002ca6d 100644 --- a/cmd2/pyscript_bridge.py +++ b/cmd2/pyscript_bridge.py @@ -87,7 +87,7 @@ class ArgparseFunctor: return commands def __getattr__(self, item: str): - """Search for a subcommand matching this item and update internal state to track the traversal""" + """Search for a sub-command matching this item and update internal state to track the traversal""" # look for sub-command under the current command/sub-command layer for action in self.__current_subcommand_parser._actions: if not action.option_strings and isinstance(action, argparse._SubParsersAction): |