summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-01-12 23:11:53 -0700
committerkotfu <kotfu@kotfu.net>2018-01-12 23:11:53 -0700
commit405f4e4e951e0af46c7c5d746459f24e5c316eab (patch)
tree7933766506292f7610d5b05aa5f6615b32e2bff7
parentc26b00853633c7df8cdee0ee49b3596154bb09c1 (diff)
downloadcmd2-git-405f4e4e951e0af46c7c5d746459f24e5c316eab.tar.gz
add use_argument_list setting
new attribute on Cmd2.cmd which defaults to false, but if set true, causes all do_* commands to receive a list of arguments, instead of a string of what the user typed.
-rwxr-xr-xcmd2.py14
-rwxr-xr-xexamples/arglist_example.py40
-rw-r--r--tests/test_argparse.py21
3 files changed, 74 insertions, 1 deletions
diff --git a/cmd2.py b/cmd2.py
index fe6213a8..0fc6c32c 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -518,6 +518,7 @@ class Cmd(cmd.Cmd):
excludeFromHistory = '''run ru r history histor histo hist his hi h edit edi ed e eof eo eos'''.split()
exclude_from_help = ['do_eof', 'do_eos'] # Commands to exclude from the help menu
reserved_words = []
+ use_argument_list = False
# Attributes which ARE dynamically settable at runtime
abbrev = False # Abbreviated commands recognized
@@ -1025,7 +1026,18 @@ class Cmd(cmd.Cmd):
func = getattr(self, funcname)
except AttributeError:
return self.default(statement)
- stop = func(statement)
+
+ if self.use_argument_list:
+ lexed_arglist = shlex.split(statement, 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:
+ temp_arglist = []
+ for arg in lexed_arglist:
+ temp_arglist.append(strip_quotes(arg))
+ lexed_arglist = temp_arglist
+ stop = func(lexed_arglist)
+ else:
+ stop = func(statement)
return stop
def default(self, statement):
diff --git a/examples/arglist_example.py b/examples/arglist_example.py
new file mode 100755
index 00000000..5025c4be
--- /dev/null
+++ b/examples/arglist_example.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+# coding=utf-8
+"""A sample application for cmd2 showing how to use argparse to
+process command line arguments for your application.
+
+Thanks to cmd2's built-in transcript testing capability, it also
+serves as a test suite for argparse_example.py when used with the
+exampleSession.txt transcript.
+
+Running `python argparse_example.py -t exampleSession.txt` will run
+all the commands in the transcript against argparse_example.py,
+verifying that the output produced matches the transcript.
+"""
+import argparse
+import sys
+
+from cmd2 import Cmd, make_option, options, with_argument_parser, with_argument_list
+
+
+class CmdLineApp(Cmd):
+ """ Example cmd2 application. """
+ def __init__(self):
+ self.use_argument_list = True
+ Cmd.__init__(self)
+
+ def do_tag(self, arglist):
+ """verion of creating an html tag using arglist instead of argparser"""
+ if len(arglist) >= 2:
+ tag = arglist[0]
+ content = arglist[1:]
+ self.poutput('<{0}>{1}</{0}>'.format(tag, ' '.join(content)))
+ else:
+ self.perror("tag requires at least 2 arguments")
+
+if __name__ == '__main__':
+ # Instantiate your cmd2 application
+ c = CmdLineApp()
+
+ # And run your cmd2 application
+ c.cmdloop()
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 308824cf..7d6d99de 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -71,12 +71,29 @@ class ArgparseApp(cmd2.Cmd):
else:
self.stdout.write('False')
+class ArglistApp(cmd2.Cmd):
+ def __init__(self):
+ self.use_argument_list = True
+ cmd2.Cmd.__init__(self)
+
+ def do_arglist(self, arglist):
+ if isinstance(arglist, list):
+ self.stdout.write('True')
+ else:
+ self.stdout.write('False')
+
@pytest.fixture
def argparse_app():
app = ArgparseApp()
app.stdout = StdOut()
return app
+@pytest.fixture
+def arglist_app():
+ app = ArglistApp()
+ app.stdout = StdOut()
+ return app
+
def test_argparse_basic_command(argparse_app):
out = run_cmd(argparse_app, 'say hello')
assert out == ['hello']
@@ -127,3 +144,7 @@ def test_argparse_arglist(argparse_app):
def test_arglist(argparse_app):
out = run_cmd(argparse_app, 'arglist "we should" get these in a list, not a string')
assert out[0] == 'True'
+
+def test_use_argument_list(arglist_app):
+ out = run_cmd(arglist_app, 'arglist "we should" get these in a list, not a string')
+ assert out[0] == 'True'