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.
36 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
38 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
39 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
40 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
42 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
43 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
75 struct _GOptionContext
79 gchar *parameter_string;
83 GTranslateFunc translate_func;
84 GDestroyNotify translate_notify;
85 gpointer translate_data;
87 guint help_enabled : 1;
88 guint ignore_unknown : 1;
90 GOptionGroup *main_group;
92 /* We keep a list of change so we can revert them */
95 /* We also keep track of all argv elements
96 * that should be NULLed or modified.
105 gchar *help_description;
107 GDestroyNotify destroy_notify;
110 GTranslateFunc translate_func;
111 GDestroyNotify translate_notify;
112 gpointer translate_data;
114 GOptionEntry *entries;
117 GOptionParseFunc pre_parse_func;
118 GOptionParseFunc post_parse_func;
119 GOptionErrorFunc error_func;
122 static void free_changes_list (GOptionContext *context,
124 static void free_pending_nulls (GOptionContext *context,
125 gboolean perform_nulls);
129 _g_unichar_get_width (gunichar c)
131 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
134 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
135 * some locales (legacy East Asian ones) */
136 if (g_unichar_iswide (c))
143 _g_utf8_strwidth (const gchar *p,
147 const gchar *start = p;
148 g_return_val_if_fail (p != NULL || max == 0, 0);
154 len += _g_unichar_get_width (g_utf8_get_char (p));
155 p = g_utf8_next_char (p);
163 /* this case may not be quite correct */
165 len += _g_unichar_get_width (g_utf8_get_char (p));
166 p = g_utf8_next_char (p);
168 while (p - start < max && *p)
170 len += _g_unichar_get_width (g_utf8_get_char (p));
171 p = g_utf8_next_char (p);
180 g_option_error_quark (void)
182 return g_quark_from_static_string ("g-option-context-error-quark");
186 * g_option_context_new:
187 * @parameter_string: a string which is displayed in
188 * the first line of <option>--help</option> output, after the
190 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
192 * Creates a new option context.
194 * The @parameter_string can serve multiple purposes. It can be used
195 * to add descriptions for "rest" arguments, which are not parsed by
196 * the #GOptionContext, typically something like "FILES" or
197 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
198 * collecting "rest" arguments, GLib handles this automatically by
199 * using the @arg_description of the corresponding #GOptionEntry in
202 * Another usage is to give a short summary of the program
203 * functionality, like " - frob the strings", which will be displayed
204 * in the same line as the usage. For a longer description of the
205 * program functionality that should be displayed as a paragraph
206 * below the usage line, use g_option_context_set_summary().
208 * Note that the @parameter_string is translated (see
209 * g_option_context_set_translate_func()).
211 * Returns: a newly created #GOptionContext, which must be
212 * freed with g_option_context_free() after use.
217 g_option_context_new (const gchar *parameter_string)
220 GOptionContext *context;
222 context = g_new0 (GOptionContext, 1);
224 context->parameter_string = g_strdup (parameter_string);
225 context->help_enabled = TRUE;
226 context->ignore_unknown = FALSE;
232 * g_option_context_free:
233 * @context: a #GOptionContext
235 * Frees context and all the groups which have been
240 void g_option_context_free (GOptionContext *context)
242 g_return_if_fail (context != NULL);
244 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
245 g_list_free (context->groups);
247 if (context->main_group)
248 g_option_group_free (context->main_group);
250 free_changes_list (context, FALSE);
251 free_pending_nulls (context, FALSE);
253 g_free (context->parameter_string);
254 g_free (context->summary);
255 g_free (context->description);
257 if (context->translate_notify)
258 (* context->translate_notify) (context->translate_data);
265 * g_option_context_set_help_enabled:
266 * @context: a #GOptionContext
267 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
269 * Enables or disables automatic generation of <option>--help</option>
270 * output. By default, g_option_context_parse() recognizes
271 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
272 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
273 * suitable output to stdout.
277 void g_option_context_set_help_enabled (GOptionContext *context,
278 gboolean help_enabled)
281 g_return_if_fail (context != NULL);
283 context->help_enabled = help_enabled;
287 * g_option_context_get_help_enabled:
288 * @context: a #GOptionContext
290 * Returns whether automatic <option>--help</option> generation
291 * is turned on for @context. See g_option_context_set_help_enabled().
293 * Returns: %TRUE if automatic help generation is turned on.
298 g_option_context_get_help_enabled (GOptionContext *context)
300 g_return_val_if_fail (context != NULL, FALSE);
302 return context->help_enabled;
306 * g_option_context_set_ignore_unknown_options:
307 * @context: a #GOptionContext
308 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
309 * an error when unknown options are met
311 * Sets whether to ignore unknown options or not. If an argument is
312 * ignored, it is left in the @argv array after parsing. By default,
313 * g_option_context_parse() treats unknown options as error.
315 * This setting does not affect non-option arguments (i.e. arguments
316 * which don't start with a dash). But note that GOption cannot reliably
317 * determine whether a non-option belongs to a preceding unknown option.
322 g_option_context_set_ignore_unknown_options (GOptionContext *context,
323 gboolean ignore_unknown)
325 g_return_if_fail (context != NULL);
327 context->ignore_unknown = ignore_unknown;
331 * g_option_context_get_ignore_unknown_options:
332 * @context: a #GOptionContext
334 * Returns whether unknown options are ignored or not. See
335 * g_option_context_set_ignore_unknown_options().
337 * Returns: %TRUE if unknown options are ignored.
342 g_option_context_get_ignore_unknown_options (GOptionContext *context)
344 g_return_val_if_fail (context != NULL, FALSE);
346 return context->ignore_unknown;
350 * g_option_context_add_group:
351 * @context: a #GOptionContext
352 * @group: the group to add
354 * Adds a #GOptionGroup to the @context, so that parsing with @context
355 * will recognize the options in the group. Note that the group will
356 * be freed together with the context when g_option_context_free() is
357 * called, so you must not free the group yourself after adding it
363 g_option_context_add_group (GOptionContext *context,
368 g_return_if_fail (context != NULL);
369 g_return_if_fail (group != NULL);
370 g_return_if_fail (group->name != NULL);
371 g_return_if_fail (group->description != NULL);
372 g_return_if_fail (group->help_description != NULL);
374 for (list = context->groups; list; list = list->next)
376 GOptionGroup *g = (GOptionGroup *)list->data;
378 if ((group->name == NULL && g->name == NULL) ||
379 (group->name && g->name && strcmp (group->name, g->name) == 0))
380 g_warning ("A group named \"%s\" is already part of this GOptionContext",
384 context->groups = g_list_append (context->groups, group);
388 * g_option_context_set_main_group:
389 * @context: a #GOptionContext
390 * @group: the group to set as main group
392 * Sets a #GOptionGroup as main group of the @context.
393 * This has the same effect as calling g_option_context_add_group(),
394 * the only difference is that the options in the main group are
395 * treated differently when generating <option>--help</option> output.
400 g_option_context_set_main_group (GOptionContext *context,
403 g_return_if_fail (context != NULL);
404 g_return_if_fail (group != NULL);
406 if (context->main_group)
408 g_warning ("This GOptionContext already has a main group");
413 context->main_group = group;
417 * g_option_context_get_main_group:
418 * @context: a #GOptionContext
420 * Returns a pointer to the main group of @context.
422 * Return value: the main group of @context, or %NULL if @context doesn't
423 * have a main group. Note that group belongs to @context and should
424 * not be modified or freed.
429 g_option_context_get_main_group (GOptionContext *context)
431 g_return_val_if_fail (context != NULL, NULL);
433 return context->main_group;
437 * g_option_context_add_main_entries:
438 * @context: a #GOptionContext
439 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
440 * @translation_domain: a translation domain to use for translating
441 * the <option>--help</option> output for the options in @entries
442 * with gettext(), or %NULL
444 * A convenience function which creates a main group if it doesn't
445 * exist, adds the @entries to it and sets the translation domain.
450 g_option_context_add_main_entries (GOptionContext *context,
451 const GOptionEntry *entries,
452 const gchar *translation_domain)
454 g_return_if_fail (entries != NULL);
456 if (!context->main_group)
457 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
459 g_option_group_add_entries (context->main_group, entries);
460 g_option_group_set_translation_domain (context->main_group, translation_domain);
464 calculate_max_length (GOptionGroup *group)
467 gint i, len, max_length;
471 for (i = 0; i < group->n_entries; i++)
473 entry = &group->entries[i];
475 if (entry->flags & G_OPTION_FLAG_HIDDEN)
478 len = _g_utf8_strwidth (entry->long_name, -1);
480 if (entry->short_name)
483 if (!NO_ARG (entry) && entry->arg_description)
484 len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description), -1);
486 max_length = MAX (max_length, len);
493 print_entry (GOptionGroup *group,
495 const GOptionEntry *entry,
500 if (entry->flags & G_OPTION_FLAG_HIDDEN)
503 if (entry->long_name[0] == 0)
506 str = g_string_new (NULL);
508 if (entry->short_name)
509 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
511 g_string_append_printf (str, " --%s", entry->long_name);
513 if (entry->arg_description)
514 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
516 g_string_append_printf (string, "%s%*s %s\n", str->str,
517 (int) (max_length + 4 - _g_utf8_strwidth (str->str, -1)), "",
518 entry->description ? TRANSLATE (group, entry->description) : "");
519 g_string_free (str, TRUE);
523 * g_option_context_get_help:
524 * @context: a #GOptionContext
525 * @main_help: if %TRUE, only include the main group
526 * @group: the #GOptionGroup to create help for, or %NULL
528 * Returns a formatted, translated help text for the given context.
529 * To obtain the text produced by <option>--help</option>, call
530 * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
531 * To obtain the text produced by <option>--help-all</option>, call
532 * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
533 * To obtain the help text for an option group, call
534 * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
536 * Returns: A newly allocated string containing the help text
541 g_option_context_get_help (GOptionContext *context,
546 gint max_length, len;
549 GHashTable *shadow_map;
551 const gchar *rest_description;
554 string = g_string_sized_new (1024);
556 rest_description = NULL;
557 if (context->main_group)
560 for (i = 0; i < context->main_group->n_entries; i++)
562 entry = &context->main_group->entries[i];
563 if (entry->long_name[0] == 0)
565 rest_description = TRANSLATE (context->main_group, entry->arg_description);
571 g_string_append_printf (string, "%s\n %s %s",
572 _("Usage:"), g_get_prgname(), _("[OPTION...]"));
574 if (rest_description)
576 g_string_printf (string, " ");
577 g_string_printf (string, rest_description);
580 if (context->parameter_string)
582 g_string_printf (string, " ");
583 g_string_printf (string, TRANSLATE (context, context->parameter_string));
586 g_string_append (string, "\n\n");
588 if (context->summary)
590 g_string_append (string, TRANSLATE (context, context->summary));
591 g_string_append (string, "\n\n");
594 memset (seen, 0, sizeof (gboolean) * 256);
595 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
597 if (context->main_group)
599 for (i = 0; i < context->main_group->n_entries; i++)
601 entry = &context->main_group->entries[i];
602 g_hash_table_insert (shadow_map,
603 (gpointer)entry->long_name,
606 if (seen[(guchar)entry->short_name])
607 entry->short_name = 0;
609 seen[(guchar)entry->short_name] = TRUE;
613 list = context->groups;
616 GOptionGroup *group = list->data;
617 for (i = 0; i < group->n_entries; i++)
619 entry = &group->entries[i];
620 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
621 !(entry->flags & G_OPTION_FLAG_NOALIAS))
622 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
624 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
626 if (seen[(guchar)entry->short_name] &&
627 !(entry->flags & G_OPTION_FLAG_NOALIAS))
628 entry->short_name = 0;
630 seen[(guchar)entry->short_name] = TRUE;
635 g_hash_table_destroy (shadow_map);
637 list = context->groups;
639 max_length = _g_utf8_strwidth ("-?, --help", -1);
643 len = _g_utf8_strwidth ("--help-all", -1);
644 max_length = MAX (max_length, len);
647 if (context->main_group)
649 len = calculate_max_length (context->main_group);
650 max_length = MAX (max_length, len);
655 GOptionGroup *group = list->data;
657 /* First, we check the --help-<groupname> options */
658 len = _g_utf8_strwidth ("--help-", -1) + _g_utf8_strwidth (group->name, -1);
659 max_length = MAX (max_length, len);
661 /* Then we go through the entries */
662 len = calculate_max_length (group);
663 max_length = MAX (max_length, len);
668 /* Add a bit of padding */
673 list = context->groups;
675 g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
676 _("Help Options:"), '?', max_length - 4, "help",
677 _("Show help options"));
679 /* We only want --help-all when there are groups */
681 g_string_append_printf (string, " --%-*s %s\n",
682 max_length, "help-all",
683 _("Show all help options"));
687 GOptionGroup *group = list->data;
689 g_string_append_printf (string, " --help-%-*s %s\n",
690 max_length - 5, group->name,
691 TRANSLATE (group, group->help_description));
696 g_string_append (string, "\n");
701 /* Print a certain group */
703 g_string_append (string, TRANSLATE (group, group->description));
704 g_string_append (string, "\n");
705 for (i = 0; i < group->n_entries; i++)
706 print_entry (group, max_length, &group->entries[i], string);
707 g_string_append (string, "\n");
711 /* Print all groups */
713 list = context->groups;
717 GOptionGroup *group = list->data;
719 g_string_append (string, group->description);
720 g_string_append (string, "\n");
721 for (i = 0; i < group->n_entries; i++)
722 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
723 print_entry (group, max_length, &group->entries[i], string);
725 g_string_append (string, "\n");
730 /* Print application options if --help or --help-all has been specified */
731 if (main_help || !group)
733 list = context->groups;
735 g_string_append (string, _("Application Options:"));
736 g_string_append (string, "\n");
737 if (context->main_group)
738 for (i = 0; i < context->main_group->n_entries; i++)
739 print_entry (context->main_group, max_length,
740 &context->main_group->entries[i], string);
744 GOptionGroup *group = list->data;
746 /* Print main entries from other groups */
747 for (i = 0; i < group->n_entries; i++)
748 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
749 print_entry (group, max_length, &group->entries[i], string);
754 g_string_append (string, "\n");
757 if (context->description)
759 g_string_append (string, TRANSLATE (context, context->description));
760 g_string_append (string, "\n");
763 return g_string_free (string, FALSE);
767 print_help (GOptionContext *context,
773 help = g_option_context_get_help (context, main_help, group);
781 parse_int (const gchar *arg_name,
790 tmp = strtol (arg, &end, 0);
792 if (*arg == '\0' || *end != '\0')
795 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
796 _("Cannot parse integer value '%s' for %s"),
802 if (*result != tmp || errno == ERANGE)
805 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
806 _("Integer value '%s' for %s out of range"),
816 parse_double (const gchar *arg_name,
825 tmp = g_strtod (arg, &end);
827 if (*arg == '\0' || *end != '\0')
830 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
831 _("Cannot parse double value '%s' for %s"),
838 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
839 _("Double value '%s' for %s out of range"),
851 parse_int64 (const gchar *arg_name,
860 tmp = g_ascii_strtoll (arg, &end, 0);
862 if (*arg == '\0' || *end != '\0')
865 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
866 _("Cannot parse integer value '%s' for %s"),
873 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
874 _("Integer value '%s' for %s out of range"),
886 get_change (GOptionContext *context,
891 Change *change = NULL;
893 for (list = context->changes; list != NULL; list = list->next)
897 if (change->arg_data == arg_data)
901 change = g_new0 (Change, 1);
902 change->arg_type = arg_type;
903 change->arg_data = arg_data;
905 context->changes = g_list_prepend (context->changes, change);
913 add_pending_null (GOptionContext *context,
919 n = g_new0 (PendingNull, 1);
923 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
927 parse_arg (GOptionContext *context,
931 const gchar *option_name,
937 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
941 case G_OPTION_ARG_NONE:
943 change = get_change (context, G_OPTION_ARG_NONE,
946 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
949 case G_OPTION_ARG_STRING:
953 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
958 change = get_change (context, G_OPTION_ARG_STRING,
960 g_free (change->allocated.str);
962 change->prev.str = *(gchar **)entry->arg_data;
963 change->allocated.str = data;
965 *(gchar **)entry->arg_data = data;
968 case G_OPTION_ARG_STRING_ARRAY:
972 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
977 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
980 if (change->allocated.array.len == 0)
982 change->prev.array = *(gchar ***)entry->arg_data;
983 change->allocated.array.data = g_new (gchar *, 2);
986 change->allocated.array.data =
987 g_renew (gchar *, change->allocated.array.data,
988 change->allocated.array.len + 2);
990 change->allocated.array.data[change->allocated.array.len] = data;
991 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
993 change->allocated.array.len ++;
995 *(gchar ***)entry->arg_data = change->allocated.array.data;
1000 case G_OPTION_ARG_FILENAME:
1005 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1010 data = g_strdup (value);
1012 change = get_change (context, G_OPTION_ARG_FILENAME,
1014 g_free (change->allocated.str);
1016 change->prev.str = *(gchar **)entry->arg_data;
1017 change->allocated.str = data;
1019 *(gchar **)entry->arg_data = data;
1023 case G_OPTION_ARG_FILENAME_ARRAY:
1028 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1033 data = g_strdup (value);
1035 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1038 if (change->allocated.array.len == 0)
1040 change->prev.array = *(gchar ***)entry->arg_data;
1041 change->allocated.array.data = g_new (gchar *, 2);
1044 change->allocated.array.data =
1045 g_renew (gchar *, change->allocated.array.data,
1046 change->allocated.array.len + 2);
1048 change->allocated.array.data[change->allocated.array.len] = data;
1049 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1051 change->allocated.array.len ++;
1053 *(gchar ***)entry->arg_data = change->allocated.array.data;
1058 case G_OPTION_ARG_INT:
1062 if (!parse_int (option_name, value,
1067 change = get_change (context, G_OPTION_ARG_INT,
1069 change->prev.integer = *(gint *)entry->arg_data;
1070 *(gint *)entry->arg_data = data;
1073 case G_OPTION_ARG_CALLBACK:
1078 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1080 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1082 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1085 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1087 data = g_strdup (value);
1091 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1093 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1097 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1105 case G_OPTION_ARG_DOUBLE:
1109 if (!parse_double (option_name, value,
1116 change = get_change (context, G_OPTION_ARG_DOUBLE,
1118 change->prev.dbl = *(gdouble *)entry->arg_data;
1119 *(gdouble *)entry->arg_data = data;
1122 case G_OPTION_ARG_INT64:
1126 if (!parse_int64 (option_name, value,
1133 change = get_change (context, G_OPTION_ARG_INT64,
1135 change->prev.int64 = *(gint64 *)entry->arg_data;
1136 *(gint64 *)entry->arg_data = data;
1140 g_assert_not_reached ();
1147 parse_short_option (GOptionContext *context,
1148 GOptionGroup *group,
1159 for (j = 0; j < group->n_entries; j++)
1161 if (arg == group->entries[j].short_name)
1164 gchar *value = NULL;
1166 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1168 if (NO_ARG (&group->entries[j]))
1172 if (*new_index > index)
1175 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1176 _("Error parsing option %s"), option_name);
1177 g_free (option_name);
1181 if (index < *argc - 1)
1183 if (!OPTIONAL_ARG (&group->entries[j]))
1185 value = (*argv)[index + 1];
1186 add_pending_null (context, &((*argv)[index + 1]), NULL);
1187 *new_index = index+1;
1191 if ((*argv)[index + 1][0] == '-')
1195 value = (*argv)[index + 1];
1196 add_pending_null (context, &((*argv)[index + 1]), NULL);
1197 *new_index = index + 1;
1201 else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1206 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1207 _("Missing argument for %s"), option_name);
1208 g_free (option_name);
1213 if (!parse_arg (context, group, &group->entries[j],
1214 value, option_name, error))
1216 g_free (option_name);
1220 g_free (option_name);
1229 parse_long_option (GOptionContext *context,
1230 GOptionGroup *group,
1241 for (j = 0; j < group->n_entries; j++)
1243 if (*index >= *argc)
1246 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1249 if (NO_ARG (&group->entries[j]) &&
1250 strcmp (arg, group->entries[j].long_name) == 0)
1254 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1255 parse_arg (context, group, &group->entries[j],
1256 NULL, option_name, error);
1257 g_free(option_name);
1259 add_pending_null (context, &((*argv)[*index]), NULL);
1264 gint len = strlen (group->entries[j].long_name);
1266 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1267 (arg[len] == '=' || arg[len] == 0))
1269 gchar *value = NULL;
1272 add_pending_null (context, &((*argv)[*index]), NULL);
1273 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1275 if (arg[len] == '=')
1276 value = arg + len + 1;
1277 else if (*index < *argc - 1)
1279 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1281 value = (*argv)[*index + 1];
1282 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1287 if ((*argv)[*index + 1][0] == '-')
1290 retval = parse_arg (context, group, &group->entries[j],
1291 NULL, option_name, error);
1293 g_free (option_name);
1298 value = (*argv)[*index + 1];
1299 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1304 else if (*index >= *argc - 1 &&
1305 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1308 retval = parse_arg (context, group, &group->entries[j],
1309 NULL, option_name, error);
1311 g_free (option_name);
1317 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1318 _("Missing argument for %s"), option_name);
1319 g_free (option_name);
1323 if (!parse_arg (context, group, &group->entries[j],
1324 value, option_name, error))
1326 g_free (option_name);
1330 g_free (option_name);
1340 parse_remaining_arg (GOptionContext *context,
1341 GOptionGroup *group,
1350 for (j = 0; j < group->n_entries; j++)
1352 if (*index >= *argc)
1355 if (group->entries[j].long_name[0])
1358 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1359 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1360 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1362 add_pending_null (context, &((*argv)[*index]), NULL);
1364 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1375 free_changes_list (GOptionContext *context,
1380 for (list = context->changes; list != NULL; list = list->next)
1382 Change *change = list->data;
1386 switch (change->arg_type)
1388 case G_OPTION_ARG_NONE:
1389 *(gboolean *)change->arg_data = change->prev.bool;
1391 case G_OPTION_ARG_INT:
1392 *(gint *)change->arg_data = change->prev.integer;
1394 case G_OPTION_ARG_STRING:
1395 case G_OPTION_ARG_FILENAME:
1396 g_free (change->allocated.str);
1397 *(gchar **)change->arg_data = change->prev.str;
1399 case G_OPTION_ARG_STRING_ARRAY:
1400 case G_OPTION_ARG_FILENAME_ARRAY:
1401 g_strfreev (change->allocated.array.data);
1402 *(gchar ***)change->arg_data = change->prev.array;
1404 case G_OPTION_ARG_DOUBLE:
1405 *(gdouble *)change->arg_data = change->prev.dbl;
1407 case G_OPTION_ARG_INT64:
1408 *(gint64 *)change->arg_data = change->prev.int64;
1411 g_assert_not_reached ();
1418 g_list_free (context->changes);
1419 context->changes = NULL;
1423 free_pending_nulls (GOptionContext *context,
1424 gboolean perform_nulls)
1428 for (list = context->pending_nulls; list != NULL; list = list->next)
1430 PendingNull *n = list->data;
1436 /* Copy back the short options */
1438 strcpy (*n->ptr + 1, n->value);
1448 g_list_free (context->pending_nulls);
1449 context->pending_nulls = NULL;
1453 * g_option_context_parse:
1454 * @context: a #GOptionContext
1455 * @argc: a pointer to the number of command line arguments
1456 * @argv: a pointer to the array of command line arguments
1457 * @error: a return location for errors
1459 * Parses the command line arguments, recognizing options
1460 * which have been added to @context. A side-effect of
1461 * calling this function is that g_set_prgname() will be
1464 * If the parsing is successful, any parsed arguments are
1465 * removed from the array and @argc and @argv are updated
1466 * accordingly. A '--' option is stripped from @argv
1467 * unless there are unparsed options before and after it,
1468 * or some of the options after it start with '-'. In case
1469 * of an error, @argc and @argv are left unmodified.
1471 * If automatic <option>--help</option> support is enabled
1472 * (see g_option_context_set_help_enabled()), and the
1473 * @argv array contains one of the recognized help options,
1474 * this function will produce help output to stdout and
1475 * call <literal>exit (0)</literal>.
1477 * Note that function depends on the
1478 * <link linkend="setlocale">current locale</link> for
1479 * automatic character set conversion of string and filename
1482 * Return value: %TRUE if the parsing was successful,
1483 * %FALSE if an error occurred
1488 g_option_context_parse (GOptionContext *context,
1496 /* Set program name */
1497 if (!g_get_prgname())
1499 if (argc && argv && *argc)
1503 prgname = g_path_get_basename ((*argv)[0]);
1504 g_set_prgname (prgname);
1508 g_set_prgname ("<unknown>");
1511 /* Call pre-parse hooks */
1512 list = context->groups;
1515 GOptionGroup *group = list->data;
1517 if (group->pre_parse_func)
1519 if (!(* group->pre_parse_func) (context, group,
1520 group->user_data, error))
1527 if (context->main_group && context->main_group->pre_parse_func)
1529 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1530 context->main_group->user_data, error))
1536 gboolean stop_parsing = FALSE;
1537 gboolean has_unknown = FALSE;
1538 gint separator_pos = 0;
1540 for (i = 1; i < *argc; i++)
1543 gboolean parsed = FALSE;
1545 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1547 if ((*argv)[i][1] == '-')
1551 arg = (*argv)[i] + 2;
1553 /* '--' terminates list of arguments */
1557 stop_parsing = TRUE;
1561 /* Handle help options */
1562 if (context->help_enabled)
1564 if (strcmp (arg, "help") == 0)
1565 print_help (context, TRUE, NULL);
1566 else if (strcmp (arg, "help-all") == 0)
1567 print_help (context, FALSE, NULL);
1568 else if (strncmp (arg, "help-", 5) == 0)
1572 list = context->groups;
1576 GOptionGroup *group = list->data;
1578 if (strcmp (arg + 5, group->name) == 0)
1579 print_help (context, FALSE, group);
1586 if (context->main_group &&
1587 !parse_long_option (context, context->main_group, &i, arg,
1588 FALSE, argc, argv, error, &parsed))
1594 /* Try the groups */
1595 list = context->groups;
1598 GOptionGroup *group = list->data;
1600 if (!parse_long_option (context, group, &i, arg,
1601 FALSE, argc, argv, error, &parsed))
1613 /* Now look for --<group>-<option> */
1614 dash = strchr (arg, '-');
1617 /* Try the groups */
1618 list = context->groups;
1621 GOptionGroup *group = list->data;
1623 if (strncmp (group->name, arg, dash - arg) == 0)
1625 if (!parse_long_option (context, group, &i, dash + 1,
1626 TRUE, argc, argv, error, &parsed))
1637 if (context->ignore_unknown)
1641 { /* short option */
1642 gint j, new_i = i, arg_length;
1643 gboolean *nulled_out = NULL;
1644 arg = (*argv)[i] + 1;
1645 arg_length = strlen (arg);
1646 nulled_out = g_newa (gboolean, arg_length);
1647 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1648 for (j = 0; j < arg_length; j++)
1650 if (context->help_enabled && arg[j] == '?')
1651 print_help (context, TRUE, NULL);
1653 if (context->main_group &&
1654 !parse_short_option (context, context->main_group,
1656 argc, argv, error, &parsed))
1660 /* Try the groups */
1661 list = context->groups;
1664 GOptionGroup *group = list->data;
1665 if (!parse_short_option (context, group, i, &new_i, arg[j],
1666 argc, argv, error, &parsed))
1674 if (context->ignore_unknown && parsed)
1675 nulled_out[j] = TRUE;
1676 else if (context->ignore_unknown)
1680 /* !context->ignore_unknown && parsed */
1682 if (context->ignore_unknown)
1684 gchar *new_arg = NULL;
1686 for (j = 0; j < arg_length; j++)
1691 new_arg = g_malloc (arg_length + 1);
1692 new_arg[arg_index++] = arg[j];
1696 new_arg[arg_index] = '\0';
1697 add_pending_null (context, &((*argv)[i]), new_arg);
1701 add_pending_null (context, &((*argv)[i]), NULL);
1709 if (!parsed && !context->ignore_unknown)
1712 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1713 _("Unknown option %s"), (*argv)[i]);
1719 /* Collect remaining args */
1720 if (context->main_group &&
1721 !parse_remaining_arg (context, context->main_group, &i,
1722 argc, argv, error, &parsed))
1725 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1730 if (separator_pos > 0)
1731 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1735 /* Call post-parse hooks */
1736 list = context->groups;
1739 GOptionGroup *group = list->data;
1741 if (group->post_parse_func)
1743 if (!(* group->post_parse_func) (context, group,
1744 group->user_data, error))
1751 if (context->main_group && context->main_group->post_parse_func)
1753 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1754 context->main_group->user_data, error))
1760 free_pending_nulls (context, TRUE);
1762 for (i = 1; i < *argc; i++)
1764 for (k = i; k < *argc; k++)
1765 if ((*argv)[k] != NULL)
1771 for (j = i + k; j < *argc; j++)
1773 (*argv)[j-k] = (*argv)[j];
1785 /* Call error hooks */
1786 list = context->groups;
1789 GOptionGroup *group = list->data;
1791 if (group->error_func)
1792 (* group->error_func) (context, group,
1793 group->user_data, error);
1798 if (context->main_group && context->main_group->error_func)
1799 (* context->main_group->error_func) (context, context->main_group,
1800 context->main_group->user_data, error);
1802 free_changes_list (context, TRUE);
1803 free_pending_nulls (context, FALSE);
1809 * g_option_group_new:
1810 * @name: the name for the option group, this is used to provide
1811 * help for the options in this group with <option>--help-</option>@name
1812 * @description: a description for this group to be shown in
1813 * <option>--help</option>. This string is translated using the translation
1814 * domain or translation function of the group
1815 * @help_description: a description for the <option>--help-</option>@name option.
1816 * This string is translated using the translation domain or translation function
1818 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1819 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1820 * @destroy: a function that will be called to free @user_data, or %NULL
1822 * Creates a new #GOptionGroup.
1824 * Return value: a newly created option group. It should be added
1825 * to a #GOptionContext or freed with g_option_group_free().
1830 g_option_group_new (const gchar *name,
1831 const gchar *description,
1832 const gchar *help_description,
1834 GDestroyNotify destroy)
1837 GOptionGroup *group;
1839 group = g_new0 (GOptionGroup, 1);
1840 group->name = g_strdup (name);
1841 group->description = g_strdup (description);
1842 group->help_description = g_strdup (help_description);
1843 group->user_data = user_data;
1844 group->destroy_notify = destroy;
1851 * g_option_group_free:
1852 * @group: a #GOptionGroup
1854 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1855 * free groups which have been added to a #GOptionContext.
1860 g_option_group_free (GOptionGroup *group)
1862 g_return_if_fail (group != NULL);
1864 g_free (group->name);
1865 g_free (group->description);
1866 g_free (group->help_description);
1868 g_free (group->entries);
1870 if (group->destroy_notify)
1871 (* group->destroy_notify) (group->user_data);
1873 if (group->translate_notify)
1874 (* group->translate_notify) (group->translate_data);
1881 * g_option_group_add_entries:
1882 * @group: a #GOptionGroup
1883 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1885 * Adds the options specified in @entries to @group.
1890 g_option_group_add_entries (GOptionGroup *group,
1891 const GOptionEntry *entries)
1895 g_return_if_fail (entries != NULL);
1897 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1899 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1901 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1903 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1905 gchar c = group->entries[i].short_name;
1909 if (c == '-' || !g_ascii_isprint (c))
1911 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1912 group->entries[i].short_name = 0;
1917 group->n_entries += n_entries;
1921 * g_option_group_set_parse_hooks:
1922 * @group: a #GOptionGroup
1923 * @pre_parse_func: a function to call before parsing, or %NULL
1924 * @post_parse_func: a function to call after parsing, or %NULL
1926 * Associates two functions with @group which will be called
1927 * from g_option_context_parse() before the first option is parsed
1928 * and after the last option has been parsed, respectively.
1930 * Note that the user data to be passed to @pre_parse_func and
1931 * @post_parse_func can be specified when constructing the group
1932 * with g_option_group_new().
1937 g_option_group_set_parse_hooks (GOptionGroup *group,
1938 GOptionParseFunc pre_parse_func,
1939 GOptionParseFunc post_parse_func)
1941 g_return_if_fail (group != NULL);
1943 group->pre_parse_func = pre_parse_func;
1944 group->post_parse_func = post_parse_func;
1948 * g_option_group_set_error_hook:
1949 * @group: a #GOptionGroup
1950 * @error_func: a function to call when an error occurs
1952 * Associates a function with @group which will be called
1953 * from g_option_context_parse() when an error occurs.
1955 * Note that the user data to be passed to @pre_parse_func and
1956 * @post_parse_func can be specified when constructing the group
1957 * with g_option_group_new().
1962 g_option_group_set_error_hook (GOptionGroup *group,
1963 GOptionErrorFunc error_func)
1965 g_return_if_fail (group != NULL);
1967 group->error_func = error_func;
1972 * g_option_group_set_translate_func:
1973 * @group: a #GOptionGroup
1974 * @func: the #GTranslateFunc, or %NULL
1975 * @data: user data to pass to @func, or %NULL
1976 * @destroy_notify: a function which gets called to free @data, or %NULL
1978 * Sets the function which is used to translate user-visible
1979 * strings, for <option>--help</option> output. Different
1980 * groups can use different #GTranslateFunc<!-- -->s. If @func
1981 * is %NULL, strings are not translated.
1983 * If you are using gettext(), you only need to set the translation
1984 * domain, see g_option_group_set_translation_domain().
1989 g_option_group_set_translate_func (GOptionGroup *group,
1990 GTranslateFunc func,
1992 GDestroyNotify destroy_notify)
1994 g_return_if_fail (group != NULL);
1996 if (group->translate_notify)
1997 group->translate_notify (group->translate_data);
1999 group->translate_func = func;
2000 group->translate_data = data;
2001 group->translate_notify = destroy_notify;
2005 dgettext_swapped (const gchar *msgid,
2006 const gchar *domainname)
2008 return dgettext (domainname, msgid);
2012 * g_option_group_set_translation_domain:
2013 * @group: a #GOptionGroup
2014 * @domain: the domain to use
2016 * A convenience function to use gettext() for translating
2017 * user-visible strings.
2022 g_option_group_set_translation_domain (GOptionGroup *group,
2023 const gchar *domain)
2025 g_return_if_fail (group != NULL);
2027 g_option_group_set_translate_func (group,
2028 (GTranslateFunc)dgettext_swapped,
2034 * g_option_context_set_translate_func:
2035 * @context: a #GOptionContext
2036 * @func: the #GTranslateFunc, or %NULL
2037 * @data: user data to pass to @func, or %NULL
2038 * @destroy_notify: a function which gets called to free @data, or %NULL
2040 * Sets the function which is used to translate the contexts
2041 * user-visible strings, for <option>--help</option> output.
2042 * If @func is %NULL, strings are not translated.
2044 * Note that option groups have their own translation functions,
2045 * this function only affects the @parameter_string (see g_option_context_nex()),
2046 * the summary (see g_option_context_set_summary()) and the description
2047 * (see g_option_context_set_description()).
2049 * If you are using gettext(), you only need to set the translation
2050 * domain, see g_context_group_set_translation_domain().
2055 g_option_context_set_translate_func (GOptionContext *context,
2056 GTranslateFunc func,
2058 GDestroyNotify destroy_notify)
2060 g_return_if_fail (context != NULL);
2062 if (context->translate_notify)
2063 context->translate_notify (context->translate_data);
2065 context->translate_func = func;
2066 context->translate_data = data;
2067 context->translate_notify = destroy_notify;
2071 * g_option_context_set_translation_domain:
2072 * @context: a #GOptionContext
2073 * @domain: the domain to use
2075 * A convenience function to use gettext() for translating
2076 * user-visible strings.
2081 g_option_context_set_translation_domain (GOptionContext *context,
2082 const gchar *domain)
2084 g_return_if_fail (context != NULL);
2086 g_option_context_set_translate_func (context,
2087 (GTranslateFunc)dgettext_swapped,
2093 * g_option_context_set_summary:
2094 * @context: a #GOptionContext
2095 * @summary: a string to be shown in <option>--help</option> output
2096 * before the list of options, or %NULL
2098 * Adds a string to be displayed in <option>--help</option> output
2099 * before the list of options. This is typically a summary of the
2100 * program functionality.
2102 * Note that the summary is translated (see
2103 * g_option_context_set_translate_func()).
2108 g_option_context_set_summary (GOptionContext *context,
2109 const gchar *summary)
2111 g_return_if_fail (context != NULL);
2113 g_free (context->summary);
2114 context->summary = g_strdup (summary);
2119 * g_option_context_get_summary:
2120 * @context: a #GOptionContext
2122 * Returns the summary. See g_option_context_set_summary().
2124 * Returns: the summary
2128 G_CONST_RETURN gchar *
2129 g_option_context_get_summary (GOptionContext *context)
2131 g_return_val_if_fail (context != NULL, NULL);
2133 return context->summary;
2137 * g_option_context_set_description:
2138 * @context: a #GOptionContext
2139 * @description: a string to be shown in <option>--help</option> output
2140 * after the list of options, or %NULL
2142 * Adds a string to be displayed in <option>--help</option> output
2143 * after the list of options. This text often includes a bug reporting
2146 * Note that the summary is translated (see
2147 * g_option_context_set_translate_func()).
2152 g_option_context_set_description (GOptionContext *context,
2153 const gchar *description)
2155 g_return_if_fail (context != NULL);
2157 g_free (context->description);
2158 context->description = g_strdup (description);
2163 * g_option_context_get_description:
2164 * @context: a #GOptionContext
2166 * Returns the description. See g_option_context_set_description().
2168 * Returns: the description
2172 G_CONST_RETURN gchar *
2173 g_option_context_get_description (GOptionContext *context)
2175 g_return_val_if_fail (context != NULL, NULL);
2177 return context->description;
2181 #define __G_OPTION_C__
2182 #include "galiasdef.c"