@@ -1819,7 +1819,7 @@ def _get_kwargs(self):
1819
1819
# ==================================
1820
1820
def add_subparsers (self , ** kwargs ):
1821
1821
if self ._subparsers is not None :
1822
- self . error ( _ ('cannot have multiple subparser arguments' ))
1822
+ raise ArgumentError ( None , _ ('cannot have multiple subparser arguments' ))
1823
1823
1824
1824
# add the parser class to the arguments if it's not present
1825
1825
kwargs .setdefault ('parser_class' , type (self ))
@@ -1874,7 +1874,8 @@ def parse_args(self, args=None, namespace=None):
1874
1874
msg = _ ('unrecognized arguments: %s' ) % ' ' .join (argv )
1875
1875
if self .exit_on_error :
1876
1876
self .error (msg )
1877
- raise ArgumentError (None , msg )
1877
+ else :
1878
+ raise ArgumentError (None , msg )
1878
1879
return args
1879
1880
1880
1881
def parse_known_args (self , args = None , namespace = None ):
@@ -2163,7 +2164,7 @@ def consume_positionals(start_index):
2163
2164
self ._get_value (action , action .default ))
2164
2165
2165
2166
if required_actions :
2166
- self . error ( _ ('the following arguments are required: %s' ) %
2167
+ raise ArgumentError ( None , _ ('the following arguments are required: %s' ) %
2167
2168
', ' .join (required_actions ))
2168
2169
2169
2170
# make sure all required groups had one option present
@@ -2179,7 +2180,7 @@ def consume_positionals(start_index):
2179
2180
for action in group ._group_actions
2180
2181
if action .help is not SUPPRESS ]
2181
2182
msg = _ ('one of the arguments %s is required' )
2182
- self . error ( msg % ' ' .join (names ))
2183
+ raise ArgumentError ( None , msg % ' ' .join (names ))
2183
2184
2184
2185
# return the updated namespace and the extra arguments
2185
2186
return namespace , extras
@@ -2206,7 +2207,7 @@ def _read_args_from_files(self, arg_strings):
2206
2207
arg_strings = self ._read_args_from_files (arg_strings )
2207
2208
new_arg_strings .extend (arg_strings )
2208
2209
except OSError as err :
2209
- self . error ( str (err ))
2210
+ raise ArgumentError ( None , str (err ))
2210
2211
2211
2212
# return the modified argument list
2212
2213
return new_arg_strings
@@ -2286,7 +2287,7 @@ def _parse_optional(self, arg_string):
2286
2287
for action , option_string , sep , explicit_arg in option_tuples ])
2287
2288
args = {'option' : arg_string , 'matches' : options }
2288
2289
msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2289
- self . error ( msg % args )
2290
+ raise ArgumentError ( None , msg % args )
2290
2291
2291
2292
# if exactly one action matched, this segmentation is good,
2292
2293
# so return the parsed action
@@ -2346,7 +2347,7 @@ def _get_option_tuples(self, option_string):
2346
2347
2347
2348
# shouldn't ever get here
2348
2349
else :
2349
- self . error ( _ ('unexpected option string: %s' ) % option_string )
2350
+ raise ArgumentError ( None , _ ('unexpected option string: %s' ) % option_string )
2350
2351
2351
2352
# return the collected option tuples
2352
2353
return result
@@ -2403,8 +2404,11 @@ def _get_nargs_pattern(self, action):
2403
2404
def parse_intermixed_args (self , args = None , namespace = None ):
2404
2405
args , argv = self .parse_known_intermixed_args (args , namespace )
2405
2406
if argv :
2406
- msg = _ ('unrecognized arguments: %s' )
2407
- self .error (msg % ' ' .join (argv ))
2407
+ msg = _ ('unrecognized arguments: %s' ) % ' ' .join (argv )
2408
+ if self .exit_on_error :
2409
+ self .error (msg )
2410
+ else :
2411
+ raise ArgumentError (None , msg )
2408
2412
return args
2409
2413
2410
2414
def parse_known_intermixed_args (self , args = None , namespace = None ):
0 commit comments