diff options
author | Dylan Baker <dylanx.c.baker@intel.com> | 2014-11-19 13:36:35 -0800 |
---|---|---|
committer | Dylan Baker <baker.dylan.c@gmail.com> | 2015-05-21 09:44:13 -0700 |
commit | 631b8aefb6a411c04abe8df6fef86cf84759b02d (patch) | |
tree | 278c7aaca1fb84f34ec91c15a6597f6a3748458c | |
parent | 280c1810135dbf2db19b378a99c919126042cc9f (diff) |
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 <dylanx.c.baker@intel.com>
Acked-by: Matt Turner <mattst88@gmail.com>
-rw-r--r-- | src/mapi/glapi/gen/gl_table.py | 70 |
1 files changed, 33 insertions, 37 deletions
diff --git a/src/mapi/glapi/gen/gl_table.py b/src/mapi/glapi/gen/gl_table.py index 32456250dc..30903fd8f6 100644 --- a/src/mapi/glapi/gen/gl_table.py +++ b/src/mapi/glapi/gen/gl_table.py @@ -26,8 +26,7 @@ # Authors: # Ian Romanick <idr@us.ibm.com> -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__': |