summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/cmd2.py b/cmd2.py
index 73ad491d..d62e8de6 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -241,6 +241,33 @@ 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")
+ # Use shlex to split the command line into a list of arguments based on shell rules
+ opts = argparser.parse_args(shlex.split(arg, posix=POSIX_SHLEX))
+ #import ipdb; ipdb.set_trace()
+
+
+ # If not using POSIX shlex, make sure to strip off outer quotes for convenience
+ if not POSIX_SHLEX and STRIP_QUOTES_FOR_NON_POSIX:
+ newopts = opts
+# for key, val in vars(opts):
+# if isinstance(val, str):
+# newopts[key] = strip_quotes(val)
+ opts = newopts
+### 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