comparison README.txt @ 13:c6e8b645c0ab

tweaking docs
author catherine@localhost
date Thu, 15 May 2008 13:13:31 -0400
parents 47af95ad83c7
children 0a316420636e
comparison
equal deleted inserted replaced
12:b636cb715925 13:c6e8b645c0ab
41 - Settable environment parameters 41 - Settable environment parameters
42 42
43 To allow a user to change an environment parameter during program execution, 43 To allow a user to change an environment parameter during program execution,
44 append the parameter's name to `Cmd.settable`. 44 append the parameter's name to `Cmd.settable`.
45 45
46 - Parsing commands with flags 46 - Parsing commands with `optparse` options (flags)
47 47
48 To allow a command to parse flags: 48 ::
49 49
50 1. Create a flagset: `flags = flagReader.FlagSet([flagReader.Flag('option')])` 50 @options([make_option('-m', '--myoption', action="store_true", help="atinLay")])
51 2. Within the command's function, `(opts, arg) = flags.parse(arg)` 51 def myfunc(self, arg):
52 3. `opts` is a dictionary whose keys are the flags given, and whose values 52 ...
53 are the arguments to those flags (if any). 53
54 54 See Python standard library's `optparse` documentation: http://docs.python.org/lib/optparse-defining-options.html
55
55 - Catherine Devlin, catherinedevlin.blogspot.com 56 - Catherine Devlin, catherinedevlin.blogspot.com
56 57
57 cmd2 can be installed with `easy_install cmd2` 58 cmd2 can be installed with `easy_install cmd2`
58 59
59 Cheese Shop page: http://pypi.python.org/pypi/cmd2/0.1 60 Cheese Shop page: http://pypi.python.org/pypi/cmd2/0.1
60 61
61 Example cmd2 application (cmd2_example.py) :: 62 Example cmd2 application (cmd2_example.py) ::
62 63
63 from cmd2 import Cmd 64 from cmd2 import Cmd, make_option, options
64 import optparse 65
65
66 class CmdLineApp(Cmd): 66 class CmdLineApp(Cmd):
67 multilineCommands = ['orate'] 67 multilineCommands = ['orate']
68 Cmd.shortcuts.update({'&': 'speak'}) 68 Cmd.shortcuts.update({'&': 'speak'})
69 maxrepeats = 3 69 maxrepeats = 3
70 Cmd.settable.append('maxrepeats') 70 Cmd.settable.append('maxrepeats')
71 speakflags = flagReader.FlagSet([flagReader.Flag('piglatin'), 71
72 flagReader.Flag('shout'), 72 @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
73 flagReader.Flag('repeat', nargs=1) 73 make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
74 ]) 74 make_option('-r', '--repeat', type="int", help="output [n] times")
75 speakOptionParser = optparse.OptionParser() 75 ])
76 speakOptionParser.add_option('-p', '--piglatin', action="store_true", help="atinLay") 76 def do_speak(self, arg, opts=None):
77 speakOptionParser.add_option('-s', '--shout', action="store_true", help="output in ALL CAPS") 77 """Repeats what you tell me to."""
78 speakOptionParser.add_option('-r', '--repeat', type="int", help="output [n] times") 78 arg = ' '.join(arg)
79 def do_speak(self, arg): 79 if opts.piglatin:
80 """Repeats what you tell me to.
81
82 args: --piglatin, -p: translate to Pig Latin
83 --shout, -s: emulate internet newbie
84 --repeat (nTimes), -r: be redundant"""
85 (options, arg) = speakOptionParser.parse_args(arg.split())
86
87 if options.piglatin:
88 arg = '%s%say' % (arg[1:], arg[0]) 80 arg = '%s%say' % (arg[1:], arg[0])
89 if options.shout: 81 if opts.shout:
90 arg = arg.upper() 82 arg = arg.upper()
91 repetitions = options.repeat or 1 83 repetitions = opts.repeat or 1
92 for i in range(min(repetitions, self.maxrepeats)): 84 for i in range(min(repetitions, self.maxrepeats)):
93 self.stdout.write(arg) 85 self.stdout.write(arg)
94 self.stdout.write('\n') 86 self.stdout.write('\n')
95 # self.stdout.write is better than "print", because Cmd can be 87 # self.stdout.write is better than "print", because Cmd can be
96 # initialized with a non-standard output destination 88 # initialized with a non-standard output destination
89
97 do_say = do_speak # now "say" is a synonym for "speak" 90 do_say = do_speak # now "say" is a synonym for "speak"
98 do_orate = do_speak # another synonym, but this one takes multi-line input 91 do_orate = do_speak # another synonym, but this one takes multi-line input
99 92
100 app = CmdLineApp() 93 app = CmdLineApp()
101 app.cmdloop() 94 app.cmdloop()
102 95
103 Sample session using the above code :: 96 Sample session using the above code ::
104 97