author | Christian Ebert <blacktrash@gmx.net> |
Wed, 09 Jan 2008 11:21:40 +0100 | |
changeset 5824 | b8e8bd3c82f6 |
parent 5638 | a9b7e425674f |
child 5878 | d39af2eabb8c |
permissions | -rw-r--r-- |
667
31a9aa890016
A number of minor fixes to problems that pychecker found.
mark.williamson@cl.cam.ac.uk
parents:
608
diff
changeset
|
1 |
import getopt |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
2 |
|
596 | 3 |
def fancyopts(args, options, state): |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
4 |
""" |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
5 |
read args, parse options, and store options in state |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
6 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
7 |
each option is a tuple of: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
8 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
9 |
short option or '' |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
10 |
long option |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
11 |
default value |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
12 |
description |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
13 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
14 |
option types include: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
15 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
16 |
boolean or none - option sets variable in state to true |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
17 |
string - parameter string is stored in state |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
18 |
list - parameter string is added to a list |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
19 |
integer - parameter strings is stored as int |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
20 |
function - call function with parameter |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
21 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
22 |
non-option args are returned |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
23 |
""" |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
24 |
namelist = [] |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
25 |
shortlist = '' |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
26 |
argmap = {} |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
27 |
defmap = {} |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
28 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
29 |
for short, name, default, comment in options: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
30 |
# convert opts to getopt format |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
31 |
oname = name |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
32 |
name = name.replace('-', '_') |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
33 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
34 |
argmap['-' + short] = argmap['--' + oname] = name |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
35 |
defmap[name] = default |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
36 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
37 |
# copy defaults to state |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
38 |
if isinstance(default, list): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
39 |
state[name] = default[:] |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
40 |
elif callable(default): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
41 |
print "whoa", name, default |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
42 |
state[name] = None |
5093
88803a69b24a
fancyopts: Copy list arguments in command table before modifying.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3749
diff
changeset
|
43 |
else: |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
44 |
state[name] = default |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
45 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
46 |
# does it take a parameter? |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
47 |
if not (default is None or default is True or default is False): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
48 |
if short: short += ':' |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
49 |
if oname: oname += '=' |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
50 |
if short: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
51 |
shortlist += short |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
52 |
if name: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
53 |
namelist.append(oname) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
54 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
55 |
# parse arguments |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
56 |
opts, args = getopt.getopt(args, shortlist, namelist) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
57 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
58 |
# transfer result to state |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
59 |
for opt, val in opts: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
60 |
name = argmap[opt] |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
61 |
t = type(defmap[name]) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
62 |
if t is type(fancyopts): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
63 |
state[name] = defmap[name](val) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
64 |
elif t is type(1): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
65 |
state[name] = int(val) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
66 |
elif t is type(''): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
67 |
state[name] = val |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
68 |
elif t is type([]): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
69 |
state[name].append(val) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
70 |
elif t is type(None) or t is type(False): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
71 |
state[name] = True |
209 | 72 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
73 |
# return unparsed args |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
74 |
return args |