comparison README.txt @ 10:47af95ad83c7

working the refactor still
author catherine@localhost
date Thu, 15 May 2008 12:21:38 -0400
parents 71067a445663
children c6e8b645c0ab
comparison
equal deleted inserted replaced
9:f807c5cfa0de 10:47af95ad83c7
58 58
59 Cheese Shop page: http://pypi.python.org/pypi/cmd2/0.1 59 Cheese Shop page: http://pypi.python.org/pypi/cmd2/0.1
60 60
61 Example cmd2 application (cmd2_example.py) :: 61 Example cmd2 application (cmd2_example.py) ::
62 62
63 from cmd2 import Cmd, flagReader 63 from cmd2 import Cmd
64 import optparse
64 65
65 class CmdLineApp(Cmd): 66 class CmdLineApp(Cmd):
66 multilineCommands = ['orate'] 67 multilineCommands = ['orate']
67 Cmd.shortcuts.update({'&': 'speak'}) 68 Cmd.shortcuts.update({'&': 'speak'})
68 maxrepeats = 3 69 maxrepeats = 3
69 Cmd.settable.append('maxrepeats') 70 Cmd.settable.append('maxrepeats')
70 speakflags = flagReader.FlagSet([flagReader.Flag('piglatin'), 71 speakflags = flagReader.FlagSet([flagReader.Flag('piglatin'),
71 flagReader.Flag('shout'), 72 flagReader.Flag('shout'),
72 flagReader.Flag('repeat', nargs=1) 73 flagReader.Flag('repeat', nargs=1)
73 ]) 74 ])
75 speakOptionParser = optparse.OptionParser()
76 speakOptionParser.add_option('-p', '--piglatin', action="store_true", help="atinLay")
77 speakOptionParser.add_option('-s', '--shout', action="store_true", help="output in ALL CAPS")
78 speakOptionParser.add_option('-r', '--repeat', type="int", help="output [n] times")
74 def do_speak(self, arg): 79 def do_speak(self, arg):
75 """Repeats what you tell me to. 80 """Repeats what you tell me to.
76 81
77 args: --piglatin, -p: translate to Pig Latin 82 args: --piglatin, -p: translate to Pig Latin
78 --shout, -s: emulate internet newbie 83 --shout, -s: emulate internet newbie
79 --repeat (nTimes), -r: be redundant""" 84 --repeat (nTimes), -r: be redundant"""
80 (options, arg) = self.speakflags.parse(arg) 85 (options, arg) = speakOptionParser.parse_args(arg.split())
81 86
82 if options.has_key('piglatin'): 87 if options.piglatin:
83 arg = '%s%say' % (arg[1:], arg[0]) 88 arg = '%s%say' % (arg[1:], arg[0])
84 if options.has_key('shout'): 89 if options.shout:
85 arg = arg.upper() 90 arg = arg.upper()
86 repetitions = options.get('repeat') 91 repetitions = options.repeat or 1
87 repetitions = int(repetitions[0]) if repetitions else 1
88 for i in range(min(repetitions, self.maxrepeats)): 92 for i in range(min(repetitions, self.maxrepeats)):
89 self.stdout.write(arg) 93 self.stdout.write(arg)
90 self.stdout.write('\n') 94 self.stdout.write('\n')
91 # self.stdout.write is better than "print", because Cmd can be 95 # self.stdout.write is better than "print", because Cmd can be
92 # initialized with a non-standard output destination 96 # initialized with a non-standard output destination