Skip to content

Commit c1cbbab

Browse files
committed
Added namespace provider argument to argparse decorators to support custom Namespaces during argument parsing
1 parent 3ee97d1 commit c1cbbab

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

cmd2/cmd2.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,16 @@ def cmd_wrapper(cmd2_instance, statement: Union[Statement, str]):
191191
return arg_decorator
192192

193193

194-
def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve_quotes: bool = False) -> \
194+
def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser,
195+
ns_provider: Optional[Callable[[None], argparse.Namespace]] = None,
196+
preserve_quotes: bool = False) -> \
195197
Callable[[argparse.Namespace, List], Optional[bool]]:
196198
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments with the given
197199
instance of argparse.ArgumentParser, but also returning unknown args as a list.
198200
199201
:param argparser: unique instance of ArgumentParser
202+
:param ns_provider: an optional function that provides the namespace used in parsing. this is useful
203+
if state data affects how the command line is parsed
200204
:param preserve_quotes: if True, then arguments passed to argparse maintain their quotes
201205
:return: function that gets passed argparse-parsed args in a Namespace and a list of unknown argument strings
202206
A member called __statement__ is added to the Namespace to provide command functions access to the
@@ -213,8 +217,13 @@ def cmd_wrapper(cmd2_instance, statement: Union[Statement, str]):
213217
statement,
214218
preserve_quotes)
215219

220+
if ns_provider is None:
221+
namespace = None
222+
else:
223+
namespace = ns_provider(cmd2_instance)
224+
216225
try:
217-
args, unknown = argparser.parse_known_args(parsed_arglist)
226+
args, unknown = argparser.parse_known_args(parsed_arglist, namespace)
218227
except SystemExit:
219228
return
220229
else:
@@ -242,11 +251,14 @@ def cmd_wrapper(cmd2_instance, statement: Union[Statement, str]):
242251

243252

244253
def with_argparser(argparser: argparse.ArgumentParser,
254+
ns_provider: Optional[Callable[[None], argparse.Namespace]] = None,
245255
preserve_quotes: bool = False) -> Callable[[argparse.Namespace], Optional[bool]]:
246256
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments
247257
with the given instance of argparse.ArgumentParser.
248258
249259
:param argparser: unique instance of ArgumentParser
260+
:param ns_provider: an optional function that provides the namespace used in parsing. this is useful
261+
if state data affects how the command line is parsed
250262
:param preserve_quotes: if True, then arguments passed to argparse maintain their quotes
251263
:return: function that gets passed the argparse-parsed args in a Namespace
252264
A member called __statement__ is added to the Namespace to provide command functions access to the
@@ -261,8 +273,14 @@ def cmd_wrapper(cmd2_instance, statement: Union[Statement, str]):
261273
statement, parsed_arglist = cmd2_instance.statement_parser.get_command_arg_list(command_name,
262274
statement,
263275
preserve_quotes)
276+
277+
if ns_provider is None:
278+
namespace = None
279+
else:
280+
namespace = ns_provider(cmd2_instance)
281+
264282
try:
265-
args = argparser.parse_args(parsed_arglist)
283+
args = argparser.parse_args(parsed_arglist, namespace)
266284
except SystemExit:
267285
return
268286
else:

0 commit comments

Comments
 (0)