Mercurial > python-cmd2
annotate example/example.py @ 309:1941e54cb776
added bash-style SELECT
author | catherine@dellzilla |
---|---|
date | Fri, 29 Jan 2010 10:32:40 -0500 |
parents | 30af90fd46c5 |
children | 9d91406ca3a7 |
rev | line source |
---|---|
106 | 1 '''A sample application for cmd2.''' |
2 | |
3 from cmd2 import Cmd, make_option, options, Cmd2TestCase | |
4 import unittest, optparse, sys | |
5 | |
6 class CmdLineApp(Cmd): | |
7 multilineCommands = ['orate'] | |
8 Cmd.shortcuts.update({'&': 'speak'}) | |
9 maxrepeats = 3 | |
291 | 10 Cmd.settable.append('maxrepeats Max number of `--repeat`s allowed') |
106 | 11 |
12 @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"), | |
13 make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), | |
14 make_option('-r', '--repeat', type="int", help="output [n] times") | |
15 ]) | |
16 def do_speak(self, arg, opts=None): | |
17 """Repeats what you tell me to.""" | |
18 arg = ''.join(arg) | |
19 if opts.piglatin: | |
301 | 20 arg = '%s%say' % (arg[1:].rstrip(), arg[0]) |
106 | 21 if opts.shout: |
22 arg = arg.upper() | |
23 repetitions = opts.repeat or 1 | |
24 for i in range(min(repetitions, self.maxrepeats)): | |
25 self.stdout.write(arg) | |
26 self.stdout.write('\n') | |
27 # self.stdout.write is better than "print", because Cmd can be | |
28 # initialized with a non-standard output destination | |
309 | 29 def do_sel(self, arg): |
30 opts = arg.split() | |
31 result = self.select(opts) | |
32 self.poutput('yay for %s' % result) | |
106 | 33 |
34 do_say = do_speak # now "say" is a synonym for "speak" | |
35 do_orate = do_speak # another synonym, but this one takes multi-line input | |
36 | |
287
1cd23003e8d5
refactoring, but something went wrong with comments
catherine@bothari
parents:
259
diff
changeset
|
37 c = CmdLineApp() |
1cd23003e8d5
refactoring, but something went wrong with comments
catherine@bothari
parents:
259
diff
changeset
|
38 |
106 | 39 class TestMyAppCase(Cmd2TestCase): |
40 CmdApp = CmdLineApp | |
41 parser = optparse.OptionParser() | |
259
5147fa4166b0
multiple transcript tests working - sinister BASH trick defeated
Catherine Devlin <catherine.devlin@gmail.com>
parents:
258
diff
changeset
|
42 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
|
43 help='Test against transcript(s) in FILE (wildcards OK)') |
106 | 44 (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
|
45 if callopts.test: |
259
5147fa4166b0
multiple transcript tests working - sinister BASH trick defeated
Catherine Devlin <catherine.devlin@gmail.com>
parents:
258
diff
changeset
|
46 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
|
47 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() |
106 | 48 unittest.main() |
49 else: | |
290 | 50 CmdLineApp().cmdloop() |
287
1cd23003e8d5
refactoring, but something went wrong with comments
catherine@bothari
parents:
259
diff
changeset
|
51 |