gtk-builder-tool: Separate commands

Add separate commands for validation and simplification.
This commit is contained in:
Matthias Clasen
2015-04-27 23:31:03 -04:00
parent 43cee06160
commit d7523423d4
2 changed files with 37 additions and 20 deletions

View File

@ -30,17 +30,31 @@
<refsynopsisdiv> <refsynopsisdiv>
<cmdsynopsis> <cmdsynopsis>
<command>gtk-builder-tool</command> <command>gtk-builder-tool</command>
<arg choice="opt"><replaceable>COMMAND</replaceable></arg>
<arg choice="plain"><replaceable>FILE</replaceable></arg> <arg choice="plain"><replaceable>FILE</replaceable></arg>
</cmdsynopsis> </cmdsynopsis>
</refsynopsisdiv> </refsynopsisdiv>
<refsect1><title>Description</title> <refsect1><title>Description</title>
<para> <para>
<command>gtk-builder-tool</command> validates and simplifies a GtkBuilder <command>gtk-builder-tool</command> can perform various operations
.ui file. Any validation errors are reported on stderr. If the file is on GtkBuilder .ui files.
valid, it is simplified by removing any properties that are set to their
default value. The resulting xml is written to stdout.
</para> </para>
</refsect1> </refsect1>
<refsect1><title>Commands</title>
<para>The following commands are understood:</para>
<variablelist>
<varlistentry>
<term><option>validate</option></term>
<listitem><para>Validate the .ui file and report errors to stderr.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>simplify</option></term>
<listitem><para>Simplify the .ui file by removing properties that
are set to their default values and write the resulting XML to stdout.</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
</refentry> </refentry>

View File

@ -362,7 +362,7 @@ GMarkupParser parser = {
NULL NULL
}; };
static gboolean static void
do_simplify (const gchar *filename) do_simplify (const gchar *filename)
{ {
GMarkupParseContext *context; GMarkupParseContext *context;
@ -373,7 +373,7 @@ do_simplify (const gchar *filename)
if (!g_file_get_contents (filename, &buffer, NULL, &error)) if (!g_file_get_contents (filename, &buffer, NULL, &error))
{ {
g_printerr (_("Can't load file: %s\n"), error->message); g_printerr (_("Can't load file: %s\n"), error->message);
return FALSE; exit (1);
} }
data.builder = gtk_builder_new (); data.builder = gtk_builder_new ();
@ -390,13 +390,11 @@ do_simplify (const gchar *filename)
if (!g_markup_parse_context_parse (context, buffer, -1, &error)) if (!g_markup_parse_context_parse (context, buffer, -1, &error))
{ {
g_printerr (_("Can't parse file: %s\n"), error->message); g_printerr (_("Can't parse file: %s\n"), error->message);
return FALSE; exit (1);
} }
return TRUE;
} }
static gboolean static void
do_validate (const gchar *filename) do_validate (const gchar *filename)
{ {
GtkBuilder *builder; GtkBuilder *builder;
@ -410,16 +408,20 @@ do_validate (const gchar *filename)
if (ret == 0) if (ret == 0)
{ {
g_printerr ("%s\n", error->message); g_printerr ("%s\n", error->message);
return FALSE; exit (1);
} }
return TRUE;
} }
static void static void
usage (void) usage (void)
{ {
g_print (_("Usage: gtk-builder-tool FILE\n" g_print (_("Usage:\n"
" gtk-builder-tool [COMMAND] FILE\n"
"\n"
"Commands:\n"
" validate Validate the file\n"
" simplify Simplify the file\n"
"\n"
"Validate and simplify GtkBuilder .ui files.\n")); "Validate and simplify GtkBuilder .ui files.\n"));
exit (1); exit (1);
} }
@ -433,14 +435,15 @@ main (int argc, char *argv[])
gtk_test_register_all_types (); gtk_test_register_all_types ();
if (argc < 2) if (argc < 3)
usage (); usage ();
if (!do_validate (argv[1])) if (strcmp (argv[1], "validate") == 0)
return 1; do_validate (argv[2]);
else if (strcmp (argv[1], "simplify") == 0)
if (!do_simplify (argv[1])) do_simplify (argv[2]);
return 1; else
usage ();
return 0; return 0;
} }