# HG changeset patch # User catherine@cordelia # Date 1210854565 14400 # Node ID f807c5cfa0defaa80aec29a612c972a27e7ec1e9 # Parent a6284fa14e894307aa44c09e8357eeb5198a0aa1 switched flagReader to optparse diff -r a6284fa14e89 -r f807c5cfa0de flagReader.py --- a/flagReader.py Wed May 14 11:37:34 2008 -0400 +++ b/flagReader.py Thu May 15 08:29:25 2008 -0400 @@ -1,9 +1,13 @@ """Defines and parses UNIX-style flags to modify command arguments. +Use of flagReader is DEPRECATED in favor of optparse from the +Python standard library. For backwards compatibility, flagReader +has been re-implemented as a wrapper around optparse. + print flagReader.FlagSet.parse.__doc__ for usage examples. """ -import re +import re, optparse class Flag(object): def __init__(self, name, abbrev=None, nargs=0): @@ -56,29 +60,26 @@ >>> f.parse('-g myarg -b') ({'gimmea': ['myarg'], 'bar': []}, '') """ + parser = optparse.OptionParser() + for flag in self.flags: + if flag.nargs: + parser.add_option(flag.fullabbrev, flag.fullname, action="store", + type="string", dest=flag.name) + else: + parser.add_option(flag.fullabbrev, flag.fullname, action="store_true", + dest=flag.name) + try: + (options, args) = parser.parse_args(arg.split()) + except SystemExit, e: + return {}, arg + result = {} - words = arg.split() - while words: - word = words[0] - flag = self.lookup.get(word) - if flag: - result[flag.name] = [] - words.pop(0) - for arg in range(flag.nargs): - try: - result[flag.name].append(words.pop(0)) - except IndexError: # there aren't as many args as we expect - raise IndexError, '%s expects %d arguments' % (word, flag.nargs) - continue # on to next word - smashedAbbrevs = self.abbrevPattern.search(word) - if smashedAbbrevs: - for abbrev in smashedAbbrevs.group(1): - result[self.lookup[abbrev].name] = [] - words.pop(0) - continue # on to next word - #if you get to here, word[0] does not denote options - break - return result, ' '.join(words) + for (k,v) in options.__dict__.items(): + if v == True: + result[k] = [] + elif v: + result[k] = [v] + return result, ' '.join(args) def _test(): import doctest