gtk-builder-tool: Reshuffle --help
authorMatthias Clasen <mclasen@redhat.com>
Sun, 17 Apr 2022 15:59:06 +0000 (11:59 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 17 Apr 2022 16:57:46 +0000 (12:57 -0400)
Use GOptionContext better.

po/POTFILES.in
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-builder-tool.c

index 0e56e619c6499bdfc76c5b3d2fd8b9942f800f10..99c2034b9a3fcb52bca646a0bcb28614ade93283 100644 (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
index feb3758cc5ea5927c964d44c81d88b95bdc1f81f..f0c6c8dc7e89a272e14a75a8ae493b912d80dc90 100644 (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, }
+  };
+
+  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);
 
-  filename = (*argv)[1];
+  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);
 }
index df6e29976fb8cdd4bc589787bc849dd6f074d3ed..6c7473d80e01ee5d8384d862af4fbffd24a879a7 100644 (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))
     {
index fb3acdde40530be53ea620acd98c2a20bb050ccb..21dcfa2dc6da0b60ecc46a9970629092a19fbf12 100644 (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);
 }
index 8551231730143d2dc58c7730c05f785cfd7f3015..aa4069fd360ffa5bf38c7b9a812fb020847e0e4f 100644 (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))
+    {
+      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 ((*argv)[i]))
+      if (!validate_file (filenames[i]))
         exit (1);
     }
+
+  g_strfreev (filenames);
 }
index 7de50fc28383d5c0cd5158e928db43e5eb080fcc..ff47e81bd3484dcea41a6c2c2c46e1d2b8869d93 100644 (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++;