Mercurial > public > mercurial-scm > hg
comparison mercurial/fancyopts.py @ 35227:98a5aa5575e7
fancyopts: fix handling of "--" value in earlygetopt()
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 25 Nov 2017 17:30:50 +0900 |
parents | 5b569d512fbd |
children | cc9d0763c8e9 |
comparison
equal
deleted
inserted
replaced
35226:5b569d512fbd | 35227:98a5aa5575e7 |
---|---|
117 >>> get([b'--cwd', b'foo', b'-Rbar', b'x', b'-q', b'y'], gnu=False) | 117 >>> get([b'--cwd', b'foo', b'-Rbar', b'x', b'-q', b'y'], gnu=False) |
118 ([('--cwd', 'foo'), ('-R', 'bar')], ['x', '-q', 'y']) | 118 ([('--cwd', 'foo'), ('-R', 'bar')], ['x', '-q', 'y']) |
119 >>> get([b'--cwd=foo', b'x', b'y', b'-R', b'bar', b'--debugger'], gnu=False) | 119 >>> get([b'--cwd=foo', b'x', b'y', b'-R', b'bar', b'--debugger'], gnu=False) |
120 ([('--cwd', 'foo')], ['x', 'y', '-R', 'bar', '--debugger']) | 120 ([('--cwd', 'foo')], ['x', 'y', '-R', 'bar', '--debugger']) |
121 >>> get([b'--unknown', b'--cwd=foo', b'--', '--debugger'], gnu=False) | 121 >>> get([b'--unknown', b'--cwd=foo', b'--', '--debugger'], gnu=False) |
122 ([], ['--unknown', '--cwd=foo', '--debugger']) | 122 ([], ['--unknown', '--cwd=foo', '--', '--debugger']) |
123 | 123 |
124 stripping early options (without loosing '--'): | 124 stripping early options (without loosing '--'): |
125 | 125 |
126 >>> get([b'x', b'-Rbar', b'--', '--debugger'], gnu=True, keepsep=True)[1] | 126 >>> get([b'x', b'-Rbar', b'--', '--debugger'], gnu=True, keepsep=True)[1] |
127 ['x', '--', '--debugger'] | 127 ['x', '--', '--debugger'] |
139 >>> get([b'-q']) | 139 >>> get([b'-q']) |
140 ([('-q', '')], []) | 140 ([('-q', '')], []) |
141 >>> get([b'-q', b'--']) | 141 >>> get([b'-q', b'--']) |
142 ([('-q', '')], []) | 142 ([('-q', '')], []) |
143 | 143 |
144 '--' may be a value: | |
145 | |
146 >>> get([b'-R', b'--', b'x']) | |
147 ([('-R', '--')], ['x']) | |
148 >>> get([b'--cwd', b'--', b'x']) | |
149 ([('--cwd', '--')], ['x']) | |
150 | |
144 value passed to bool options: | 151 value passed to bool options: |
145 | 152 |
146 >>> get([b'--debugger=foo', b'x']) | 153 >>> get([b'--debugger=foo', b'x']) |
147 ([], ['--debugger=foo', 'x']) | 154 ([], ['--debugger=foo', 'x']) |
148 >>> get([b'-qfoo', b'x']) | 155 >>> get([b'-qfoo', b'x']) |
161 '-' is a valid non-option argument: | 168 '-' is a valid non-option argument: |
162 | 169 |
163 >>> get([b'-', b'y']) | 170 >>> get([b'-', b'y']) |
164 ([], ['-', 'y']) | 171 ([], ['-', 'y']) |
165 """ | 172 """ |
166 # ignoring everything just after '--' isn't correct as '--' may be an | |
167 # option value (e.g. ['-R', '--']), but we do that consistently. | |
168 try: | |
169 argcount = args.index('--') | |
170 except ValueError: | |
171 argcount = len(args) | |
172 | |
173 parsedopts = [] | 173 parsedopts = [] |
174 parsedargs = [] | 174 parsedargs = [] |
175 pos = 0 | 175 pos = 0 |
176 while pos < argcount: | 176 while pos < len(args): |
177 arg = args[pos] | 177 arg = args[pos] |
178 if arg == '--': | |
179 pos += not keepsep | |
180 break | |
178 flag, hasval, val, takeval = _earlyoptarg(arg, shortlist, namelist) | 181 flag, hasval, val, takeval = _earlyoptarg(arg, shortlist, namelist) |
179 if not hasval and takeval and pos + 1 >= argcount: | 182 if not hasval and takeval and pos + 1 >= len(args): |
180 # missing last argument | 183 # missing last argument |
181 break | 184 break |
182 if not flag or hasval and not takeval: | 185 if not flag or hasval and not takeval: |
183 # non-option argument or -b/--bool=INVALID_VALUE | 186 # non-option argument or -b/--bool=INVALID_VALUE |
184 if gnu: | 187 if gnu: |
193 else: | 196 else: |
194 # -s/--str VALUE | 197 # -s/--str VALUE |
195 parsedopts.append((flag, args[pos + 1])) | 198 parsedopts.append((flag, args[pos + 1])) |
196 pos += 2 | 199 pos += 2 |
197 | 200 |
198 parsedargs.extend(args[pos:argcount]) | 201 parsedargs.extend(args[pos:]) |
199 parsedargs.extend(args[argcount + (not keepsep):]) | |
200 return parsedopts, parsedargs | 202 return parsedopts, parsedargs |
201 | 203 |
202 def fancyopts(args, options, state, gnu=False, early=False, optaliases=None): | 204 def fancyopts(args, options, state, gnu=False, early=False, optaliases=None): |
203 """ | 205 """ |
204 read args, parse options, and store options in state | 206 read args, parse options, and store options in state |