generator: Improve handling of command-line parameters

Bundle Options.cs from the Mono source tree, and use it to parse the
command-line options for gapi-codegen. This gives us clearer code,
descriptions for each option, and a nice "--help" output.

This does not change the options syntax, except that -I|--include needs
to specified for each file to include. Two Makefiles in sample/ are
updated for that change.
This commit is contained in:
Bertrand Lorentz 2012-11-25 14:52:07 +01:00
parent ab61fbccbd
commit 629a34aa4f
6 changed files with 1418 additions and 47 deletions

View file

@ -30,12 +30,7 @@ namespace GtkSharp.Generation {
public static int Main (string[] args) public static int Main (string[] args)
{ {
if (args.Length < 2) { bool show_help = false;
Console.WriteLine ("Usage: codegen --generate <filename1...>");
return 0;
}
bool generate = false;
string dir = ""; string dir = "";
string assembly_name = ""; string assembly_name = "";
string glue_filename = ""; string glue_filename = "";
@ -44,48 +39,67 @@ namespace GtkSharp.Generation {
SymbolTable table = SymbolTable.Table; SymbolTable table = SymbolTable.Table;
var gens = new List<IGeneratable> (); var gens = new List<IGeneratable> ();
foreach (string arg in args) {
if (arg.StartsWith ("--customdir=")) {
Console.WriteLine ("Using .custom files is not supported anymore, use partial classes instead.");
return 0;
}
string filename = arg;
if (arg == "--generate") {
generate = true;
continue;
} else if (arg == "--include") {
generate = false;
continue;
} else if (arg.StartsWith ("-I:")) {
generate = false;
filename = filename.Substring (3);
} else if (arg.StartsWith ("--outdir=")) {
generate = false;
dir = arg.Substring (9);
continue;
} else if (arg.StartsWith ("--assembly-name=")) {
generate = false;
assembly_name = arg.Substring (16);
continue;
} else if (arg.StartsWith ("--glue-filename=")) {
generate = false;
glue_filename = arg.Substring (16);
continue;
} else if (arg.StartsWith ("--glue-includes=")) {
generate = false;
glue_includes = arg.Substring (16);
continue;
} else if (arg.StartsWith ("--gluelib-name=")) {
generate = false;
gluelib_name = arg.Substring (15);
continue;
}
Parser p = new Parser (); var filenames = new List<string> ();
var includes = new List<string> ();
var options = new OptionSet () {
{ "generate=", "Generate the C# code for this GAPI XML file.",
(string v) => { filenames.Add (v); } },
{ "I|include=", "GAPI XML file that contain symbols used in the main GAPI XML file.",
(string v) => { includes.Add (v); } },
{ "outdir=", "Directory where the C# files will be generated.",
(string v) => { dir = v; } },
{ "assembly-name=", "Name of the assembly for which the code is generated.",
(string v) => { assembly_name = v; } },
{ "glue-filename=", "Filename for the generated C glue code.",
(string v) => { glue_filename = v; } },
{ "glue-includes=", "Content of #include directive to add in the generated C glue code.",
(string v) => { glue_includes = v; } },
{ "gluelib-name=", "Name of the C library into which the C glue code will be compiled. " +
"Used to generated correct DllImport attributes.",
(string v) => { gluelib_name = v; } },
{ "h|help", "Show this message and exit",
v => show_help = v != null },
};
List<string> extra;
try {
extra = options.Parse (args);
}
catch (OptionException e) {
Console.Write ("gapi-codegen: ");
Console.WriteLine (e.Message);
Console.WriteLine ("Try `gapi-codegen --help' for more information.");
return 0;
}
if (show_help) {
ShowHelp (options);
return 1;
}
if (filenames.Count == 0) {
Console.WriteLine ("You need to specify a file to process using the --generate option.");
Console.WriteLine ("Try `gapi-codegen --help' for more information.");
return 0;
}
if (extra.Exists (v => { return v.StartsWith ("--customdir"); })) {
Console.WriteLine ("Using .custom files is not supported anymore, use partial classes instead.");
return 0;
}
Parser p = new Parser ();
foreach (string include in includes) {
IGeneratable[] curr_gens = p.Parse (include);
table.AddTypes (curr_gens);
}
foreach (string filename in filenames) {
IGeneratable[] curr_gens = p.Parse (filename); IGeneratable[] curr_gens = p.Parse (filename);
table.AddTypes (curr_gens); table.AddTypes (curr_gens);
if (generate) gens.AddRange (curr_gens);
gens.AddRange (curr_gens);
} }
// Now that everything is loaded, validate all the to-be- // Now that everything is loaded, validate all the to-be-
@ -117,5 +131,13 @@ namespace GtkSharp.Generation {
Statistics.Report(); Statistics.Report();
return 0; return 0;
} }
static void ShowHelp (OptionSet p)
{
Console.WriteLine ("Usage: gapi-codegen [OPTIONS]+");
Console.WriteLine ();
Console.WriteLine ("Options:");
p.WriteOptionDescriptions (Console.Out);
}
} }
} }

View file

@ -46,6 +46,7 @@ sources = \
ObjectBase.cs \ ObjectBase.cs \
ObjectGen.cs \ ObjectGen.cs \
OpaqueGen.cs \ OpaqueGen.cs \
Options.cs \
OwnableGen.cs \ OwnableGen.cs \
Parameter.cs \ Parameter.cs \
Parameters.cs \ Parameters.cs \

1347
generator/Options.cs Normal file

File diff suppressed because it is too large Load diff

View file

@ -90,6 +90,7 @@
<Compile Include="XmlElementExtensions.cs" /> <Compile Include="XmlElementExtensions.cs" />
<Compile Include="Parameter.cs" /> <Compile Include="Parameter.cs" />
<Compile Include="ArrayParameter.cs" /> <Compile Include="ArrayParameter.cs" />
<Compile Include="Options.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="DESIGN" /> <None Include="DESIGN" />

View file

@ -18,7 +18,7 @@ libopaque_la_LIBADD = $(GTK_LIBS)
INCLUDES = $(GTK_CFLAGS) INCLUDES = $(GTK_CFLAGS)
generated/*.cs: opaque-api.xml generated/*.cs: opaque-api.xml
$(RUNTIME) ../../generator/gapi_codegen.exe --generate $(srcdir)/opaque-api.xml --include ../../gtk/gtk-api.xml ../../gdk/gdk-api.xml --outdir=generated --assembly-name=opaque-sharp $(RUNTIME) ../../generator/gapi_codegen.exe --generate $(srcdir)/opaque-api.xml --include=../../gtk/gtk-api.xml --include=../../gdk/gdk-api.xml --outdir=generated --assembly-name=opaque-sharp
api: api:
PATH=../../parser:$(PATH) $(RUNTIME) ../../parser/gapi-parser.exe opaque-sources.xml PATH=../../parser:$(PATH) $(RUNTIME) ../../parser/gapi-parser.exe opaque-sources.xml

View file

@ -18,7 +18,7 @@ libvalobj_la_LIBADD = $(GTK_LIBS)
INCLUDES = $(GTK_CFLAGS) INCLUDES = $(GTK_CFLAGS)
Valobj.cs: valobj-api.xml Valobj.cs: valobj-api.xml
$(RUNTIME) ../../generator/gapi_codegen.exe --generate $(srcdir)/valobj-api.xml --include ../../gtk/gtk-api.xml ../../gdk/gdk-api.xml --outdir=. --assembly-name=valobj-sharp $(RUNTIME) ../../generator/gapi_codegen.exe --generate $(srcdir)/valobj-api.xml --include=../../gtk/gtk-api.xml --include=../../gdk/gdk-api.xml --outdir=. --assembly-name=valobj-sharp
api: api:
PATH=../../parser:$(PATH) $(RUNTIME) ../../parser/gapi-parser.exe valobj-sources.xml PATH=../../parser:$(PATH) $(RUNTIME) ../../parser/gapi-parser.exe valobj-sources.xml