@@ -1843,7 +1843,7 @@ def _get_kwargs(self):
1843
1843
# ==================================
1844
1844
def add_subparsers (self , ** kwargs ):
1845
1845
if self ._subparsers is not None :
1846
- self . error ( _ ('cannot have multiple subparser arguments' ))
1846
+ raise ArgumentError ( None , _ ('cannot have multiple subparser arguments' ))
1847
1847
1848
1848
# add the parser class to the arguments if it's not present
1849
1849
kwargs .setdefault ('parser_class' , type (self ))
@@ -1898,7 +1898,8 @@ def parse_args(self, args=None, namespace=None):
1898
1898
msg = _ ('unrecognized arguments: %s' ) % ' ' .join (argv )
1899
1899
if self .exit_on_error :
1900
1900
self .error (msg )
1901
- raise ArgumentError (None , msg )
1901
+ else :
1902
+ raise ArgumentError (None , msg )
1902
1903
return args
1903
1904
1904
1905
def parse_known_args (self , args = None , namespace = None ):
@@ -2177,7 +2178,7 @@ def consume_positionals(start_index):
2177
2178
self ._get_value (action , action .default ))
2178
2179
2179
2180
if required_actions :
2180
- self . error ( _ ('the following arguments are required: %s' ) %
2181
+ raise ArgumentError ( None , _ ('the following arguments are required: %s' ) %
2181
2182
', ' .join (required_actions ))
2182
2183
2183
2184
# make sure all required groups had one option present
@@ -2193,7 +2194,7 @@ def consume_positionals(start_index):
2193
2194
for action in group ._group_actions
2194
2195
if action .help is not SUPPRESS ]
2195
2196
msg = _ ('one of the arguments %s is required' )
2196
- self . error ( msg % ' ' .join (names ))
2197
+ raise ArgumentError ( None , msg % ' ' .join (names ))
2197
2198
2198
2199
# return the updated namespace and the extra arguments
2199
2200
return namespace , extras
@@ -2220,7 +2221,7 @@ def _read_args_from_files(self, arg_strings):
2220
2221
arg_strings = self ._read_args_from_files (arg_strings )
2221
2222
new_arg_strings .extend (arg_strings )
2222
2223
except OSError as err :
2223
- self . error ( str (err ))
2224
+ raise ArgumentError ( None , str (err ))
2224
2225
2225
2226
# return the modified argument list
2226
2227
return new_arg_strings
@@ -2300,7 +2301,7 @@ def _parse_optional(self, arg_string):
2300
2301
for action , option_string , sep , explicit_arg in option_tuples ])
2301
2302
args = {'option' : arg_string , 'matches' : options }
2302
2303
msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2303
- self . error ( msg % args )
2304
+ raise ArgumentError ( None , msg % args )
2304
2305
2305
2306
# if exactly one action matched, this segmentation is good,
2306
2307
# so return the parsed action
@@ -2360,7 +2361,7 @@ def _get_option_tuples(self, option_string):
2360
2361
2361
2362
# shouldn't ever get here
2362
2363
else :
2363
- self . error ( _ ('unexpected option string: %s' ) % option_string )
2364
+ raise ArgumentError ( None , _ ('unexpected option string: %s' ) % option_string )
2364
2365
2365
2366
# return the collected option tuples
2366
2367
return result
@@ -2417,8 +2418,11 @@ def _get_nargs_pattern(self, action):
2417
2418
def parse_intermixed_args (self , args = None , namespace = None ):
2418
2419
args , argv = self .parse_known_intermixed_args (args , namespace )
2419
2420
if argv :
2420
- msg = _ ('unrecognized arguments: %s' )
2421
- 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 )
2422
2426
return args
2423
2427
2424
2428
def parse_known_intermixed_args (self , args = None , namespace = None ):
0 commit comments