comparison cmd2.py @ 211:5da7d72e72ee

print full help on -h
author catherine@Elli.myhome.westell.com
date Thu, 12 Mar 2009 07:58:32 -0400
parents 3f8fac776845
children 9c4c4d012a92
comparison
equal deleted inserted replaced
210:3f8fac776845 211:5da7d72e72ee
25 flagReader.py options are still supported for backward compatibility 25 flagReader.py options are still supported for backward compatibility
26 """ 26 """
27 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest 27 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest
28 import unittest, string, datetime 28 import unittest, string, datetime
29 from optparse import make_option 29 from optparse import make_option
30 __version__ = '0.4.7' 30 __version__ = '0.4.8'
31 31
32 class OptionParser(optparse.OptionParser): 32 class OptionParser(optparse.OptionParser):
33 def exit(self, status=0, msg=None): 33 def exit(self, status=0, msg=None):
34 self.values._exit = True 34 self.values._exit = True
35 if msg: 35 if msg:
36 print msg 36 print msg
37
38 def print_help(self, *args, **kwargs):
39 # now, I need to call help of the calling function. Hmm.
40 try:
41 print self._func.__doc__
42 except AttributeError:
43 pass
44 optparse.OptionParser.print_help(self, *args, **kwargs)
37 45
38 def error(self, msg): 46 def error(self, msg):
39 """error(msg : string) 47 """error(msg : string)
40 48
41 Print a usage message incorporating 'msg' to stderr and exit. 49 Print a usage message incorporating 'msg' to stderr and exit.
57 def option_setup(func): 65 def option_setup(func):
58 optionParser = OptionParser() 66 optionParser = OptionParser()
59 for opt in option_list: 67 for opt in option_list:
60 optionParser.add_option(opt) 68 optionParser.add_option(opt)
61 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_')) 69 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_'))
70 optionParser._func = func
62 def newFunc(instance, arg): 71 def newFunc(instance, arg):
63 try: 72 try:
64 opts, newArgList = optionParser.parse_args(arg.split()) # doesn't understand quoted strings shouldn't be dissected! 73 opts, newArgList = optionParser.parse_args(arg.split()) # doesn't understand quoted strings shouldn't be dissected!
65 newArgs = remainingArgs(arg, newArgList) # should it permit flags after args? 74 newArgs = remainingArgs(arg, newArgList) # should it permit flags after args?
66 except (optparse.OptionValueError, optparse.BadOptionError, 75 except (optparse.OptionValueError, optparse.BadOptionError,
244 'terminators': str(self.terminators), 253 'terminators': str(self.terminators),
245 'settable': ' '.join(self.settable) 254 'settable': ' '.join(self.settable)
246 }) 255 })
247 256
248 def do_help(self, arg): 257 def do_help(self, arg):
249 cmd.Cmd.do_help(self, arg)
250 try: 258 try:
251 fn = getattr(self, 'do_' + arg) 259 fn = getattr(self, 'do_' + arg)
252 if fn and fn.optionParser: 260 if fn and fn.optionParser:
253 fn.optionParser.print_help(file=self.stdout) 261 fn.optionParser.print_help(file=self.stdout)
262 return
254 except AttributeError: 263 except AttributeError:
255 pass 264 pass
265 cmd.Cmd.do_help(self, arg)
256 266
257 def __init__(self, *args, **kwargs): 267 def __init__(self, *args, **kwargs):
258 cmd.Cmd.__init__(self, *args, **kwargs) 268 cmd.Cmd.__init__(self, *args, **kwargs)
259 self.history = History() 269 self.history = History()
260 self._init_parser() 270 self._init_parser()