Do not show empty groups in --help output. Initial patch from Yevgen

Muntyan. (#504142)

* glib/goption.c: Do not show empty groups in --help output.
* glib/tests/Makefile.am: Add option-context.c
* glib/tests/option-context.c: Test skipping of empty groups.

svn path=/trunk/; revision=6198
This commit is contained in:
Mathias Hasselmann 2007-12-24 11:09:03 +00:00
parent fcf262e393
commit 005a22ab10
4 changed files with 241 additions and 17 deletions

View File

@ -1,3 +1,12 @@
2007-12-18 Mathias Hasselmann <mathias@openismus.com>
Do not show empty groups in --help output. Initial patch from Yevgen
Muntyan. (#504142)
* glib/goption.c: Do not show empty groups in --help output.
* glib/tests/Makefile.am: Add option-context.c
* glib/tests/option-context.c: Test skipping of empty groups.
2007-12-22 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c: Remove wrong documentation about start group

View File

@ -520,6 +520,45 @@ print_entry (GOptionGroup *group,
g_string_free (str, TRUE);
}
static gboolean
group_has_visible_entries (GOptionGroup *group,
gboolean main_entries)
{
GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
GOptionEntry *entry;
gint i, l;
if (!main_entries)
reject_filter |= G_OPTION_FLAG_IN_MAIN;
for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
{
entry = &group->entries[i];
if (main_entries && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
continue;
if (!(entry->flags & reject_filter))
return TRUE;
}
return FALSE;
}
static gboolean
group_list_has_visible_entires (GList *group_list,
gboolean main_entries)
{
while (group_list)
{
if (group_has_visible_entries (group_list->data, main_entries))
return TRUE;
group_list = group_list->next;
}
return FALSE;
}
/**
* g_option_context_get_help:
* @context: a #GOptionContext
@ -686,10 +725,11 @@ g_option_context_get_help (GOptionContext *context,
while (list)
{
GOptionGroup *group = list->data;
g_string_append_printf (string, " --help-%-*s %s\n",
max_length - 5, group->name,
TRANSLATE (group, group->help_description));
if (group_has_visible_entries (group, FALSE))
g_string_append_printf (string, " --help-%-*s %s\n",
max_length - 5, group->name,
TRANSLATE (group, group->help_description));
list = list->next;
}
@ -700,12 +740,15 @@ g_option_context_get_help (GOptionContext *context,
if (group)
{
/* Print a certain group */
g_string_append (string, TRANSLATE (group, group->description));
g_string_append (string, "\n");
for (i = 0; i < group->n_entries; i++)
print_entry (group, max_length, &group->entries[i], string);
g_string_append (string, "\n");
if (group_has_visible_entries (group, FALSE))
{
g_string_append (string, TRANSLATE (group, group->description));
g_string_append (string, "\n");
for (i = 0; i < group->n_entries; i++)
print_entry (group, max_length, &group->entries[i], string);
g_string_append (string, "\n");
}
}
else if (!main_help)
{
@ -717,19 +760,25 @@ g_option_context_get_help (GOptionContext *context,
{
GOptionGroup *group = list->data;
g_string_append (string, group->description);
g_string_append (string, "\n");
for (i = 0; i < group->n_entries; i++)
if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
print_entry (group, max_length, &group->entries[i], string);
if (group_has_visible_entries (group, FALSE))
{
g_string_append (string, group->description);
g_string_append (string, "\n");
for (i = 0; i < group->n_entries; i++)
if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
print_entry (group, max_length, &group->entries[i], string);
g_string_append (string, "\n");
g_string_append (string, "\n");
}
list = list->next;
}
}
/* Print application options if --help or --help-all has been specified */
if (main_help || !group)
if ((main_help || !group) &&
(group_has_visible_entries (context->main_group, TRUE) ||
group_list_has_visible_entires (context->groups, TRUE)))
{
list = context->groups;

View File

@ -10,6 +10,10 @@ TEST_PROGS += testing
testing_SOURCES = testing.c
testing_LDADD = $(progs_ldadd)
TEST_PROGS += option-context
option_context_SOURCES = option-context.c
option_context_LDADD = $(progs_ldadd)
# some testing of gtester funcitonality
XMLLINT=xmllint
gtester-xmllint-check: # check testreport xml with xmllint if present

162
glib/tests/option-context.c Normal file
View File

@ -0,0 +1,162 @@
/* Unit tests for GOptionContext
* Copyright (C) 2007 Openismus GmbH
* Authors: Mathias Hasselmann
*
* This work is provided "as is"; redistribution and modification
* in whole or in part, in any medium, physical or electronic is
* permitted without restriction.
*
* This work is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* In no event shall the authors or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*/
#include <glib/gtestutils.h>
static void
group_captions (void)
{
gchar *help_variants[] = { "--help", "--help-all", "--help-test" };
GOptionEntry main_entries[] = {
{ "main-switch", 0,
G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_IN_MAIN,
G_OPTION_ARG_NONE, NULL,
"A switch that is in the main group", NULL },
{ NULL }
};
GOptionEntry group_entries[] = {
{ "test-switch", 0,
G_OPTION_FLAG_NO_ARG,
G_OPTION_ARG_NONE, NULL,
"A switch that is in the test group", NULL },
{ NULL }
};
gint i, j;
g_test_bug ("504142");
for (i = 0; i < 4; ++i)
{
gboolean have_main_entries = (0 != (i & 1));
gboolean have_test_entries = (0 != (i & 2));
GOptionContext *options;
GOptionGroup *group;
options = g_option_context_new (NULL);
group = g_option_group_new ("test", "Test Options",
"Show all test options",
NULL, NULL);
if (have_main_entries)
g_option_context_add_main_entries (options, main_entries, NULL);
if (have_test_entries)
g_option_group_add_entries (group, group_entries);
g_option_context_add_group (options, group);
for (j = 0; j < G_N_ELEMENTS (help_variants); ++j)
{
gchar *args[] = { __FILE__, help_variants[j], NULL };
g_test_message ("test setup: args='%s', main-entries=%d, test-entries=%d",
args[1], have_main_entries, have_test_entries);
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT |
G_TEST_TRAP_SILENCE_STDERR))
{
gchar **argv = args;
gint argc = 2;
g_option_context_parse (options, &argc, &argv, NULL);
g_assert_not_reached ();
}
else
{
gboolean expect_main_description = FALSE;
gboolean expect_main_switch = FALSE;
gboolean expect_test_description = FALSE;
gboolean expect_test_switch = FALSE;
gboolean expect_test_group = FALSE;
g_test_trap_assert_passed ();
g_test_trap_assert_stderr ("");
switch (j)
{
case 0:
g_assert_cmpstr ("--help", ==, args[1]);
expect_main_switch = have_main_entries;
expect_test_group = have_test_entries;
break;
case 1:
g_assert_cmpstr ("--help-all", ==, args[1]);
expect_main_switch = have_main_entries;
expect_test_switch = have_test_entries;
expect_test_group = have_test_entries;
break;
case 2:
g_assert_cmpstr ("--help-test", ==, args[1]);
expect_test_switch = have_test_entries;
break;
default:
g_assert_not_reached ();
break;
}
expect_main_description |= expect_main_switch;
expect_test_description |= expect_test_switch;
if (expect_main_description)
g_test_trap_assert_stdout ("*Application Options*");
else
g_test_trap_assert_stdout_unmatched ("*Application Options*");
if (expect_main_switch)
g_test_trap_assert_stdout ("*--main-switch*");
else
g_test_trap_assert_stdout_unmatched ("*--main-switch*");
if (expect_test_description)
g_test_trap_assert_stdout ("*Test Options*");
else
g_test_trap_assert_stdout_unmatched ("*Test Options*");
if (expect_test_switch)
g_test_trap_assert_stdout ("*--test-switch*");
else
g_test_trap_assert_stdout_unmatched ("*--test-switch*");
if (expect_test_group)
g_test_trap_assert_stdout ("*--help-test*");
else
g_test_trap_assert_stdout_unmatched ("*--help-test*");
}
}
}
}
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_bug_base ("http://bugzilla.gnome.org/");
g_test_add_func ("/group/captions", group_captions);
return g_test_run();
}