diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-11-11 12:03:38 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-11-11 12:03:38 -0500 |
commit | dbc9f819182b26d0ae68f55e24467411d5711290 (patch) | |
tree | beb1a79dc1a5f4863182f61c38859fa59619c933 /examples/argparse_example.py | |
parent | af06c06a5e580c92cf934f110c9e88ec75cde235 (diff) | |
download | cmd2-git-dbc9f819182b26d0ae68f55e24467411d5711290.tar.gz |
Made a couple fixes to existing examples
Changes include:
- argparse_example.py modified to do pass all unknown args onto cmd2 and allow arguments at invocation
- example.py comments modified to indicate it is intended to be used with transcript_regext.txt
- exampleSession.txt fixed so it works properly with argparse_example.py
Diffstat (limited to 'examples/argparse_example.py')
-rwxr-xr-x | examples/argparse_example.py | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 1358559c..6fc2b15b 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 @@ -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 @@ -68,8 +70,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: @@ -79,12 +80,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() |