diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-18 20:05:22 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-18 20:05:22 -0400 |
commit | 6b0d357f1bf10c99397e88a6e914f53f95add6a6 (patch) | |
tree | 212996e2b24340c890ba4bdcaece402e8cf09aad /cmd2 | |
parent | d589e0a91afb609f0401deac20c9f709ad996b25 (diff) | |
parent | 9b1852a9f99a4fa248224c18c8cd2092edbbd8d2 (diff) | |
download | cmd2-git-6b0d357f1bf10c99397e88a6e914f53f95add6a6.tar.gz |
Merge branch 'master' into sigint_protection
Diffstat (limited to 'cmd2')
-rw-r--r-- | cmd2/__init__.py | 2 | ||||
-rw-r--r-- | cmd2/argparse_custom.py | 10 | ||||
-rw-r--r-- | cmd2/cmd2.py | 34 |
3 files changed, 23 insertions, 23 deletions
diff --git a/cmd2/__init__.py b/cmd2/__init__.py index f05e29ec..2b3bec5d 100644 --- a/cmd2/__init__.py +++ b/cmd2/__init__.py @@ -11,7 +11,7 @@ except DistributionNotFound: pass from .ansi import style -from .argparse_custom import ArgParser, CompletionItem +from .argparse_custom import Cmd2ArgumentParser, CompletionItem from .cmd2 import Cmd, Statement, EmptyStatement, categorize from .cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category from .constants import DEFAULT_SHORTCUTS diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py index 5d8e76ef..8da47461 100644 --- a/cmd2/argparse_custom.py +++ b/cmd2/argparse_custom.py @@ -1,14 +1,14 @@ # coding=utf-8 """ This module adds capabilities to argparse by patching a few of its functions. It also defines a parser -class called ArgParser which improves error and help output over normal argparse. All cmd2 code uses +class called Cmd2ArgumentParser which improves error and help output over normal argparse. All cmd2 code uses this parser and it is recommended that developers of cmd2-based apps either use it or write their own parser that inherits from it. This will give a consistent look-and-feel between the help/error output of built-in cmd2 commands and the app-specific commands. -Since the new capabilities are added by patching at the argparse API level, they are available whether or -not ArgParser is used. However, the help and error output of ArgParser is customized to notate nargs ranges -whereas any other parser class won't be as explicit in their output. +Since the new capabilities are added by patching at the argparse API level, they are available whether or not +Cmd2ArgumentParser is used. However, the help and error output of Cmd2ArgumentParser is customized to notate +nargs ranges whereas any other parser class won't be as explicit in their output. ############################################################################################################ # Added capabilities @@ -634,7 +634,7 @@ class Cmd2HelpFormatter(argparse.RawTextHelpFormatter): # noinspection PyCompatibility -class ArgParser(argparse.ArgumentParser): +class Cmd2ArgumentParser(argparse.ArgumentParser): """Custom ArgumentParser class that improves error and help output""" def __init__(self, *args, **kwargs) -> None: diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 0336c617..d8bb3b1b 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -43,7 +43,7 @@ from collections import namedtuple from contextlib import redirect_stdout from typing import Any, Callable, Dict, Iterable, List, Mapping, Optional, Tuple, Type, Union -from . import ArgParser, CompletionItem +from . import Cmd2ArgumentParser, CompletionItem from . import ansi from . import constants from . import plugin @@ -2390,7 +2390,7 @@ class Cmd(cmd.Cmd): "An alias is a command that enables replacement of a word by another string.") alias_epilog = ("See also:\n" " macro") - alias_parser = ArgParser(description=alias_description, epilog=alias_epilog, prog='alias') + alias_parser = Cmd2ArgumentParser(description=alias_description, epilog=alias_epilog, prog='alias') # Add sub-commands to alias alias_subparsers = alias_parser.add_subparsers() @@ -2570,7 +2570,7 @@ class Cmd(cmd.Cmd): "A macro is similar to an alias, but it can contain argument placeholders.") macro_epilog = ("See also:\n" " alias") - macro_parser = ArgParser(description=macro_description, epilog=macro_epilog, prog='macro') + macro_parser = Cmd2ArgumentParser(description=macro_description, epilog=macro_epilog, prog='macro') # Add sub-commands to macro macro_subparsers = macro_parser.add_subparsers() @@ -2702,7 +2702,7 @@ class Cmd(cmd.Cmd): return matches - help_parser = ArgParser() + help_parser = Cmd2ArgumentParser() help_parser.add_argument('command', nargs=argparse.OPTIONAL, help="command to retrieve help for", completer_method=complete_help_command) help_parser.add_argument('subcommand', nargs=argparse.REMAINDER, help="sub-command to retrieve help for", @@ -2866,7 +2866,7 @@ class Cmd(cmd.Cmd): command = '' self.stdout.write("\n") - @with_argparser(ArgParser()) + @with_argparser(Cmd2ArgumentParser()) def do_shortcuts(self, _: argparse.Namespace) -> None: """List available shortcuts""" # Sort the shortcut tuples by name @@ -2874,13 +2874,13 @@ class Cmd(cmd.Cmd): result = "\n".join('{}: {}'.format(sc[0], sc[1]) for sc in sorted_shortcuts) self.poutput("Shortcuts for other commands:\n{}".format(result)) - @with_argparser(ArgParser(epilog=INTERNAL_COMMAND_EPILOG)) + @with_argparser(Cmd2ArgumentParser(epilog=INTERNAL_COMMAND_EPILOG)) def do_eof(self, _: argparse.Namespace) -> bool: """Called when <Ctrl>-D is pressed""" # Return True to stop the command loop return True - @with_argparser(ArgParser()) + @with_argparser(Cmd2ArgumentParser()) def do_quit(self, _: argparse.Namespace) -> bool: """Exit this application""" # Return True to stop the command loop @@ -2975,7 +2975,7 @@ class Cmd(cmd.Cmd): "Accepts abbreviated parameter names so long as there is no ambiguity.\n" "Call without arguments for a list of settable parameters with their values.") - set_parser = ArgParser(description=set_description) + set_parser = Cmd2ArgumentParser(description=set_description) set_parser.add_argument('-a', '--all', action='store_true', help='display read-only settings as well') set_parser.add_argument('-l', '--long', action='store_true', help='describe function of parameter') set_parser.add_argument('param', nargs=argparse.OPTIONAL, help='parameter to set or view', @@ -3020,7 +3020,7 @@ class Cmd(cmd.Cmd): if onchange_hook is not None: onchange_hook(old=orig_value, new=new_value) # pylint: disable=not-callable - shell_parser = ArgParser() + shell_parser = Cmd2ArgumentParser() shell_parser.add_argument('command', help='the command to run', completer_method=shell_cmd_complete) shell_parser.add_argument('command_args', nargs=argparse.REMAINDER, help='arguments to pass to command', completer_method=path_complete) @@ -3185,7 +3185,7 @@ class Cmd(cmd.Cmd): "If you see strange parsing behavior, it's best to just open the Python shell\n" "by providing no arguments to py and run more complex statements there.") - py_parser = ArgParser(description=py_description) + py_parser = Cmd2ArgumentParser(description=py_description) py_parser.add_argument('command', nargs=argparse.OPTIONAL, help="command to run") py_parser.add_argument('remainder', nargs=argparse.REMAINDER, help="remainder of command") @@ -3292,7 +3292,7 @@ class Cmd(cmd.Cmd): return bridge.stop - run_pyscript_parser = ArgParser() + run_pyscript_parser = Cmd2ArgumentParser() run_pyscript_parser.add_argument('script_path', help='path to the script file', completer_method=path_complete) run_pyscript_parser.add_argument('script_arguments', nargs=argparse.REMAINDER, help='arguments to pass to script', completer_method=path_complete) @@ -3325,7 +3325,7 @@ class Cmd(cmd.Cmd): # Only include the do_ipy() method if IPython is available on the system if ipython_available: # pragma: no cover - @with_argparser(ArgParser()) + @with_argparser(Cmd2ArgumentParser()) def do_ipy(self, _: argparse.Namespace) -> None: """Enter an interactive IPython shell""" from .pyscript_bridge import PyscriptBridge @@ -3348,7 +3348,7 @@ class Cmd(cmd.Cmd): history_description = "View, run, edit, save, or clear previously entered commands" - history_parser = ArgParser(description=history_description) + history_parser = Cmd2ArgumentParser(description=history_description) history_action_group = history_parser.add_mutually_exclusive_group() history_action_group.add_argument('-r', '--run', action='store_true', help='run selected history items') history_action_group.add_argument('-e', '--edit', action='store_true', @@ -3647,7 +3647,7 @@ class Cmd(cmd.Cmd): "\n" " set editor (program-name)") - edit_parser = ArgParser(description=edit_description) + edit_parser = Cmd2ArgumentParser(description=edit_description) edit_parser.add_argument('file_path', nargs=argparse.OPTIONAL, help="path to a file to open in editor", completer_method=path_complete) @@ -3679,7 +3679,7 @@ class Cmd(cmd.Cmd): "If the -t/--transcript flag is used, this command instead records\n" "the output of the script commands to a transcript for testing purposes.\n") - run_script_parser = ArgParser(description=run_script_description) + run_script_parser = Cmd2ArgumentParser(description=run_script_description) run_script_parser.add_argument('-t', '--transcript', metavar='TRANSCRIPT_FILE', help='record the output of the script as a transcript file', completer_method=path_complete) @@ -3746,8 +3746,8 @@ class Cmd(cmd.Cmd): relative_run_script_epilog = ("Notes:\n" " This command is intended to only be used within text file scripts.") - relative_run_script_parser = ArgParser(description=relative_run_script_description, - epilog=relative_run_script_epilog) + relative_run_script_parser = Cmd2ArgumentParser(description=relative_run_script_description, + epilog=relative_run_script_epilog) relative_run_script_parser.add_argument('file_path', help='a file path pointing to a script') @with_argparser(relative_run_script_parser) |