@@ -1971,11 +1971,11 @@ def _parse_known_args(self, arg_strings, namespace):
1971
1971
# otherwise, add the arg to the arg strings
1972
1972
# and note the index if it was an option
1973
1973
else :
1974
- option_tuple = self ._parse_optional (arg_string )
1975
- if option_tuple is None :
1974
+ option_tuples = self ._parse_optional (arg_string )
1975
+ if option_tuples is None :
1976
1976
pattern = 'A'
1977
1977
else :
1978
- option_string_indices [i ] = option_tuple
1978
+ option_string_indices [i ] = option_tuples
1979
1979
pattern = 'O'
1980
1980
arg_string_pattern_parts .append (pattern )
1981
1981
@@ -2009,8 +2009,16 @@ def take_action(action, argument_strings, option_string=None):
2009
2009
def consume_optional (start_index ):
2010
2010
2011
2011
# get the optional identified at this index
2012
- option_tuple = option_string_indices [start_index ]
2013
- action , option_string , sep , explicit_arg = option_tuple
2012
+ option_tuples = option_string_indices [start_index ]
2013
+ # if multiple actions match, the option string was ambiguous
2014
+ if len (option_tuples ) > 1 :
2015
+ options = ', ' .join ([option_string
2016
+ for action , option_string , sep , explicit_arg in option_tuples ])
2017
+ args = {'option' : arg_string , 'matches' : options }
2018
+ msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2019
+ raise ArgumentError (None , msg % args )
2020
+
2021
+ action , option_string , sep , explicit_arg = option_tuples [0 ]
2014
2022
2015
2023
# identify additional optionals in the same arg string
2016
2024
# (e.g. -xyz is the same as -x -y -z if no args are required)
@@ -2287,7 +2295,7 @@ def _parse_optional(self, arg_string):
2287
2295
# if the option string is present in the parser, return the action
2288
2296
if arg_string in self ._option_string_actions :
2289
2297
action = self ._option_string_actions [arg_string ]
2290
- return action , arg_string , None , None
2298
+ return [( action , arg_string , None , None )]
2291
2299
2292
2300
# if it's just a single character, it was meant to be positional
2293
2301
if len (arg_string ) == 1 :
@@ -2297,25 +2305,14 @@ def _parse_optional(self, arg_string):
2297
2305
option_string , sep , explicit_arg = arg_string .partition ('=' )
2298
2306
if sep and option_string in self ._option_string_actions :
2299
2307
action = self ._option_string_actions [option_string ]
2300
- return action , option_string , sep , explicit_arg
2308
+ return [( action , option_string , sep , explicit_arg )]
2301
2309
2302
2310
# search through all possible prefixes of the option string
2303
2311
# and all actions in the parser for possible interpretations
2304
2312
option_tuples = self ._get_option_tuples (arg_string )
2305
2313
2306
- # if multiple actions match, the option string was ambiguous
2307
- if len (option_tuples ) > 1 :
2308
- options = ', ' .join ([option_string
2309
- for action , option_string , sep , explicit_arg in option_tuples ])
2310
- args = {'option' : arg_string , 'matches' : options }
2311
- msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2312
- raise ArgumentError (None , msg % args )
2313
-
2314
- # if exactly one action matched, this segmentation is good,
2315
- # so return the parsed action
2316
- elif len (option_tuples ) == 1 :
2317
- option_tuple , = option_tuples
2318
- return option_tuple
2314
+ if option_tuples :
2315
+ return option_tuples
2319
2316
2320
2317
# if it was not found as an option, but it looks like a negative
2321
2318
# number, it was meant to be positional
@@ -2330,7 +2327,7 @@ def _parse_optional(self, arg_string):
2330
2327
2331
2328
# it was meant to be an optional but there is no such option
2332
2329
# in this parser (though it might be a valid option in a subparser)
2333
- return None , arg_string , None , None
2330
+ return [( None , arg_string , None , None )]
2334
2331
2335
2332
def _get_option_tuples (self , option_string ):
2336
2333
result = []
@@ -2380,43 +2377,40 @@ def _get_nargs_pattern(self, action):
2380
2377
# in all examples below, we have to allow for '--' args
2381
2378
# which are represented as '-' in the pattern
2382
2379
nargs = action .nargs
2380
+ # if this is an optional action, -- is not allowed
2381
+ option = action .option_strings
2383
2382
2384
2383
# the default (None) is assumed to be a single argument
2385
2384
if nargs is None :
2386
- nargs_pattern = '(-*A-*)'
2385
+ nargs_pattern = '([A])' if option else '( -*A-*)'
2387
2386
2388
2387
# allow zero or one arguments
2389
2388
elif nargs == OPTIONAL :
2390
- nargs_pattern = '(-*A?-*)'
2389
+ nargs_pattern = '(A?)' if option else '( -*A?-*)'
2391
2390
2392
2391
# allow zero or more arguments
2393
2392
elif nargs == ZERO_OR_MORE :
2394
- nargs_pattern = '(-*[A-]*)'
2393
+ nargs_pattern = '(A*)' if option else '( -*[A-]*)'
2395
2394
2396
2395
# allow one or more arguments
2397
2396
elif nargs == ONE_OR_MORE :
2398
- nargs_pattern = '(-*A[A-]*)'
2397
+ nargs_pattern = '(A+)' if option else '( -*A[A-]*)'
2399
2398
2400
2399
# allow any number of options or arguments
2401
2400
elif nargs == REMAINDER :
2402
- nargs_pattern = '([- AO]*)'
2401
+ nargs_pattern = '([AO]*)' if option else '(. *)'
2403
2402
2404
2403
# allow one argument followed by any number of options or arguments
2405
2404
elif nargs == PARSER :
2406
- nargs_pattern = '(-*A[-AO]*)'
2405
+ nargs_pattern = '(A[AO]*)' if option else '( -*A[-AO]*)'
2407
2406
2408
2407
# suppress action, like nargs=0
2409
2408
elif nargs == SUPPRESS :
2410
- nargs_pattern = '(-* -*)'
2409
+ nargs_pattern = '()' if option else '( -*)'
2411
2410
2412
2411
# all others should be integers
2413
2412
else :
2414
- nargs_pattern = '(-*%s-*)' % '-*' .join ('A' * nargs )
2415
-
2416
- # if this is an optional action, -- is not allowed
2417
- if action .option_strings :
2418
- nargs_pattern = nargs_pattern .replace ('-*' , '' )
2419
- nargs_pattern = nargs_pattern .replace ('-' , '' )
2413
+ nargs_pattern = '([AO]{%d})' % nargs if option else '((?:-*A){%d}-*)' % nargs
2420
2414
2421
2415
# return the pattern
2422
2416
return nargs_pattern
0 commit comments