@@ -1928,11 +1928,11 @@ def _parse_known_args(self, arg_strings, namespace):
1928
1928
# otherwise, add the arg to the arg strings
1929
1929
# and note the index if it was an option
1930
1930
else :
1931
- option_tuple = self ._parse_optional (arg_string )
1932
- if option_tuple is None :
1931
+ option_tuples = self ._parse_optional (arg_string )
1932
+ if option_tuples is None :
1933
1933
pattern = 'A'
1934
1934
else :
1935
- option_string_indices [i ] = option_tuple
1935
+ option_string_indices [i ] = option_tuples
1936
1936
pattern = 'O'
1937
1937
arg_string_pattern_parts .append (pattern )
1938
1938
@@ -1967,8 +1967,16 @@ def take_action(action, argument_strings, option_string=None):
1967
1967
def consume_optional (start_index ):
1968
1968
1969
1969
# get the optional identified at this index
1970
- option_tuple = option_string_indices [start_index ]
1971
- action , option_string , sep , explicit_arg = option_tuple
1970
+ option_tuples = option_string_indices [start_index ]
1971
+ # if multiple actions match, the option string was ambiguous
1972
+ if len (option_tuples ) > 1 :
1973
+ options = ', ' .join ([option_string
1974
+ for action , option_string , sep , explicit_arg in option_tuples ])
1975
+ args = {'option' : arg_string , 'matches' : options }
1976
+ msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
1977
+ raise ArgumentError (None , msg % args )
1978
+
1979
+ action , option_string , sep , explicit_arg = option_tuples [0 ]
1972
1980
1973
1981
# identify additional optionals in the same arg string
1974
1982
# (e.g. -xyz is the same as -x -y -z if no args are required)
@@ -2254,7 +2262,7 @@ def _parse_optional(self, arg_string):
2254
2262
# if the option string is present in the parser, return the action
2255
2263
if arg_string in self ._option_string_actions :
2256
2264
action = self ._option_string_actions [arg_string ]
2257
- return action , arg_string , None , None
2265
+ return [( action , arg_string , None , None )]
2258
2266
2259
2267
# if it's just a single character, it was meant to be positional
2260
2268
if len (arg_string ) == 1 :
@@ -2264,25 +2272,14 @@ def _parse_optional(self, arg_string):
2264
2272
option_string , sep , explicit_arg = arg_string .partition ('=' )
2265
2273
if sep and option_string in self ._option_string_actions :
2266
2274
action = self ._option_string_actions [option_string ]
2267
- return action , option_string , sep , explicit_arg
2275
+ return [( action , option_string , sep , explicit_arg )]
2268
2276
2269
2277
# search through all possible prefixes of the option string
2270
2278
# and all actions in the parser for possible interpretations
2271
2279
option_tuples = self ._get_option_tuples (arg_string )
2272
2280
2273
- # if multiple actions match, the option string was ambiguous
2274
- if len (option_tuples ) > 1 :
2275
- options = ', ' .join ([option_string
2276
- for action , option_string , sep , explicit_arg in option_tuples ])
2277
- args = {'option' : arg_string , 'matches' : options }
2278
- msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2279
- raise ArgumentError (None , msg % args )
2280
-
2281
- # if exactly one action matched, this segmentation is good,
2282
- # so return the parsed action
2283
- elif len (option_tuples ) == 1 :
2284
- option_tuple , = option_tuples
2285
- return option_tuple
2281
+ if option_tuples :
2282
+ return option_tuples
2286
2283
2287
2284
# if it was not found as an option, but it looks like a negative
2288
2285
# number, it was meant to be positional
@@ -2297,7 +2294,7 @@ def _parse_optional(self, arg_string):
2297
2294
2298
2295
# it was meant to be an optional but there is no such option
2299
2296
# in this parser (though it might be a valid option in a subparser)
2300
- return None , arg_string , None , None
2297
+ return [( None , arg_string , None , None )]
2301
2298
2302
2299
def _get_option_tuples (self , option_string ):
2303
2300
result = []
@@ -2347,43 +2344,40 @@ def _get_nargs_pattern(self, action):
2347
2344
# in all examples below, we have to allow for '--' args
2348
2345
# which are represented as '-' in the pattern
2349
2346
nargs = action .nargs
2347
+ # if this is an optional action, -- is not allowed
2348
+ option = action .option_strings
2350
2349
2351
2350
# the default (None) is assumed to be a single argument
2352
2351
if nargs is None :
2353
- nargs_pattern = '(-*A-*)'
2352
+ nargs_pattern = '([A])' if option else '( -*A-*)'
2354
2353
2355
2354
# allow zero or one arguments
2356
2355
elif nargs == OPTIONAL :
2357
- nargs_pattern = '(-*A?-*)'
2356
+ nargs_pattern = '(A?)' if option else '( -*A?-*)'
2358
2357
2359
2358
# allow zero or more arguments
2360
2359
elif nargs == ZERO_OR_MORE :
2361
- nargs_pattern = '(-*[A-]*)'
2360
+ nargs_pattern = '(A*)' if option else '( -*[A-]*)'
2362
2361
2363
2362
# allow one or more arguments
2364
2363
elif nargs == ONE_OR_MORE :
2365
- nargs_pattern = '(-*A[A-]*)'
2364
+ nargs_pattern = '(A+)' if option else '( -*A[A-]*)'
2366
2365
2367
2366
# allow any number of options or arguments
2368
2367
elif nargs == REMAINDER :
2369
- nargs_pattern = '([- AO]*)'
2368
+ nargs_pattern = '([AO]*)' if option else '(. *)'
2370
2369
2371
2370
# allow one argument followed by any number of options or arguments
2372
2371
elif nargs == PARSER :
2373
- nargs_pattern = '(-*A[-AO]*)'
2372
+ nargs_pattern = '(A[AO]*)' if option else '( -*A[-AO]*)'
2374
2373
2375
2374
# suppress action, like nargs=0
2376
2375
elif nargs == SUPPRESS :
2377
- nargs_pattern = '(-* -*)'
2376
+ nargs_pattern = '()' if option else '( -*)'
2378
2377
2379
2378
# all others should be integers
2380
2379
else :
2381
- nargs_pattern = '(-*%s-*)' % '-*' .join ('A' * nargs )
2382
-
2383
- # if this is an optional action, -- is not allowed
2384
- if action .option_strings :
2385
- nargs_pattern = nargs_pattern .replace ('-*' , '' )
2386
- nargs_pattern = nargs_pattern .replace ('-' , '' )
2380
+ nargs_pattern = '([AO]{%d})' % nargs if option else '((?:-*A){%d}-*)' % nargs
2387
2381
2388
2382
# return the pattern
2389
2383
return nargs_pattern
0 commit comments