annotate example/example.py @ 326:237a89d5a4a9

run() working
author catherine@dellzilla
date Thu, 11 Feb 2010 17:47:29 -0500
parents 4172feeddf76
children e9eea93c777c
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
325
4172feeddf76 want to incorporate run() for tests - not yet working
catherine@dellzilla
parents: 310
diff changeset
3 from cmd2 import Cmd, make_option, options, Cmd2TestCase, run
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
291
dfdbd93a2fae bugfixes to set
catherine@bothari
parents: 290
diff changeset
10 Cmd.settable.append('maxrepeats Max number of `--repeat`s allowed')
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
11
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
12 @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
13 make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
14 make_option('-r', '--repeat', type="int", help="output [n] times")
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
15 ])
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
16 def do_speak(self, arg, opts=None):
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
17 """Repeats what you tell me to."""
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
18 arg = ''.join(arg)
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
19 if opts.piglatin:
301
30af90fd46c5 refactor mostly done?
catherine@dellzilla
parents: 291
diff changeset
20 arg = '%s%say' % (arg[1:].rstrip(), arg[0])
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
21 if opts.shout:
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
22 arg = arg.upper()
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
23 repetitions = opts.repeat or 1
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
24 for i in range(min(repetitions, self.maxrepeats)):
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
25 self.stdout.write(arg)
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
26 self.stdout.write('\n')
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
27 # self.stdout.write is better than "print", because Cmd can be
310
9d91406ca3a7 select polished
catherine@dellzilla
parents: 309
diff changeset
28 # initialized with a non-standard output destination
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
29
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
30 do_say = do_speak # now "say" is a synonym for "speak"
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
31 do_orate = do_speak # another synonym, but this one takes multi-line input
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
32
287
1cd23003e8d5 refactoring, but something went wrong with comments
catherine@bothari
parents: 259
diff changeset
33 c = CmdLineApp()
326
237a89d5a4a9 run() working
catherine@dellzilla
parents: 325
diff changeset
34 run(c)
287
1cd23003e8d5 refactoring, but something went wrong with comments
catherine@bothari
parents: 259
diff changeset
35
326
237a89d5a4a9 run() working
catherine@dellzilla
parents: 325
diff changeset
36 '''
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
37 class TestMyAppCase(Cmd2TestCase):
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
38 CmdApp = CmdLineApp
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
39 parser = optparse.OptionParser()
259
5147fa4166b0 multiple transcript tests working - sinister BASH trick defeated
Catherine Devlin <catherine.devlin@gmail.com>
parents: 258
diff changeset
40 parser.add_option('-t', '--test', dest='test', action="store_true",
5147fa4166b0 multiple transcript tests working - sinister BASH trick defeated
Catherine Devlin <catherine.devlin@gmail.com>
parents: 258
diff changeset
41 help='Test against transcript(s) in FILE (wildcards OK)')
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
42 (callopts, callargs) = parser.parse_args()
257
d62bb3dd58a0 multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents: 254
diff changeset
43 if callopts.test:
259
5147fa4166b0 multiple transcript tests working - sinister BASH trick defeated
Catherine Devlin <catherine.devlin@gmail.com>
parents: 258
diff changeset
44 CmdLineApp.testfiles = callargs
257
d62bb3dd58a0 multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents: 254
diff changeset
45 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
106
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
46 unittest.main()
2d3232693807 prompt-setting quirks
catherine@dellzilla
parents:
diff changeset
47 else:
326
237a89d5a4a9 run() working
catherine@dellzilla
parents: 325
diff changeset
48 CmdLineApp().cmdloop()'''