gtk-builder-tool: Reshuffle --help

Use GOptionContext better.
This commit is contained in:
Matthias Clasen 2022-04-17 11:59:06 -04:00
parent b88ac0890e
commit c5e26dd591
6 changed files with 96 additions and 30 deletions

View File

@ -403,6 +403,9 @@ modules/printbackends/gtkprintbackendlpr.c
modules/printbackends/gtkprintercups.c
tools/encodesymbolic.c
tools/gtk-builder-tool.c
tools/gtk-builder-tool-enumerate.c
tools/gtk-builder-tool-preview.c
tools/gtk-builder-tool-simplify.c
tools/gtk-builder-tool-validate.c
tools/gtk-launch.c
tools/updateiconcache.c

View File

@ -17,6 +17,8 @@
* Author: Matthias Clasen
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@ -46,12 +48,42 @@ do_enumerate (int *argc, const char ***argv)
GSList *list, *l;
GObject *object;
const char *name;
const char *filename;
char **filenames = NULL;
GOptionContext *context;
const GOptionEntry entries[] = {
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
{ NULL, }
};
filename = (*argv)[1];
g_set_prgname ("gtk4-builder-tool enumerate");
context = g_option_context_new (NULL);
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_set_summary (context, _("List all named objects."));
if (!g_option_context_parse (context, argc, (char ***)argv, &error))
{
g_printerr ("%s\n", error->message);
g_error_free (error);
exit (1);
}
g_option_context_free (context);
if (filenames == NULL)
{
g_printerr ("No .ui file specified\n");
exit (1);
}
if (g_strv_length (filenames) > 1)
{
g_printerr ("Can only enumerate a single .ui file\n");
exit (1);
}
builder = gtk_builder_new ();
ret = gtk_builder_add_from_file (builder, filename, &error);
ret = gtk_builder_add_from_file (builder, filenames[0], &error);
if (ret == 0)
{
@ -72,4 +104,6 @@ do_enumerate (int *argc, const char ***argv)
g_slist_free (list);
g_object_unref (builder);
g_strfreev (filenames);
}

View File

@ -17,6 +17,8 @@
* Author: Matthias Clasen
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@ -173,9 +175,9 @@ do_preview (int *argc,
char *css = NULL;
char **filenames = NULL;
const GOptionEntry entries[] = {
{ "id", 0, 0, G_OPTION_ARG_STRING, &id, NULL, NULL },
{ "css", 0, 0, G_OPTION_ARG_FILENAME, &css, NULL, NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, NULL },
{ "id", 0, 0, G_OPTION_ARG_STRING, &id, N_("Preview only the named object"), N_("ID") },
{ "css", 0, 0, G_OPTION_ARG_FILENAME, &css, N_("Use style from CSS file"), N_("FILE") },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
{ NULL, }
};
GError *error = NULL;
@ -186,9 +188,11 @@ do_preview (int *argc,
exit (1);
}
g_set_prgname ("gtk4-builder-tool preview");
context = g_option_context_new (NULL);
g_option_context_set_help_enabled (context, FALSE);
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_set_summary (context, _("Preview the file."));
if (!g_option_context_parse (context, argc, (char ***)argv, &error))
{

View File

@ -17,6 +17,8 @@
* Author: Matthias Clasen
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@ -2351,28 +2353,30 @@ do_simplify (int *argc,
gboolean replace = FALSE;
gboolean convert3to4 = FALSE;
char **filenames = NULL;
GOptionContext *ctx;
GOptionContext *context;
const GOptionEntry entries[] = {
{ "replace", 0, 0, G_OPTION_ARG_NONE, &replace, NULL, NULL },
{ "3to4", 0, 0, G_OPTION_ARG_NONE, &convert3to4, NULL, NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, NULL },
{ "replace", 0, 0, G_OPTION_ARG_NONE, &replace, N_("Replace the file"), NULL },
{ "3to4", 0, 0, G_OPTION_ARG_NONE, &convert3to4, N_("Convert from GTK 3 to GTK 4"), NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
{ NULL, }
};
GError *error = NULL;
int i;
ctx = g_option_context_new (NULL);
g_option_context_set_help_enabled (ctx, FALSE);
g_option_context_add_main_entries (ctx, entries, NULL);
g_set_prgname ("gtk4-builder-tool simplify");
context = g_option_context_new (NULL);
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_set_summary (context, _("Simplify the file."));
if (!g_option_context_parse (ctx, argc, (char ***)argv, &error))
if (!g_option_context_parse (context, argc, (char ***)argv, &error))
{
g_printerr ("%s\n", error->message);
g_error_free (error);
exit (1);
}
g_option_context_free (ctx);
g_option_context_free (context);
if (filenames == NULL)
{
@ -2391,4 +2395,6 @@ do_simplify (int *argc,
if (!simplify_file (filenames[i], replace, convert3to4))
exit (1);
}
g_strfreev (filenames);
}

View File

@ -17,6 +17,8 @@
* Author: Matthias Clasen
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@ -150,11 +152,35 @@ validate_file (const char *filename)
void
do_validate (int *argc, const char ***argv)
{
GError *error = NULL;
char **filenames = NULL;
GOptionContext *context;
const GOptionEntry entries[] = {
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
{ NULL, }
};
int i;
for (i = 1; i < *argc; i++)
g_set_prgname ("gtk4-builder-tool validate");
context = g_option_context_new (NULL);
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_set_summary (context, _("Validate the file."));
if (!g_option_context_parse (context, argc, (char ***)argv, &error))
{
if (!validate_file ((*argv)[i]))
g_printerr ("%s\n", error->message);
g_error_free (error);
exit (1);
}
g_option_context_free (context);
for (i = 0; filenames[i]; i++)
{
if (!validate_file (filenames[i]))
exit (1);
}
g_strfreev (filenames);
}

View File

@ -34,21 +34,14 @@ usage (void)
g_print (_("Usage:\n"
" gtk-builder-tool [COMMAND] [OPTION…] FILE\n"
"\n"
"Perform various tasks on GtkBuilder .ui files.\n"
"\n"
"Commands:\n"
" validate Validate the file\n"
" simplify Simplify the file\n"
" enumerate List all named objects\n"
" preview Preview the file\n"
"\n"
"Simplify Options:\n"
" --replace Replace the file\n"
" --3to4 Convert from GTK 3 to GTK 4\n"
"\n"
"Preview Options:\n"
" --id=ID Preview only the named object\n"
" --css=FILE Use style from CSS file\n"
"\n"
"Perform various tasks on GtkBuilder .ui files.\n"));
"\n"));
exit (1);
}
@ -117,10 +110,10 @@ main (int argc, const char *argv[])
gtk_test_register_all_types ();
if (argc < 3)
if (argc < 2)
usage ();
if (strcmp (argv[2], "--help") == 0)
if (strcmp (argv[1], "--help") == 0)
usage ();
argv++;