comparison README.txt @ 112:e3b8eaadea56

going to collapse down out of overdone package structure
author catherine@Elli.myhome.westell.com
date Sat, 25 Oct 2008 19:28:51 -0400
parents 234fb764becd
children 0820c42ea23e
comparison
equal deleted inserted replaced
111:c1e5df33721f 112:e3b8eaadea56
61 61
62 cmd2 can be installed with `easy_install cmd2` 62 cmd2 can be installed with `easy_install cmd2`
63 63
64 Cheese Shop page: http://pypi.python.org/pypi/cmd2 64 Cheese Shop page: http://pypi.python.org/pypi/cmd2
65 65
66 Example cmd2 application (cmd2_example.py) :: 66 Example cmd2 application (example/example.py) ::
67 67
68 from cmd2 import Cmd, make_option, options 68 '''A sample application for cmd2.'''
69 69
70 from cmd2 import Cmd, make_option, options, Cmd2TestCase
71 import unittest, optparse, sys
72
70 class CmdLineApp(Cmd): 73 class CmdLineApp(Cmd):
71 multilineCommands = ['orate'] 74 multilineCommands = ['orate']
72 Cmd.shortcuts.update({'&': 'speak'}) 75 Cmd.shortcuts.update({'&': 'speak'})
73 maxrepeats = 3 76 maxrepeats = 3
74 Cmd.settable.append('maxrepeats') 77 Cmd.settable.append('maxrepeats')
75 78
76 @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"), 79 @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
77 make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), 80 make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
78 make_option('-r', '--repeat', type="int", help="output [n] times") 81 make_option('-r', '--repeat', type="int", help="output [n] times")
79 ]) 82 ])
80 def do_speak(self, arg, opts=None): 83 def do_speak(self, arg, opts=None):
81 """Repeats what you tell me to.""" 84 """Repeats what you tell me to."""
82 arg = ' '.join(arg) 85 arg = ''.join(arg)
83 if opts.piglatin: 86 if opts.piglatin:
84 arg = '%s%say' % (arg[1:], arg[0]) 87 arg = '%s%say' % (arg[1:], arg[0])
85 if opts.shout: 88 if opts.shout:
86 arg = arg.upper() 89 arg = arg.upper()
87 repetitions = opts.repeat or 1 90 repetitions = opts.repeat or 1
88 for i in range(min(repetitions, self.maxrepeats)): 91 for i in range(min(repetitions, self.maxrepeats)):
89 self.stdout.write(arg) 92 self.stdout.write(arg)
90 self.stdout.write('\n') 93 self.stdout.write('\n')
91 # self.stdout.write is better than "print", because Cmd can be 94 # self.stdout.write is better than "print", because Cmd can be
92 # initialized with a non-standard output destination 95 # initialized with a non-standard output destination
93 96
94 do_say = do_speak # now "say" is a synonym for "speak" 97 do_say = do_speak # now "say" is a synonym for "speak"
95 do_orate = do_speak # another synonym, but this one takes multi-line input 98 do_orate = do_speak # another synonym, but this one takes multi-line input
96 99
97 app = CmdLineApp() 100 class TestMyAppCase(Cmd2TestCase):
98 app.cmdloop() 101 CmdApp = CmdLineApp
102 transcriptFileName = 'exampleSession.txt'
103
104 parser = optparse.OptionParser()
105 parser.add_option('-t', '--test', dest='unittests', action='store_true', default=False, help='Run unit test suite')
106 (callopts, callargs) = parser.parse_args()
107 if callopts.unittests:
108 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
109 unittest.main()
110 else:
111 app = CmdLineApp()
112 app.cmdloop()
99 113
100 Sample session using the above code :: 114 Sample session using the above code ::
101 115
102 c:\cmd2>python cmd2_example.py 116 c:\cmd2>python cmd2_example.py
103 (Cmd) speak softly 117 (Cmd) speak softly