comparison cmd2/example.py @ 106:2d3232693807

prompt-setting quirks
author catherine@dellzilla
date Fri, 24 Oct 2008 16:03:58 -0400
parents
children
comparison
equal deleted inserted replaced
105:130340609e19 106:2d3232693807
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
10 Cmd.settable.append('maxrepeats')
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:
20 arg = '%s%say' % (arg[1:], arg[0])
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
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
33 class TestMyAppCase(Cmd2TestCase):
34 CmdApp = CmdLineApp
35 transcriptFileName = 'exampleSession.txt'
36
37 parser = optparse.OptionParser()
38 parser.add_option('-t', '--test', dest='unittests', action='store_true', default=False, help='Run unit test suite')
39 (callopts, callargs) = parser.parse_args()
40 if callopts.unittests:
41 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
42 unittest.main()
43 else:
44 app = CmdLineApp()
45 app.cmdloop()