annotate flagReader.py @ 9:f807c5cfa0de

switched flagReader to optparse
author catherine@cordelia
date Thu, 15 May 2008 08:29:25 -0400
parents febfdc79550b
children 47af95ad83c7
rev   line source
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
1 """Defines and parses UNIX-style flags to modify command arguments.
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
2
9
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
3 Use of flagReader is DEPRECATED in favor of optparse from the
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
4 Python standard library. For backwards compatibility, flagReader
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
5 has been re-implemented as a wrapper around optparse.
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
6
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
7 print flagReader.FlagSet.parse.__doc__ for usage examples.
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
8 """
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
9
9
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
10 import re, optparse
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
11
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
12 class Flag(object):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
13 def __init__(self, name, abbrev=None, nargs=0):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
14 """Flag(name, abbrev=None, nargs=0) : Defines a flag.
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
15
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
16 name: the full name of the flag (double-dash form)
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
17 abbrev: the single-letter abbreviated form of the flag; defaults to
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
18 nargs: number of arguments expected after the flag"""
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
19
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
20 self.name = name
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
21 self.abbrev = abbrev or name[0]
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
22 self.fullabbrev = '-%s' % (self.abbrev)
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
23 self.fullname = '--%s' % (name)
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
24 self.nargs = nargs
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
25
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
26 class FlagSet(object):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
27 def __init__(self, flags):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
28 if not issubclass(type(flags), list):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
29 raise TypeError, 'Argument must be a list'
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
30 self.flags = flags
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
31 self.lookup = {}
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
32 for flag in self.flags:
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
33 self.lookup[flag.abbrev] = flag
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
34 self.lookup[flag.fullabbrev] = flag
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
35 self.lookup[flag.fullname] = flag
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
36 self.abbrevPattern = re.compile('^-([%s]+)$' % (''.join(f.abbrev for f in flags)))
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
37 def parse(self, arg):
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
38 """
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
39 Finds flags; returns {flag: (values, if any)} and the remaining argument.
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
40
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
41 >>> f = FlagSet([Flag('foo'), Flag('bar'), Flag('gimmea', nargs=1)])
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
42 >>> f.parse('-fb')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
43 ({'foo': [], 'bar': []}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
44 >>> f.parse('no flags')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
45 ({}, 'no flags')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
46 >>> f.parse('-f blah')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
47 ({'foo': []}, 'blah')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
48 >>> f.parse('--bar')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
49 ({'bar': []}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
50 >>> f.parse('--bar -f')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
51 ({'foo': [], 'bar': []}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
52 >>> f.parse('--notaflag')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
53 ({}, '--notaflag')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
54 >>> f.parse('')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
55 ({}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
56 >>> f.parse('--gimmea bee -f and then some other stuff')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
57 ({'gimmea': ['bee'], 'foo': []}, 'and then some other stuff')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
58 >>> f.parse('hidden -bar')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
59 ({}, 'hidden -bar')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
60 >>> f.parse('-g myarg -b')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
61 ({'gimmea': ['myarg'], 'bar': []}, '')
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
62 """
9
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
63 parser = optparse.OptionParser()
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
64 for flag in self.flags:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
65 if flag.nargs:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
66 parser.add_option(flag.fullabbrev, flag.fullname, action="store",
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
67 type="string", dest=flag.name)
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
68 else:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
69 parser.add_option(flag.fullabbrev, flag.fullname, action="store_true",
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
70 dest=flag.name)
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
71 try:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
72 (options, args) = parser.parse_args(arg.split())
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
73 except SystemExit, e:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
74 return {}, arg
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
75
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
76 result = {}
9
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
77 for (k,v) in options.__dict__.items():
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
78 if v == True:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
79 result[k] = []
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
80 elif v:
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
81 result[k] = [v]
f807c5cfa0de switched flagReader to optparse
catherine@cordelia
parents: 0
diff changeset
82 return result, ' '.join(args)
0
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
83
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
84 def _test():
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
85 import doctest
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
86 doctest.testmod()
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
87
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
88 if __name__ == '__main__':
febfdc79550b moved repository to Assembla
catherine@DellZilla.myhome.westell.com
parents:
diff changeset
89 _test()