@@ -1843,7 +1843,7 @@ def _get_kwargs(self):
18431843 # ==================================
18441844 def add_subparsers (self , ** kwargs ):
18451845 if self ._subparsers is not None :
1846- self . error ( _ ('cannot have multiple subparser arguments' ))
1846+ raise ArgumentError ( None , _ ('cannot have multiple subparser arguments' ))
18471847
18481848 # add the parser class to the arguments if it's not present
18491849 kwargs .setdefault ('parser_class' , type (self ))
@@ -1895,8 +1895,11 @@ def _get_positional_actions(self):
18951895 def parse_args (self , args = None , namespace = None ):
18961896 args , argv = self .parse_known_args (args , namespace )
18971897 if argv :
1898- msg = _ ('unrecognized arguments: %s' )
1899- self .error (msg % ' ' .join (argv ))
1898+ msg = _ ('unrecognized arguments: %s' ) % ' ' .join (argv )
1899+ if self .exit_on_error :
1900+ self .error (msg )
1901+ else :
1902+ raise ArgumentError (None , msg )
19001903 return args
19011904
19021905 def parse_known_args (self , args = None , namespace = None ):
@@ -2175,7 +2178,7 @@ def consume_positionals(start_index):
21752178 self ._get_value (action , action .default ))
21762179
21772180 if required_actions :
2178- self . error ( _ ('the following arguments are required: %s' ) %
2181+ raise ArgumentError ( None , _ ('the following arguments are required: %s' ) %
21792182 ', ' .join (required_actions ))
21802183
21812184 # make sure all required groups had one option present
@@ -2191,7 +2194,7 @@ def consume_positionals(start_index):
21912194 for action in group ._group_actions
21922195 if action .help is not SUPPRESS ]
21932196 msg = _ ('one of the arguments %s is required' )
2194- self . error ( msg % ' ' .join (names ))
2197+ raise ArgumentError ( None , msg % ' ' .join (names ))
21952198
21962199 # return the updated namespace and the extra arguments
21972200 return namespace , extras
@@ -2218,7 +2221,7 @@ def _read_args_from_files(self, arg_strings):
22182221 arg_strings = self ._read_args_from_files (arg_strings )
22192222 new_arg_strings .extend (arg_strings )
22202223 except OSError as err :
2221- self . error ( str (err ))
2224+ raise ArgumentError ( None , str (err ))
22222225
22232226 # return the modified argument list
22242227 return new_arg_strings
@@ -2298,7 +2301,7 @@ def _parse_optional(self, arg_string):
22982301 for action , option_string , sep , explicit_arg in option_tuples ])
22992302 args = {'option' : arg_string , 'matches' : options }
23002303 msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2301- self . error ( msg % args )
2304+ raise ArgumentError ( None , msg % args )
23022305
23032306 # if exactly one action matched, this segmentation is good,
23042307 # so return the parsed action
@@ -2358,7 +2361,7 @@ def _get_option_tuples(self, option_string):
23582361
23592362 # shouldn't ever get here
23602363 else :
2361- self . error ( _ ('unexpected option string: %s' ) % option_string )
2364+ raise ArgumentError ( None , _ ('unexpected option string: %s' ) % option_string )
23622365
23632366 # return the collected option tuples
23642367 return result
@@ -2415,8 +2418,11 @@ def _get_nargs_pattern(self, action):
24152418 def parse_intermixed_args (self , args = None , namespace = None ):
24162419 args , argv = self .parse_known_intermixed_args (args , namespace )
24172420 if argv :
2418- msg = _ ('unrecognized arguments: %s' )
2419- self .error (msg % ' ' .join (argv ))
2421+ msg = _ ('unrecognized arguments: %s' ) % ' ' .join (argv )
2422+ if self .exit_on_error :
2423+ self .error (msg )
2424+ else :
2425+ raise ArgumentError (None , msg )
24202426 return args
24212427
24222428 def parse_known_intermixed_args (self , args = None , namespace = None ):
0 commit comments