Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
``AutoCompleter`` which has since developed a dependency on ``cmd2`` methods.
* Removed ability to call commands in ``pyscript`` as if they were functions (e.g ``app.help()``) in favor
of only supporting one ``pyscript`` interface. This simplifies future maintenance.
* To fix an exception caused by passing arguments to ``@with_argument_list``, the basic usage of this
decorator had to change. It must now always have parenthesis.
* @with_argument_list() instead of @with_argument_list


## 0.9.10 (February 22, 2019)
* Bug Fixes
Expand Down
19 changes: 10 additions & 9 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,26 @@ def cat_decorator(func):
return cat_decorator


def with_argument_list(func: Callable[[Statement], Optional[bool]],
preserve_quotes: bool = False) -> Callable[[List], Optional[bool]]:
def with_argument_list(preserve_quotes: bool = False) -> Callable[[List], Optional[bool]]:
"""A decorator to alter the arguments passed to a do_* cmd2 method. Default passes a string of whatever the user
typed. With this decorator, the decorated method will receive a list of arguments parsed from user input using
shlex.split().

:param func: do_* method this decorator is wrapping
:param preserve_quotes: if True, then argument quotes will not be stripped
:return: function that gets passed a list of argument strings
"""
import functools

@functools.wraps(func)
def cmd_wrapper(self, cmdline):
lexed_arglist = parse_quoted_string(cmdline, preserve_quotes)
return func(self, lexed_arglist)
def arg_decorator(func: Callable[[Statement], Optional[bool]]):
@functools.wraps(func)
def cmd_wrapper(self, cmdline):
lexed_arglist = parse_quoted_string(cmdline, preserve_quotes)
return func(self, lexed_arglist)

cmd_wrapper.__doc__ = func.__doc__
return cmd_wrapper
cmd_wrapper.__doc__ = func.__doc__
return cmd_wrapper

return arg_decorator


def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve_quotes: bool = False) -> \
Expand Down
2 changes: 1 addition & 1 deletion docs/argument_processing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ argument list instead of a string::
# cmdline contains a string
pass

@with_argument_list
@with_argument_list()
def do_speak(self, arglist):
# arglist contains a list of arguments
pass
Expand Down
13 changes: 0 additions & 13 deletions docs/freefeatures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,6 @@ of using ``pyscript`` is shown below along with the **examples/arg_printer.py**
arg 2: 'bar'
arg 3: 'baz'

.. note::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend keeping this note and just updating it to show the required parentheses.


If you want to be able to pass arguments with spaces to scripts, then we strongly recommend using one of the decorators,
such as ``with_argument_list``. ``cmd2`` will pass your **do_*** methods a list of arguments in this case.

When using this decorator, you can then put arguments in quotes like so (NOTE: the ``do_pyscript`` method uses this decorator::

(Cmd) pyscript examples/arg_printer.py hello '23 fnord'
Running Python script 'arg_printer.py' which was called with 2 arguments
arg 1: 'hello'
arg 2: '23 fnord'


IPython (optional)
==================

Expand Down
2 changes: 1 addition & 1 deletion examples/arg_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def do_aprint(self, statement):
self.poutput('statement.argv = {!r}'.format(statement.argv))
self.poutput('statement.command = {!r}'.format(statement.command))

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_lprint(self, arglist):
"""Print the argument list this basic command is called with."""
self.poutput('lprint was called with the following list of arguments: {!r}'.format(arglist))
Expand Down
2 changes: 1 addition & 1 deletion examples/decorator_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def do_tag(self, args):
"""create a html tag"""
self.poutput('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_tagg(self, arglist):
"""verion of creating an html tag using arglist instead of argparser"""
if len(arglist) >= 2:
Expand Down
2 changes: 1 addition & 1 deletion examples/exit_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ReplWithExitCode(cmd2.Cmd):
def __init__(self):
super().__init__()

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_exit(self, arg_list: List[str]) -> bool:
"""Exit the application with an optional exit code.

Expand Down
2 changes: 1 addition & 1 deletion examples/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def abbrev_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.Postpars
data.statement = self.statement_parser.parse(raw)
return data

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_list(self, arglist: List[str]) -> None:
"""Generate a list of 10 numbers."""
if arglist:
Expand Down
4 changes: 2 additions & 2 deletions examples/paged_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def page_file(self, file_path: str, chop: bool=False):
except FileNotFoundError:
self.perror('ERROR: file {!r} not found'.format(filename), traceback_war=False)

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_page_wrap(self, args: List[str]):
"""Read in a text file and display its output in a pager, wrapping long lines if they don't fit.

Expand All @@ -37,7 +37,7 @@ def do_page_wrap(self, args: List[str]):

complete_page_wrap = cmd2.Cmd.path_complete

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_page_truncate(self, args: List[str]):
"""Read in a text file and display its output in a pager, truncating long lines if they don't fit.

Expand Down
2 changes: 1 addition & 1 deletion examples/python_scripting.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def postcmd(self, stop: bool, line: str) -> bool:
self._set_prompt()
return stop

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_cd(self, arglist):
"""Change directory.
Usage:
Expand Down
2 changes: 1 addition & 1 deletion examples/tab_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def complete_add_item(self, text, line, begidx, endidx):

return self.flag_based_complete(text, line, begidx, endidx, flag_dict=flag_dict)

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_list_item(self, args):
"""List item command help"""
self.poutput("You listed {}".format(args))
Expand Down
6 changes: 3 additions & 3 deletions tests/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def do_tag(self, args):
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
self.stdout.write('\n')

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_arglist(self, arglist):
if isinstance(arglist, list):
self.stdout.write('True')
else:
self.stdout.write('False')

@cmd2.with_argument_list
@cmd2.with_argument_list
@cmd2.with_argument_list()
@cmd2.with_argument_list()
def do_arglisttwice(self, arglist):
if isinstance(arglist, list):
self.stdout.write(' '.join(arglist))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2266,7 +2266,7 @@ class ReplWithExitCode(cmd2.Cmd):
def __init__(self):
super().__init__()

@cmd2.with_argument_list
@cmd2.with_argument_list()
def do_exit(self, arg_list) -> bool:
"""Exit the application with an optional exit code.

Expand Down