Skip to content

Commit 787cc0a

Browse files
authored
Merge pull request #236 from python-cmd2/example_fixes
Made a couple fixes to existing examples
2 parents af06c06 + dbc9f81 commit 787cc0a

File tree

3 files changed

+27
-81
lines changed

3 files changed

+27
-81
lines changed

examples/argparse_example.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python
22
# coding=utf-8
33
"""A sample application for cmd2 showing how to use Argparse to process command line arguments for your application.
4-
It doubles as an example of how you can still do transcript testing even if allow_cli_args is false.
4+
It parses command line arguments looking for known arguments, but then still passes any unknown arguments onto cmd2
5+
to treat them as arguments at invocation.
56
67
Thanks to cmd2's built-in transcript testing capability, it also serves as a test suite for argparse_example.py when
78
used with the exampleSession.txt transcript.
@@ -10,6 +11,7 @@
1011
argparse_example.py, verifying that the output produced matches the transcript.
1112
"""
1213
import argparse
14+
import sys
1315

1416
from cmd2 import Cmd, make_option, options
1517

@@ -28,7 +30,7 @@ def __init__(self, ip_addr=None, port=None, transcript_files=None):
2830
Cmd.__init__(self, use_ipython=False, transcript_files=transcript_files)
2931

3032
# Disable cmd's usage of command-line arguments as commands to be run at invocation
31-
self.allow_cli_args = False
33+
# self.allow_cli_args = False
3234

3335
# Example of args set from the command-line (but they aren't being used here)
3436
self._ip = ip_addr
@@ -68,8 +70,7 @@ def do_speak(self, arg, opts=None):
6870
parser.add_argument('-i', '--ip', type=str, help='IPv4 address')
6971

7072
# Add an argument which enables transcript testing
71-
parser.add_argument('-t', '--test', type=str, help='Test against transcript in FILE (wildcards OK)')
72-
args = parser.parse_args()
73+
args, unknown_args = parser.parse_known_args()
7374

7475
port = None
7576
if args.port:
@@ -79,12 +80,11 @@ def do_speak(self, arg, opts=None):
7980
if args.ip:
8081
ip_addr = args.ip
8182

82-
transcripts = None
83-
if args.test:
84-
transcripts = [args.test]
83+
# Perform surgery on sys.argv to remove the arguments which have already been processed by argparse
84+
sys.argv = sys.argv[:1] + unknown_args
8585

8686
# Instantiate your cmd2 application
87-
c = CmdLineApp(transcript_files=transcripts)
87+
c = CmdLineApp()
8888

8989
# And run your cmd2 application
9090
c.cmdloop()

examples/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
A sample application for cmd2.
55
66
Thanks to cmd2's built-in transcript testing capability, it also serves as a
7-
test suite for example.py when used with the exampleSession.txt transcript.
7+
test suite for example.py when used with the transcript_regex.txt transcript.
88
9-
Running `python example.py -t exampleSession.txt` will run all the commands in
9+
Running `python example.py -t transcript_regex.txt` will run all the commands in
1010
the transcript against example.py, verifying that the output produced matches
1111
the transcript.
1212
"""

examples/exampleSession.txt

Lines changed: 17 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,18 @@
1-
# Run this transcript with "python example.py -t exampleSession.txt"
2-
(Cmd) help
3-
4-
Documented commands (type help <topic>):
5-
========================================
6-
_relative_load edit history orate pyscript run say shell show
7-
cmdenvironment help load py quit save set shortcuts speak
8-
9-
(Cmd) help say
10-
Repeats what you tell me to.
11-
Usage: speak [options] arg
12-
13-
Options:
14-
-h, --help show this help message and exit
15-
-p, --piglatin atinLay
16-
-s, --shout N00B EMULATION MODE
17-
-r REPEAT, --repeat=REPEAT
18-
output [n] times
19-
20-
(Cmd) say goodnight, Gracie
21-
goodnight, Gracie
22-
(Cmd) say -ps --repeat=5 goodnight, Gracie
23-
OODNIGHT, GRACIEGAY
24-
OODNIGHT, GRACIEGAY
25-
OODNIGHT, GRACIEGAY
26-
(Cmd) set maxrepeats 5
27-
maxrepeats - was: 3
28-
now: 5
29-
(Cmd) say -ps --repeat=5 goodnight, Gracie
30-
OODNIGHT, GRACIEGAY
31-
OODNIGHT, GRACIEGAY
32-
OODNIGHT, GRACIEGAY
33-
OODNIGHT, GRACIEGAY
34-
OODNIGHT, GRACIEGAY
35-
(Cmd) hi
36-
-------------------------[1]
37-
help
38-
-------------------------[2]
39-
help say
40-
-------------------------[3]
41-
say goodnight, Gracie
42-
-------------------------[4]
43-
say -ps --repeat=5 goodnight, Gracie
44-
-------------------------[5]
45-
set maxrepeats 5
46-
-------------------------[6]
47-
say -ps --repeat=5 goodnight, Gracie
48-
(Cmd) run 4
49-
say -ps --repeat=5 goodnight, Gracie
50-
51-
OODNIGHT, GRACIEGAY
52-
OODNIGHT, GRACIEGAY
53-
OODNIGHT, GRACIEGAY
54-
OODNIGHT, GRACIEGAY
55-
OODNIGHT, GRACIEGAY
56-
(Cmd) orate Four score and
57-
> seven releases ago
58-
> our BDFL
59-
> blah blah blah
60-
Four score and
61-
seven releases ago
62-
our BDFL
63-
blah blah blah
64-
(Cmd) & look, a shortcut!
65-
look, a shortcut!
66-
(Cmd) show color
1+
# Run this transcript with "python argparse_example.py -t exampleSession.txt"
2+
# The regex for colors is because no color on Windows.
3+
# The regex for editor will match whatever program you use.
4+
# regexes on prompts just make the trailing space obvious
5+
(Cmd) set
6+
abbrev: False
7+
autorun_on_edit: False
678
colors: /(True|False)/
68-
(Cmd) set prompt "---> "
69-
prompt - was: (Cmd)
70-
now: --->
71-
---> say goodbye
72-
goodbye
9+
continuation_prompt: >/ /
10+
debug: False
11+
echo: False
12+
editor: /.*?/
13+
feedback_to_output: False
14+
locals_in_py: True
15+
maxrepeats: 3
16+
prompt: (Cmd)/ /
17+
quiet: False
18+
timing: False

0 commit comments

Comments
 (0)