diff options
author | Jared Crapo <jared@kotfu.net> | 2017-12-11 19:15:15 -0700 |
---|---|---|
committer | Jared Crapo <jared@kotfu.net> | 2017-12-11 19:15:15 -0700 |
commit | 67e669e3345fb637e4f4779691a7a8ec4b1763f6 (patch) | |
tree | 7632e68cc845e1089111e9f10b105bce8c6d5a46 /examples | |
parent | 4f57dc14a7ffc2cbd741622da7d05d42ba7f7789 (diff) | |
parent | 63b0c6b3a92cab837a71bbad78e777c522823c93 (diff) | |
download | cmd2-git-67e669e3345fb637e4f4779691a7a8ec4b1763f6.tar.gz |
Merge branch 'master' of https://github.com/python-cmd2/cmd2 into argparse
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/arg_print.py | 9 | ||||
-rwxr-xr-x | examples/argparse_example.py | 16 | ||||
-rwxr-xr-x | examples/example.py | 4 | ||||
-rw-r--r-- | examples/exampleSession.txt | 88 |
4 files changed, 35 insertions, 82 deletions
diff --git a/examples/arg_print.py b/examples/arg_print.py index 20fa7c02..849cf386 100755 --- a/examples/arg_print.py +++ b/examples/arg_print.py @@ -6,6 +6,8 @@ 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. + +It also serves as an example of how to create command aliases (shortcuts). """ import pyparsing import cmd2 @@ -19,8 +21,13 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): # Uncomment this line to disable Python-style comments but still allow C-style comments # self.commentGrammars = pyparsing.Or([pyparsing.cStyleComment]) - # Make sure to call this super class __init__ after setting commentGrammars and not before + # Create command aliases which are shorter + self.shortcuts.update({'ap': 'aprint', 'op': 'oprint'}) + + # Make sure to call this super class __init__ *after* setting commentGrammars and/or updating shortcuts cmd2.Cmd.__init__(self) + # 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 commentGrammars, shortcuts, multilineCommands, etc. def do_aprint(self, arg): """Print the argument string this basic command is called with.""" diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 8c25b0ef..805bab77 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -1,7 +1,8 @@ #!/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. -It doubles as an example of how you can still do transcript testing even if allow_cli_args is false. +It parses command line arguments looking for known arguments, but then still passes any unknown arguments onto cmd2 +to treat them as arguments at invocation. 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. @@ -10,6 +11,7 @@ Running `python argparse_example.py -t exampleSession.txt` will run all the comm 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 @@ -28,7 +30,7 @@ class CmdLineApp(Cmd): Cmd.__init__(self, use_ipython=False, transcript_files=transcript_files) # Disable cmd's usage of command-line arguments as commands to be run at invocation - self.allow_cli_args = False + # self.allow_cli_args = False # Example of args set from the command-line (but they aren't being used here) self._ip = ip_addr @@ -92,8 +94,7 @@ if __name__ == '__main__': parser.add_argument('-i', '--ip', type=str, help='IPv4 address') # Add an argument which enables transcript testing - parser.add_argument('-t', '--test', type=str, help='Test against transcript in FILE (wildcards OK)') - args = parser.parse_args() + args, unknown_args = parser.parse_known_args() port = None if args.port: @@ -103,12 +104,11 @@ if __name__ == '__main__': if args.ip: ip_addr = args.ip - transcripts = None - if args.test: - transcripts = [args.test] + # Perform surgery on sys.argv to remove the arguments which have already been processed by argparse + sys.argv = sys.argv[:1] + unknown_args # Instantiate your cmd2 application - c = CmdLineApp(transcript_files=transcripts) + c = CmdLineApp() # And run your cmd2 application c.cmdloop() diff --git a/examples/example.py b/examples/example.py index e4158f13..889e62c0 100755 --- a/examples/example.py +++ b/examples/example.py @@ -4,9 +4,9 @@ A sample application for cmd2. Thanks to cmd2's built-in transcript testing capability, it also serves as a -test suite for example.py when used with the exampleSession.txt transcript. +test suite for example.py when used with the transcript_regex.txt transcript. -Running `python example.py -t exampleSession.txt` will run all the commands in +Running `python example.py -t transcript_regex.txt` will run all the commands in the transcript against example.py, verifying that the output produced matches the transcript. """ diff --git a/examples/exampleSession.txt b/examples/exampleSession.txt index de7eea96..ef6df857 100644 --- a/examples/exampleSession.txt +++ b/examples/exampleSession.txt @@ -1,72 +1,18 @@ -# Run this transcript with "python example.py -t exampleSession.txt" -(Cmd) help - -Documented commands (type help <topic>): -======================================== -_relative_load edit history orate pyscript run say shell show -cmdenvironment help load py quit save set shortcuts speak - -(Cmd) help say -Repeats what you tell me to. -Usage: speak [options] arg - -Options: - -h, --help show this help message and exit - -p, --piglatin atinLay - -s, --shout N00B EMULATION MODE - -r REPEAT, --repeat=REPEAT - output [n] times - -(Cmd) say goodnight, Gracie -goodnight, Gracie -(Cmd) say -ps --repeat=5 goodnight, Gracie -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -(Cmd) set maxrepeats 5 -maxrepeats - was: 3 -now: 5 -(Cmd) say -ps --repeat=5 goodnight, Gracie -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -(Cmd) hi --------------------------[1] -help --------------------------[2] -help say --------------------------[3] -say goodnight, Gracie --------------------------[4] -say -ps --repeat=5 goodnight, Gracie --------------------------[5] -set maxrepeats 5 --------------------------[6] -say -ps --repeat=5 goodnight, Gracie -(Cmd) run 4 -say -ps --repeat=5 goodnight, Gracie - -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -OODNIGHT, GRACIEGAY -(Cmd) orate Four score and -> seven releases ago -> our BDFL -> blah blah blah -Four score and -seven releases ago -our BDFL -blah blah blah -(Cmd) & look, a shortcut! -look, a shortcut! -(Cmd) show color +# Run this transcript with "python argparse_example.py -t exampleSession.txt" +# The regex for colors is because no color on Windows. +# The regex for editor will match whatever program you use. +# regexes on prompts just make the trailing space obvious +(Cmd) set +abbrev: False +autorun_on_edit: False colors: /(True|False)/ -(Cmd) set prompt "---> " -prompt - was: (Cmd) -now: ---> ----> say goodbye -goodbye +continuation_prompt: >/ / +debug: False +echo: False +editor: /.*?/ +feedback_to_output: False +locals_in_py: True +maxrepeats: 3 +prompt: (Cmd)/ / +quiet: False +timing: False |