From cf718cc964f86dc49c1fc9ed5e39aa5bd87ad931 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 19 Nov 2014 13:36:35 -0800 Subject: [PATCH] glapi: gl_table.py: replace getopt with argparse. This results in slightly less code, but code that is much more readable. It has the advantage of putting everything together in one place, all of the code is self documenting, help messages are auto-generated, choices are automatically enforced, and the syntax is much less C like, taking advantage of python features and idioms. Signed-off-by: Dylan Baker Acked-by: Matt Turner --- src/mapi/glapi/gen/gl_table.py | 70 ++++++++++++++++------------------ 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/src/mapi/glapi/gen/gl_table.py b/src/mapi/glapi/gen/gl_table.py index 32456250dc4..30903fd8f60 100644 --- a/src/mapi/glapi/gen/gl_table.py +++ b/src/mapi/glapi/gen/gl_table.py @@ -26,8 +26,7 @@ # Authors: # Ian Romanick -import sys -import getopt +import argparse import gl_XML import license @@ -203,45 +202,42 @@ class PrintRemapTable(gl_XML.gl_print_base): return -def show_usage(): - print "Usage: %s [-f input_file_name] [-m mode] [-c ver]" % sys.argv[0] - print " -m mode Mode can be 'table' or 'remap_table'." - print " -c ver Version can be 'es1' or 'es2'." - sys.exit(1) +def _parser(): + """Parse arguments and return a namespace.""" + parser = argparse.ArgumentParser() + parser.add_argument('-f', '--filename', + type=gl_XML.parse_GL_API, + default='gl_API.xml', + metavar="input_file_name", + dest='api', + help="Path to an XML description of OpenGL API.") + parser.add_argument('-m', '--mode', + choices=['table', 'remap_table'], + default='table', + metavar="mode", + help="Generate either a table or a remap_table") + parser.add_argument('-c', '--es-version', + choices=[None, 'es1', 'es2'], + default=None, + metavar="ver", + dest='es', + help="filter functions for es") + return parser.parse_args() def main(): """Main function.""" - file_name = "gl_API.xml" - - try: - args, _ = getopt.getopt(sys.argv[1:], "f:m:c:") - except Exception: - show_usage() - - mode = "table" - es = None - for (arg, val) in args: - if arg == "-f": - file_name = val - elif arg == "-m": - mode = val - elif arg == "-c": - es = val - - if mode == "table": - printer = PrintGlTable(es) - elif mode == "remap_table": - printer = PrintRemapTable(es) - else: - show_usage() - - api = gl_XML.parse_GL_API(file_name) - - if es is not None: - api.filter_functions_by_api(es) - - printer.Print(api) + args = _parser() + + if args.mode == "table": + printer = PrintGlTable(args.es) + elif args.mode == "remap_table": + printer = PrintRemapTable(args.es) + + if args.es is not None: + args.api.filter_functions_by_api(args.es) + + printer.Print(args.api) if __name__ == '__main__': -- 2.30.2