From 2114f5a98d0d80774306279e1694de074ca86aa0 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 2 Apr 2015 13:12:21 +0200 Subject: [PATCH] qapi: Factor parse_command_line() out of the generators Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- scripts/qapi-commands.py | 34 +++------------------------------- scripts/qapi-event.py | 32 +------------------------------- scripts/qapi-types.py | 38 +++++--------------------------------- scripts/qapi-visit.py | 37 +++++-------------------------------- scripts/qapi.py | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 127 deletions(-) diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index c94a19b91d..2889877680 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -15,9 +15,7 @@ from ordereddict import OrderedDict from qapi import * import re -import sys import os -import getopt import errno def generate_command_decl(name, args, ret_type): @@ -376,42 +374,16 @@ def gen_command_def_prologue(prefix="", proxy=False): ret += '#include "%sqmp-commands.h"' % prefix return ret + "\n\n" - -try: - opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m", - ["source", "header", "prefix=", - "input-file=", "output-dir=", - "middle"]) -except getopt.GetoptError, err: - print str(err) - sys.exit(1) - -output_dir = "" -prefix = "" c_file = 'qmp-marshal.c' h_file = 'qmp-commands.h' middle_mode = False -do_c = False -do_h = False +(input_file, output_dir, do_c, do_h, prefix, opts) = \ + parse_command_line("m", ["middle"]) for o, a in opts: - if o in ("-p", "--prefix"): - prefix = a - elif o in ("-i", "--input-file"): - input_file = a - elif o in ("-o", "--output-dir"): - output_dir = a + "/" - elif o in ("-m", "--middle"): + if o in ("-m", "--middle"): middle_mode = True - elif o in ("-c", "--source"): - do_c = True - elif o in ("-h", "--header"): - do_h = True - -if not do_c and not do_h: - do_c = True - do_h = True c_file = output_dir + prefix + c_file h_file = output_dir + prefix + h_file diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py index 3e1f4cf0fd..bc5ca4ad11 100644 --- a/scripts/qapi-event.py +++ b/scripts/qapi-event.py @@ -11,9 +11,7 @@ from ordereddict import OrderedDict from qapi import * -import sys import os -import getopt import errno def _generate_event_api_name(event_name, params): @@ -219,38 +217,10 @@ const char *%(event_enum_name)s_lookup[] = { # Start the real job -try: - opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:", - ["source", "header", "prefix=", - "input-file=", "output-dir="]) -except getopt.GetoptError, err: - print str(err) - sys.exit(1) - -input_file = "" -output_dir = "" -prefix = "" c_file = 'qapi-event.c' h_file = 'qapi-event.h' -do_c = False -do_h = False - -for o, a in opts: - if o in ("-p", "--prefix"): - prefix = a - elif o in ("-i", "--input-file"): - input_file = a - elif o in ("-o", "--output-dir"): - output_dir = a + "/" - elif o in ("-c", "--source"): - do_c = True - elif o in ("-h", "--header"): - do_h = True - -if not do_c and not do_h: - do_c = True - do_h = True +(input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line() c_file = output_dir + prefix + c_file h_file = output_dir + prefix + h_file diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py index 56651451c9..62044c11cb 100644 --- a/scripts/qapi-types.py +++ b/scripts/qapi-types.py @@ -11,9 +11,7 @@ from ordereddict import OrderedDict from qapi import * -import sys import os -import getopt import errno def generate_fwd_struct(name, members, builtin_type=False): @@ -275,42 +273,16 @@ void qapi_free_%(name)s(%(c_type)s obj) c_type=c_type(name), name=c_name(name)) return ret - -try: - opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:", - ["source", "header", "builtins", - "prefix=", "input-file=", "output-dir="]) -except getopt.GetoptError, err: - print str(err) - sys.exit(1) - -output_dir = "" -input_file = "" -prefix = "" c_file = 'qapi-types.c' h_file = 'qapi-types.h' - -do_c = False -do_h = False do_builtins = False -for o, a in opts: - if o in ("-p", "--prefix"): - prefix = a - elif o in ("-i", "--input-file"): - input_file = a - elif o in ("-o", "--output-dir"): - output_dir = a + "/" - elif o in ("-c", "--source"): - do_c = True - elif o in ("-h", "--header"): - do_h = True - elif o in ("-b", "--builtins"): - do_builtins = True +(input_file, output_dir, do_c, do_h, prefix, opts) = \ + parse_command_line("b", ["builtins"]) -if not do_c and not do_h: - do_c = True - do_h = True +for o, a in opts: + if o in ("-b", "--builtins"): + do_builtins = True c_file = output_dir + prefix + c_file h_file = output_dir + prefix + h_file diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index e511be357d..75f0cf3db5 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -15,9 +15,7 @@ from ordereddict import OrderedDict from qapi import * import re -import sys import os -import getopt import errno implicit_structs = [] @@ -376,41 +374,16 @@ void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **er ''', name=c_name(name)) -try: - opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:", - ["source", "header", "builtins", "prefix=", - "input-file=", "output-dir="]) -except getopt.GetoptError, err: - print str(err) - sys.exit(1) - -input_file = "" -output_dir = "" -prefix = "" c_file = 'qapi-visit.c' h_file = 'qapi-visit.h' - -do_c = False -do_h = False do_builtins = False -for o, a in opts: - if o in ("-p", "--prefix"): - prefix = a - elif o in ("-i", "--input-file"): - input_file = a - elif o in ("-o", "--output-dir"): - output_dir = a + "/" - elif o in ("-c", "--source"): - do_c = True - elif o in ("-h", "--header"): - do_h = True - elif o in ("-b", "--builtins"): - do_builtins = True +(input_file, output_dir, do_c, do_h, prefix, opts) = \ + parse_command_line("b", ["builtins"]) -if not do_c and not do_h: - do_c = True - do_h = True +for o, a in opts: + if o in ("-b", "--builtins"): + do_builtins = True c_file = output_dir + prefix + c_file h_file = output_dir + prefix + h_file diff --git a/scripts/qapi.py b/scripts/qapi.py index 273afedc3d..b97dd0b14a 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -13,6 +13,7 @@ import re from ordereddict import OrderedDict +import getopt import os import sys import string @@ -978,3 +979,42 @@ def guardend(name): ''', name=guardname(name)) + +def parse_command_line(extra_options = "", extra_long_options = []): + + try: + opts, args = getopt.gnu_getopt(sys.argv[1:], + "chp:i:o:" + extra_options, + ["source", "header", "prefix=", + "input-file=", "output-dir="] + + extra_long_options) + except getopt.GetoptError, err: + print str(err) + sys.exit(1) + + output_dir = "" + prefix = "" + do_c = False + do_h = False + extra_opts = [] + + for oa in opts: + o, a = oa + if o in ("-p", "--prefix"): + prefix = a + elif o in ("-i", "--input-file"): + input_file = a + elif o in ("-o", "--output-dir"): + output_dir = a + "/" + elif o in ("-c", "--source"): + do_c = True + elif o in ("-h", "--header"): + do_h = True + else: + extra_opts.append(oa) + + if not do_c and not do_h: + do_c = True + do_h = True + + return (input_file, output_dir, do_c, do_h, prefix, extra_opts)