@@ -2033,7 +2033,7 @@ def consume_optional(start_index):
2033
2033
2034
2034
# get the optional identified at this index
2035
2035
option_tuple = option_string_indices [start_index ]
2036
- action , option_string , explicit_arg = option_tuple
2036
+ action , option_string , sep , explicit_arg = option_tuple
2037
2037
2038
2038
# identify additional optionals in the same arg string
2039
2039
# (e.g. -xyz is the same as -x -y -z if no args are required)
@@ -2060,18 +2060,27 @@ def consume_optional(start_index):
2060
2060
and option_string [1 ] not in chars
2061
2061
and explicit_arg != ''
2062
2062
):
2063
+ if sep or explicit_arg [0 ] in chars :
2064
+ msg = _ ('ignored explicit argument %r' )
2065
+ raise ArgumentError (action , msg % explicit_arg )
2063
2066
action_tuples .append ((action , [], option_string ))
2064
2067
char = option_string [0 ]
2065
2068
option_string = char + explicit_arg [0 ]
2066
- new_explicit_arg = explicit_arg [1 :] or None
2067
2069
optionals_map = self ._option_string_actions
2068
2070
if option_string in optionals_map :
2069
2071
action = optionals_map [option_string ]
2070
- explicit_arg = new_explicit_arg
2072
+ explicit_arg = explicit_arg [1 :]
2073
+ if not explicit_arg :
2074
+ sep = explicit_arg = None
2075
+ elif explicit_arg [0 ] == '=' :
2076
+ sep = '='
2077
+ explicit_arg = explicit_arg [1 :]
2078
+ else :
2079
+ sep = ''
2071
2080
else :
2072
- msg = _ ( 'ignored explicit argument %r' )
2073
- raise ArgumentError ( action , msg % explicit_arg )
2074
-
2081
+ extras . append ( char + explicit_arg )
2082
+ stop = start_index + 1
2083
+ break
2075
2084
# if the action expect exactly one argument, we've
2076
2085
# successfully matched the option; exit the loop
2077
2086
elif arg_count == 1 :
@@ -2299,18 +2308,17 @@ def _parse_optional(self, arg_string):
2299
2308
# if the option string is present in the parser, return the action
2300
2309
if arg_string in self ._option_string_actions :
2301
2310
action = self ._option_string_actions [arg_string ]
2302
- return action , arg_string , None
2311
+ return action , arg_string , None , None
2303
2312
2304
2313
# if it's just a single character, it was meant to be positional
2305
2314
if len (arg_string ) == 1 :
2306
2315
return None
2307
2316
2308
2317
# if the option string before the "=" is present, return the action
2309
- if '=' in arg_string :
2310
- option_string , explicit_arg = arg_string .split ('=' , 1 )
2311
- if option_string in self ._option_string_actions :
2312
- action = self ._option_string_actions [option_string ]
2313
- return action , option_string , explicit_arg
2318
+ option_string , sep , explicit_arg = arg_string .partition ('=' )
2319
+ if sep and option_string in self ._option_string_actions :
2320
+ action = self ._option_string_actions [option_string ]
2321
+ return action , option_string , sep , explicit_arg
2314
2322
2315
2323
# search through all possible prefixes of the option string
2316
2324
# and all actions in the parser for possible interpretations
@@ -2319,7 +2327,7 @@ def _parse_optional(self, arg_string):
2319
2327
# if multiple actions match, the option string was ambiguous
2320
2328
if len (option_tuples ) > 1 :
2321
2329
options = ', ' .join ([option_string
2322
- for action , option_string , explicit_arg in option_tuples ])
2330
+ for action , option_string , sep , explicit_arg in option_tuples ])
2323
2331
args = {'option' : arg_string , 'matches' : options }
2324
2332
msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2325
2333
self .error (msg % args )
@@ -2343,7 +2351,7 @@ def _parse_optional(self, arg_string):
2343
2351
2344
2352
# it was meant to be an optional but there is no such option
2345
2353
# in this parser (though it might be a valid option in a subparser)
2346
- return None , arg_string , None
2354
+ return None , arg_string , None , None
2347
2355
2348
2356
def _get_option_tuples (self , option_string ):
2349
2357
result = []
@@ -2353,34 +2361,31 @@ def _get_option_tuples(self, option_string):
2353
2361
chars = self .prefix_chars
2354
2362
if option_string [0 ] in chars and option_string [1 ] in chars :
2355
2363
if self .allow_abbrev :
2356
- if '=' in option_string :
2357
- option_prefix , explicit_arg = option_string .split ('=' , 1 )
2358
- else :
2359
- option_prefix = option_string
2360
- explicit_arg = None
2364
+ option_prefix , sep , explicit_arg = option_string .partition ('=' )
2365
+ if not sep :
2366
+ sep = explicit_arg = None
2361
2367
for option_string in self ._option_string_actions :
2362
2368
if option_string .startswith (option_prefix ):
2363
2369
action = self ._option_string_actions [option_string ]
2364
- tup = action , option_string , explicit_arg
2370
+ tup = action , option_string , sep , explicit_arg
2365
2371
result .append (tup )
2366
2372
2367
2373
# single character options can be concatenated with their arguments
2368
2374
# but multiple character options always have to have their argument
2369
2375
# separate
2370
2376
elif option_string [0 ] in chars and option_string [1 ] not in chars :
2371
2377
option_prefix = option_string
2372
- explicit_arg = None
2373
2378
short_option_prefix = option_string [:2 ]
2374
2379
short_explicit_arg = option_string [2 :]
2375
2380
2376
2381
for option_string in self ._option_string_actions :
2377
2382
if option_string == short_option_prefix :
2378
2383
action = self ._option_string_actions [option_string ]
2379
- tup = action , option_string , short_explicit_arg
2384
+ tup = action , option_string , '' , short_explicit_arg
2380
2385
result .append (tup )
2381
2386
elif option_string .startswith (option_prefix ):
2382
2387
action = self ._option_string_actions [option_string ]
2383
- tup = action , option_string , explicit_arg
2388
+ tup = action , option_string , None , None
2384
2389
result .append (tup )
2385
2390
2386
2391
# shouldn't ever get here
0 commit comments