summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorJared Crapo <jared@kotfu.net>2017-12-11 19:14:09 -0700
committerJared Crapo <jared@kotfu.net>2017-12-11 19:14:09 -0700
commit4f57dc14a7ffc2cbd741622da7d05d42ba7f7789 (patch)
treea7342e6981d44b9b19b9852c3f0e822aa702f659 /cmd2.py
parentb16b91268ecbea934e96a68b7feb2965f4ab3c18 (diff)
downloadcmd2-git-4f57dc14a7ffc2cbd741622da7d05d42ba7f7789.tar.gz
Plan and first working code for argparse decorator
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/cmd2.py b/cmd2.py
index f01067a9..64388de9 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -233,6 +233,21 @@ def strip_quotes(arg):
return arg
+def with_argument_parser(argparser):
+ """A decorator to alter a cmd2 method to populate its ``opts``
+ argument by parsing arguments with the given instance of
+ argparse.ArgumentParser.
+ """
+ def arg_decorator(func):
+ def cmd_wrapper(instance, arg):
+ print("before command")
+ opts = argparser.parse_args(shlex.split(arg, posix=POSIX_SHLEX))
+ func(instance, arg, opts)
+ print("after command")
+ return cmd_wrapper
+ return arg_decorator
+
+
def options(option_list, arg_desc="arg"):
"""Used as a decorator and passed a list of optparse-style options,
alters a cmd2 method to populate its ``opts`` argument from its