author | Anton Shestakov <av6@dwimlabs.net> |
Wed, 22 Nov 2017 22:18:06 +0800 | |
changeset 35162 | bdd2e18b54c5 |
parent 33103 | db8531c45953 |
child 35169 | 898c6f812a51 |
permissions | -rw-r--r-- |
8230
ec98f35e3e16
fancyopts: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7772
diff
changeset
|
1 |
# fancyopts.py - better command line parsing |
ec98f35e3e16
fancyopts: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7772
diff
changeset
|
2 |
# |
ec98f35e3e16
fancyopts: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7772
diff
changeset
|
3 |
# Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others |
ec98f35e3e16
fancyopts: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7772
diff
changeset
|
4 |
# |
ec98f35e3e16
fancyopts: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7772
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
10263 | 6 |
# GNU General Public License version 2 or any later version. |
8230
ec98f35e3e16
fancyopts: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7772
diff
changeset
|
7 |
|
25947
6002e2d95e54
fancyopts: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25563
diff
changeset
|
8 |
from __future__ import absolute_import |
6002e2d95e54
fancyopts: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25563
diff
changeset
|
9 |
|
6002e2d95e54
fancyopts: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25563
diff
changeset
|
10 |
from .i18n import _ |
30578
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29947
diff
changeset
|
11 |
from . import ( |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29947
diff
changeset
|
12 |
error, |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29947
diff
changeset
|
13 |
pycompat, |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29947
diff
changeset
|
14 |
) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
15 |
|
29947
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
16 |
# Set of flags to not apply boolean negation logic on |
32291
bd872f64a8ba
cleanup: use set literals
Martin von Zweigbergk <martinvonz@google.com>
parents:
30578
diff
changeset
|
17 |
nevernegate = { |
29947
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
18 |
# avoid --no-noninteractive |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
19 |
'noninteractive', |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
20 |
# These two flags are special because they cause hg to do one |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
21 |
# thing and then exit, and so aren't suitable for use in things |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
22 |
# like aliases anyway. |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
23 |
'help', |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
24 |
'version', |
32291
bd872f64a8ba
cleanup: use set literals
Martin von Zweigbergk <martinvonz@google.com>
parents:
30578
diff
changeset
|
25 |
} |
29947
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
26 |
|
7772
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
27 |
def gnugetopt(args, options, longoptions): |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
28 |
"""Parse options mostly like getopt.gnu_getopt. |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
29 |
|
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
30 |
This is different from getopt.gnu_getopt in that an argument of - will |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
31 |
become an argument of - instead of vanishing completely. |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
32 |
""" |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
33 |
extraargs = [] |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
34 |
if '--' in args: |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
35 |
stopindex = args.index('--') |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
36 |
extraargs = args[stopindex + 1:] |
7772
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
37 |
args = args[:stopindex] |
30578
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29947
diff
changeset
|
38 |
opts, parseargs = pycompat.getoptb(args, options, longoptions) |
7772
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
39 |
args = [] |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
40 |
while parseargs: |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
41 |
arg = parseargs.pop(0) |
33103
db8531c45953
py3: slice over bytes to prevent getting it's ascii value
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32291
diff
changeset
|
42 |
if arg and arg[0:1] == '-' and len(arg) > 1: |
7772
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
43 |
parseargs.insert(0, arg) |
30578
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29947
diff
changeset
|
44 |
topts, newparseargs = pycompat.getoptb(parseargs,\ |
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29947
diff
changeset
|
45 |
options, longoptions) |
7772
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
46 |
opts = opts + topts |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
47 |
parseargs = newparseargs |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
48 |
else: |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
49 |
args.append(arg) |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
50 |
args.extend(extraargs) |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
51 |
return opts, args |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
52 |
|
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
53 |
|
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
54 |
def fancyopts(args, options, state, gnu=False): |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
55 |
""" |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
56 |
read args, parse options, and store options in state |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
57 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
58 |
each option is a tuple of: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
59 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
60 |
short option or '' |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
61 |
long option |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
62 |
default value |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
63 |
description |
11321
40c06bbf58be
help: show value requirement and multiple occurrence of options
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
10282
diff
changeset
|
64 |
option value label(optional) |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
65 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
66 |
option types include: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
67 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
68 |
boolean or none - option sets variable in state to true |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
69 |
string - parameter string is stored in state |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
70 |
list - parameter string is added to a list |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
71 |
integer - parameter strings is stored as int |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
72 |
function - call function with parameter |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
73 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
74 |
non-option args are returned |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
75 |
""" |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
76 |
namelist = [] |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
77 |
shortlist = '' |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
78 |
argmap = {} |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
79 |
defmap = {} |
29947
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
80 |
negations = {} |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
81 |
alllong = set(o[1] for o in options) |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
82 |
|
11321
40c06bbf58be
help: show value requirement and multiple occurrence of options
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
10282
diff
changeset
|
83 |
for option in options: |
40c06bbf58be
help: show value requirement and multiple occurrence of options
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
10282
diff
changeset
|
84 |
if len(option) == 5: |
40c06bbf58be
help: show value requirement and multiple occurrence of options
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
10282
diff
changeset
|
85 |
short, name, default, comment, dummy = option |
40c06bbf58be
help: show value requirement and multiple occurrence of options
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
10282
diff
changeset
|
86 |
else: |
40c06bbf58be
help: show value requirement and multiple occurrence of options
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
10282
diff
changeset
|
87 |
short, name, default, comment = option |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
88 |
# convert opts to getopt format |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
89 |
oname = name |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
90 |
name = name.replace('-', '_') |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
91 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
92 |
argmap['-' + short] = argmap['--' + oname] = name |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
93 |
defmap[name] = default |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
94 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
95 |
# copy defaults to state |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
96 |
if isinstance(default, list): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
97 |
state[name] = default[:] |
21794
753af9ee7c81
fancyopts: restore use of callable() since it was readded in Python 3.2
Augie Fackler <raf@durin42.com>
parents:
20034
diff
changeset
|
98 |
elif callable(default): |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
99 |
state[name] = None |
5093
88803a69b24a
fancyopts: Copy list arguments in command table before modifying.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3749
diff
changeset
|
100 |
else: |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
101 |
state[name] = default |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
102 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
103 |
# does it take a parameter? |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
104 |
if not (default is None or default is True or default is False): |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
105 |
if short: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
106 |
short += ':' |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
107 |
if oname: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
108 |
oname += '=' |
29947
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
109 |
elif oname not in nevernegate: |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
110 |
if oname.startswith('no-'): |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
111 |
insert = oname[3:] |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
112 |
else: |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
113 |
insert = 'no-' + oname |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
114 |
# backout (as a practical example) has both --commit and |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
115 |
# --no-commit options, so we don't want to allow the |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
116 |
# negations of those flags. |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
117 |
if insert not in alllong: |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
118 |
assert ('--' + oname) not in negations |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
119 |
negations['--' + insert] = '--' + oname |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
120 |
namelist.append(insert) |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
121 |
if short: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
122 |
shortlist += short |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
123 |
if name: |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
124 |
namelist.append(oname) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
125 |
|
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
126 |
# parse arguments |
7772
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
127 |
if gnu: |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
128 |
parse = gnugetopt |
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
129 |
else: |
30578
c6ce11f2ee50
py3: make a bytes version of getopt.getopt()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29947
diff
changeset
|
130 |
parse = pycompat.getoptb |
7772
88887054d277
fancyopts: Parse options that occur after arguments.
Augie Fackler <durin42@gmail.com>
parents:
5878
diff
changeset
|
131 |
opts, args = parse(args, shortlist, namelist) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
132 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
133 |
# transfer result to state |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
134 |
for opt, val in opts: |
29947
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
135 |
boolval = True |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
136 |
negation = negations.get(opt, False) |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
137 |
if negation: |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
138 |
opt = negation |
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
139 |
boolval = False |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
140 |
name = argmap[opt] |
25563
69e8384a436c
fancyopts: allow all callable as default parameter value
introom <i@introo.me>
parents:
21794
diff
changeset
|
141 |
obj = defmap[name] |
69e8384a436c
fancyopts: allow all callable as default parameter value
introom <i@introo.me>
parents:
21794
diff
changeset
|
142 |
t = type(obj) |
69e8384a436c
fancyopts: allow all callable as default parameter value
introom <i@introo.me>
parents:
21794
diff
changeset
|
143 |
if callable(obj): |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
144 |
state[name] = defmap[name](val) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
145 |
elif t is type(1): |
17712
c4717f44c1f1
fancyopts: don't show a traceback on invalid integer values
Idan Kamara <idankk86@gmail.com>
parents:
14943
diff
changeset
|
146 |
try: |
c4717f44c1f1
fancyopts: don't show a traceback on invalid integer values
Idan Kamara <idankk86@gmail.com>
parents:
14943
diff
changeset
|
147 |
state[name] = int(val) |
c4717f44c1f1
fancyopts: don't show a traceback on invalid integer values
Idan Kamara <idankk86@gmail.com>
parents:
14943
diff
changeset
|
148 |
except ValueError: |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25947
diff
changeset
|
149 |
raise error.Abort(_('invalid value %r for option %s, ' |
17712
c4717f44c1f1
fancyopts: don't show a traceback on invalid integer values
Idan Kamara <idankk86@gmail.com>
parents:
14943
diff
changeset
|
150 |
'expected int') % (val, opt)) |
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
151 |
elif t is type(''): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
152 |
state[name] = val |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
153 |
elif t is type([]): |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
154 |
state[name].append(val) |
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
155 |
elif t is type(None) or t is type(False): |
29947
e1f0ec0b7d2d
flags: allow specifying --no-boolean-flag on the command line (BC)
Augie Fackler <augie@google.com>
parents:
26587
diff
changeset
|
156 |
state[name] = boolval |
209 | 157 |
|
5638
a9b7e425674f
fancyopts: lots of cleanups
Matt Mackall <mpm@selenic.com>
parents:
5093
diff
changeset
|
158 |
# return unparsed args |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
159 |
return args |