Mercurial > python-cmd2
comparison cmd2/flagReader.py @ 100:3de2a3eb765a
oops, renesting directory
author | catherine@dellzilla |
---|---|
date | Mon, 29 Sep 2008 12:48:29 -0400 |
parents | flagReader.py@47af95ad83c7 |
children |
comparison
equal
deleted
inserted
replaced
99:00898931969b | 100:3de2a3eb765a |
---|---|
1 """Defines and parses UNIX-style flags to modify command arguments. | |
2 | |
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 | |
7 print flagReader.FlagSet.parse.__doc__ for usage examples. | |
8 """ | |
9 | |
10 import re, optparse, warnings | |
11 warnings.warn("""flagReader has been deprecated. Use optparse instead.""", DeprecationWarning) | |
12 | |
13 class Flag(object): | |
14 def __init__(self, name, abbrev=None, nargs=0): | |
15 """Flag(name, abbrev=None, nargs=0) : Defines a flag. | |
16 | |
17 name: the full name of the flag (double-dash form) | |
18 abbrev: the single-letter abbreviated form of the flag; defaults to | |
19 nargs: number of arguments expected after the flag""" | |
20 | |
21 self.name = name | |
22 self.abbrev = abbrev or name[0] | |
23 self.fullabbrev = '-%s' % (self.abbrev) | |
24 self.fullname = '--%s' % (name) | |
25 self.nargs = nargs | |
26 | |
27 class FlagSet(object): | |
28 def __init__(self, flags): | |
29 if not issubclass(type(flags), list): | |
30 raise TypeError, 'Argument must be a list' | |
31 self.flags = flags | |
32 self.lookup = {} | |
33 for flag in self.flags: | |
34 self.lookup[flag.abbrev] = flag | |
35 self.lookup[flag.fullabbrev] = flag | |
36 self.lookup[flag.fullname] = flag | |
37 self.abbrevPattern = re.compile('^-([%s]+)$' % (''.join(f.abbrev for f in flags))) | |
38 def parse(self, arg): | |
39 """ | |
40 Finds flags; returns {flag: (values, if any)} and the remaining argument. | |
41 | |
42 >>> f = FlagSet([Flag('foo'), Flag('bar'), Flag('gimmea', nargs=1)]) | |
43 >>> f.parse('-fb') | |
44 ({'foo': [], 'bar': []}, '') | |
45 >>> f.parse('no flags') | |
46 ({}, 'no flags') | |
47 >>> f.parse('-f blah') | |
48 ({'foo': []}, 'blah') | |
49 >>> f.parse('--bar') | |
50 ({'bar': []}, '') | |
51 >>> f.parse('--bar -f') | |
52 ({'foo': [], 'bar': []}, '') | |
53 >>> f.parse('--notaflag') | |
54 ({}, '--notaflag') | |
55 >>> f.parse('') | |
56 ({}, '') | |
57 >>> f.parse('--gimmea bee -f and then some other stuff') | |
58 ({'gimmea': ['bee'], 'foo': []}, 'and then some other stuff') | |
59 >>> f.parse('hidden -bar') | |
60 ({}, 'hidden -bar') | |
61 >>> f.parse('-g myarg -b') | |
62 ({'gimmea': ['myarg'], 'bar': []}, '') | |
63 """ | |
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 | |
77 result = {} | |
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) | |
84 | |
85 def _test(): | |
86 import doctest | |
87 doctest.testmod() | |
88 | |
89 if __name__ == '__main__': | |
90 _test() |