summaryrefslogtreecommitdiff
path: root/examples/arg_print.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-05-25 12:35:24 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-05-25 12:35:24 -0400
commit4c67d3372db924e87be920d89aadecb946584b31 (patch)
treebf57a15570248a2e3fbfb058a817d6c743cc9167 /examples/arg_print.py
parent0e4131ba2616bd33c9cb5e93105dbd6d511a71ab (diff)
parentcad21a60fa92ebe4a7c177142d273f9f7497967b (diff)
downloadcmd2-git-4c67d3372db924e87be920d89aadecb946584b31.tar.gz
Merge branch 'master' into pyshell_readline
Diffstat (limited to 'examples/arg_print.py')
-rwxr-xr-xexamples/arg_print.py25
1 files changed, 13 insertions, 12 deletions
diff --git a/examples/arg_print.py b/examples/arg_print.py
index b2f0fcda..4f0ca709 100755
--- a/examples/arg_print.py
+++ b/examples/arg_print.py
@@ -2,18 +2,17 @@
# coding=utf-8
"""A simple example demonstrating the following:
1) How arguments and options get parsed and passed to commands
- 2) How to change what syntax get parsed as a comment and stripped from the arguments
+ 2) How to change what syntax get parsed as a comment and stripped from
+ the arguments
-This is intended to serve as a live demonstration so that developers can experiment with and understand how command
-and argument parsing is intended to work.
+This is intended to serve as a live demonstration so that developers can
+experiment with and understand how command and argument parsing work.
It also serves as an example of how to create command aliases (shortcuts).
"""
import argparse
-from cmd2 import cmd2
-from cmd2.cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args
-
+import cmd2
class ArgumentAndOptionPrinter(cmd2.Cmd):
""" Example cmd2 application where we create commands that just print the arguments they are called with."""
@@ -27,11 +26,14 @@ class ArgumentAndOptionPrinter(cmd2.Cmd):
# NOTE: It is critical that the super class __init__ method be called AFTER updating certain parameters which
# are not settable at runtime. This includes the shortcuts, multiline_commands, etc.
- def do_aprint(self, arg):
+ def do_aprint(self, statement):
"""Print the argument string this basic command is called with."""
- self.poutput('aprint was called with argument: {!r}'.format(arg))
+ self.poutput('aprint was called with argument: {!r}'.format(statement))
+ self.poutput('statement.raw = {!r}'.format(statement.raw))
+ self.poutput('statement.argv = {!r}'.format(statement.argv))
+ self.poutput('statement.command = {!r}'.format(statement.command))
- @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))
@@ -42,7 +44,7 @@ class ArgumentAndOptionPrinter(cmd2.Cmd):
oprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
oprint_parser.add_argument('words', nargs='+', help='words to print')
- @with_argparser(oprint_parser)
+ @cmd2.with_argparser(oprint_parser)
def do_oprint(self, args):
"""Print the options and argument list this options command was called with."""
self.poutput('oprint was called with the following\n\toptions: {!r}'.format(args))
@@ -51,13 +53,12 @@ class ArgumentAndOptionPrinter(cmd2.Cmd):
pprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
pprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
pprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
- @with_argparser_and_unknown_args(pprint_parser)
+ @cmd2.with_argparser_and_unknown_args(pprint_parser)
def do_pprint(self, args, unknown):
"""Print the options and argument list this options command was called with."""
self.poutput('oprint was called with the following\n\toptions: {!r}\n\targuments: {}'.format(args, unknown))
-
if __name__ == '__main__':
app = ArgumentAndOptionPrinter()
app.cmdloop()