comparison cmd2.py @ 10:47af95ad83c7

working the refactor still
author catherine@localhost
date Thu, 15 May 2008 12:21:38 -0400
parents 78033fd1078d
children 34107b79c840
comparison
equal deleted inserted replaced
9:f807c5cfa0de 10:47af95ad83c7
18 edited commands end with "EOF". Hmm. 18 edited commands end with "EOF". Hmm.
19 example of flag usage 19 example of flag usage
20 20
21 - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com 21 - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com
22 """ 22 """
23 import cmd, re, os, sys 23 import cmd, re, os, sys, optparse
24 import flagReader 24 from optparse import make_option
25
26 def options(option_list):
27 def option_setup(func):
28 optionParser = optparse.OptionParser()
29 for opt in option_list:
30 optionParser.add_option(opt)
31 def newFunc(instance, arg):
32 optionParser.set_usage("%s [options] arg" % func.__name__)
33 try:
34 opts, arg = optionParser.parse_args(arg.split())
35 except optparse.OptionValueError, e:
36 print e
37 optionParser.print_help()
38 return
39 result = func(instance, arg, opts)
40 #import pdb; pdb.set_trace()
41 return result
42 newFunc.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help())
43 return newFunc
44 return option_setup
25 45
26 class Cmd(cmd.Cmd): 46 class Cmd(cmd.Cmd):
27 caseInsensitive = True 47 caseInsensitive = True
28 multilineCommands = [] 48 multilineCommands = []
29 continuationPrompt = '> ' 49 continuationPrompt = '> '
51 { 'casesensitive': ('not ' and self.caseInsensitive) or '', 71 { 'casesensitive': ('not ' and self.caseInsensitive) or '',
52 'terminators': ' '.join(self.terminators), 72 'terminators': ' '.join(self.terminators),
53 'settable': ' '.join(self.settable) 73 'settable': ' '.join(self.settable)
54 }) 74 })
55 75
76 def do_help(self, arg):
77 cmd.Cmd.do_help(self, arg)
78 try:
79 fn = getattr(self, 'do_' + arg)
80 if fn and fn.optionParser:
81 fn.optionParser.print_help(file=self.stdout)
82 except AttributeError:
83 pass
84
56 def __init__(self, *args, **kwargs): 85 def __init__(self, *args, **kwargs):
57 cmd.Cmd.__init__(self, *args, **kwargs) 86 cmd.Cmd.__init__(self, *args, **kwargs)
58 self.history = History() 87 self.history = History()
59 88
60 def do_shortcuts(self, args): 89 def do_shortcuts(self, args):