diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-08-23 13:43:29 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-08-23 13:43:29 -0400 |
commit | d464e2a7f625af607defca08c5f92fb530c6de3e (patch) | |
tree | 155a74f4d3b45dc9c8214871e9bf78df2e9a0ae9 | |
parent | 548c97c516eea815471c0759f7b005ea836fea00 (diff) | |
download | cmd2-git-custom_command_completer.tar.gz |
Updated documentation for setting custom parserscustom_command_completer
-rw-r--r-- | cmd2/argparse_custom.py | 5 | ||||
-rw-r--r-- | examples/custom_parser.py | 2 | ||||
-rwxr-xr-x | examples/override_parser.py | 4 |
3 files changed, 8 insertions, 3 deletions
diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py index 14f3c110..4f2ef116 100644 --- a/cmd2/argparse_custom.py +++ b/cmd2/argparse_custom.py @@ -1320,6 +1320,9 @@ DEFAULT_ARGUMENT_PARSER: Type[argparse.ArgumentParser] = Cmd2ArgumentParser def set_default_argument_parser(parser: Type[argparse.ArgumentParser]) -> None: - """Set the default ArgumentParser class for a cmd2 app""" + """ + Set the default ArgumentParser class for a cmd2 app. This must be called prior to loading cmd2.py if + you want to override the parser for cmd2's built-in commands. See examples/override_parser.py. + """ global DEFAULT_ARGUMENT_PARSER DEFAULT_ARGUMENT_PARSER = parser diff --git a/examples/custom_parser.py b/examples/custom_parser.py index 6df68bfa..ea66e7e1 100644 --- a/examples/custom_parser.py +++ b/examples/custom_parser.py @@ -31,6 +31,8 @@ class CustomParser(Cmd2ArgumentParser): linum += 1 self.print_usage(sys.stderr) + + # Format errors with style_warning() formatted_message = ansi.style_warning(formatted_message) self.exit(2, '{}\n\n'.format(formatted_message)) diff --git a/examples/override_parser.py b/examples/override_parser.py index bbe9f6e2..2e778c07 100755 --- a/examples/override_parser.py +++ b/examples/override_parser.py @@ -6,14 +6,14 @@ The standard parser used by cmd2 built-in commands is Cmd2ArgumentParser. The following code shows how to override it with your own parser class. """ -# First set a value called argparse.cmd2_parser_module with the module that defines the custom parser +# First set a value called argparse.cmd2_parser_module with the module that defines the custom parser. # See the code for custom_parser.py. It simply defines a parser and calls cmd2.set_default_argument_parser() # with the custom parser's type. import argparse argparse.cmd2_parser_module = 'examples.custom_parser' -# Next import stuff from cmd2. It will import your module just before the cmd2.Cmd class file is imported +# Next import from cmd2. It will import your module just before the cmd2.Cmd class file is imported # and therefore override the parser class it uses on its commands. from cmd2 import ( cmd2, |