1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
From: "FeRD (Frank Dana)" <ferdnyc@gmail.com>
Date: Fri, 12 Sep 2025 01:38:57 -0400
Subject: get_help(): Ignore width of invisible options
When get_help() gets ready to lay out the help text into columns,
it first goes through and computes the max_width of the strings in
the left column. Problem is, it measures the width of every
available option, whether or not they'll actually be displayed.
Instead, let's use the same criteria used when deciding whether
to display an option, to decide whether or not to account for it
when computing max_width. This way, the layout is sized for the
help that's actually being produced.
Bug: https://gitlab.gnome.org/GNOME/glib/-/issues/3781
Origin: upstream, 2.86.1, commit:bff4dcb3d803c73c6601d75fe2d700ee7016af03
---
glib/goption.c | 22 +++++++----
glib/tests/option-context.c | 96 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 110 insertions(+), 8 deletions(-)
diff --git a/glib/goption.c b/glib/goption.c
index 38303fb..8ddb8cd 100644
--- a/glib/goption.c
+++ b/glib/goption.c
@@ -816,16 +816,22 @@ g_option_context_get_help (GOptionContext *context,
{
GOptionGroup *g = list->data;
- if (context->help_enabled)
+ if (!group || group == g)
{
- /* First, we check the --help-<groupname> options */
- len = _g_utf8_strwidth ("--help-") + _g_utf8_strwidth (g->name);
- max_length = MAX (max_length, len);
- }
+ if (context->help_enabled)
+ {
+ /* First, we check the --help-<groupname> options */
+ len = _g_utf8_strwidth ("--help-") + _g_utf8_strwidth (g->name);
+ max_length = MAX (max_length, len);
+ }
- /* Then we go through the entries */
- len = calculate_max_length (g, aliases);
- max_length = MAX (max_length, len);
+ /* Then we go through the entries */
+ if (group_has_visible_entries (context, g, main_help))
+ {
+ len = calculate_max_length (g, aliases);
+ max_length = MAX (max_length, len);
+ }
+ }
list = list->next;
}
diff --git a/glib/tests/option-context.c b/glib/tests/option-context.c
index 11a50b1..b9a31b7 100644
--- a/glib/tests/option-context.c
+++ b/glib/tests/option-context.c
@@ -2237,6 +2237,100 @@ test_help_deprecated (void)
g_option_context_free (context);
}
+static void
+test_get_help_format (void)
+{
+ GOptionContext *context = NULL;
+ GOptionGroup *group = NULL;
+ char *str = NULL;
+ char *arg = NULL;
+ GOptionEntry entries[] = {
+ { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Test flag", "ARG" },
+ { "other", 'o', 0, G_OPTION_ARG_NONE, NULL, "Another flag", NULL },
+ G_OPTION_ENTRY_NULL
+ };
+
+ GOptionEntry group_entries[] = {
+ { "group-flag-with-a-really-long-name", 'g', 0, G_OPTION_ARG_STRING, &arg, "Group test", "GROUP_ARG" },
+ { "other-flag-with-an-even-longer-name-somehow", 'o', G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_NONE, NULL, "Group flag", NULL },
+ G_OPTION_ENTRY_NULL
+ };
+
+ context = g_option_context_new ("main");
+ g_option_context_add_main_entries (context, entries, NULL);
+ g_option_context_set_summary (context, "Summary");
+ g_option_context_set_description (context, "Description");
+ g_option_context_set_help_enabled (context, FALSE);
+
+ group = g_option_group_new ("really-long-group-name", "Group1-description", "Group1-help", NULL, NULL);
+ g_option_group_add_entries (group, group_entries);
+
+ g_option_context_add_group (context, g_steal_pointer (&group));
+
+ str = g_option_context_get_help (context, TRUE, NULL);
+ g_assert_nonnull (strstr (str, "test=ARG"));
+ g_assert_null (strstr (str, "group-name"));
+ g_assert_null (strstr (str, "longer-name-somehow"));
+
+ /* Measure distance between columns */
+ const char *options = strstr (str, "test=ARG");
+ const char *descriptions = strstr (str, "Test flag");
+ g_assert_cmpint (descriptions - options, <, 20);
+
+ g_free (str);
+ g_free (arg);
+ g_option_context_free (context);
+}
+
+static void
+test_group_get_help_format (void)
+{
+ GOptionContext *context = NULL;
+ GOptionGroup *group = NULL;
+ char *str = NULL;
+ char *arg = NULL;
+ GOptionEntry entries[] = {
+ { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Test flag", "ARG" },
+ { "other", 'o', 0, G_OPTION_ARG_NONE, NULL, "Another flag", NULL },
+ G_OPTION_ENTRY_NULL
+ };
+
+ GOptionEntry group_entries[] = {
+ { "group-flag-with-a-really-long-name", 'g', 0, G_OPTION_ARG_STRING, &arg, "Group test", "GROUP_ARG" },
+ { "other-flag-with-an-even-longer-name-somehow", 'o', G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_NONE, NULL, "Group flag", NULL },
+ G_OPTION_ENTRY_NULL
+ };
+
+ context = g_option_context_new ("main");
+ g_option_context_add_main_entries (context, entries, NULL);
+ g_option_context_set_summary (context, "Summary");
+ g_option_context_set_description (context, "Description");
+ g_option_context_set_help_enabled (context, FALSE);
+
+ group = g_option_group_new ("really-long-group-name", "Group1-description", "Group1-help", NULL, NULL);
+ g_option_group_add_entries (group, group_entries);
+
+ g_option_context_add_group (context, group);
+
+ str = g_option_context_get_help (context, FALSE, group);
+ g_assert_null (strstr (str, "test=ARG"));
+ g_assert_nonnull (strstr (str, "--group-flag-with"));
+ g_assert_nonnull (strstr (str, "--other-flag-with"));
+ g_assert_nonnull (strstr (str, "=GROUP_ARG"));
+ g_assert_nonnull (strstr (str, "Group test"));
+
+ /* Measure distance between columns */
+ const char *options_start = strstr (str, "--group-flag-with");
+ const char *options_end = strstr (str, "=GROUP_ARG");
+ const char *descriptions = strstr (str, "Group test");
+ g_assert_cmpint (descriptions - options_start, >, 50);
+ g_assert_cmpint (descriptions - options_end, <, 20);
+
+ g_free (str);
+ g_free (arg);
+ g_option_context_free (context);
+}
+
static void
set_bool (gpointer data)
{
@@ -2692,6 +2786,8 @@ main (int argc,
g_test_add_func ("/option/help/no-options", test_help_no_options);
g_test_add_func ("/option/help/no-help-options", test_help_no_help_options);
g_test_add_func ("/option/help/deprecated", test_help_deprecated);
+ g_test_add_func ("/option/help/main-get_help-format", test_get_help_format);
+ g_test_add_func ("/option/help/group-get_help-format", test_group_get_help_format);
g_test_add_func ("/option/basic", test_basic);
g_test_add_func ("/option/translate", test_translate);
|