Mercurial > python-cmd2
annotate example/example.py @ 424:6773286315f0
require pyparsing 1.5.6 for its Python 3 power
author | catherine.devlin@gmail.com |
---|---|
date | Wed, 20 Jul 2011 23:23:09 -0400 |
parents | f16f444a4d10 |
children | bfbe4241bd6b |
rev | line source |
---|---|
106 | 1 '''A sample application for cmd2.''' |
2 | |
334 | 3 from cmd2 import Cmd, make_option, options |
106 | 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") | |
413
f16f444a4d10
added arg_desc to @options, thanks Renzo Crispiatico
Catherine Devlin <catherine.devlin@gmail.com>
parents:
334
diff
changeset
|
15 ], arg_desc = '(text to say)') |
106 | 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 | |
310 | 28 # initialized with a non-standard output destination |
106 | 29 |
30 do_say = do_speak # now "say" is a synonym for "speak" | |
31 do_orate = do_speak # another synonym, but this one takes multi-line input | |
32 | |
287
1cd23003e8d5
refactoring, but something went wrong with comments
catherine@bothari
parents:
259
diff
changeset
|
33 c = CmdLineApp() |
334 | 34 c.cmdloop() |