summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcmd2.py2
-rw-r--r--docs/argument_processing.rst13
-rwxr-xr-xexamples/argparse_example.py4
-rw-r--r--tests/test_argparse.py21
4 files changed, 27 insertions, 13 deletions
diff --git a/cmd2.py b/cmd2.py
index 7540bd00..a660b348 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -258,7 +258,7 @@ def with_argument_parser(argparser):
temp_arglist.append(strip_quotes(arg))
lexed_arglist = temp_arglist
opts = argparser.parse_args(lexed_arglist)
- func(instance, cmdline, opts)
+ func(instance, lexed_arglist, opts)
# argparser defaults the program name to sys.argv[0]
# we want it to be the name of our command
diff --git a/docs/argument_processing.rst b/docs/argument_processing.rst
index f2cd394b..a8dabbab 100644
--- a/docs/argument_processing.rst
+++ b/docs/argument_processing.rst
@@ -13,8 +13,8 @@ Argument Processing
These features are all provided by the ``@with_argument_parser`` decorator.
-Using the decorator
-===================
+Using the argument parser decorator
+===================================
For each command in the ``cmd2`` subclass which requires argument parsing,
create an instance of ``argparse.ArgumentParser()`` which can parse the
@@ -31,8 +31,9 @@ Here's what it looks like::
argparser.add_argument('word', nargs='?', help='word to say')
@with_argument_parser(argparser)
- def do_speak(self, argv, opts)
+ def do_speak(self, arglist, opts)
"""Repeats what you tell me to."""
+ # arglist contains a list of arguments as parsed by shlex.split()
arg = opts.word
if opts.piglatin:
arg = '%s%say' % (arg[1:], arg[0])
@@ -42,6 +43,8 @@ Here's what it looks like::
for i in range(min(repetitions, self.maxrepeats)):
self.poutput(arg)
+This decorator also changes the value passed to the first argument of the ``do_*`` method. Instead of a string, the method will be passed a list of arguments as parsed by ``shlex.split()``.
+
.. note::
The ``@with_argument_parser`` decorator sets the ``prog`` variable in
@@ -62,7 +65,7 @@ appended to the docstring for the method of that command. With this code::
argparser.add_argument('tag', nargs=1, help='tag')
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@with_argument_parser(argparser)
- def do_tag(self, cmdline, args=None):
+ def do_tag(self, arglist, args=None):
"""create a html tag"""
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
self.stdout.write('\n')
@@ -88,7 +91,7 @@ If you would prefer the short description of your command to come after the usag
argparser.add_argument('tag', nargs=1, help='tag')
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@with_argument_parser(argparser)
- def do_tag(self, cmdline, args=None):
+ def do_tag(self, arglist, args=None):
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
self.stdout.write('\n')
diff --git a/examples/argparse_example.py b/examples/argparse_example.py
index d784ccf5..3574f549 100755
--- a/examples/argparse_example.py
+++ b/examples/argparse_example.py
@@ -47,7 +47,7 @@ class CmdLineApp(Cmd):
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
argparser.add_argument('words', nargs='+', help='words to say')
@with_argument_parser(argparser)
- def do_speak(self, cmdline, args=None):
+ def do_speak(self, arglist, args=None):
"""Repeats what you tell me to."""
words = []
for word in args.words:
@@ -68,7 +68,7 @@ class CmdLineApp(Cmd):
argparser.add_argument('tag', nargs=1, help='tag')
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@with_argument_parser(argparser)
- def do_tag(self, argv, args=None):
+ def do_tag(self, arglist, args=None):
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
self.stdout.write('\n')
# self.stdout.write is better than "print", because Cmd can be
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 52ce7de8..1ca455e9 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -20,7 +20,7 @@ class ArgparseApp(cmd2.Cmd):
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
argparser.add_argument('words', nargs='+', help='words to say')
@cmd2.with_argument_parser(argparser)
- def do_say(self, cmdline, args=None):
+ def do_say(self, arglist, args=None):
"""Repeat what you tell me to."""
words = []
for word in args.words:
@@ -40,21 +40,30 @@ class ArgparseApp(cmd2.Cmd):
argparser.add_argument('tag', nargs=1, help='tag')
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@cmd2.with_argument_parser(argparser)
- def do_tag(self, cmdline, args=None):
+ def do_tag(self, arglist, args=None):
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
self.stdout.write('\n')
argparser = argparse.ArgumentParser()
argparser.add_argument('args', nargs='*')
@cmd2.with_argument_parser(argparser)
- def do_compare(self, cmdline, args=None):
- cmdline_str = re.sub('\s+', ' ', cmdline)
+ def do_compare(self, arglist, args=None):
+ cmdline_str = re.sub('\s+', ' ', ' '.join(arglist))
args_str = re.sub('\s+', ' ', ' '.join(args.args))
if cmdline_str == args_str:
self.stdout.write('True')
else:
self.stdout.write('False')
+ argparser = argparse.ArgumentParser()
+ argparser.add_argument('args', nargs='*')
+ @cmd2.with_argument_parser(argparser)
+ def do_arglist(self, arglist, args=None):
+ if isinstance(arglist, list):
+ self.stdout.write('True')
+ else:
+ self.stdout.write('False')
+
@pytest.fixture
def argparse_app():
app = ArgparseApp()
@@ -104,4 +113,6 @@ def test_argparse_cmdline(argparse_app):
out = run_cmd(argparse_app, 'compare this is a test')
assert out[0] == 'True'
- \ No newline at end of file
+def test_argparse_arglist(argparse_app):
+ out = run_cmd(argparse_app, 'arglist "some arguments" and some more')
+ assert out[0] == 'True'