# HG changeset patch # User catherine@localhost # Date 1210868498 14400 # Node ID 47af95ad83c7a72ae305fd18490935149943b28a # Parent f807c5cfa0defaa80aec29a612c972a27e7ec1e9 working the refactor still diff -r f807c5cfa0de -r 47af95ad83c7 README.txt --- a/README.txt Thu May 15 08:29:25 2008 -0400 +++ b/README.txt Thu May 15 12:21:38 2008 -0400 @@ -60,7 +60,8 @@ Example cmd2 application (cmd2_example.py) :: - from cmd2 import Cmd, flagReader + from cmd2 import Cmd + import optparse class CmdLineApp(Cmd): multilineCommands = ['orate'] @@ -71,20 +72,23 @@ flagReader.Flag('shout'), flagReader.Flag('repeat', nargs=1) ]) + speakOptionParser = optparse.OptionParser() + speakOptionParser.add_option('-p', '--piglatin', action="store_true", help="atinLay") + speakOptionParser.add_option('-s', '--shout', action="store_true", help="output in ALL CAPS") + speakOptionParser.add_option('-r', '--repeat', type="int", help="output [n] times") def do_speak(self, arg): """Repeats what you tell me to. args: --piglatin, -p: translate to Pig Latin --shout, -s: emulate internet newbie --repeat (nTimes), -r: be redundant""" - (options, arg) = self.speakflags.parse(arg) + (options, arg) = speakOptionParser.parse_args(arg.split()) - if options.has_key('piglatin'): + if options.piglatin: arg = '%s%say' % (arg[1:], arg[0]) - if options.has_key('shout'): + if options.shout: arg = arg.upper() - repetitions = options.get('repeat') - repetitions = int(repetitions[0]) if repetitions else 1 + repetitions = options.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): self.stdout.write(arg) self.stdout.write('\n') diff -r f807c5cfa0de -r 47af95ad83c7 cmd2.py --- a/cmd2.py Thu May 15 08:29:25 2008 -0400 +++ b/cmd2.py Thu May 15 12:21:38 2008 -0400 @@ -20,8 +20,28 @@ - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com """ -import cmd, re, os, sys -import flagReader +import cmd, re, os, sys, optparse +from optparse import make_option + +def options(option_list): + def option_setup(func): + optionParser = optparse.OptionParser() + for opt in option_list: + optionParser.add_option(opt) + def newFunc(instance, arg): + optionParser.set_usage("%s [options] arg" % func.__name__) + try: + opts, arg = optionParser.parse_args(arg.split()) + except optparse.OptionValueError, e: + print e + optionParser.print_help() + return + result = func(instance, arg, opts) + #import pdb; pdb.set_trace() + return result + newFunc.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help()) + return newFunc + return option_setup class Cmd(cmd.Cmd): caseInsensitive = True @@ -53,6 +73,15 @@ 'settable': ' '.join(self.settable) }) + def do_help(self, arg): + cmd.Cmd.do_help(self, arg) + try: + fn = getattr(self, 'do_' + arg) + if fn and fn.optionParser: + fn.optionParser.print_help(file=self.stdout) + except AttributeError: + pass + def __init__(self, *args, **kwargs): cmd.Cmd.__init__(self, *args, **kwargs) self.history = History() diff -r f807c5cfa0de -r 47af95ad83c7 flagReader.py --- a/flagReader.py Thu May 15 08:29:25 2008 -0400 +++ b/flagReader.py Thu May 15 12:21:38 2008 -0400 @@ -7,7 +7,8 @@ print flagReader.FlagSet.parse.__doc__ for usage examples. """ -import re, optparse +import re, optparse, warnings +warnings.warn("""flagReader has been deprecated. Use optparse instead.""", DeprecationWarning) class Flag(object): def __init__(self, name, abbrev=None, nargs=0):