summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcmd2.py22
-rwxr-xr-xexamples/argparse_example.py32
-rw-r--r--tests/test_argparse.py36
3 files changed, 48 insertions, 42 deletions
diff --git a/cmd2.py b/cmd2.py
index d62e8de6..15aa4576 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -31,6 +31,7 @@ import datetime
import glob
import io
import optparse
+import argparse
import os
import platform
import re
@@ -248,22 +249,19 @@ def with_argument_parser(argparser):
"""
def arg_decorator(func):
def cmd_wrapper(instance, arg):
- #print("before command")
+ 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()
-
-
+ lexed_arglist = shlex.split(arg, posix=POSIX_SHLEX)
# 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))
+ temp_arglist = []
+ for arg in lexed_arglist:
+ temp_arglist.append(strip_quotes(arg))
+ lexed_arglist = temp_arglist
+ opts = argparser.parse_args(lexed_arglist)
+
func(instance, arg, opts)
- #print("after command")
+ print("after command")
return cmd_wrapper
return arg_decorator
diff --git a/examples/argparse_example.py b/examples/argparse_example.py
index 805bab77..96f8118d 100755
--- a/examples/argparse_example.py
+++ b/examples/argparse_example.py
@@ -45,14 +45,16 @@ class CmdLineApp(Cmd):
])
def do_speak(self, arg, opts=None):
"""Repeats what you tell me to."""
- arg = ''.join(arg)
- if opts.piglatin:
- arg = '%s%say' % (arg[1:], arg[0])
- if opts.shout:
- arg = arg.upper()
+ words = []
+ for word in arg:
+ if opts.piglatin:
+ word = '%s%say' % (word[1:], word[0])
+ if opts.shout:
+ arg = arg.upper()
+ words.append(word)
repetitions = opts.repeat or 1
for i in range(min(repetitions, self.maxrepeats)):
- self.stdout.write(arg)
+ self.stdout.write(' '.join(words))
self.stdout.write('\n')
# self.stdout.write is better than "print", because Cmd can be
# initialized with a non-standard output destination
@@ -64,20 +66,20 @@ class CmdLineApp(Cmd):
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
- argparser.add_argument('word', nargs='?', help='word to say')
+ argparser.add_argument('words', nargs='+', help='words to say')
@with_argument_parser(argparser)
def do_sspeak(self, rawarg, args=None):
"""Repeats what you tell me to."""
- word = args.word
- if word is None:
- word = ''
- if args.piglatin:
- word = '%s%say' % (word[1:], word[0])
- if args.shout:
- word = word.upper()
+ words = []
+ for word in args.words:
+ if args.piglatin:
+ word = '%s%say' % (word[1:], word[0])
+ if args.shout:
+ word = word.upper()
+ words.append(word)
repetitions = args.repeat or 1
for i in range(min(repetitions, self.maxrepeats)):
- self.stdout.write(word)
+ self.stdout.write(' '.join(words))
self.stdout.write('\n')
# self.stdout.write is better than "print", because Cmd can be
# initialized with a non-standard output destination
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 82932e6d..dec54c5d 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -20,19 +20,21 @@ class ArgparseApp(cmd2.Cmd):
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
- argparser.add_argument('word', nargs='?', help='word to say')
+ argparser.add_argument('words', nargs='+', help='words to say')
@cmd2.with_argument_parser(argparser)
def do_say(self, cmdline, args=None):
- word = args.word
- if word is None:
- word = ''
- if args.piglatin:
- word = '%s%say' % (word[1:], word[0])
- if args.shout:
- word = word.upper()
+ words = []
+ for word in args.words:
+ if word is None:
+ word = ''
+ if args.piglatin:
+ word = '%s%say' % (word[1:], word[0])
+ if args.shout:
+ word = word.upper()
+ words.append(word)
repetitions = args.repeat or 1
for i in range(min(repetitions, self.maxrepeats)):
- self.stdout.write(word)
+ self.stdout.write(' '.join(words))
self.stdout.write('\n')
@pytest.fixture
@@ -45,10 +47,14 @@ def test_argparse_basic_command(argparse_app):
out = run_cmd(argparse_app, 'say hello')
assert out == ['hello']
-#def test_argparse_quoted_arguments(argparse_app):
-# out = run_cmd(argparse_app, 'say "hello there"')
-# assert out == ['hello there']
+def test_argparse_quoted_arguments(argparse_app):
+ argparse_app.POSIX = False
+ argparse_app.STRIP_QUOTES_FOR_NON_POSIX = True
+ out = run_cmd(argparse_app, 'say "hello there"')
+ assert out == ['hello there']
-#def test_pargparse_quoted_arguments_too_many(argparse_app):
-# out = run_cmd(argparse_app, 'say "hello there" morty')
-# assert out == ['hello there morty']
+def test_pargparse_quoted_arguments_too_many(argparse_app):
+ argparse_app.POSIX = False
+ argparse_app.STRIP_QUOTES_FOR_NON_POSIX = True
+ out = run_cmd(argparse_app, 'say "hello there" morty')
+ assert out == ['hello there morty']