summaryrefslogtreecommitdiff
path: root/cmd2/argparse_custom.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-25 16:11:49 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-26 17:52:34 -0400
commit478ea83336a4d1659ad06ef365db3dc5e051e46d (patch)
treecbaff855e2d73f1d86ae1230f32770e31c62ad6b /cmd2/argparse_custom.py
parent97c348c599d8fa963553593e5c19fb100b85e313 (diff)
downloadcmd2-git-478ea83336a4d1659ad06ef365db3dc5e051e46d.tar.gz
The functions cmd2 adds to Namespaces (get_statement() and get_handler()) are now
Cmd2AttributeWrapper objects named cmd2_statement and cmd2_handler. This makes it easy to filter out which attributes in an argparse.Namespace were added by cmd2.
Diffstat (limited to 'cmd2/argparse_custom.py')
-rw-r--r--cmd2/argparse_custom.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py
index 12c18644..45abe6b2 100644
--- a/cmd2/argparse_custom.py
+++ b/cmd2/argparse_custom.py
@@ -221,7 +221,7 @@ import re
import sys
# noinspection PyUnresolvedReferences,PyProtectedMember
from argparse import ONE_OR_MORE, ZERO_OR_MORE, ArgumentError, _
-from typing import Callable, Optional, Tuple, Type, Union
+from typing import Any, Callable, Optional, Tuple, Type, Union
from . import ansi, constants
@@ -904,6 +904,24 @@ class Cmd2ArgumentParser(argparse.ArgumentParser):
ansi.style_aware_write(file, message)
+class Cmd2AttributeWrapper:
+ """
+ Wraps a cmd2-specific attribute added to an argparse Namespace.
+ This makes it easy to know which attributes in a Namespace are
+ arguments from a parser and which were added by cmd2.
+ """
+ def __init__(self, attribute: Any):
+ self.__attribute = attribute
+
+ def get(self) -> Any:
+ """Get the value of the attribute"""
+ return self.__attribute
+
+ def set(self, new_val: Any) -> None:
+ """Set the value of the attribute"""
+ self.__attribute = new_val
+
+
# The default ArgumentParser class for a cmd2 app
DEFAULT_ARGUMENT_PARSER = Cmd2ArgumentParser