author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
Mon, 31 Mar 2008 18:49:15 +0200 | |
changeset 6420 | 626a8e396846 |
parent 5878 | d39af2eabb8c |
child 7772 | 88887054d277 |
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 |
state[name] = None |
5093
88803a69b24a
fancyopts: Copy list arguments in command table before modifying.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3749
diff
changeset
|
42 |
else: |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
43 |
state[name] = default |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
44 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
45 |
# does it take a parameter? |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
46 |
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
|
47 |
if short: short += ':' |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
48 |
if oname: oname += '=' |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
49 |
if short: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
50 |
shortlist += short |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
51 |
if name: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
52 |
namelist.append(oname) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
53 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
54 |
# parse arguments |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
55 |
opts, args = getopt.getopt(args, shortlist, namelist) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
56 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
57 |
# transfer result to state |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
58 |
for opt, val in opts: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
59 |
name = argmap[opt] |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
60 |
t = type(defmap[name]) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
61 |
if t is type(fancyopts): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
62 |
state[name] = defmap[name](val) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
63 |
elif t is type(1): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
64 |
state[name] = int(val) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
65 |
elif t is type(''): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
66 |
state[name] = val |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
67 |
elif t is type([]): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
68 |
state[name].append(val) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
69 |
elif t is type(None) or t is type(False): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
70 |
state[name] = True |
209 | 71 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
72 |
# return unparsed args |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
73 |
return args |