[glib] GOptionContext: Don't show headings without options



commit 64bfbc2cf074de087c4e195d6409af9c24dc2f28
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jun 29 13:47:15 2012 -0400

    GOptionContext: Don't show headings without options
    
    If the only entry in the main group is for the rest args,
    don't bother printing "Application Options".

 glib/goption.c              |    2 ++
 glib/tests/option-context.c |   27 ++++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletions(-)
---
diff --git a/glib/goption.c b/glib/goption.c
index 1a16a72..e405546 100644
--- a/glib/goption.c
+++ b/glib/goption.c
@@ -639,6 +639,8 @@ group_has_visible_entries (GOptionContext *context,
 
       if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
         continue;
+      if (entry->long_name[0] == 0) /* ignore rest entry */
+        continue;
       if (!(entry->flags & reject_filter))
         return TRUE;
     }
diff --git a/glib/tests/option-context.c b/glib/tests/option-context.c
index 5008117..da511ad 100644
--- a/glib/tests/option-context.c
+++ b/glib/tests/option-context.c
@@ -1993,6 +1993,30 @@ test_help (void)
 }
 
 static void
+test_help_no_options (void)
+{
+  GOptionContext *context;
+  gchar **sarr = NULL;
+  GOptionEntry entries[] = {
+    { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
+    { NULL }
+  };
+  gchar *str;
+
+  context = g_option_context_new ("blabla");
+  g_option_context_add_main_entries (context, entries, NULL);
+
+  str = g_option_context_get_help (context, FALSE, NULL);
+  g_assert (strstr (str, "blabla") != NULL);
+  g_assert (strstr (str, "REST") != NULL);
+  g_assert (strstr (str, "Help Options") != NULL);
+  g_assert (strstr (str, "Application Options") == NULL);
+
+  g_free (str);
+  g_option_context_free (context);
+}
+
+static void
 set_bool (gpointer data)
 {
   gboolean *b = data;
@@ -2198,7 +2222,8 @@ main (int   argc,
   g_test_init (&argc, &argv, NULL);
 
   g_test_bug_base ("http://bugzilla.gnome.org/";);
-  g_test_add_func ("/option/help", test_help);
+  g_test_add_func ("/option/help/options", test_help);
+  g_test_add_func ("/option/help/no-options", test_help_no_options);
 
   g_test_add_func ("/option/basic", test_basic);
   g_test_add_func ("/option/group/captions", test_group_captions);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]