diff options
author | kotfu <kotfu@kotfu.net> | 2018-01-14 20:54:09 -0700 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2018-01-14 20:54:09 -0700 |
commit | eac6929c75195dba07da5d183c485918bb6899cf (patch) | |
tree | 0d394abf8e58f6e492dc6fb5558fd1c1dbedf074 | |
parent | 22b4f4a6cfd1481c574afb0bababd1aeb6bda3af (diff) | |
download | cmd2-git-eac6929c75195dba07da5d183c485918bb6899cf.tar.gz |
remove use_argument_list attribute
-rwxr-xr-x | cmd2.py | 8 | ||||
-rw-r--r-- | docs/argument_processing.rst | 34 | ||||
-rwxr-xr-x | examples/arglist_example.py | 40 | ||||
-rw-r--r-- | tests/test_argparse.py | 34 |
4 files changed, 3 insertions, 113 deletions
@@ -520,7 +520,6 @@ 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 @@ -1024,16 +1023,13 @@ class Cmd(cmd.Cmd): funcname = self._func_named(statement.parsed.command) if not funcname: return self.default(statement) + try: func = getattr(self, funcname) except AttributeError: return self.default(statement) - if self.use_argument_list: - lexed_arglist = parse_quoted_string(cmdline) - stop = func(lexed_arglist) - else: - stop = func(statement) + stop = func(statement) return stop def default(self, statement): diff --git a/docs/argument_processing.rst b/docs/argument_processing.rst index fdbfb137..4ab4e12f 100644 --- a/docs/argument_processing.rst +++ b/docs/argument_processing.rst @@ -146,39 +146,7 @@ Receiving an argument list ========================== The default behavior of ``cmd2`` is to pass the user input directly to your -``do_*`` methods as a string. If you don't want to use the full argument parser support outlined above, you can still have ``cmd2`` apply shell parsing rules to the user input and pass you a list of arguments instead of a string. There are two methods to effect this change. - -The ``use_argument_list`` attribute of a ``cmd2`` class or subclass defaults to ``False``. Set it to ``True`` and all of your ``do_*`` methods will receive a list of arguments parsed by ``shlex.split()`` instead of receiving a string:: - - class CmdLineApp(cmd2.Cmd): - """ Example cmd2 application. """ - def __init__(self): - self.use_argument_list = True - Cmd.__init__(self) - - def do_speak(self, arglist): - # instead of a string, arglist contains a list of arguments - pass - -Any ``do_*`` methods decorated with ``@with_argument_parser()`` will receive both a list of arguments (instead of a string) and the parsed options as parameters:: - - class CmdLineApp(cmd2.Cmd): - """ Example cmd2 application. """ - def __init__(self): - self.use_argument_list = True - Cmd.__init__(self) - - argparser = argparse.ArgumentParser() - 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, arglist, args=None): - """create a html tag""" - # arglist contains a list of arguments, not a string - self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content))) - self.stdout.write('\n') - -Instead of having all of your ``do_*`` methods receive an argument list, you can choose to only have certain methods receive the argument list (instead of a string). Leave the ``use_argument_list`` attribute at it's default value of ``False``. Then apply the ``@with_argument_list`` decorator only to those methods that should receive an argument list instead of a string:: +``do_*`` methods as a string. If you don't want to use the full argument parser support outlined above, you can still have ``cmd2`` apply shell parsing rules to the user input and pass you a list of arguments instead of a string. Apply the ``@with_argument_list`` decorator to those methods that should receive an argument list instead of a string:: class CmdLineApp(cmd2.Cmd): """ Example cmd2 application. """ diff --git a/examples/arglist_example.py b/examples/arglist_example.py deleted file mode 100755 index 5025c4be..00000000 --- a/examples/arglist_example.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/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 3ad1dc97..cfe0ec5c 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -91,34 +91,12 @@ class ArgparseApp(cmd2.Cmd): 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): - """Print true if the arglist parameter is passed as a list.""" - if isinstance(arglist, list): - self.stdout.write('True') - else: - self.stdout.write('False') - - @cmd2.with_argument_list - def do_arglistwithdecorator(self, arglist): - self.stdout.write(' '.join(arglist)) - - @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') @@ -178,15 +156,3 @@ def test_arglist_decorator_twice(argparse_app): def test_arglist_and_argparser(argparse_app): out = run_cmd(argparse_app, 'arglistandargparser some "quoted words"') assert out[0] == 'some quoted words' - -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' - -def test_arglist_attribute_and_decorator(arglist_app): - out = run_cmd(arglist_app, 'arglistwithdecorator "we should" get these') - assert out[0] == 'we should get these' - -def test_arglist_help(arglist_app): - out = run_cmd(arglist_app, 'help arglist') - assert out[0] == 'Print true if the arglist parameter is passed as a list.' |