@@ -2004,7 +2004,7 @@ def consume_optional(start_index):
2004
2004
2005
2005
# get the optional identified at this index
2006
2006
option_tuple = option_string_indices [start_index ]
2007
- action , option_string , explicit_arg = option_tuple
2007
+ action , option_string , sep , explicit_arg = option_tuple
2008
2008
2009
2009
# identify additional optionals in the same arg string
2010
2010
# (e.g. -xyz is the same as -x -y -z if no args are required)
@@ -2031,18 +2031,27 @@ def consume_optional(start_index):
2031
2031
and option_string [1 ] not in chars
2032
2032
and explicit_arg != ''
2033
2033
):
2034
+ if sep or explicit_arg [0 ] in chars :
2035
+ msg = _ ('ignored explicit argument %r' )
2036
+ raise ArgumentError (action , msg % explicit_arg )
2034
2037
action_tuples .append ((action , [], option_string ))
2035
2038
char = option_string [0 ]
2036
2039
option_string = char + explicit_arg [0 ]
2037
- new_explicit_arg = explicit_arg [1 :] or None
2038
2040
optionals_map = self ._option_string_actions
2039
2041
if option_string in optionals_map :
2040
2042
action = optionals_map [option_string ]
2041
- explicit_arg = new_explicit_arg
2043
+ explicit_arg = explicit_arg [1 :]
2044
+ if not explicit_arg :
2045
+ sep = explicit_arg = None
2046
+ elif explicit_arg [0 ] == '=' :
2047
+ sep = '='
2048
+ explicit_arg = explicit_arg [1 :]
2049
+ else :
2050
+ sep = ''
2042
2051
else :
2043
- msg = _ ( 'ignored explicit argument %r' )
2044
- raise ArgumentError ( action , msg % explicit_arg )
2045
-
2052
+ extras . append ( char + explicit_arg )
2053
+ stop = start_index + 1
2054
+ break
2046
2055
# if the action expect exactly one argument, we've
2047
2056
# successfully matched the option; exit the loop
2048
2057
elif arg_count == 1 :
@@ -2262,18 +2271,17 @@ def _parse_optional(self, arg_string):
2262
2271
# if the option string is present in the parser, return the action
2263
2272
if arg_string in self ._option_string_actions :
2264
2273
action = self ._option_string_actions [arg_string ]
2265
- return action , arg_string , None
2274
+ return action , arg_string , None , None
2266
2275
2267
2276
# if it's just a single character, it was meant to be positional
2268
2277
if len (arg_string ) == 1 :
2269
2278
return None
2270
2279
2271
2280
# if the option string before the "=" is present, return the action
2272
- if '=' in arg_string :
2273
- option_string , explicit_arg = arg_string .split ('=' , 1 )
2274
- if option_string in self ._option_string_actions :
2275
- action = self ._option_string_actions [option_string ]
2276
- return action , option_string , explicit_arg
2281
+ option_string , sep , explicit_arg = arg_string .partition ('=' )
2282
+ if sep and option_string in self ._option_string_actions :
2283
+ action = self ._option_string_actions [option_string ]
2284
+ return action , option_string , sep , explicit_arg
2277
2285
2278
2286
# search through all possible prefixes of the option string
2279
2287
# and all actions in the parser for possible interpretations
@@ -2282,7 +2290,7 @@ def _parse_optional(self, arg_string):
2282
2290
# if multiple actions match, the option string was ambiguous
2283
2291
if len (option_tuples ) > 1 :
2284
2292
options = ', ' .join ([option_string
2285
- for action , option_string , explicit_arg in option_tuples ])
2293
+ for action , option_string , sep , explicit_arg in option_tuples ])
2286
2294
args = {'option' : arg_string , 'matches' : options }
2287
2295
msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2288
2296
self .error (msg % args )
@@ -2306,7 +2314,7 @@ def _parse_optional(self, arg_string):
2306
2314
2307
2315
# it was meant to be an optional but there is no such option
2308
2316
# in this parser (though it might be a valid option in a subparser)
2309
- return None , arg_string , None
2317
+ return None , arg_string , None , None
2310
2318
2311
2319
def _get_option_tuples (self , option_string ):
2312
2320
result = []
@@ -2316,34 +2324,31 @@ def _get_option_tuples(self, option_string):
2316
2324
chars = self .prefix_chars
2317
2325
if option_string [0 ] in chars and option_string [1 ] in chars :
2318
2326
if self .allow_abbrev :
2319
- if '=' in option_string :
2320
- option_prefix , explicit_arg = option_string .split ('=' , 1 )
2321
- else :
2322
- option_prefix = option_string
2323
- explicit_arg = None
2327
+ option_prefix , sep , explicit_arg = option_string .partition ('=' )
2328
+ if not sep :
2329
+ sep = explicit_arg = None
2324
2330
for option_string in self ._option_string_actions :
2325
2331
if option_string .startswith (option_prefix ):
2326
2332
action = self ._option_string_actions [option_string ]
2327
- tup = action , option_string , explicit_arg
2333
+ tup = action , option_string , sep , explicit_arg
2328
2334
result .append (tup )
2329
2335
2330
2336
# single character options can be concatenated with their arguments
2331
2337
# but multiple character options always have to have their argument
2332
2338
# separate
2333
2339
elif option_string [0 ] in chars and option_string [1 ] not in chars :
2334
2340
option_prefix = option_string
2335
- explicit_arg = None
2336
2341
short_option_prefix = option_string [:2 ]
2337
2342
short_explicit_arg = option_string [2 :]
2338
2343
2339
2344
for option_string in self ._option_string_actions :
2340
2345
if option_string == short_option_prefix :
2341
2346
action = self ._option_string_actions [option_string ]
2342
- tup = action , option_string , short_explicit_arg
2347
+ tup = action , option_string , '' , short_explicit_arg
2343
2348
result .append (tup )
2344
2349
elif option_string .startswith (option_prefix ):
2345
2350
action = self ._option_string_actions [option_string ]
2346
- tup = action , option_string , explicit_arg
2351
+ tup = action , option_string , None , None
2347
2352
result .append (tup )
2348
2353
2349
2354
# shouldn't ever get here
0 commit comments