From 1795e0aeb0ee918324273148e23455ec9a795185 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 11 Feb 2005 04:11:34 +0000 Subject: [PATCH] Take main group options into account when calculating column size. 2005-02-10 Matthias Clasen * glib/goption.c (print_help): Take main group options into account when calculating column size. (#166921) (g_option_context_parse): Accept -? as documented. (#166977) --- ChangeLog | 7 +- ChangeLog.pre-2-10 | 7 +- ChangeLog.pre-2-12 | 7 +- ChangeLog.pre-2-8 | 7 +- docs/reference/ChangeLog | 4 ++ docs/reference/glib/tmpl/option.sgml | 4 +- glib/goption.c | 101 +++++++++++++++++---------- 7 files changed, 96 insertions(+), 41 deletions(-) diff --git a/ChangeLog b/ChangeLog index e38e04cca..37c8d9a0f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ -2005-02-09 Matthias Clasen +2005-02-10 Matthias Clasen + * glib/goption.c (print_help): Take main group options into + account when calculating column size. (#166921) + (g_option_context_parse): Accept -? as documented. (#166977) + +2005-02-09 Matthias Clasen * glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path here. (#166801, Kjartan Maraas) diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index e38e04cca..37c8d9a0f 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,5 +1,10 @@ -2005-02-09 Matthias Clasen +2005-02-10 Matthias Clasen + * glib/goption.c (print_help): Take main group options into + account when calculating column size. (#166921) + (g_option_context_parse): Accept -? as documented. (#166977) + +2005-02-09 Matthias Clasen * glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path here. (#166801, Kjartan Maraas) diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index e38e04cca..37c8d9a0f 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,5 +1,10 @@ -2005-02-09 Matthias Clasen +2005-02-10 Matthias Clasen + * glib/goption.c (print_help): Take main group options into + account when calculating column size. (#166921) + (g_option_context_parse): Accept -? as documented. (#166977) + +2005-02-09 Matthias Clasen * glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path here. (#166801, Kjartan Maraas) diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index e38e04cca..37c8d9a0f 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,5 +1,10 @@ -2005-02-09 Matthias Clasen +2005-02-10 Matthias Clasen + * glib/goption.c (print_help): Take main group options into + account when calculating column size. (#166921) + (g_option_context_parse): Accept -? as documented. (#166977) + +2005-02-09 Matthias Clasen * glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path here. (#166801, Kjartan Maraas) diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index 7a8e1b4b7..8d72fdba5 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,7 @@ +2005-02-10 Matthias Clasen + + * glib/tmpl/option.sgml: Fix a typo. (#166985) + 2005-02-07 Matthias Clasen * glib/glib-sections.txt: Add g_listenv. diff --git a/docs/reference/glib/tmpl/option.sgml b/docs/reference/glib/tmpl/option.sgml index a65d4b8e1..7363cb00b 100644 --- a/docs/reference/glib/tmpl/option.sgml +++ b/docs/reference/glib/tmpl/option.sgml @@ -56,7 +56,7 @@ Usage: testtreemodel [OPTION...] - test tree model performance Help Options: - --help Show help options + -?, --help Show help options --help-all Show all help options --help-gtk Show GTK+ Options @@ -299,7 +299,7 @@ g_option_context_add_main_entries() or g_option_group_add_entries(). --groupname-long_name. @short_name: If an option has a short name, it can be specified -short_name in a commandline. -@flags: Flags from #GOptionEntryFlags. +@flags: Flags from #GOptionFlags. @arg: The type of the option, as a #GOptionArg. @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data must point to a #GOptionArgFunc callback function, which will be called to handle diff --git a/glib/goption.c b/glib/goption.c index a7ff836bc..98d2b5065 100644 --- a/glib/goption.c +++ b/glib/goption.c @@ -362,6 +362,35 @@ g_option_context_add_main_entries (GOptionContext *context, g_option_group_set_translation_domain (context->main_group, translation_domain); } +static gint +calculate_max_length (GOptionGroup *group) +{ + GOptionEntry *entry; + gint i, len, max_length; + + max_length = 0; + + for (i = 0; i < group->n_entries; i++) + { + entry = &group->entries[i]; + + if (entry->flags & G_OPTION_FLAG_HIDDEN) + continue; + + len = g_utf8_strlen (entry->long_name, -1); + + if (entry->short_name) + len += 4; + + if (entry->arg != G_OPTION_ARG_NONE && entry->arg_description) + len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1); + + max_length = MAX (max_length, len); + } + + return max_length; +} + static void print_entry (GOptionGroup *group, gint max_length, @@ -393,11 +422,12 @@ print_entry (GOptionGroup *group, static void print_help (GOptionContext *context, gboolean main_help, - GOptionGroup *group) + GOptionGroup *group) { GList *list; gint max_length, len; gint i; + GOptionEntry *entry; GHashTable *shadow_map; gboolean seen[256]; @@ -412,14 +442,15 @@ print_help (GOptionContext *context, { for (i = 0; i < context->main_group->n_entries; i++) { + entry = &context->main_group->entries[i]; g_hash_table_insert (shadow_map, - (gpointer)context->main_group->entries[i].long_name, - context->main_group->entries + i); + (gpointer)entry->long_name, + entry); - if (seen[(guchar)context->main_group->entries[i].short_name]) - context->main_group->entries[i].short_name = 0; + if (seen[(guchar)entry->short_name]) + entry->short_name = 0; else - seen[(guchar)context->main_group->entries[i].short_name] = TRUE; + seen[(guchar)entry->short_name] = TRUE; } } @@ -429,15 +460,16 @@ print_help (GOptionContext *context, GOptionGroup *group = list->data; for (i = 0; i < group->n_entries; i++) { - if (g_hash_table_lookup (shadow_map, group->entries[i].long_name)) - group->entries[i].long_name = g_strdup_printf ("%s-%s", group->name, group->entries[i].long_name); + entry = &group->entries[i]; + if (g_hash_table_lookup (shadow_map, entry->long_name)) + entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name); else - g_hash_table_insert (shadow_map, (gpointer)group->entries[i].long_name, group->entries + i); + g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry); - if (seen[(guchar)group->entries[i].short_name]) - group->entries[i].short_name = 0; + if (seen[(guchar)entry->short_name]) + entry->short_name = 0; else - seen[(guchar)group->entries[i].short_name] = TRUE; + seen[(guchar)entry->short_name] = TRUE; } list = list->next; } @@ -446,7 +478,7 @@ print_help (GOptionContext *context, list = context->groups; - max_length = g_utf8_strlen ("--help, -?", -1); + max_length = g_utf8_strlen ("-?, --help", -1); if (list) { @@ -454,6 +486,12 @@ print_help (GOptionContext *context, max_length = MAX (max_length, len); } + if (context->main_group) + { + len = calculate_max_length (context->main_group); + max_length = MAX (max_length, len); + } + while (list != NULL) { GOptionGroup *group = list->data; @@ -463,22 +501,8 @@ print_help (GOptionContext *context, max_length = MAX (max_length, len); /* Then we go through the entries */ - for (i = 0; i < group->n_entries; i++) - { - if (group->entries[i].flags & G_OPTION_FLAG_HIDDEN) - continue; - - len = g_utf8_strlen (group->entries[i].long_name, -1); - - if (group->entries[i].short_name) - len += 4; - - if (group->entries[i].arg != G_OPTION_ARG_NONE && - group->entries[i].arg_description) - len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1); - - max_length = MAX (max_length, len); - } + len = calculate_max_length (group); + max_length = MAX (max_length, len); list = list->next; } @@ -490,18 +514,21 @@ print_help (GOptionContext *context, { list = context->groups; - g_print ("%s\n --%-*s %s\n", - _("Help Options:"), max_length, "help", _("Show help options")); + g_print ("%s\n -%c, --%-*s %s\n", + _("Help Options:"), '?', max_length - 4, "help", + _("Show help options")); /* We only want --help-all when there are groups */ if (list) - g_print (" --%-*s %s\n", max_length, "help-all", _("Show all help options")); - + g_print (" --%-*s %s\n", max_length, "help-all", + _("Show all help options")); + while (list) { GOptionGroup *group = list->data; - g_print (" --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description)); + g_print (" --help-%-*s %s\n", max_length - 5, group->name, + TRANSLATE (group, group->help_description)); list = list->next; } @@ -548,7 +575,8 @@ print_help (GOptionContext *context, if (context->main_group) for (i = 0; i < context->main_group->n_entries; i++) - print_entry (context->main_group, max_length, &context->main_group->entries[i]); + print_entry (context->main_group, max_length, + &context->main_group->entries[i]); while (list != NULL) { @@ -1244,6 +1272,9 @@ g_option_context_parse (GOptionContext *context, for (j = 0; j < strlen (arg); j++) { + if (context->help_enabled && arg[j] == '?') + print_help (context, TRUE, NULL); + parsed = FALSE; if (context->main_group &&