1 /* goption.c - Option parser
3 * Copyright (C) 1999, 2003 Red Hat Software
4 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
34 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
36 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
37 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
38 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
40 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
41 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
73 struct _GOptionContext
77 gchar *parameter_string;
81 GTranslateFunc translate_func;
82 GDestroyNotify translate_notify;
83 gpointer translate_data;
85 guint help_enabled : 1;
86 guint ignore_unknown : 1;
88 GOptionGroup *main_group;
90 /* We keep a list of change so we can revert them */
93 /* We also keep track of all argv elements
94 * that should be NULLed or modified.
103 gchar *help_description;
105 GDestroyNotify destroy_notify;
108 GTranslateFunc translate_func;
109 GDestroyNotify translate_notify;
110 gpointer translate_data;
112 GOptionEntry *entries;
115 GOptionParseFunc pre_parse_func;
116 GOptionParseFunc post_parse_func;
117 GOptionErrorFunc error_func;
120 static void free_changes_list (GOptionContext *context,
122 static void free_pending_nulls (GOptionContext *context,
123 gboolean perform_nulls);
127 _g_unichar_get_width (gunichar c)
129 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
132 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
133 * some locales (legacy East Asian ones) */
134 if (g_unichar_iswide (c))
141 _g_utf8_strwidth (const gchar *p,
145 const gchar *start = p;
146 g_return_val_if_fail (p != NULL || max == 0, 0);
152 len += _g_unichar_get_width (g_utf8_get_char (p));
153 p = g_utf8_next_char (p);
161 /* this case may not be quite correct */
163 len += _g_unichar_get_width (g_utf8_get_char (p));
164 p = g_utf8_next_char (p);
166 while (p - start < max && *p)
168 len += _g_unichar_get_width (g_utf8_get_char (p));
169 p = g_utf8_next_char (p);
178 g_option_error_quark (void)
180 return g_quark_from_static_string ("g-option-context-error-quark");
184 * g_option_context_new:
185 * @parameter_string: a string which is displayed in
186 * the first line of <option>--help</option> output, after the
188 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
190 * Creates a new option context.
192 * The @parameter_string can serve multiple purposes. It can be used
193 * to add descriptions for "rest" arguments, which are not parsed by
194 * the #GOptionContext, typically something like "FILES" or
195 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
196 * collecting "rest" arguments, GLib handles this automatically by
197 * using the @arg_description of the corresponding #GOptionEntry in
200 * Another usage is to give a short summary of the program
201 * functionality, like " - frob the strings", which will be displayed
202 * in the same line as the usage. For a longer description of the
203 * program functionality that should be displayed as a paragraph
204 * below the usage line, use g_option_context_set_summary().
206 * Note that the @parameter_string is translated (see
207 * g_option_context_set_translate_func()).
209 * Returns: a newly created #GOptionContext, which must be
210 * freed with g_option_context_free() after use.
215 g_option_context_new (const gchar *parameter_string)
218 GOptionContext *context;
220 context = g_new0 (GOptionContext, 1);
222 context->parameter_string = g_strdup (parameter_string);
223 context->help_enabled = TRUE;
224 context->ignore_unknown = FALSE;
230 * g_option_context_free:
231 * @context: a #GOptionContext
233 * Frees context and all the groups which have been
238 void g_option_context_free (GOptionContext *context)
240 g_return_if_fail (context != NULL);
242 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
243 g_list_free (context->groups);
245 if (context->main_group)
246 g_option_group_free (context->main_group);
248 free_changes_list (context, FALSE);
249 free_pending_nulls (context, FALSE);
251 g_free (context->parameter_string);
252 g_free (context->summary);
253 g_free (context->description);
255 if (context->translate_notify)
256 (* context->translate_notify) (context->translate_data);
263 * g_option_context_set_help_enabled:
264 * @context: a #GOptionContext
265 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
267 * Enables or disables automatic generation of <option>--help</option>
268 * output. By default, g_option_context_parse() recognizes
269 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
270 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
271 * suitable output to stdout.
275 void g_option_context_set_help_enabled (GOptionContext *context,
276 gboolean help_enabled)
279 g_return_if_fail (context != NULL);
281 context->help_enabled = help_enabled;
285 * g_option_context_get_help_enabled:
286 * @context: a #GOptionContext
288 * Returns whether automatic <option>--help</option> generation
289 * is turned on for @context. See g_option_context_set_help_enabled().
291 * Returns: %TRUE if automatic help generation is turned on.
296 g_option_context_get_help_enabled (GOptionContext *context)
298 g_return_val_if_fail (context != NULL, FALSE);
300 return context->help_enabled;
304 * g_option_context_set_ignore_unknown_options:
305 * @context: a #GOptionContext
306 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
307 * an error when unknown options are met
309 * Sets whether to ignore unknown options or not. If an argument is
310 * ignored, it is left in the @argv array after parsing. By default,
311 * g_option_context_parse() treats unknown options as error.
313 * This setting does not affect non-option arguments (i.e. arguments
314 * which don't start with a dash). But note that GOption cannot reliably
315 * determine whether a non-option belongs to a preceding unknown option.
320 g_option_context_set_ignore_unknown_options (GOptionContext *context,
321 gboolean ignore_unknown)
323 g_return_if_fail (context != NULL);
325 context->ignore_unknown = ignore_unknown;
329 * g_option_context_get_ignore_unknown_options:
330 * @context: a #GOptionContext
332 * Returns whether unknown options are ignored or not. See
333 * g_option_context_set_ignore_unknown_options().
335 * Returns: %TRUE if unknown options are ignored.
340 g_option_context_get_ignore_unknown_options (GOptionContext *context)
342 g_return_val_if_fail (context != NULL, FALSE);
344 return context->ignore_unknown;
348 * g_option_context_add_group:
349 * @context: a #GOptionContext
350 * @group: the group to add
352 * Adds a #GOptionGroup to the @context, so that parsing with @context
353 * will recognize the options in the group. Note that the group will
354 * be freed together with the context when g_option_context_free() is
355 * called, so you must not free the group yourself after adding it
361 g_option_context_add_group (GOptionContext *context,
366 g_return_if_fail (context != NULL);
367 g_return_if_fail (group != NULL);
368 g_return_if_fail (group->name != NULL);
369 g_return_if_fail (group->description != NULL);
370 g_return_if_fail (group->help_description != NULL);
372 for (list = context->groups; list; list = list->next)
374 GOptionGroup *g = (GOptionGroup *)list->data;
376 if ((group->name == NULL && g->name == NULL) ||
377 (group->name && g->name && strcmp (group->name, g->name) == 0))
378 g_warning ("A group named \"%s\" is already part of this GOptionContext",
382 context->groups = g_list_append (context->groups, group);
386 * g_option_context_set_main_group:
387 * @context: a #GOptionContext
388 * @group: the group to set as main group
390 * Sets a #GOptionGroup as main group of the @context.
391 * This has the same effect as calling g_option_context_add_group(),
392 * the only difference is that the options in the main group are
393 * treated differently when generating <option>--help</option> output.
398 g_option_context_set_main_group (GOptionContext *context,
401 g_return_if_fail (context != NULL);
402 g_return_if_fail (group != NULL);
404 if (context->main_group)
406 g_warning ("This GOptionContext already has a main group");
411 context->main_group = group;
415 * g_option_context_get_main_group:
416 * @context: a #GOptionContext
418 * Returns a pointer to the main group of @context.
420 * Return value: the main group of @context, or %NULL if @context doesn't
421 * have a main group. Note that group belongs to @context and should
422 * not be modified or freed.
427 g_option_context_get_main_group (GOptionContext *context)
429 g_return_val_if_fail (context != NULL, NULL);
431 return context->main_group;
435 * g_option_context_add_main_entries:
436 * @context: a #GOptionContext
437 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
438 * @translation_domain: a translation domain to use for translating
439 * the <option>--help</option> output for the options in @entries
440 * with gettext(), or %NULL
442 * A convenience function which creates a main group if it doesn't
443 * exist, adds the @entries to it and sets the translation domain.
448 g_option_context_add_main_entries (GOptionContext *context,
449 const GOptionEntry *entries,
450 const gchar *translation_domain)
452 g_return_if_fail (entries != NULL);
454 if (!context->main_group)
455 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
457 g_option_group_add_entries (context->main_group, entries);
458 g_option_group_set_translation_domain (context->main_group, translation_domain);
462 calculate_max_length (GOptionGroup *group)
465 gint i, len, max_length;
469 for (i = 0; i < group->n_entries; i++)
471 entry = &group->entries[i];
473 if (entry->flags & G_OPTION_FLAG_HIDDEN)
476 len = _g_utf8_strwidth (entry->long_name, -1);
478 if (entry->short_name)
481 if (!NO_ARG (entry) && entry->arg_description)
482 len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description), -1);
484 max_length = MAX (max_length, len);
491 print_entry (GOptionGroup *group,
493 const GOptionEntry *entry)
497 if (entry->flags & G_OPTION_FLAG_HIDDEN)
500 if (entry->long_name[0] == 0)
503 str = g_string_new (NULL);
505 if (entry->short_name)
506 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
508 g_string_append_printf (str, " --%s", entry->long_name);
510 if (entry->arg_description)
511 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
513 g_print ("%s%*s %s\n", str->str,
514 (int) (max_length + 4 - _g_utf8_strwidth (str->str, -1)), "",
515 entry->description ? TRANSLATE (group, entry->description) : "");
516 g_string_free (str, TRUE);
520 print_help (GOptionContext *context,
525 gint max_length, len;
528 GHashTable *shadow_map;
530 const gchar *rest_description;
532 rest_description = NULL;
533 if (context->main_group)
536 for (i = 0; i < context->main_group->n_entries; i++)
538 entry = &context->main_group->entries[i];
539 if (entry->long_name[0] == 0)
541 rest_description = TRANSLATE (context->main_group, entry->arg_description);
547 g_print ("%s\n %s %s%s%s%s%s\n\n",
548 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
549 rest_description ? " " : "",
550 rest_description ? rest_description : "",
551 context->parameter_string ? " " : "",
552 context->parameter_string ? TRANSLATE (context, context->parameter_string) : "");
554 if (context->summary)
555 g_print ("%s\n\n", TRANSLATE (context, context->summary));
557 memset (seen, 0, sizeof (gboolean) * 256);
558 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
560 if (context->main_group)
562 for (i = 0; i < context->main_group->n_entries; i++)
564 entry = &context->main_group->entries[i];
565 g_hash_table_insert (shadow_map,
566 (gpointer)entry->long_name,
569 if (seen[(guchar)entry->short_name])
570 entry->short_name = 0;
572 seen[(guchar)entry->short_name] = TRUE;
576 list = context->groups;
579 GOptionGroup *group = list->data;
580 for (i = 0; i < group->n_entries; i++)
582 entry = &group->entries[i];
583 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
584 !(entry->flags & G_OPTION_FLAG_NOALIAS))
585 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
587 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
589 if (seen[(guchar)entry->short_name] &&
590 !(entry->flags & G_OPTION_FLAG_NOALIAS))
591 entry->short_name = 0;
593 seen[(guchar)entry->short_name] = TRUE;
598 g_hash_table_destroy (shadow_map);
600 list = context->groups;
602 max_length = _g_utf8_strwidth ("-?, --help", -1);
606 len = _g_utf8_strwidth ("--help-all", -1);
607 max_length = MAX (max_length, len);
610 if (context->main_group)
612 len = calculate_max_length (context->main_group);
613 max_length = MAX (max_length, len);
618 GOptionGroup *group = list->data;
620 /* First, we check the --help-<groupname> options */
621 len = _g_utf8_strwidth ("--help-", -1) + _g_utf8_strwidth (group->name, -1);
622 max_length = MAX (max_length, len);
624 /* Then we go through the entries */
625 len = calculate_max_length (group);
626 max_length = MAX (max_length, len);
631 /* Add a bit of padding */
636 list = context->groups;
638 g_print ("%s\n -%c, --%-*s %s\n",
639 _("Help Options:"), '?', max_length - 4, "help",
640 _("Show help options"));
642 /* We only want --help-all when there are groups */
644 g_print (" --%-*s %s\n", max_length, "help-all",
645 _("Show all help options"));
649 GOptionGroup *group = list->data;
651 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
652 TRANSLATE (group, group->help_description));
662 /* Print a certain group */
664 g_print ("%s\n", TRANSLATE (group, group->description));
665 for (i = 0; i < group->n_entries; i++)
666 print_entry (group, max_length, &group->entries[i]);
671 /* Print all groups */
673 list = context->groups;
677 GOptionGroup *group = list->data;
679 g_print ("%s\n", group->description);
681 for (i = 0; i < group->n_entries; i++)
682 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
683 print_entry (group, max_length, &group->entries[i]);
690 /* Print application options if --help or --help-all has been specified */
691 if (main_help || !group)
693 list = context->groups;
695 g_print ("%s\n", _("Application Options:"));
697 if (context->main_group)
698 for (i = 0; i < context->main_group->n_entries; i++)
699 print_entry (context->main_group, max_length,
700 &context->main_group->entries[i]);
704 GOptionGroup *group = list->data;
706 /* Print main entries from other groups */
707 for (i = 0; i < group->n_entries; i++)
708 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
709 print_entry (group, max_length, &group->entries[i]);
717 if (context->description)
718 g_print ("%s\n", TRANSLATE (context, context->description));
724 parse_int (const gchar *arg_name,
733 tmp = strtol (arg, &end, 0);
735 if (*arg == '\0' || *end != '\0')
738 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
739 _("Cannot parse integer value '%s' for %s"),
745 if (*result != tmp || errno == ERANGE)
748 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
749 _("Integer value '%s' for %s out of range"),
759 parse_double (const gchar *arg_name,
768 tmp = g_strtod (arg, &end);
770 if (*arg == '\0' || *end != '\0')
773 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
774 _("Cannot parse double value '%s' for %s"),
781 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
782 _("Double value '%s' for %s out of range"),
794 parse_int64 (const gchar *arg_name,
803 tmp = g_ascii_strtoll (arg, &end, 0);
805 if (*arg == '\0' || *end != '\0')
808 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
809 _("Cannot parse integer value '%s' for %s"),
816 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
817 _("Integer value '%s' for %s out of range"),
829 get_change (GOptionContext *context,
834 Change *change = NULL;
836 for (list = context->changes; list != NULL; list = list->next)
840 if (change->arg_data == arg_data)
844 change = g_new0 (Change, 1);
845 change->arg_type = arg_type;
846 change->arg_data = arg_data;
848 context->changes = g_list_prepend (context->changes, change);
856 add_pending_null (GOptionContext *context,
862 n = g_new0 (PendingNull, 1);
866 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
870 parse_arg (GOptionContext *context,
874 const gchar *option_name,
880 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
884 case G_OPTION_ARG_NONE:
886 change = get_change (context, G_OPTION_ARG_NONE,
889 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
892 case G_OPTION_ARG_STRING:
896 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
901 change = get_change (context, G_OPTION_ARG_STRING,
903 g_free (change->allocated.str);
905 change->prev.str = *(gchar **)entry->arg_data;
906 change->allocated.str = data;
908 *(gchar **)entry->arg_data = data;
911 case G_OPTION_ARG_STRING_ARRAY:
915 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
920 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
923 if (change->allocated.array.len == 0)
925 change->prev.array = *(gchar ***)entry->arg_data;
926 change->allocated.array.data = g_new (gchar *, 2);
929 change->allocated.array.data =
930 g_renew (gchar *, change->allocated.array.data,
931 change->allocated.array.len + 2);
933 change->allocated.array.data[change->allocated.array.len] = data;
934 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
936 change->allocated.array.len ++;
938 *(gchar ***)entry->arg_data = change->allocated.array.data;
943 case G_OPTION_ARG_FILENAME:
948 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
953 data = g_strdup (value);
955 change = get_change (context, G_OPTION_ARG_FILENAME,
957 g_free (change->allocated.str);
959 change->prev.str = *(gchar **)entry->arg_data;
960 change->allocated.str = data;
962 *(gchar **)entry->arg_data = data;
966 case G_OPTION_ARG_FILENAME_ARRAY:
971 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
976 data = g_strdup (value);
978 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
981 if (change->allocated.array.len == 0)
983 change->prev.array = *(gchar ***)entry->arg_data;
984 change->allocated.array.data = g_new (gchar *, 2);
987 change->allocated.array.data =
988 g_renew (gchar *, change->allocated.array.data,
989 change->allocated.array.len + 2);
991 change->allocated.array.data[change->allocated.array.len] = data;
992 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
994 change->allocated.array.len ++;
996 *(gchar ***)entry->arg_data = change->allocated.array.data;
1001 case G_OPTION_ARG_INT:
1005 if (!parse_int (option_name, value,
1010 change = get_change (context, G_OPTION_ARG_INT,
1012 change->prev.integer = *(gint *)entry->arg_data;
1013 *(gint *)entry->arg_data = data;
1016 case G_OPTION_ARG_CALLBACK:
1021 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1023 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1025 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1028 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1030 data = g_strdup (value);
1034 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1036 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1040 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1048 case G_OPTION_ARG_DOUBLE:
1052 if (!parse_double (option_name, value,
1059 change = get_change (context, G_OPTION_ARG_DOUBLE,
1061 change->prev.dbl = *(gdouble *)entry->arg_data;
1062 *(gdouble *)entry->arg_data = data;
1065 case G_OPTION_ARG_INT64:
1069 if (!parse_int64 (option_name, value,
1076 change = get_change (context, G_OPTION_ARG_INT64,
1078 change->prev.int64 = *(gint64 *)entry->arg_data;
1079 *(gint64 *)entry->arg_data = data;
1083 g_assert_not_reached ();
1090 parse_short_option (GOptionContext *context,
1091 GOptionGroup *group,
1102 for (j = 0; j < group->n_entries; j++)
1104 if (arg == group->entries[j].short_name)
1107 gchar *value = NULL;
1109 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1111 if (NO_ARG (&group->entries[j]))
1115 if (*new_index > index)
1118 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1119 _("Error parsing option %s"), option_name);
1120 g_free (option_name);
1124 if (index < *argc - 1)
1126 if (!OPTIONAL_ARG (&group->entries[j]))
1128 value = (*argv)[index + 1];
1129 add_pending_null (context, &((*argv)[index + 1]), NULL);
1130 *new_index = index+1;
1134 if ((*argv)[index + 1][0] == '-')
1138 value = (*argv)[index + 1];
1139 add_pending_null (context, &((*argv)[index + 1]), NULL);
1140 *new_index = index + 1;
1144 else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1149 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1150 _("Missing argument for %s"), option_name);
1151 g_free (option_name);
1156 if (!parse_arg (context, group, &group->entries[j],
1157 value, option_name, error))
1159 g_free (option_name);
1163 g_free (option_name);
1172 parse_long_option (GOptionContext *context,
1173 GOptionGroup *group,
1184 for (j = 0; j < group->n_entries; j++)
1186 if (*index >= *argc)
1189 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1192 if (NO_ARG (&group->entries[j]) &&
1193 strcmp (arg, group->entries[j].long_name) == 0)
1197 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1198 parse_arg (context, group, &group->entries[j],
1199 NULL, option_name, error);
1200 g_free(option_name);
1202 add_pending_null (context, &((*argv)[*index]), NULL);
1207 gint len = strlen (group->entries[j].long_name);
1209 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1210 (arg[len] == '=' || arg[len] == 0))
1212 gchar *value = NULL;
1215 add_pending_null (context, &((*argv)[*index]), NULL);
1216 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1218 if (arg[len] == '=')
1219 value = arg + len + 1;
1220 else if (*index < *argc - 1)
1222 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1224 value = (*argv)[*index + 1];
1225 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1230 if ((*argv)[*index + 1][0] == '-')
1233 retval = parse_arg (context, group, &group->entries[j],
1234 NULL, option_name, error);
1236 g_free (option_name);
1241 value = (*argv)[*index + 1];
1242 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1247 else if (*index >= *argc - 1 &&
1248 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1251 retval = parse_arg (context, group, &group->entries[j],
1252 NULL, option_name, error);
1254 g_free (option_name);
1260 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1261 _("Missing argument for %s"), option_name);
1262 g_free (option_name);
1266 if (!parse_arg (context, group, &group->entries[j],
1267 value, option_name, error))
1269 g_free (option_name);
1273 g_free (option_name);
1283 parse_remaining_arg (GOptionContext *context,
1284 GOptionGroup *group,
1293 for (j = 0; j < group->n_entries; j++)
1295 if (*index >= *argc)
1298 if (group->entries[j].long_name[0])
1301 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1302 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1304 add_pending_null (context, &((*argv)[*index]), NULL);
1306 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1317 free_changes_list (GOptionContext *context,
1322 for (list = context->changes; list != NULL; list = list->next)
1324 Change *change = list->data;
1328 switch (change->arg_type)
1330 case G_OPTION_ARG_NONE:
1331 *(gboolean *)change->arg_data = change->prev.bool;
1333 case G_OPTION_ARG_INT:
1334 *(gint *)change->arg_data = change->prev.integer;
1336 case G_OPTION_ARG_STRING:
1337 case G_OPTION_ARG_FILENAME:
1338 g_free (change->allocated.str);
1339 *(gchar **)change->arg_data = change->prev.str;
1341 case G_OPTION_ARG_STRING_ARRAY:
1342 case G_OPTION_ARG_FILENAME_ARRAY:
1343 g_strfreev (change->allocated.array.data);
1344 *(gchar ***)change->arg_data = change->prev.array;
1346 case G_OPTION_ARG_DOUBLE:
1347 *(gdouble *)change->arg_data = change->prev.dbl;
1349 case G_OPTION_ARG_INT64:
1350 *(gint64 *)change->arg_data = change->prev.int64;
1353 g_assert_not_reached ();
1360 g_list_free (context->changes);
1361 context->changes = NULL;
1365 free_pending_nulls (GOptionContext *context,
1366 gboolean perform_nulls)
1370 for (list = context->pending_nulls; list != NULL; list = list->next)
1372 PendingNull *n = list->data;
1378 /* Copy back the short options */
1380 strcpy (*n->ptr + 1, n->value);
1390 g_list_free (context->pending_nulls);
1391 context->pending_nulls = NULL;
1395 * g_option_context_parse:
1396 * @context: a #GOptionContext
1397 * @argc: a pointer to the number of command line arguments
1398 * @argv: a pointer to the array of command line arguments
1399 * @error: a return location for errors
1401 * Parses the command line arguments, recognizing options
1402 * which have been added to @context. A side-effect of
1403 * calling this function is that g_set_prgname() will be
1406 * If the parsing is successful, any parsed arguments are
1407 * removed from the array and @argc and @argv are updated
1408 * accordingly. A '--' option is stripped from @argv
1409 * unless there are unparsed options before and after it,
1410 * or some of the options after it start with '-'. In case
1411 * of an error, @argc and @argv are left unmodified.
1413 * If automatic <option>--help</option> support is enabled
1414 * (see g_option_context_set_help_enabled()), and the
1415 * @argv array contains one of the recognized help options,
1416 * this function will produce help output to stdout and
1417 * call <literal>exit (0)</literal>.
1419 * Note that function depends on the
1420 * <link linkend="setlocale">current locale</link> for
1421 * automatic character set conversion of string and filename
1424 * Return value: %TRUE if the parsing was successful,
1425 * %FALSE if an error occurred
1430 g_option_context_parse (GOptionContext *context,
1438 /* Set program name */
1439 if (!g_get_prgname())
1441 if (argc && argv && *argc)
1445 prgname = g_path_get_basename ((*argv)[0]);
1446 g_set_prgname (prgname);
1450 g_set_prgname ("<unknown>");
1453 /* Call pre-parse hooks */
1454 list = context->groups;
1457 GOptionGroup *group = list->data;
1459 if (group->pre_parse_func)
1461 if (!(* group->pre_parse_func) (context, group,
1462 group->user_data, error))
1469 if (context->main_group && context->main_group->pre_parse_func)
1471 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1472 context->main_group->user_data, error))
1478 gboolean stop_parsing = FALSE;
1479 gboolean has_unknown = FALSE;
1480 gint separator_pos = 0;
1482 for (i = 1; i < *argc; i++)
1485 gboolean parsed = FALSE;
1487 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1489 if ((*argv)[i][1] == '-')
1493 arg = (*argv)[i] + 2;
1495 /* '--' terminates list of arguments */
1499 stop_parsing = TRUE;
1503 /* Handle help options */
1504 if (context->help_enabled)
1506 if (strcmp (arg, "help") == 0)
1507 print_help (context, TRUE, NULL);
1508 else if (strcmp (arg, "help-all") == 0)
1509 print_help (context, FALSE, NULL);
1510 else if (strncmp (arg, "help-", 5) == 0)
1514 list = context->groups;
1518 GOptionGroup *group = list->data;
1520 if (strcmp (arg + 5, group->name) == 0)
1521 print_help (context, FALSE, group);
1528 if (context->main_group &&
1529 !parse_long_option (context, context->main_group, &i, arg,
1530 FALSE, argc, argv, error, &parsed))
1536 /* Try the groups */
1537 list = context->groups;
1540 GOptionGroup *group = list->data;
1542 if (!parse_long_option (context, group, &i, arg,
1543 FALSE, argc, argv, error, &parsed))
1555 /* Now look for --<group>-<option> */
1556 dash = strchr (arg, '-');
1559 /* Try the groups */
1560 list = context->groups;
1563 GOptionGroup *group = list->data;
1565 if (strncmp (group->name, arg, dash - arg) == 0)
1567 if (!parse_long_option (context, group, &i, dash + 1,
1568 TRUE, argc, argv, error, &parsed))
1579 if (context->ignore_unknown)
1583 { /* short option */
1584 gint j, new_i = i, arg_length;
1585 gboolean *nulled_out = NULL;
1586 arg = (*argv)[i] + 1;
1587 arg_length = strlen (arg);
1588 nulled_out = g_newa (gboolean, arg_length);
1589 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1590 for (j = 0; j < arg_length; j++)
1592 if (context->help_enabled && arg[j] == '?')
1593 print_help (context, TRUE, NULL);
1595 if (context->main_group &&
1596 !parse_short_option (context, context->main_group,
1598 argc, argv, error, &parsed))
1602 /* Try the groups */
1603 list = context->groups;
1606 GOptionGroup *group = list->data;
1607 if (!parse_short_option (context, group, i, &new_i, arg[j],
1608 argc, argv, error, &parsed))
1616 if (context->ignore_unknown && parsed)
1617 nulled_out[j] = TRUE;
1618 else if (context->ignore_unknown)
1622 /* !context->ignore_unknown && parsed */
1624 if (context->ignore_unknown)
1626 gchar *new_arg = NULL;
1628 for (j = 0; j < arg_length; j++)
1633 new_arg = g_malloc (arg_length + 1);
1634 new_arg[arg_index++] = arg[j];
1638 new_arg[arg_index] = '\0';
1639 add_pending_null (context, &((*argv)[i]), new_arg);
1643 add_pending_null (context, &((*argv)[i]), NULL);
1651 if (!parsed && !context->ignore_unknown)
1654 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1655 _("Unknown option %s"), (*argv)[i]);
1661 /* Collect remaining args */
1662 if (context->main_group &&
1663 !parse_remaining_arg (context, context->main_group, &i,
1664 argc, argv, error, &parsed))
1667 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1672 if (separator_pos > 0)
1673 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1677 /* Call post-parse hooks */
1678 list = context->groups;
1681 GOptionGroup *group = list->data;
1683 if (group->post_parse_func)
1685 if (!(* group->post_parse_func) (context, group,
1686 group->user_data, error))
1693 if (context->main_group && context->main_group->post_parse_func)
1695 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1696 context->main_group->user_data, error))
1702 free_pending_nulls (context, TRUE);
1704 for (i = 1; i < *argc; i++)
1706 for (k = i; k < *argc; k++)
1707 if ((*argv)[k] != NULL)
1713 for (j = i + k; j < *argc; j++)
1715 (*argv)[j-k] = (*argv)[j];
1727 /* Call error hooks */
1728 list = context->groups;
1731 GOptionGroup *group = list->data;
1733 if (group->error_func)
1734 (* group->error_func) (context, group,
1735 group->user_data, error);
1740 if (context->main_group && context->main_group->error_func)
1741 (* context->main_group->error_func) (context, context->main_group,
1742 context->main_group->user_data, error);
1744 free_changes_list (context, TRUE);
1745 free_pending_nulls (context, FALSE);
1751 * g_option_group_new:
1752 * @name: the name for the option group, this is used to provide
1753 * help for the options in this group with <option>--help-</option>@name
1754 * @description: a description for this group to be shown in
1755 * <option>--help</option>. This string is translated using the translation
1756 * domain or translation function of the group
1757 * @help_description: a description for the <option>--help-</option>@name option.
1758 * This string is translated using the translation domain or translation function
1760 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1761 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1762 * @destroy: a function that will be called to free @user_data, or %NULL
1764 * Creates a new #GOptionGroup.
1766 * Return value: a newly created option group. It should be added
1767 * to a #GOptionContext or freed with g_option_group_free().
1772 g_option_group_new (const gchar *name,
1773 const gchar *description,
1774 const gchar *help_description,
1776 GDestroyNotify destroy)
1779 GOptionGroup *group;
1781 group = g_new0 (GOptionGroup, 1);
1782 group->name = g_strdup (name);
1783 group->description = g_strdup (description);
1784 group->help_description = g_strdup (help_description);
1785 group->user_data = user_data;
1786 group->destroy_notify = destroy;
1793 * g_option_group_free:
1794 * @group: a #GOptionGroup
1796 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1797 * free groups which have been added to a #GOptionContext.
1802 g_option_group_free (GOptionGroup *group)
1804 g_return_if_fail (group != NULL);
1806 g_free (group->name);
1807 g_free (group->description);
1808 g_free (group->help_description);
1810 g_free (group->entries);
1812 if (group->destroy_notify)
1813 (* group->destroy_notify) (group->user_data);
1815 if (group->translate_notify)
1816 (* group->translate_notify) (group->translate_data);
1823 * g_option_group_add_entries:
1824 * @group: a #GOptionGroup
1825 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1827 * Adds the options specified in @entries to @group.
1832 g_option_group_add_entries (GOptionGroup *group,
1833 const GOptionEntry *entries)
1837 g_return_if_fail (entries != NULL);
1839 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1841 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1843 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1845 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1847 gchar c = group->entries[i].short_name;
1851 if (c == '-' || !g_ascii_isprint (c))
1853 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1854 group->entries[i].short_name = 0;
1859 group->n_entries += n_entries;
1863 * g_option_group_set_parse_hooks:
1864 * @group: a #GOptionGroup
1865 * @pre_parse_func: a function to call before parsing, or %NULL
1866 * @post_parse_func: a function to call after parsing, or %NULL
1868 * Associates two functions with @group which will be called
1869 * from g_option_context_parse() before the first option is parsed
1870 * and after the last option has been parsed, respectively.
1872 * Note that the user data to be passed to @pre_parse_func and
1873 * @post_parse_func can be specified when constructing the group
1874 * with g_option_group_new().
1879 g_option_group_set_parse_hooks (GOptionGroup *group,
1880 GOptionParseFunc pre_parse_func,
1881 GOptionParseFunc post_parse_func)
1883 g_return_if_fail (group != NULL);
1885 group->pre_parse_func = pre_parse_func;
1886 group->post_parse_func = post_parse_func;
1890 * g_option_group_set_error_hook:
1891 * @group: a #GOptionGroup
1892 * @error_func: a function to call when an error occurs
1894 * Associates a function with @group which will be called
1895 * from g_option_context_parse() when an error occurs.
1897 * Note that the user data to be passed to @pre_parse_func and
1898 * @post_parse_func can be specified when constructing the group
1899 * with g_option_group_new().
1904 g_option_group_set_error_hook (GOptionGroup *group,
1905 GOptionErrorFunc error_func)
1907 g_return_if_fail (group != NULL);
1909 group->error_func = error_func;
1914 * g_option_group_set_translate_func:
1915 * @group: a #GOptionGroup
1916 * @func: the #GTranslateFunc, or %NULL
1917 * @data: user data to pass to @func, or %NULL
1918 * @destroy_notify: a function which gets called to free @data, or %NULL
1920 * Sets the function which is used to translate user-visible
1921 * strings, for <option>--help</option> output. Different
1922 * groups can use different #GTranslateFunc<!-- -->s. If @func
1923 * is %NULL, strings are not translated.
1925 * If you are using gettext(), you only need to set the translation
1926 * domain, see g_option_group_set_translation_domain().
1931 g_option_group_set_translate_func (GOptionGroup *group,
1932 GTranslateFunc func,
1934 GDestroyNotify destroy_notify)
1936 g_return_if_fail (group != NULL);
1938 if (group->translate_notify)
1939 group->translate_notify (group->translate_data);
1941 group->translate_func = func;
1942 group->translate_data = data;
1943 group->translate_notify = destroy_notify;
1947 dgettext_swapped (const gchar *msgid,
1948 const gchar *domainname)
1950 return dgettext (domainname, msgid);
1954 * g_option_group_set_translation_domain:
1955 * @group: a #GOptionGroup
1956 * @domain: the domain to use
1958 * A convenience function to use gettext() for translating
1959 * user-visible strings.
1964 g_option_group_set_translation_domain (GOptionGroup *group,
1965 const gchar *domain)
1967 g_return_if_fail (group != NULL);
1969 g_option_group_set_translate_func (group,
1970 (GTranslateFunc)dgettext_swapped,
1976 * g_option_context_set_translate_func:
1977 * @context: a #GOptionContext
1978 * @func: the #GTranslateFunc, or %NULL
1979 * @data: user data to pass to @func, or %NULL
1980 * @destroy_notify: a function which gets called to free @data, or %NULL
1982 * Sets the function which is used to translate the contexts
1983 * user-visible strings, for <option>--help</option> output.
1984 * If @func is %NULL, strings are not translated.
1986 * Note that option groups have their own translation functions,
1987 * this function only affects the @parameter_string (see g_option_context_nex()),
1988 * the summary (see g_option_context_set_summary()) and the description
1989 * (see g_option_context_set_description()).
1991 * If you are using gettext(), you only need to set the translation
1992 * domain, see g_context_group_set_translation_domain().
1997 g_option_context_set_translate_func (GOptionContext *context,
1998 GTranslateFunc func,
2000 GDestroyNotify destroy_notify)
2002 g_return_if_fail (context != NULL);
2004 if (context->translate_notify)
2005 context->translate_notify (context->translate_data);
2007 context->translate_func = func;
2008 context->translate_data = data;
2009 context->translate_notify = destroy_notify;
2013 * g_option_context_set_translation_domain:
2014 * @context: a #GOptionContext
2015 * @domain: the domain to use
2017 * A convenience function to use gettext() for translating
2018 * user-visible strings.
2023 g_option_context_set_translation_domain (GOptionContext *context,
2024 const gchar *domain)
2026 g_return_if_fail (context != NULL);
2028 g_option_context_set_translate_func (context,
2029 (GTranslateFunc)dgettext_swapped,
2035 * g_option_context_set_summary:
2036 * @context: a #GOptionContext
2037 * @summary: a string to be shown in <option>--help</option> output
2038 * before the list of options, or %NULL
2040 * Adds a string to be displayed in <option>--help</option> output
2041 * before the list of options. This is typically a summary of the
2042 * program functionality.
2044 * Note that the summary is translated (see
2045 * g_option_context_set_translate_func()).
2050 g_option_context_set_summary (GOptionContext *context,
2051 const gchar *summary)
2053 g_return_if_fail (context != NULL);
2055 g_free (context->summary);
2056 context->summary = g_strdup (summary);
2061 * g_option_context_get_summary:
2062 * @context: a #GOptionContext
2064 * Returns the summary. See g_option_context_set_summary().
2066 * Returns: the summary
2070 G_CONST_RETURN gchar *
2071 g_option_context_get_summary (GOptionContext *context)
2073 g_return_val_if_fail (context != NULL, NULL);
2075 return context->summary;
2079 * g_option_context_set_description:
2080 * @context: a #GOptionContext
2081 * @description: a string to be shown in <option>--help</option> output
2082 * after the list of options, or %NULL
2084 * Adds a string to be displayed in <option>--help</option> output
2085 * after the list of options. This text often includes a bug reporting
2088 * Note that the summary is translated (see
2089 * g_option_context_set_translate_func()).
2094 g_option_context_set_description (GOptionContext *context,
2095 const gchar *description)
2097 g_return_if_fail (context != NULL);
2099 g_free (context->description);
2100 context->description = g_strdup (description);
2105 * g_option_context_get_description:
2106 * @context: a #GOptionContext
2108 * Returns the description. See g_option_context_set_description().
2110 * Returns: the description
2114 G_CONST_RETURN gchar *
2115 g_option_context_get_description (GOptionContext *context)
2117 g_return_val_if_fail (context != NULL, NULL);
2119 return context->description;
2123 #define __G_OPTION_C__
2124 #include "galiasdef.c"