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