@@ -70,10 +70,13 @@ Instructions for implementing each feature follow.
7070
7171- Parsing commands with ` optparse ` options (flags)
7272
73- @options([make_option('-m', '--myoption', action="store_true", help="all about my option")])
74- def do_myfunc(self, arg, opts):
75- if opts.myoption:
76- ...
73+ ``` python
74+ @options ([make_option(' -m' , ' --myoption' , action = " store_true" , help = " all about my option" )])
75+ def do_myfunc (self , arg , opts ):
76+ if opts.myoption:
77+ # TODO : Do something useful
78+ pass
79+ ```
7780
7881 See Python standard library' s ``optparse`` documentation: http://docs.python.org/lib/optparse-defining-options.html
7982
@@ -91,126 +94,135 @@ A couple tutorials on using cmd2 exist:
9194Example Application
9295------------------ -
9396
94- Example cmd2 application (** examples/example.py** ) ::
95-
96- '''A sample application for cmd2.'''
97-
98- from cmd2 import Cmd, make_option, options
99-
100- class CmdLineApp(Cmd):
101- multilineCommands = ['orate']
102- Cmd.shortcuts.update({'&': 'speak'})
103- maxrepeats = 3
104- Cmd.settable.append('maxrepeats')
105-
106- # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
107- # default_to_shell = True
108-
109- @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
110- make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
111- make_option('-r', '--repeat', type="int", help="output [n] times")
112- ])
113- def do_speak(self, arg, opts=None):
114- """Repeats what you tell me to."""
115- arg = ''.join(arg)
116- if opts.piglatin:
117- arg = '%s%say' % (arg[1:], arg[0])
118- if opts.shout:
119- arg = arg.upper()
120- repetitions = opts.repeat or 1
121- for i in range(min(repetitions, self.maxrepeats)):
122- self.stdout.write(arg)
123- self.stdout.write('\n')
124- # self.stdout.write is better than "print", because Cmd can be
125- # initialized with a non-standard output destination
126-
127- do_say = do_speak # now "say" is a synonym for "speak"
128- do_orate = do_speak # another synonym, but this one takes multi-line input
129-
130- if __name__ == '__main__':
131- c = CmdLineApp()
132- c.cmdloop()
97+ Example cmd2 application (** examples/ example.py** ):
98+
99+ ```python
100+ ''' A sample application for cmd2.'''
101+
102+ from cmd2 import Cmd, make_option, options
103+
104+ class CmdLineApp (Cmd ):
105+ multilineCommands = [' orate' ]
106+ Cmd.shortcuts.update({' &' : ' speak' })
107+ maxrepeats = 3
108+ Cmd.settable.append(' maxrepeats' )
109+
110+ # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
111+ # default_to_shell = True
112+
113+ @options ([make_option(' -p' , ' --piglatin' , action = " store_true" , help = " atinLay" ),
114+ make_option(' -s' , ' --shout' , action = " store_true" , help = " N00B EMULATION MODE" ),
115+ make_option(' -r' , ' --repeat' , type = " int" , help = " output [n] times" )
116+ ])
117+ def do_speak (self , arg , opts = None ):
118+ """ Repeats what you tell me to."""
119+ arg = ' ' .join(arg)
120+ if opts.piglatin:
121+ arg = ' %s%s ay' % (arg[1 :], arg[0 ])
122+ if opts.shout:
123+ arg = arg.upper()
124+ repetitions = opts.repeat or 1
125+ for i in range (min (repetitions, self .maxrepeats)):
126+ self .stdout.write(arg)
127+ self .stdout.write(' \n ' )
128+ # self.stdout.write is better than "print", because Cmd can be
129+ # initialized with a non-standard output destination
130+
131+ do_say = do_speak # now "say" is a synonym for "speak"
132+ do_orate = do_speak # another synonym, but this one takes multi-line input
133+
134+ if __name__ == ' __main__' :
135+ c = CmdLineApp()
136+ c.cmdloop()
137+ ```
133138
134139The following is a sample session running example.py.
135140Thanks to Cmd2's built-in transcript testing capability, it also serves as a test
136141suite for example.py when saved as * exampleSession.txt* .
137- Running::
138-
139- python example.py -t exampleSession.txt
142+ Running
140143
144+ ``` bash
145+ python example.py -t exampleSession.txt
146+ ```
141147will run all the commands in the transcript against ` example.py ` , verifying that the output produced
142148matches the transcript.
143149
144- example/exampleSession.txt::
145-
146- (Cmd) help
147-
148- Documented commands (type help <topic>):
149- ========================================
150- _relative_load edit help list orate py run say shell show
151- cmdenvironment eof history load pause quit save set shortcuts speak
152-
153- (Cmd) help say
154- Repeats what you tell me to.
155- Usage: speak [options] arg
156-
157- Options:
158- -h, --help show this help message and exit
159- -p, --piglatin atinLay
160- -s, --shout N00B EMULATION MODE
161- -r REPEAT, --repeat=REPEAT
162- output [n] times
163-
164- (Cmd) say goodnight, Gracie
165- goodnight, Gracie
166- (Cmd) say -ps --repeat=5 goodnight, Gracie
167- OODNIGHT, GRACIEGAY
168- OODNIGHT, GRACIEGAY
169- OODNIGHT, GRACIEGAY
170- (Cmd) set maxrepeats 5
171- maxrepeats - was: 3
172- now: 5
173- (Cmd) say -ps --repeat=5 goodnight, Gracie
174- OODNIGHT, GRACIEGAY
175- OODNIGHT, GRACIEGAY
176- OODNIGHT, GRACIEGAY
177- OODNIGHT, GRACIEGAY
178- OODNIGHT, GRACIEGAY
179- (Cmd) hi
180- -------------------------[1]
181- help
182- -------------------------[2]
183- help say
184- -------------------------[3]
185- say goodnight, Gracie
186- -------------------------[4]
187- say -ps --repeat=5 goodnight, Gracie
188- -------------------------[5]
189- set maxrepeats 5
190- -------------------------[6]
191- say -ps --repeat=5 goodnight, Gracie
192- (Cmd) run 4
193- say -ps --repeat=5 goodnight, Gracie
194-
195- OODNIGHT, GRACIEGAY
196- OODNIGHT, GRACIEGAY
197- OODNIGHT, GRACIEGAY
198- OODNIGHT, GRACIEGAY
199- OODNIGHT, GRACIEGAY
200- (Cmd) orate Four score and
201- > seven releases ago
202- > our BDFL
203- > blah blah blah
204- Four score and
205- seven releases ago
206- our BDFL
207- blah blah blah
208- (Cmd) & look, a shortcut!
209- look, a shortcut!
210- (Cmd) show color
211- colors: True
212- (Cmd) set prompt "---> "
213- prompt - was: (Cmd)
214- now: --->
215- ---> say goodbye
216- goodbye
150+ example/exampleSession.txt:
151+
152+ ``` text
153+ (Cmd) help
154+
155+ Documented commands (type help <topic>):
156+ ========================================
157+ _relative_load edit help list orate py run say shell show
158+ cmdenvironment eof history load pause quit save set shortcuts speak
159+
160+ (Cmd) help say
161+ Repeats what you tell me to.
162+ Usage: speak [options] arg
163+
164+ Options:
165+ -h, --help show this help message and exit
166+ -p, --piglatin atinLay
167+ -s, --shout N00B EMULATION MODE
168+ -r REPEAT, --repeat=REPEAT
169+ output [n] times
170+
171+ (Cmd) say goodnight, Gracie
172+ goodnight, Gracie
173+ (Cmd) say -ps --repeat=5 goodnight, Gracie
174+ OODNIGHT, GRACIEGAY
175+ OODNIGHT, GRACIEGAY
176+ OODNIGHT, GRACIEGAY
177+ (Cmd) set maxrepeats 5
178+ maxrepeats - was: 3
179+ now: 5
180+ (Cmd) say -ps --repeat=5 goodnight, Gracie
181+ OODNIGHT, GRACIEGAY
182+ OODNIGHT, GRACIEGAY
183+ OODNIGHT, GRACIEGAY
184+ OODNIGHT, GRACIEGAY
185+ OODNIGHT, GRACIEGAY
186+ (Cmd) hi
187+ -------------------------[1]
188+ help
189+ -------------------------[2]
190+ help say
191+ -------------------------[3]
192+ say goodnight, Gracie
193+ -------------------------[4]
194+ say -ps --repeat=5 goodnight, Gracie
195+ -------------------------[5]
196+ set maxrepeats 5
197+ -------------------------[6]
198+ say -ps --repeat=5 goodnight, Gracie
199+ (Cmd) run 4
200+ say -ps --repeat=5 goodnight, Gracie
201+
202+ OODNIGHT, GRACIEGAY
203+ OODNIGHT, GRACIEGAY
204+ OODNIGHT, GRACIEGAY
205+ OODNIGHT, GRACIEGAY
206+ OODNIGHT, GRACIEGAY
207+ (Cmd) orate Four score and
208+ > seven releases ago
209+ > our BDFL
210+ > blah blah blah
211+ Four score and
212+ seven releases ago
213+ our BDFL
214+ blah blah blah
215+ (Cmd) & look, a shortcut!
216+ look, a shortcut!
217+ (Cmd) show color
218+ colors: /(True|False)/
219+ (Cmd) set prompt "---> "
220+ prompt - was: (Cmd)
221+ now: --->
222+ ---> say goodbye
223+ goodbye
224+ ```
225+
226+ Note how a regular expression ` /(True|False)/ ` is used near the end for output of the ** show color** command since
227+ colored text is currently not available for cmd2 on Windows. Regular expressions can be used anywhere within a
228+ transcript file simply by embedding them within two forward slashes, ` / ` .
0 commit comments