Mercurial > python-cmd2
changeset 10:47af95ad83c7
working the refactor still
author | catherine@localhost |
---|---|
date | Thu, 15 May 2008 12:21:38 -0400 |
parents | f807c5cfa0de |
children | 34107b79c840 |
files | README.txt cmd2.py flagReader.py |
diffstat | 3 files changed, 43 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- 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')
--- 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()
--- 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):