@@ -1982,7 +1982,7 @@ def consume_optional(start_index):
1982
1982
1983
1983
# get the optional identified at this index
1984
1984
option_tuple = option_string_indices [start_index ]
1985
- action , option_string , explicit_arg = option_tuple
1985
+ action , option_string , sep , explicit_arg = option_tuple
1986
1986
1987
1987
# identify additional optionals in the same arg string
1988
1988
# (e.g. -xyz is the same as -x -y -z if no args are required)
@@ -2009,18 +2009,27 @@ def consume_optional(start_index):
2009
2009
and option_string [1 ] not in chars
2010
2010
and explicit_arg != ''
2011
2011
):
2012
+ if sep or explicit_arg [0 ] in chars :
2013
+ msg = _ ('ignored explicit argument %r' )
2014
+ raise ArgumentError (action , msg % explicit_arg )
2012
2015
action_tuples .append ((action , [], option_string ))
2013
2016
char = option_string [0 ]
2014
2017
option_string = char + explicit_arg [0 ]
2015
- new_explicit_arg = explicit_arg [1 :] or None
2016
2018
optionals_map = self ._option_string_actions
2017
2019
if option_string in optionals_map :
2018
2020
action = optionals_map [option_string ]
2019
- explicit_arg = new_explicit_arg
2021
+ explicit_arg = explicit_arg [1 :]
2022
+ if not explicit_arg :
2023
+ sep = explicit_arg = None
2024
+ elif explicit_arg [0 ] == '=' :
2025
+ sep = '='
2026
+ explicit_arg = explicit_arg [1 :]
2027
+ else :
2028
+ sep = ''
2020
2029
else :
2021
- msg = _ ( 'ignored explicit argument %r' )
2022
- raise ArgumentError ( action , msg % explicit_arg )
2023
-
2030
+ extras . append ( char + explicit_arg )
2031
+ stop = start_index + 1
2032
+ break
2024
2033
# if the action expect exactly one argument, we've
2025
2034
# successfully matched the option; exit the loop
2026
2035
elif arg_count == 1 :
@@ -2238,18 +2247,17 @@ def _parse_optional(self, arg_string):
2238
2247
# if the option string is present in the parser, return the action
2239
2248
if arg_string in self ._option_string_actions :
2240
2249
action = self ._option_string_actions [arg_string ]
2241
- return action , arg_string , None
2250
+ return action , arg_string , None , None
2242
2251
2243
2252
# if it's just a single character, it was meant to be positional
2244
2253
if len (arg_string ) == 1 :
2245
2254
return None
2246
2255
2247
2256
# if the option string before the "=" is present, return the action
2248
- if '=' in arg_string :
2249
- option_string , explicit_arg = arg_string .split ('=' , 1 )
2250
- if option_string in self ._option_string_actions :
2251
- action = self ._option_string_actions [option_string ]
2252
- return action , option_string , explicit_arg
2257
+ option_string , sep , explicit_arg = arg_string .partition ('=' )
2258
+ if sep and option_string in self ._option_string_actions :
2259
+ action = self ._option_string_actions [option_string ]
2260
+ return action , option_string , sep , explicit_arg
2253
2261
2254
2262
# search through all possible prefixes of the option string
2255
2263
# and all actions in the parser for possible interpretations
@@ -2258,7 +2266,7 @@ def _parse_optional(self, arg_string):
2258
2266
# if multiple actions match, the option string was ambiguous
2259
2267
if len (option_tuples ) > 1 :
2260
2268
options = ', ' .join ([option_string
2261
- for action , option_string , explicit_arg in option_tuples ])
2269
+ for action , option_string , sep , explicit_arg in option_tuples ])
2262
2270
args = {'option' : arg_string , 'matches' : options }
2263
2271
msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2264
2272
self .error (msg % args )
@@ -2282,7 +2290,7 @@ def _parse_optional(self, arg_string):
2282
2290
2283
2291
# it was meant to be an optional but there is no such option
2284
2292
# in this parser (though it might be a valid option in a subparser)
2285
- return None , arg_string , None
2293
+ return None , arg_string , None , None
2286
2294
2287
2295
def _get_option_tuples (self , option_string ):
2288
2296
result = []
@@ -2292,34 +2300,31 @@ def _get_option_tuples(self, option_string):
2292
2300
chars = self .prefix_chars
2293
2301
if option_string [0 ] in chars and option_string [1 ] in chars :
2294
2302
if self .allow_abbrev :
2295
- if '=' in option_string :
2296
- option_prefix , explicit_arg = option_string .split ('=' , 1 )
2297
- else :
2298
- option_prefix = option_string
2299
- explicit_arg = None
2303
+ option_prefix , sep , explicit_arg = option_string .partition ('=' )
2304
+ if not sep :
2305
+ sep = explicit_arg = None
2300
2306
for option_string in self ._option_string_actions :
2301
2307
if option_string .startswith (option_prefix ):
2302
2308
action = self ._option_string_actions [option_string ]
2303
- tup = action , option_string , explicit_arg
2309
+ tup = action , option_string , sep , explicit_arg
2304
2310
result .append (tup )
2305
2311
2306
2312
# single character options can be concatenated with their arguments
2307
2313
# but multiple character options always have to have their argument
2308
2314
# separate
2309
2315
elif option_string [0 ] in chars and option_string [1 ] not in chars :
2310
2316
option_prefix = option_string
2311
- explicit_arg = None
2312
2317
short_option_prefix = option_string [:2 ]
2313
2318
short_explicit_arg = option_string [2 :]
2314
2319
2315
2320
for option_string in self ._option_string_actions :
2316
2321
if option_string == short_option_prefix :
2317
2322
action = self ._option_string_actions [option_string ]
2318
- tup = action , option_string , short_explicit_arg
2323
+ tup = action , option_string , '' , short_explicit_arg
2319
2324
result .append (tup )
2320
2325
elif option_string .startswith (option_prefix ):
2321
2326
action = self ._option_string_actions [option_string ]
2322
- tup = action , option_string , explicit_arg
2327
+ tup = action , option_string , None , None
2323
2328
result .append (tup )
2324
2329
2325
2330
# shouldn't ever get here
0 commit comments