@@ -1956,11 +1956,11 @@ def _parse_known_args(self, arg_strings, namespace):
1956
1956
# otherwise, add the arg to the arg strings
1957
1957
# and note the index if it was an option
1958
1958
else :
1959
- option_tuple = self ._parse_optional (arg_string )
1960
- if option_tuple is None :
1959
+ option_tuples = self ._parse_optional (arg_string )
1960
+ if option_tuples is None :
1961
1961
pattern = 'A'
1962
1962
else :
1963
- option_string_indices [i ] = option_tuple
1963
+ option_string_indices [i ] = option_tuples
1964
1964
pattern = 'O'
1965
1965
arg_string_pattern_parts .append (pattern )
1966
1966
@@ -1995,8 +1995,16 @@ def take_action(action, argument_strings, option_string=None):
1995
1995
def consume_optional (start_index ):
1996
1996
1997
1997
# get the optional identified at this index
1998
- option_tuple = option_string_indices [start_index ]
1999
- action , option_string , sep , explicit_arg = option_tuple
1998
+ option_tuples = option_string_indices [start_index ]
1999
+ # if multiple actions match, the option string was ambiguous
2000
+ if len (option_tuples ) > 1 :
2001
+ options = ', ' .join ([option_string
2002
+ for action , option_string , sep , explicit_arg in option_tuples ])
2003
+ args = {'option' : arg_string , 'matches' : options }
2004
+ msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2005
+ raise ArgumentError (None , msg % args )
2006
+
2007
+ action , option_string , sep , explicit_arg = option_tuples [0 ]
2000
2008
2001
2009
# identify additional optionals in the same arg string
2002
2010
# (e.g. -xyz is the same as -x -y -z if no args are required)
@@ -2282,7 +2290,7 @@ def _parse_optional(self, arg_string):
2282
2290
# if the option string is present in the parser, return the action
2283
2291
if arg_string in self ._option_string_actions :
2284
2292
action = self ._option_string_actions [arg_string ]
2285
- return action , arg_string , None , None
2293
+ return [( action , arg_string , None , None )]
2286
2294
2287
2295
# if it's just a single character, it was meant to be positional
2288
2296
if len (arg_string ) == 1 :
@@ -2292,25 +2300,14 @@ def _parse_optional(self, arg_string):
2292
2300
option_string , sep , explicit_arg = arg_string .partition ('=' )
2293
2301
if sep and option_string in self ._option_string_actions :
2294
2302
action = self ._option_string_actions [option_string ]
2295
- return action , option_string , sep , explicit_arg
2303
+ return [( action , option_string , sep , explicit_arg )]
2296
2304
2297
2305
# search through all possible prefixes of the option string
2298
2306
# and all actions in the parser for possible interpretations
2299
2307
option_tuples = self ._get_option_tuples (arg_string )
2300
2308
2301
- # if multiple actions match, the option string was ambiguous
2302
- if len (option_tuples ) > 1 :
2303
- options = ', ' .join ([option_string
2304
- for action , option_string , sep , explicit_arg in option_tuples ])
2305
- args = {'option' : arg_string , 'matches' : options }
2306
- msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2307
- raise ArgumentError (None , msg % args )
2308
-
2309
- # if exactly one action matched, this segmentation is good,
2310
- # so return the parsed action
2311
- elif len (option_tuples ) == 1 :
2312
- option_tuple , = option_tuples
2313
- return option_tuple
2309
+ if option_tuples :
2310
+ return option_tuples
2314
2311
2315
2312
# if it was not found as an option, but it looks like a negative
2316
2313
# number, it was meant to be positional
@@ -2325,7 +2322,7 @@ def _parse_optional(self, arg_string):
2325
2322
2326
2323
# it was meant to be an optional but there is no such option
2327
2324
# in this parser (though it might be a valid option in a subparser)
2328
- return None , arg_string , None , None
2325
+ return [( None , arg_string , None , None )]
2329
2326
2330
2327
def _get_option_tuples (self , option_string ):
2331
2328
result = []
@@ -2375,43 +2372,40 @@ def _get_nargs_pattern(self, action):
2375
2372
# in all examples below, we have to allow for '--' args
2376
2373
# which are represented as '-' in the pattern
2377
2374
nargs = action .nargs
2375
+ # if this is an optional action, -- is not allowed
2376
+ option = action .option_strings
2378
2377
2379
2378
# the default (None) is assumed to be a single argument
2380
2379
if nargs is None :
2381
- nargs_pattern = '(-*A-*)'
2380
+ nargs_pattern = '([A])' if option else '( -*A-*)'
2382
2381
2383
2382
# allow zero or one arguments
2384
2383
elif nargs == OPTIONAL :
2385
- nargs_pattern = '(-*A?-*)'
2384
+ nargs_pattern = '(A?)' if option else '( -*A?-*)'
2386
2385
2387
2386
# allow zero or more arguments
2388
2387
elif nargs == ZERO_OR_MORE :
2389
- nargs_pattern = '(-*[A-]*)'
2388
+ nargs_pattern = '(A*)' if option else '( -*[A-]*)'
2390
2389
2391
2390
# allow one or more arguments
2392
2391
elif nargs == ONE_OR_MORE :
2393
- nargs_pattern = '(-*A[A-]*)'
2392
+ nargs_pattern = '(A+)' if option else '( -*A[A-]*)'
2394
2393
2395
2394
# allow any number of options or arguments
2396
2395
elif nargs == REMAINDER :
2397
- nargs_pattern = '([- AO]*)'
2396
+ nargs_pattern = '([AO]*)' if option else '(. *)'
2398
2397
2399
2398
# allow one argument followed by any number of options or arguments
2400
2399
elif nargs == PARSER :
2401
- nargs_pattern = '(-*A[-AO]*)'
2400
+ nargs_pattern = '(A[AO]*)' if option else '( -*A[-AO]*)'
2402
2401
2403
2402
# suppress action, like nargs=0
2404
2403
elif nargs == SUPPRESS :
2405
- nargs_pattern = '(-* -*)'
2404
+ nargs_pattern = '()' if option else '( -*)'
2406
2405
2407
2406
# all others should be integers
2408
2407
else :
2409
- nargs_pattern = '(-*%s-*)' % '-*' .join ('A' * nargs )
2410
-
2411
- # if this is an optional action, -- is not allowed
2412
- if action .option_strings :
2413
- nargs_pattern = nargs_pattern .replace ('-*' , '' )
2414
- nargs_pattern = nargs_pattern .replace ('-' , '' )
2408
+ nargs_pattern = '([AO]{%d})' % nargs if option else '((?:-*A){%d}-*)' % nargs
2415
2409
2416
2410
# return the pattern
2417
2411
return nargs_pattern
0 commit comments