202202
203203TEMPDIR = os .path .abspath (tempfile .gettempdir ())
204204
205+ class _ArgParser (argparse .ArgumentParser ):
206+
207+ def error (self , message ):
208+ super ().error (message + "\n Pass -h or --help for complete help." )
209+
205210def _create_parser ():
206211 # Set prog to prevent the uninformative "__main__.py" from displaying in
207212 # error messages when using "python -m test ...".
208- parser = argparse .ArgumentParser (prog = 'regrtest.py' ,
209- usage = USAGE ,
210- description = DESCRIPTION ,
211- epilog = EPILOG ,
212- add_help = False ,
213- formatter_class =
214- argparse .RawDescriptionHelpFormatter )
213+ parser = _ArgParser (prog = 'regrtest.py' ,
214+ usage = USAGE ,
215+ description = DESCRIPTION ,
216+ epilog = EPILOG ,
217+ add_help = False ,
218+ formatter_class = argparse .RawDescriptionHelpFormatter )
215219
216220 # Arguments with this clause added to its help are described further in
217221 # the epilog's "Additional option details" section.
@@ -301,8 +305,18 @@ def _create_parser():
301305
302306 return parser
303307
308+ # TODO: remove this function as described in issue #16799, for example.
309+ # We use this function since regrtest.main() was originally written to use
310+ # getopt for parsing.
304311def _convert_namespace_to_getopt (ns ):
305- """Convert an argparse.Namespace object to a getopt-style (opts, args)."""
312+ """Convert an argparse.Namespace object to a getopt-style opts list.
313+
314+ The return value of this function mimics the first element of
315+ getopt.getopt()'s (opts, args) return value. In addition, the (option,
316+ value) pairs in the opts list are sorted by option and use the long
317+ option string. The args part of (opts, args) can be mimicked by the
318+ args attribute of the Namespace object we are using in regrtest.
319+ """
306320 opts = []
307321 args_dict = vars (ns )
308322 for key in sorted (args_dict .keys ()):
@@ -319,21 +333,7 @@ def _convert_namespace_to_getopt(ns):
319333 # includes these with value '' in the opts list.
320334 val = ''
321335 opts .append (('--' + key , val ))
322- return opts , ns .args
323-
324- # This function has a getopt-style return value because regrtest.main()
325- # was originally written using getopt.
326- # TODO: switch this to return an argparse.Namespace instance.
327- def _parse_args (args = None ):
328- """Parse arguments, and return a getopt-style (opts, args).
329-
330- This method mimics the return value of getopt.getopt(). In addition,
331- the (option, value) pairs in opts are sorted by option and use the long
332- option string.
333- """
334- parser = _create_parser ()
335- ns = parser .parse_args (args = args )
336- return _convert_namespace_to_getopt (ns )
336+ return opts
337337
338338
339339def main (tests = None , testdir = None , verbose = 0 , quiet = False ,
@@ -381,7 +381,11 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
381381
382382 support .record_original_stdout (sys .stdout )
383383
384- opts , args = _parse_args ()
384+ parser = _create_parser ()
385+ ns = parser .parse_args ()
386+ opts = _convert_namespace_to_getopt (ns )
387+ args = ns .args
388+ usage = parser .error
385389
386390 # Defaults
387391 if random_seed is None :
0 commit comments