annotate example/example.py @ 436:c4c35f002aef 0.6.4

to version 0.6.4
author catherine.devlin@gmail.com
date Thu, 25 Aug 2011 16:27:42 -0400
parents bfbe4241bd6b
children
rev   line source
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
1 '''A sample application for cmd2.'''
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
2
334
99dd71cb477a run() folded into app.cmdloop()
cat@eee
parents: 327
diff changeset
3 from cmd2 import Cmd, make_option, options
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
4 import unittest, optparse, sys
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
5
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
6 class CmdLineApp(Cmd):
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
7 multilineCommands = ['orate']
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
8 Cmd.shortcuts.update({'&': 'speak'})
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
9 maxrepeats = 3
435
bfbe4241bd6b custom double-redirector fixed
catherine.devlin@gmail.com
parents: 413
diff changeset
10 redirector = '->'
291
dfdbd93a2fae bugfixes to set
catherine@bothari
parents: 290
diff changeset
11 Cmd.settable.append('maxrepeats Max number of `--repeat`s allowed')
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
12
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
13 @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
14 make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
15 make_option('-r', '--repeat', type="int", help="output [n] times")
413
f16f444a4d10 added arg_desc to @options, thanks Renzo Crispiatico
Catherine Devlin <catherine.devlin@gmail.com>
parents: 334
diff changeset
16 ], arg_desc = '(text to say)')
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
17 def do_speak(self, arg, opts=None):
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
18 """Repeats what you tell me to."""
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
19 arg = ''.join(arg)
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
20 if opts.piglatin:
301
30af90fd46c5 refactor mostly done?
catherine@dellzilla
parents: 291
diff changeset
21 arg = '%s%say' % (arg[1:].rstrip(), arg[0])
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
22 if opts.shout:
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
23 arg = arg.upper()
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
24 repetitions = opts.repeat or 1
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
25 for i in range(min(repetitions, self.maxrepeats)):
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
26 self.stdout.write(arg)
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
27 self.stdout.write('\n')
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
28 # self.stdout.write is better than "print", because Cmd can be
310
9d91406ca3a7 select polished
catherine@dellzilla
parents: 309
diff changeset
29 # initialized with a non-standard output destination
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
30
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
31 do_say = do_speak # now "say" is a synonym for "speak"
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
32 do_orate = do_speak # another synonym, but this one takes multi-line input
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
33
287
1cd23003e8d5 refactoring, but something went wrong with comments
catherine@bothari
parents: 259
diff changeset
34 c = CmdLineApp()
334
99dd71cb477a run() folded into app.cmdloop()
cat@eee
parents: 327
diff changeset
35 c.cmdloop()