@@ -1791,7 +1791,7 @@ def _get_kwargs(self):
1791
1791
# ==================================
1792
1792
def add_subparsers (self , ** kwargs ):
1793
1793
if self ._subparsers is not None :
1794
- self . error ( _ ('cannot have multiple subparser arguments' ))
1794
+ raise ArgumentError ( None , _ ('cannot have multiple subparser arguments' ))
1795
1795
1796
1796
# add the parser class to the arguments if it's not present
1797
1797
kwargs .setdefault ('parser_class' , type (self ))
@@ -1846,7 +1846,8 @@ def parse_args(self, args=None, namespace=None):
1846
1846
msg = _ ('unrecognized arguments: %s' ) % ' ' .join (argv )
1847
1847
if self .exit_on_error :
1848
1848
self .error (msg )
1849
- raise ArgumentError (None , msg )
1849
+ else :
1850
+ raise ArgumentError (None , msg )
1850
1851
return args
1851
1852
1852
1853
def parse_known_args (self , args = None , namespace = None ):
@@ -2135,7 +2136,7 @@ def consume_positionals(start_index):
2135
2136
self ._get_value (action , action .default ))
2136
2137
2137
2138
if required_actions :
2138
- self . error ( _ ('the following arguments are required: %s' ) %
2139
+ raise ArgumentError ( None , _ ('the following arguments are required: %s' ) %
2139
2140
', ' .join (required_actions ))
2140
2141
2141
2142
# make sure all required groups had one option present
@@ -2151,7 +2152,7 @@ def consume_positionals(start_index):
2151
2152
for action in group ._group_actions
2152
2153
if action .help is not SUPPRESS ]
2153
2154
msg = _ ('one of the arguments %s is required' )
2154
- self . error ( msg % ' ' .join (names ))
2155
+ raise ArgumentError ( None , msg % ' ' .join (names ))
2155
2156
2156
2157
# return the updated namespace and the extra arguments
2157
2158
return namespace , extras
@@ -2178,7 +2179,7 @@ def _read_args_from_files(self, arg_strings):
2178
2179
arg_strings = self ._read_args_from_files (arg_strings )
2179
2180
new_arg_strings .extend (arg_strings )
2180
2181
except OSError as err :
2181
- self . error ( str (err ))
2182
+ raise ArgumentError ( None , str (err ))
2182
2183
2183
2184
# return the modified argument list
2184
2185
return new_arg_strings
@@ -2258,7 +2259,7 @@ def _parse_optional(self, arg_string):
2258
2259
for action , option_string , sep , explicit_arg in option_tuples ])
2259
2260
args = {'option' : arg_string , 'matches' : options }
2260
2261
msg = _ ('ambiguous option: %(option)s could match %(matches)s' )
2261
- self . error ( msg % args )
2262
+ raise ArgumentError ( None , msg % args )
2262
2263
2263
2264
# if exactly one action matched, this segmentation is good,
2264
2265
# so return the parsed action
@@ -2318,7 +2319,7 @@ def _get_option_tuples(self, option_string):
2318
2319
2319
2320
# shouldn't ever get here
2320
2321
else :
2321
- self . error ( _ ('unexpected option string: %s' ) % option_string )
2322
+ raise ArgumentError ( None , _ ('unexpected option string: %s' ) % option_string )
2322
2323
2323
2324
# return the collected option tuples
2324
2325
return result
@@ -2375,8 +2376,11 @@ def _get_nargs_pattern(self, action):
2375
2376
def parse_intermixed_args (self , args = None , namespace = None ):
2376
2377
args , argv = self .parse_known_intermixed_args (args , namespace )
2377
2378
if argv :
2378
- msg = _ ('unrecognized arguments: %s' )
2379
- self .error (msg % ' ' .join (argv ))
2379
+ msg = _ ('unrecognized arguments: %s' ) % ' ' .join (argv )
2380
+ if self .exit_on_error :
2381
+ self .error (msg )
2382
+ else :
2383
+ raise ArgumentError (None , msg )
2380
2384
return args
2381
2385
2382
2386
def parse_known_intermixed_args (self , args = None , namespace = None ):
0 commit comments