return len;
}
-
-GQuark
-g_option_error_quark (void)
-{
- return g_quark_from_static_string ("g-option-context-error-quark");
-}
+G_DEFINE_QUARK (g-option-context-error-quark, g_option_error)
/**
* g_option_context_new:
- * @parameter_string: a string which is displayed in
+ * @parameter_string: (allow-none): a string which is displayed in
* the first line of <option>--help</option> output, after the
* usage summary
* <literal><replaceable>programname</replaceable> [OPTION...]</literal>
}
static gint
-calculate_max_length (GOptionGroup *group)
+calculate_max_length (GOptionGroup *group,
+ GHashTable *aliases)
{
GOptionEntry *entry;
gint i, len, max_length;
+ const gchar *long_name;
max_length = 0;
if (entry->flags & G_OPTION_FLAG_HIDDEN)
continue;
- len = _g_utf8_strwidth (entry->long_name);
+ long_name = g_hash_table_lookup (aliases, &entry->long_name);
+ if (!long_name)
+ long_name = entry->long_name;
+ len = _g_utf8_strwidth (long_name);
if (entry->short_name)
len += 4;
print_entry (GOptionGroup *group,
gint max_length,
const GOptionEntry *entry,
- GString *string)
+ GString *string,
+ GHashTable *aliases)
{
GString *str;
+ const gchar *long_name;
if (entry->flags & G_OPTION_FLAG_HIDDEN)
return;
if (entry->long_name[0] == 0)
return;
+ long_name = g_hash_table_lookup (aliases, &entry->long_name);
+ if (!long_name)
+ long_name = entry->long_name;
+
str = g_string_new (NULL);
if (entry->short_name)
- g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
+ g_string_append_printf (str, " -%c, --%s", entry->short_name, long_name);
else
- g_string_append_printf (str, " --%s", entry->long_name);
+ g_string_append_printf (str, " --%s", long_name);
if (entry->arg_description)
g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
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;
}
}
static gboolean
-group_list_has_visible_entires (GOptionContext *context,
+group_list_has_visible_entries (GOptionContext *context,
GList *group_list,
gboolean main_entries)
{
GOptionGroup *group)
{
GList *list;
- gint max_length, len;
+ gint max_length = 0, len;
gint i;
GOptionEntry *entry;
GHashTable *shadow_map;
+ GHashTable *aliases;
gboolean seen[256];
const gchar *rest_description;
GString *string;
memset (seen, 0, sizeof (gboolean) * 256);
shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
+ aliases = g_hash_table_new_full (NULL, NULL, NULL, g_free);
if (context->main_group)
{
entry = &g->entries[i];
if (g_hash_table_lookup (shadow_map, entry->long_name) &&
!(entry->flags & G_OPTION_FLAG_NOALIAS))
- entry->long_name = g_strdup_printf ("%s-%s", g->name, entry->long_name);
+ {
+ g_hash_table_insert (aliases, &entry->long_name,
+ g_strdup_printf ("%s-%s", g->name, entry->long_name));
+ }
else
g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
list = context->groups;
- max_length = _g_utf8_strwidth ("-?, --help");
-
- if (list)
+ if (context->help_enabled)
{
- len = _g_utf8_strwidth ("--help-all");
- max_length = MAX (max_length, len);
+ max_length = _g_utf8_strwidth ("-?, --help");
+
+ if (list)
+ {
+ len = _g_utf8_strwidth ("--help-all");
+ max_length = MAX (max_length, len);
+ }
}
if (context->main_group)
{
- len = calculate_max_length (context->main_group);
+ len = calculate_max_length (context->main_group, aliases);
max_length = MAX (max_length, len);
}
{
GOptionGroup *g = list->data;
- /* 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);
+ len = calculate_max_length (g, aliases);
max_length = MAX (max_length, len);
list = list->next;
/* Add a bit of padding */
max_length += 4;
- if (!group)
+ if (!group && context->help_enabled)
{
list = context->groups;
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);
+ print_entry (group, max_length, &group->entries[i], string, aliases);
g_string_append (string, "\n");
}
}
g_string_append (string, "\n");
for (i = 0; i < g->n_entries; i++)
if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
- print_entry (g, max_length, &g->entries[i], string);
+ print_entry (g, max_length, &g->entries[i], string, aliases);
g_string_append (string, "\n");
}
/* Print application options if --help or --help-all has been specified */
if ((main_help || !group) &&
(group_has_visible_entries (context, context->main_group, TRUE) ||
- group_list_has_visible_entires (context, context->groups, TRUE)))
+ group_list_has_visible_entries (context, context->groups, TRUE)))
{
list = context->groups;
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], string);
+ &context->main_group->entries[i], string, aliases);
while (list != NULL)
{
/* Print main entries from other groups */
for (i = 0; i < g->n_entries; i++)
if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
- print_entry (g, max_length, &g->entries[i], string);
+ print_entry (g, max_length, &g->entries[i], string, aliases);
list = list->next;
}
g_string_append (string, "\n");
}
+ g_hash_table_destroy (aliases);
+
return g_string_free (string, FALSE);
}
cmdline = (char **) realloc (cmdline, len);
- if (sysctl (mib, nitems (mib), cmdline, &len, NULL, 0) == -1)
+ if (sysctl (mib, G_N_ELEMENTS (mib), cmdline, &len, NULL, 0) == -1)
{
g_free (cmdline);
return NULL;