comparison cmd2.py @ 159:5a94501b6b93

fix new bug in argument parsing
author catherine@dellzilla
date Mon, 01 Dec 2008 11:07:29 -0500
parents 87d3f3203b96
children 59b7847ec3ca
comparison
equal deleted inserted replaced
158:87d3f3203b96 159:5a94501b6b93
50 optionParser.add_option(opt) 50 optionParser.add_option(opt)
51 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_')) 51 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_'))
52 def newFunc(instance, arg): 52 def newFunc(instance, arg):
53 try: 53 try:
54 opts, newArgs = optionParser.parse_args(arg.split()) 54 opts, newArgs = optionParser.parse_args(arg.split())
55 arg.parsed['args'] = arg[arg.find(newArgs[0]):] 55 newArgs = arg[arg.find(newArgs[0]):]
56 except (optparse.OptionValueError, optparse.BadOptionError, 56 except (optparse.OptionValueError, optparse.BadOptionError,
57 optparse.OptionError, optparse.AmbiguousOptionError, 57 optparse.OptionError, optparse.AmbiguousOptionError,
58 optparse.OptionConflictError), e: 58 optparse.OptionConflictError), e:
59 print e 59 print e
60 optionParser.print_help() 60 optionParser.print_help()
61 return 61 return
62 if hasattr(opts, '_exit'): 62 if hasattr(opts, '_exit'):
63 return None 63 return None
64 arg = arg.parser('%s %s' % (arg.parsed.command, newArgs))
64 result = func(instance, arg, opts) 65 result = func(instance, arg, opts)
65 return result 66 return result
66 newFunc.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help()) 67 newFunc.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help())
67 return newFunc 68 return newFunc
68 return option_setup 69 return option_setup
69 70
70 class PasteBufferError(EnvironmentError): 71 class PasteBufferError(EnvironmentError):
135 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') 136 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
136 setPasteBuffer = getPasteBuffer 137 setPasteBuffer = getPasteBuffer
137 writeToPasteBuffer = getPasteBuffer 138 writeToPasteBuffer = getPasteBuffer
138 139
139 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t') 140 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t')
141 '''
140 def parseSearchResults(pattern, s): 142 def parseSearchResults(pattern, s):
141 generator = pattern.scanString(s) 143 generator = pattern.scanString(s)
142 try: 144 try:
143 result, start, stop = generator.next() 145 result, start, stop = generator.next()
144 result['before'], result['after'] = s[:start], s[stop:] 146 result['before'], result['after'] = s[:start], s[stop:]
145 result['upToIncluding'] = s[:stop] 147 result['upToIncluding'] = s[:stop]
146 except StopIteration: 148 except StopIteration:
147 result = pyparsing.ParseResults('') 149 result = pyparsing.ParseResults('')
148 result['before'] = s 150 result['before'] = s
149 return result 151 return result
150 152 '''
151 153
152 def replaceInput(source): 154 def replaceInput(source):
153 if source: 155 if source:
154 newinput = open(source[0], 'r').read() 156 newinput = open(source[0], 'r').read()
155 else: 157 else:
418 if useTerminatorFrom: 420 if useTerminatorFrom:
419 result['terminator'] = useTerminatorFrom.parsed.terminator 421 result['terminator'] = useTerminatorFrom.parsed.terminator
420 result['suffix'] = useTerminatorFrom.parsed.suffix 422 result['suffix'] = useTerminatorFrom.parsed.suffix
421 p = ParsedString(result.args) 423 p = ParsedString(result.args)
422 p.parsed = result 424 p.parsed = result
425 p.parser = self.parsed
423 return p 426 return p
424 427
425 def onecmd(self, line, assumeComplete=False): 428 def onecmd(self, line, assumeComplete=False):
426 """Interpret the argument as though it had been typed in response 429 """Interpret the argument as though it had been typed in response
427 to the prompt. 430 to the prompt.