Take main group options into account when calculating column size.

2005-02-10  Matthias Clasen  <mclasen@redhat.com>

	* glib/goption.c (print_help): Take main group options into
	account when calculating column size.  (#166921)
	(g_option_context_parse): Accept -? as documented.  (#166977)
This commit is contained in:
Matthias Clasen 2005-02-11 04:11:34 +00:00 committed by Matthias Clasen
parent de2763c99a
commit 1795e0aeb0
7 changed files with 96 additions and 41 deletions

View File

@ -1,5 +1,10 @@
2005-02-09 Matthias Clasen <mclasen@redhat.com> 2005-02-10 Matthias Clasen <mclasen@redhat.com>
* 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 <mclasen@redhat.com>
* glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path * glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path
here. (#166801, Kjartan Maraas) here. (#166801, Kjartan Maraas)

View File

@ -1,5 +1,10 @@
2005-02-09 Matthias Clasen <mclasen@redhat.com> 2005-02-10 Matthias Clasen <mclasen@redhat.com>
* 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 <mclasen@redhat.com>
* glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path * glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path
here. (#166801, Kjartan Maraas) here. (#166801, Kjartan Maraas)

View File

@ -1,5 +1,10 @@
2005-02-09 Matthias Clasen <mclasen@redhat.com> 2005-02-10 Matthias Clasen <mclasen@redhat.com>
* 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 <mclasen@redhat.com>
* glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path * glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path
here. (#166801, Kjartan Maraas) here. (#166801, Kjartan Maraas)

View File

@ -1,5 +1,10 @@
2005-02-09 Matthias Clasen <mclasen@redhat.com> 2005-02-10 Matthias Clasen <mclasen@redhat.com>
* 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 <mclasen@redhat.com>
* glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path * glib/gkeyfile.c (find_file_in_data_dirs): Don't leak path
here. (#166801, Kjartan Maraas) here. (#166801, Kjartan Maraas)

View File

@ -1,3 +1,7 @@
2005-02-10 Matthias Clasen <mclasen@redhat.com>
* glib/tmpl/option.sgml: Fix a typo. (#166985)
2005-02-07 Matthias Clasen <mclasen@redhat.com> 2005-02-07 Matthias Clasen <mclasen@redhat.com>
* glib/glib-sections.txt: Add g_listenv. * glib/glib-sections.txt: Add g_listenv.

View File

@ -56,7 +56,7 @@ Usage:
testtreemodel [OPTION...] - test tree model performance testtreemodel [OPTION...] - test tree model performance
Help Options: Help Options:
--help Show help options -?, --help Show help options
--help-all Show all help options --help-all Show all help options
--help-gtk Show GTK+ Options --help-gtk Show GTK+ Options
@ -299,7 +299,7 @@ g_option_context_add_main_entries() or g_option_group_add_entries().
--<replaceable>groupname</replaceable>-<replaceable>long_name</replaceable>. --<replaceable>groupname</replaceable>-<replaceable>long_name</replaceable>.
@short_name: If an option has a short name, it can be specified @short_name: If an option has a short name, it can be specified
-<replaceable>short_name</replaceable> in a commandline. -<replaceable>short_name</replaceable> in a commandline.
@flags: Flags from #GOptionEntryFlags. @flags: Flags from #GOptionFlags.
@arg: The type of the option, as a #GOptionArg. @arg: The type of the option, as a #GOptionArg.
@arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data must @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 point to a #GOptionArgFunc callback function, which will be called to handle

View File

@ -362,6 +362,35 @@ g_option_context_add_main_entries (GOptionContext *context,
g_option_group_set_translation_domain (context->main_group, translation_domain); 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 static void
print_entry (GOptionGroup *group, print_entry (GOptionGroup *group,
gint max_length, gint max_length,
@ -393,11 +422,12 @@ print_entry (GOptionGroup *group,
static void static void
print_help (GOptionContext *context, print_help (GOptionContext *context,
gboolean main_help, gboolean main_help,
GOptionGroup *group) GOptionGroup *group)
{ {
GList *list; GList *list;
gint max_length, len; gint max_length, len;
gint i; gint i;
GOptionEntry *entry;
GHashTable *shadow_map; GHashTable *shadow_map;
gboolean seen[256]; gboolean seen[256];
@ -412,14 +442,15 @@ print_help (GOptionContext *context,
{ {
for (i = 0; i < context->main_group->n_entries; i++) for (i = 0; i < context->main_group->n_entries; i++)
{ {
entry = &context->main_group->entries[i];
g_hash_table_insert (shadow_map, g_hash_table_insert (shadow_map,
(gpointer)context->main_group->entries[i].long_name, (gpointer)entry->long_name,
context->main_group->entries + i); entry);
if (seen[(guchar)context->main_group->entries[i].short_name]) if (seen[(guchar)entry->short_name])
context->main_group->entries[i].short_name = 0; entry->short_name = 0;
else 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; GOptionGroup *group = list->data;
for (i = 0; i < group->n_entries; i++) for (i = 0; i < group->n_entries; i++)
{ {
if (g_hash_table_lookup (shadow_map, group->entries[i].long_name)) entry = &group->entries[i];
group->entries[i].long_name = g_strdup_printf ("%s-%s", group->name, group->entries[i].long_name); if (g_hash_table_lookup (shadow_map, entry->long_name))
entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
else 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]) if (seen[(guchar)entry->short_name])
group->entries[i].short_name = 0; entry->short_name = 0;
else else
seen[(guchar)group->entries[i].short_name] = TRUE; seen[(guchar)entry->short_name] = TRUE;
} }
list = list->next; list = list->next;
} }
@ -446,7 +478,7 @@ print_help (GOptionContext *context,
list = context->groups; list = context->groups;
max_length = g_utf8_strlen ("--help, -?", -1); max_length = g_utf8_strlen ("-?, --help", -1);
if (list) if (list)
{ {
@ -454,6 +486,12 @@ print_help (GOptionContext *context,
max_length = MAX (max_length, len); 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) while (list != NULL)
{ {
GOptionGroup *group = list->data; GOptionGroup *group = list->data;
@ -463,22 +501,8 @@ print_help (GOptionContext *context,
max_length = MAX (max_length, len); max_length = MAX (max_length, len);
/* Then we go through the entries */ /* Then we go through the entries */
for (i = 0; i < group->n_entries; i++) len = calculate_max_length (group);
{ max_length = MAX (max_length, len);
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);
}
list = list->next; list = list->next;
} }
@ -490,18 +514,21 @@ print_help (GOptionContext *context,
{ {
list = context->groups; list = context->groups;
g_print ("%s\n --%-*s %s\n", g_print ("%s\n -%c, --%-*s %s\n",
_("Help Options:"), max_length, "help", _("Show help options")); _("Help Options:"), '?', max_length - 4, "help",
_("Show help options"));
/* We only want --help-all when there are groups */ /* We only want --help-all when there are groups */
if (list) 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) while (list)
{ {
GOptionGroup *group = list->data; 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; list = list->next;
} }
@ -548,7 +575,8 @@ print_help (GOptionContext *context,
if (context->main_group) if (context->main_group)
for (i = 0; i < context->main_group->n_entries; i++) 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) while (list != NULL)
{ {
@ -1244,6 +1272,9 @@ g_option_context_parse (GOptionContext *context,
for (j = 0; j < strlen (arg); j++) for (j = 0; j < strlen (arg); j++)
{ {
if (context->help_enabled && arg[j] == '?')
print_help (context, TRUE, NULL);
parsed = FALSE; parsed = FALSE;
if (context->main_group && if (context->main_group &&