Skip to content

Commit 40f75f7

Browse files
committed
Updated docs and example
1 parent 7db2786 commit 40f75f7

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
also be colored.
77
* `help_error` - the error that prints when no help information can be found
88
* `default_error` - the error that prints when a non-existent command is run
9-
* The `with_argparser` decorators now add a Statement object to the `argparse.Namespace` object they pass
10-
to the `do_*` functions. It is stored in an attribute called `__statement__`. This can be useful if a command
11-
function needs to know the command line for things like logging.
9+
* The `with_argparser` decorators now add the Statement object created when parsing the command line to the
10+
`argparse.Namespace` object they pass to the `do_*` methods. It is stored in an attribute called `__statement__`.
11+
This can be useful if a command function needs to know the command line for things like logging.
1212
* Potentially breaking changes
1313
* The following commands now write to stderr instead of stdout when printing an error. This will make catching
1414
errors easier in pyscript.

cmd2/parsing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def tokenize(self, line: str, expand: bool = True) -> List[str]:
351351
:param expand: If True, then aliases and shortcuts will be expanded.
352352
Set this to False if no expansion should occur because the command name is already known.
353353
Otherwise the command could be expanded if it matched an alias name. This is for cases where
354-
a do_* function was called manually (e.g do_help('alias').
354+
a do_* method was called manually (e.g do_help('alias').
355355
:return: A list of tokens
356356
:raises ValueError if there are unclosed quotation marks.
357357
"""
@@ -381,7 +381,7 @@ def parse(self, line: str, expand: bool = True) -> Statement:
381381
:param expand: If True, then aliases and shortcuts will be expanded.
382382
Set this to False if no expansion should occur because the command name is already known.
383383
Otherwise the command could be expanded if it matched an alias name. This is for cases where
384-
a do_* function was called manually (e.g do_help('alias').
384+
a do_* method was called manually (e.g do_help('alias').
385385
:return: A parsed Statement
386386
:raises ValueError if there are unclosed quotation marks
387387
"""

docs/argument_processing.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Argument Processing
1010
1. Parsing input and quoted strings like the Unix shell
1111
2. Parse the resulting argument list using an instance of ``argparse.ArgumentParser`` that you provide
1212
3. Passes the resulting ``argparse.Namespace`` object to your command function. The ``Namespace`` includes the
13-
``Statement`` object that was parsed from the command line. It is stored in the ``__statement__`` attribute of
14-
the ``Namespace``.
13+
``Statement`` object that was created when parsing the command line. It is stored in the ``__statement__``
14+
attribute of the ``Namespace``.
1515
4. Adds the usage message from the argument parser to your command.
1616
5. Checks if the ``-h/--help`` option is present, and if so, display the help message for the command
1717

examples/decorator_example.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313
import argparse
1414
import sys
15+
from typing import List
1516

1617
import cmd2
1718

@@ -46,7 +47,7 @@ def __init__(self, ip_addr=None, port=None, transcript_files=None):
4647
speak_parser.add_argument('words', nargs='+', help='words to say')
4748

4849
@cmd2.with_argparser(speak_parser)
49-
def do_speak(self, args):
50+
def do_speak(self, args: argparse.Namespace):
5051
"""Repeats what you tell me to."""
5152
words = []
5253
for word in args.words:
@@ -67,13 +68,18 @@ def do_speak(self, args):
6768
tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
6869

6970
@cmd2.with_argparser(tag_parser)
70-
def do_tag(self, args):
71-
"""create a html tag"""
71+
def do_tag(self, args: argparse.Namespace):
72+
"""create an html tag"""
73+
# The Namespace always includes the Statement object created when parsing the command line
74+
statement = args.__statement__
75+
76+
self.poutput("The command line you ran was: {}".format(statement.command_and_args))
77+
self.poutput("It generated this tag:")
7278
self.poutput('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
7379

7480
@cmd2.with_argument_list
75-
def do_tagg(self, arglist):
76-
"""verion of creating an html tag using arglist instead of argparser"""
81+
def do_tagg(self, arglist: List[str]):
82+
"""version of creating an html tag using arglist instead of argparser"""
7783
if len(arglist) >= 2:
7884
tag = arglist[0]
7985
content = arglist[1:]

0 commit comments

Comments
 (0)