##// END OF EJS Templates
fancyopts: add copyright and license header
Martin Geisler -
r8230:ec98f35e default
parent child Browse files
Show More
@@ -1,103 +1,110
1 # fancyopts.py - better command line parsing
2 #
3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
7
1 8 import getopt
2 9
3 10 def gnugetopt(args, options, longoptions):
4 11 """Parse options mostly like getopt.gnu_getopt.
5 12
6 13 This is different from getopt.gnu_getopt in that an argument of - will
7 14 become an argument of - instead of vanishing completely.
8 15 """
9 16 extraargs = []
10 17 if '--' in args:
11 18 stopindex = args.index('--')
12 19 extraargs = args[stopindex+1:]
13 20 args = args[:stopindex]
14 21 opts, parseargs = getopt.getopt(args, options, longoptions)
15 22 args = []
16 23 while parseargs:
17 24 arg = parseargs.pop(0)
18 25 if arg and arg[0] == '-' and len(arg) > 1:
19 26 parseargs.insert(0, arg)
20 27 topts, newparseargs = getopt.getopt(parseargs, options, longoptions)
21 28 opts = opts + topts
22 29 parseargs = newparseargs
23 30 else:
24 31 args.append(arg)
25 32 args.extend(extraargs)
26 33 return opts, args
27 34
28 35
29 36 def fancyopts(args, options, state, gnu=False):
30 37 """
31 38 read args, parse options, and store options in state
32 39
33 40 each option is a tuple of:
34 41
35 42 short option or ''
36 43 long option
37 44 default value
38 45 description
39 46
40 47 option types include:
41 48
42 49 boolean or none - option sets variable in state to true
43 50 string - parameter string is stored in state
44 51 list - parameter string is added to a list
45 52 integer - parameter strings is stored as int
46 53 function - call function with parameter
47 54
48 55 non-option args are returned
49 56 """
50 57 namelist = []
51 58 shortlist = ''
52 59 argmap = {}
53 60 defmap = {}
54 61
55 62 for short, name, default, comment in options:
56 63 # convert opts to getopt format
57 64 oname = name
58 65 name = name.replace('-', '_')
59 66
60 67 argmap['-' + short] = argmap['--' + oname] = name
61 68 defmap[name] = default
62 69
63 70 # copy defaults to state
64 71 if isinstance(default, list):
65 72 state[name] = default[:]
66 73 elif callable(default):
67 74 state[name] = None
68 75 else:
69 76 state[name] = default
70 77
71 78 # does it take a parameter?
72 79 if not (default is None or default is True or default is False):
73 80 if short: short += ':'
74 81 if oname: oname += '='
75 82 if short:
76 83 shortlist += short
77 84 if name:
78 85 namelist.append(oname)
79 86
80 87 # parse arguments
81 88 if gnu:
82 89 parse = gnugetopt
83 90 else:
84 91 parse = getopt.getopt
85 92 opts, args = parse(args, shortlist, namelist)
86 93
87 94 # transfer result to state
88 95 for opt, val in opts:
89 96 name = argmap[opt]
90 97 t = type(defmap[name])
91 98 if t is type(fancyopts):
92 99 state[name] = defmap[name](val)
93 100 elif t is type(1):
94 101 state[name] = int(val)
95 102 elif t is type(''):
96 103 state[name] = val
97 104 elif t is type([]):
98 105 state[name].append(val)
99 106 elif t is type(None) or t is type(False):
100 107 state[name] = True
101 108
102 109 # return unparsed args
103 110 return args
General Comments 0
You need to be logged in to leave comments. Login now