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;
79 gboolean help_enabled;
80 gboolean ignore_unknown;
82 GOptionGroup *main_group;
84 /* We keep a list of change so we can revert them */
87 /* We also keep track of all argv elements that should be NULLed or
97 gchar *help_description;
99 GDestroyNotify destroy_notify;
102 GTranslateFunc translate_func;
103 GDestroyNotify translate_notify;
104 gpointer translate_data;
106 GOptionEntry *entries;
109 GOptionParseFunc pre_parse_func;
110 GOptionParseFunc post_parse_func;
111 GOptionErrorFunc error_func;
114 static void free_changes_list (GOptionContext *context,
116 static void free_pending_nulls (GOptionContext *context,
117 gboolean perform_nulls);
120 g_option_error_quark (void)
122 return g_quark_from_static_string ("g-option-context-error-quark");
126 * g_option_context_new:
127 * @parameter_string: a string which is displayed in
128 * the first line of <option>--help</option> output, after the
130 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
132 * Creates a new option context.
134 * The @parameter_text can serve multiple purposes. It can be used
135 * to add descriptions for "rest" arguments, which are not parsed by
136 * the #GOptionContext, typically something like "FILES" or
137 * "FILE1 FILE2...". (If you are using #G_OPTION_REMAINING for
138 * collecting "rest" arguments, GLib handles this automatically by
139 * using the @arg_description of the corresponding #GOptionEntry in
140 * the usage summary.)
142 * Another common usage is to give a summary of the program
143 * functionality. This can be a short summary on the same line,
144 * like " - frob the strings", or a longer description in a paragraph
145 * below the usage summary. In this case, @parameter_string should start
146 * with two newlines, to separate the description from the usage summary:
147 * "\n\nA program to frob strings, which will..."
149 * Returns: a newly created #GOptionContext, which must be
150 * freed with g_option_context_free() after use.
155 g_option_context_new (const gchar *parameter_string)
158 GOptionContext *context;
160 context = g_new0 (GOptionContext, 1);
162 context->parameter_string = g_strdup (parameter_string);
163 context->help_enabled = TRUE;
164 context->ignore_unknown = FALSE;
170 * g_option_context_free:
171 * @context: a #GOptionContext
173 * Frees context and all the groups which have been
178 void g_option_context_free (GOptionContext *context)
180 g_return_if_fail (context != NULL);
182 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
183 g_list_free (context->groups);
185 if (context->main_group)
186 g_option_group_free (context->main_group);
188 free_changes_list (context, FALSE);
189 free_pending_nulls (context, FALSE);
191 g_free (context->parameter_string);
198 * g_option_context_set_help_enabled:
199 * @context: a #GOptionContext
200 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
202 * Enables or disables automatic generation of <option>--help</option>
203 * output. By default, g_option_context_parse() recognizes
204 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
205 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
206 * suitable output to stdout.
210 void g_option_context_set_help_enabled (GOptionContext *context,
211 gboolean help_enabled)
214 g_return_if_fail (context != NULL);
216 context->help_enabled = help_enabled;
220 * g_option_context_get_help_enabled:
221 * @context: a #GOptionContext
223 * Returns whether automatic <option>--help</option> generation
224 * is turned on for @context. See g_option_context_set_help_enabled().
226 * Returns: %TRUE if automatic help generation is turned on.
231 g_option_context_get_help_enabled (GOptionContext *context)
233 g_return_val_if_fail (context != NULL, FALSE);
235 return context->help_enabled;
239 * g_option_context_set_ignore_unknown_options:
240 * @context: a #GOptionContext
241 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
242 * an error when unknown options are met
244 * Sets whether to ignore unknown options or not. If an argument is
245 * ignored, it is left in the @argv array after parsing. By default,
246 * g_option_context_parse() treats unknown options as error.
248 * This setting does not affect non-option arguments (i.e. arguments
249 * which don't start with a dash). But note that GOption cannot reliably
250 * determine whether a non-option belongs to a preceding unknown option.
255 g_option_context_set_ignore_unknown_options (GOptionContext *context,
256 gboolean ignore_unknown)
258 g_return_if_fail (context != NULL);
260 context->ignore_unknown = ignore_unknown;
264 * g_option_context_get_ignore_unknown_options:
265 * @context: a #GOptionContext
267 * Returns whether unknown options are ignored or not. See
268 * g_option_context_set_ignore_unknown_options().
270 * Returns: %TRUE if unknown options are ignored.
275 g_option_context_get_ignore_unknown_options (GOptionContext *context)
277 g_return_val_if_fail (context != NULL, FALSE);
279 return context->ignore_unknown;
283 * g_option_context_add_group:
284 * @context: a #GOptionContext
285 * @group: the group to add
287 * Adds a #GOptionGroup to the @context, so that parsing with @context
288 * will recognize the options in the group. Note that the group will
289 * be freed together with the context when g_option_context_free() is
290 * called, so you must not free the group yourself after adding it
296 g_option_context_add_group (GOptionContext *context,
301 g_return_if_fail (context != NULL);
302 g_return_if_fail (group != NULL);
303 g_return_if_fail (group->name != NULL);
304 g_return_if_fail (group->description != NULL);
305 g_return_if_fail (group->help_description != NULL);
307 for (list = context->groups; list; list = list->next)
309 GOptionGroup *g = (GOptionGroup *)list->data;
311 if ((group->name == NULL && g->name == NULL) ||
312 (group->name && g->name && strcmp (group->name, g->name) == 0))
313 g_warning ("A group named \"%s\" is already part of this GOptionContext",
317 context->groups = g_list_append (context->groups, group);
321 * g_option_context_set_main_group:
322 * @context: a #GOptionContext
323 * @group: the group to set as main group
325 * Sets a #GOptionGroup as main group of the @context.
326 * This has the same effect as calling g_option_context_add_group(),
327 * the only difference is that the options in the main group are
328 * treated differently when generating <option>--help</option> output.
333 g_option_context_set_main_group (GOptionContext *context,
336 g_return_if_fail (context != NULL);
337 g_return_if_fail (group != NULL);
339 if (context->main_group)
341 g_warning ("This GOptionContext already has a main group");
346 context->main_group = group;
350 * g_option_context_get_main_group:
351 * @context: a #GOptionContext
353 * Returns a pointer to the main group of @context.
355 * Return value: the main group of @context, or %NULL if @context doesn't
356 * have a main group. Note that group belongs to @context and should
357 * not be modified or freed.
362 g_option_context_get_main_group (GOptionContext *context)
364 g_return_val_if_fail (context != NULL, NULL);
366 return context->main_group;
370 * g_option_context_add_main_entries:
371 * @context: a #GOptionContext
372 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
373 * @translation_domain: a translation domain to use for translating
374 * the <option>--help</option> output for the options in @entries
375 * with gettext(), or %NULL
377 * A convenience function which creates a main group if it doesn't
378 * exist, adds the @entries to it and sets the translation domain.
383 g_option_context_add_main_entries (GOptionContext *context,
384 const GOptionEntry *entries,
385 const gchar *translation_domain)
387 g_return_if_fail (entries != NULL);
389 if (!context->main_group)
390 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
392 g_option_group_add_entries (context->main_group, entries);
393 g_option_group_set_translation_domain (context->main_group, translation_domain);
397 calculate_max_length (GOptionGroup *group)
400 gint i, len, max_length;
404 for (i = 0; i < group->n_entries; i++)
406 entry = &group->entries[i];
408 if (entry->flags & G_OPTION_FLAG_HIDDEN)
411 len = g_utf8_strlen (entry->long_name, -1);
413 if (entry->short_name)
416 if (!NO_ARG (entry) && entry->arg_description)
417 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
419 max_length = MAX (max_length, len);
426 print_entry (GOptionGroup *group,
428 const GOptionEntry *entry)
432 if (entry->flags & G_OPTION_FLAG_HIDDEN)
435 if (entry->long_name[0] == 0)
438 str = g_string_new (NULL);
440 if (entry->short_name)
441 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
443 g_string_append_printf (str, " --%s", entry->long_name);
445 if (entry->arg_description)
446 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
448 g_print ("%-*s %s\n", max_length + 4, str->str,
449 entry->description ? TRANSLATE (group, entry->description) : "");
450 g_string_free (str, TRUE);
454 print_help (GOptionContext *context,
459 gint max_length, len;
462 GHashTable *shadow_map;
464 const gchar *rest_description;
466 rest_description = NULL;
467 if (context->main_group)
469 for (i = 0; i < context->main_group->n_entries; i++)
471 entry = &context->main_group->entries[i];
472 if (entry->long_name[0] == 0)
474 rest_description = entry->arg_description;
480 g_print ("%s\n %s %s%s%s%s%s\n\n",
481 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
482 rest_description ? " " : "",
483 rest_description ? rest_description : "",
484 context->parameter_string ? " " : "",
485 context->parameter_string ? context->parameter_string : "");
487 memset (seen, 0, sizeof (gboolean) * 256);
488 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
490 if (context->main_group)
492 for (i = 0; i < context->main_group->n_entries; i++)
494 entry = &context->main_group->entries[i];
495 g_hash_table_insert (shadow_map,
496 (gpointer)entry->long_name,
499 if (seen[(guchar)entry->short_name])
500 entry->short_name = 0;
502 seen[(guchar)entry->short_name] = TRUE;
506 list = context->groups;
509 GOptionGroup *group = list->data;
510 for (i = 0; i < group->n_entries; i++)
512 entry = &group->entries[i];
513 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
514 !(entry->flags && G_OPTION_FLAG_NOALIAS))
515 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
517 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
519 if (seen[(guchar)entry->short_name] &&
520 !(entry->flags && G_OPTION_FLAG_NOALIAS))
521 entry->short_name = 0;
523 seen[(guchar)entry->short_name] = TRUE;
528 g_hash_table_destroy (shadow_map);
530 list = context->groups;
532 max_length = g_utf8_strlen ("-?, --help", -1);
536 len = g_utf8_strlen ("--help-all", -1);
537 max_length = MAX (max_length, len);
540 if (context->main_group)
542 len = calculate_max_length (context->main_group);
543 max_length = MAX (max_length, len);
548 GOptionGroup *group = list->data;
550 /* First, we check the --help-<groupname> options */
551 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
552 max_length = MAX (max_length, len);
554 /* Then we go through the entries */
555 len = calculate_max_length (group);
556 max_length = MAX (max_length, len);
561 /* Add a bit of padding */
566 list = context->groups;
568 g_print ("%s\n -%c, --%-*s %s\n",
569 _("Help Options:"), '?', max_length - 4, "help",
570 _("Show help options"));
572 /* We only want --help-all when there are groups */
574 g_print (" --%-*s %s\n", max_length, "help-all",
575 _("Show all help options"));
579 GOptionGroup *group = list->data;
581 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
582 TRANSLATE (group, group->help_description));
592 /* Print a certain group */
594 g_print ("%s\n", TRANSLATE (group, group->description));
595 for (i = 0; i < group->n_entries; i++)
596 print_entry (group, max_length, &group->entries[i]);
601 /* Print all groups */
603 list = context->groups;
607 GOptionGroup *group = list->data;
609 g_print ("%s\n", group->description);
611 for (i = 0; i < group->n_entries; i++)
612 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
613 print_entry (group, max_length, &group->entries[i]);
620 /* Print application options if --help or --help-all has been specified */
621 if (main_help || !group)
623 list = context->groups;
625 g_print ("%s\n", _("Application Options:"));
627 if (context->main_group)
628 for (i = 0; i < context->main_group->n_entries; i++)
629 print_entry (context->main_group, max_length,
630 &context->main_group->entries[i]);
634 GOptionGroup *group = list->data;
636 /* Print main entries from other groups */
637 for (i = 0; i < group->n_entries; i++)
638 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
639 print_entry (group, max_length, &group->entries[i]);
651 parse_int (const gchar *arg_name,
660 tmp = strtol (arg, &end, 0);
662 if (*arg == '\0' || *end != '\0')
665 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
666 _("Cannot parse integer value '%s' for %s"),
672 if (*result != tmp || errno == ERANGE)
675 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
676 _("Integer value '%s' for %s out of range"),
686 parse_double (const gchar *arg_name,
695 tmp = g_strtod (arg, &end);
697 if (*arg == '\0' || *end != '\0')
700 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
701 _("Cannot parse double value '%s' for %s"),
708 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
709 _("Double value '%s' for %s out of range"),
721 parse_int64 (const gchar *arg_name,
730 tmp = strtoll (arg, &end, 0);
732 if (*arg == '\0' || *end != '\0')
735 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
736 _("Cannot parse integer value '%s' for %s"),
743 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
744 _("Integer value '%s' for %s out of range"),
756 get_change (GOptionContext *context,
761 Change *change = NULL;
763 for (list = context->changes; list != NULL; list = list->next)
767 if (change->arg_data == arg_data)
771 change = g_new0 (Change, 1);
772 change->arg_type = arg_type;
773 change->arg_data = arg_data;
775 context->changes = g_list_prepend (context->changes, change);
783 add_pending_null (GOptionContext *context,
789 n = g_new0 (PendingNull, 1);
793 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
797 parse_arg (GOptionContext *context,
801 const gchar *option_name,
807 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
811 case G_OPTION_ARG_NONE:
813 change = get_change (context, G_OPTION_ARG_NONE,
816 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
819 case G_OPTION_ARG_STRING:
823 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
828 change = get_change (context, G_OPTION_ARG_STRING,
830 g_free (change->allocated.str);
832 change->prev.str = *(gchar **)entry->arg_data;
833 change->allocated.str = data;
835 *(gchar **)entry->arg_data = data;
838 case G_OPTION_ARG_STRING_ARRAY:
842 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
847 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
850 if (change->allocated.array.len == 0)
852 change->prev.array = *(gchar ***)entry->arg_data;
853 change->allocated.array.data = g_new (gchar *, 2);
856 change->allocated.array.data =
857 g_renew (gchar *, change->allocated.array.data,
858 change->allocated.array.len + 2);
860 change->allocated.array.data[change->allocated.array.len] = data;
861 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
863 change->allocated.array.len ++;
865 *(gchar ***)entry->arg_data = change->allocated.array.data;
870 case G_OPTION_ARG_FILENAME:
875 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
880 data = g_strdup (value);
882 change = get_change (context, G_OPTION_ARG_FILENAME,
884 g_free (change->allocated.str);
886 change->prev.str = *(gchar **)entry->arg_data;
887 change->allocated.str = data;
889 *(gchar **)entry->arg_data = data;
893 case G_OPTION_ARG_FILENAME_ARRAY:
898 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
903 data = g_strdup (value);
905 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
908 if (change->allocated.array.len == 0)
910 change->prev.array = *(gchar ***)entry->arg_data;
911 change->allocated.array.data = g_new (gchar *, 2);
914 change->allocated.array.data =
915 g_renew (gchar *, change->allocated.array.data,
916 change->allocated.array.len + 2);
918 change->allocated.array.data[change->allocated.array.len] = data;
919 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
921 change->allocated.array.len ++;
923 *(gchar ***)entry->arg_data = change->allocated.array.data;
928 case G_OPTION_ARG_INT:
932 if (!parse_int (option_name, value,
937 change = get_change (context, G_OPTION_ARG_INT,
939 change->prev.integer = *(gint *)entry->arg_data;
940 *(gint *)entry->arg_data = data;
943 case G_OPTION_ARG_CALLBACK:
948 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
950 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
952 else if (entry->flags & G_OPTION_FLAG_FILENAME)
955 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
957 data = g_strdup (value);
961 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
963 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
967 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
975 case G_OPTION_ARG_DOUBLE:
979 if (!parse_double (option_name, value,
986 change = get_change (context, G_OPTION_ARG_DOUBLE,
988 change->prev.dbl = *(gdouble *)entry->arg_data;
989 *(gdouble *)entry->arg_data = data;
992 case G_OPTION_ARG_INT64:
996 if (!parse_int64 (option_name, value,
1003 change = get_change (context, G_OPTION_ARG_INT64,
1005 change->prev.int64 = *(gint64 *)entry->arg_data;
1006 *(gint64 *)entry->arg_data = data;
1010 g_assert_not_reached ();
1017 parse_short_option (GOptionContext *context,
1018 GOptionGroup *group,
1029 for (j = 0; j < group->n_entries; j++)
1031 if (arg == group->entries[j].short_name)
1034 gchar *value = NULL;
1036 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1038 if (NO_ARG (&group->entries[j]))
1042 if (*new_index > index)
1045 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1046 _("Error parsing option %s"), option_name);
1047 g_free (option_name);
1051 if (index < *argc - 1)
1053 if (!OPTIONAL_ARG (&group->entries[j]))
1055 value = (*argv)[index + 1];
1056 add_pending_null (context, &((*argv)[index + 1]), NULL);
1057 *new_index = index+1;
1061 if ((*argv)[index + 1][0] == '-')
1065 value = (*argv)[index + 1];
1066 add_pending_null (context, &((*argv)[index + 1]), NULL);
1067 *new_index = index + 1;
1071 else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1076 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1077 _("Missing argument for %s"), option_name);
1078 g_free (option_name);
1083 if (!parse_arg (context, group, &group->entries[j],
1084 value, option_name, error))
1086 g_free (option_name);
1090 g_free (option_name);
1099 parse_long_option (GOptionContext *context,
1100 GOptionGroup *group,
1111 for (j = 0; j < group->n_entries; j++)
1113 if (*index >= *argc)
1116 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1119 if (NO_ARG (&group->entries[j]) &&
1120 strcmp (arg, group->entries[j].long_name) == 0)
1124 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1125 parse_arg (context, group, &group->entries[j],
1126 NULL, option_name, error);
1127 g_free(option_name);
1129 add_pending_null (context, &((*argv)[*index]), NULL);
1134 gint len = strlen (group->entries[j].long_name);
1136 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1137 (arg[len] == '=' || arg[len] == 0))
1139 gchar *value = NULL;
1142 add_pending_null (context, &((*argv)[*index]), NULL);
1143 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1145 if (arg[len] == '=')
1146 value = arg + len + 1;
1147 else if (*index < *argc - 1)
1149 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1151 value = (*argv)[*index + 1];
1152 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1157 if ((*argv)[*index + 1][0] == '-')
1160 retval = parse_arg (context, group, &group->entries[j],
1161 NULL, option_name, error);
1163 g_free (option_name);
1168 value = (*argv)[*index + 1];
1169 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1174 else if (*index >= *argc - 1 &&
1175 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1178 retval = parse_arg (context, group, &group->entries[j],
1179 NULL, option_name, error);
1181 g_free (option_name);
1187 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1188 _("Missing argument for %s"), option_name);
1189 g_free (option_name);
1193 if (!parse_arg (context, group, &group->entries[j],
1194 value, option_name, error))
1196 g_free (option_name);
1200 g_free (option_name);
1210 parse_remaining_arg (GOptionContext *context,
1211 GOptionGroup *group,
1220 for (j = 0; j < group->n_entries; j++)
1222 if (*index >= *argc)
1225 if (group->entries[j].long_name[0])
1228 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1229 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1231 add_pending_null (context, &((*argv)[*index]), NULL);
1233 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1244 free_changes_list (GOptionContext *context,
1249 for (list = context->changes; list != NULL; list = list->next)
1251 Change *change = list->data;
1255 switch (change->arg_type)
1257 case G_OPTION_ARG_NONE:
1258 *(gboolean *)change->arg_data = change->prev.bool;
1260 case G_OPTION_ARG_INT:
1261 *(gint *)change->arg_data = change->prev.integer;
1263 case G_OPTION_ARG_STRING:
1264 case G_OPTION_ARG_FILENAME:
1265 g_free (change->allocated.str);
1266 *(gchar **)change->arg_data = change->prev.str;
1268 case G_OPTION_ARG_STRING_ARRAY:
1269 case G_OPTION_ARG_FILENAME_ARRAY:
1270 g_strfreev (change->allocated.array.data);
1271 *(gchar ***)change->arg_data = change->prev.array;
1273 case G_OPTION_ARG_DOUBLE:
1274 *(gdouble *)change->arg_data = change->prev.dbl;
1276 case G_OPTION_ARG_INT64:
1277 *(gint64 *)change->arg_data = change->prev.int64;
1280 g_assert_not_reached ();
1287 g_list_free (context->changes);
1288 context->changes = NULL;
1292 free_pending_nulls (GOptionContext *context,
1293 gboolean perform_nulls)
1297 for (list = context->pending_nulls; list != NULL; list = list->next)
1299 PendingNull *n = list->data;
1305 /* Copy back the short options */
1307 strcpy (*n->ptr + 1, n->value);
1317 g_list_free (context->pending_nulls);
1318 context->pending_nulls = NULL;
1322 * g_option_context_parse:
1323 * @context: a #GOptionContext
1324 * @argc: a pointer to the number of command line arguments.
1325 * @argv: a pointer to the array of command line arguments.
1326 * @error: a return location for errors
1328 * Parses the command line arguments, recognizing options
1329 * which have been added to @context. A side-effect of
1330 * calling this function is that g_set_prgname() will be
1333 * If the parsing is successful, any parsed arguments are
1334 * removed from the array and @argc and @argv are updated
1335 * accordingly. A '--' option is stripped from @argv
1336 * unless there are unparsed options before and after it,
1337 * or some of the options after it start with '-'. In case
1338 * of an error, @argc and @argv are left unmodified.
1340 * If automatic <option>--help</option> support is enabled
1341 * (see g_option_context_set_help_enabled()), and the
1342 * @argv array contains one of the recognized help options,
1343 * this function will produce help output to stdout and
1344 * call <literal>exit (0)</literal>.
1346 * Return value: %TRUE if the parsing was successful,
1347 * %FALSE if an error occurred
1352 g_option_context_parse (GOptionContext *context,
1360 /* Set program name */
1361 if (!g_get_prgname())
1363 if (argc && argv && *argc)
1367 prgname = g_path_get_basename ((*argv)[0]);
1368 g_set_prgname (prgname);
1372 g_set_prgname ("<unknown>");
1375 /* Call pre-parse hooks */
1376 list = context->groups;
1379 GOptionGroup *group = list->data;
1381 if (group->pre_parse_func)
1383 if (!(* group->pre_parse_func) (context, group,
1384 group->user_data, error))
1391 if (context->main_group && context->main_group->pre_parse_func)
1393 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1394 context->main_group->user_data, error))
1400 gboolean stop_parsing = FALSE;
1401 gboolean has_unknown = FALSE;
1402 gint separator_pos = 0;
1404 for (i = 1; i < *argc; i++)
1407 gboolean parsed = FALSE;
1409 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1411 if ((*argv)[i][1] == '-')
1415 arg = (*argv)[i] + 2;
1417 /* '--' terminates list of arguments */
1421 stop_parsing = TRUE;
1425 /* Handle help options */
1426 if (context->help_enabled)
1428 if (strcmp (arg, "help") == 0)
1429 print_help (context, TRUE, NULL);
1430 else if (strcmp (arg, "help-all") == 0)
1431 print_help (context, FALSE, NULL);
1432 else if (strncmp (arg, "help-", 5) == 0)
1436 list = context->groups;
1440 GOptionGroup *group = list->data;
1442 if (strcmp (arg + 5, group->name) == 0)
1443 print_help (context, FALSE, group);
1450 if (context->main_group &&
1451 !parse_long_option (context, context->main_group, &i, arg,
1452 FALSE, argc, argv, error, &parsed))
1458 /* Try the groups */
1459 list = context->groups;
1462 GOptionGroup *group = list->data;
1464 if (!parse_long_option (context, group, &i, arg,
1465 FALSE, argc, argv, error, &parsed))
1477 /* Now look for --<group>-<option> */
1478 dash = strchr (arg, '-');
1481 /* Try the groups */
1482 list = context->groups;
1485 GOptionGroup *group = list->data;
1487 if (strncmp (group->name, arg, dash - arg) == 0)
1489 if (!parse_long_option (context, group, &i, dash + 1,
1490 TRUE, argc, argv, error, &parsed))
1501 if (context->ignore_unknown)
1505 { /* short option */
1506 gint j, new_i = i, arg_length;
1507 gboolean *nulled_out = NULL;
1508 arg = (*argv)[i] + 1;
1509 arg_length = strlen (arg);
1510 nulled_out = g_newa (gboolean, arg_length);
1511 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1512 for (j = 0; j < arg_length; j++)
1514 if (context->help_enabled && arg[j] == '?')
1515 print_help (context, TRUE, NULL);
1517 if (context->main_group &&
1518 !parse_short_option (context, context->main_group,
1520 argc, argv, error, &parsed))
1524 /* Try the groups */
1525 list = context->groups;
1528 GOptionGroup *group = list->data;
1529 if (!parse_short_option (context, group, i, &new_i, arg[j],
1530 argc, argv, error, &parsed))
1538 if (context->ignore_unknown && parsed)
1539 nulled_out[j] = TRUE;
1540 else if (context->ignore_unknown)
1544 /* !context->ignore_unknown && parsed */
1546 if (context->ignore_unknown)
1548 gchar *new_arg = NULL;
1550 for (j = 0; j < arg_length; j++)
1555 new_arg = g_malloc (arg_length + 1);
1556 new_arg[arg_index++] = arg[j];
1560 new_arg[arg_index] = '\0';
1561 add_pending_null (context, &((*argv)[i]), new_arg);
1565 add_pending_null (context, &((*argv)[i]), NULL);
1573 if (!parsed && !context->ignore_unknown)
1576 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1577 _("Unknown option %s"), (*argv)[i]);
1583 /* Collect remaining args */
1584 if (context->main_group &&
1585 !parse_remaining_arg (context, context->main_group, &i,
1586 argc, argv, error, &parsed))
1589 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1594 if (separator_pos > 0)
1595 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1599 /* Call post-parse hooks */
1600 list = context->groups;
1603 GOptionGroup *group = list->data;
1605 if (group->post_parse_func)
1607 if (!(* group->post_parse_func) (context, group,
1608 group->user_data, error))
1615 if (context->main_group && context->main_group->post_parse_func)
1617 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1618 context->main_group->user_data, error))
1624 free_pending_nulls (context, TRUE);
1626 for (i = 1; i < *argc; i++)
1628 for (k = i; k < *argc; k++)
1629 if ((*argv)[k] != NULL)
1635 for (j = i + k; j < *argc; j++)
1637 (*argv)[j-k] = (*argv)[j];
1649 /* Call error hooks */
1650 list = context->groups;
1653 GOptionGroup *group = list->data;
1655 if (group->error_func)
1656 (* group->error_func) (context, group,
1657 group->user_data, error);
1662 if (context->main_group && context->main_group->error_func)
1663 (* context->main_group->error_func) (context, context->main_group,
1664 context->main_group->user_data, error);
1666 free_changes_list (context, TRUE);
1667 free_pending_nulls (context, FALSE);
1673 * g_option_group_new:
1674 * @name: the name for the option group, this is used to provide
1675 * help for the options in this group with <option>--help-</option>@name
1676 * @description: a description for this group to be shown in
1677 * <option>--help</option>. This string is translated using the translation
1678 * domain or translation function of the group
1679 * @help_description: a description for the <option>--help-</option>@name option.
1680 * This string is translated using the translation domain or translation function
1682 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1683 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1684 * @destroy: a function that will be called to free @user_data, or %NULL
1686 * Creates a new #GOptionGroup.
1688 * Return value: a newly created option group. It should be added
1689 * to a #GOptionContext or freed with g_option_group_free().
1694 g_option_group_new (const gchar *name,
1695 const gchar *description,
1696 const gchar *help_description,
1698 GDestroyNotify destroy)
1701 GOptionGroup *group;
1703 group = g_new0 (GOptionGroup, 1);
1704 group->name = g_strdup (name);
1705 group->description = g_strdup (description);
1706 group->help_description = g_strdup (help_description);
1707 group->user_data = user_data;
1708 group->destroy_notify = destroy;
1715 * g_option_group_free:
1716 * @group: a #GOptionGroup
1718 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1719 * free groups which have been added to a #GOptionContext.
1724 g_option_group_free (GOptionGroup *group)
1726 g_return_if_fail (group != NULL);
1728 g_free (group->name);
1729 g_free (group->description);
1730 g_free (group->help_description);
1732 g_free (group->entries);
1734 if (group->destroy_notify)
1735 (* group->destroy_notify) (group->user_data);
1737 if (group->translate_notify)
1738 (* group->translate_notify) (group->translate_data);
1745 * g_option_group_add_entries:
1746 * @group: a #GOptionGroup
1747 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1749 * Adds the options specified in @entries to @group.
1754 g_option_group_add_entries (GOptionGroup *group,
1755 const GOptionEntry *entries)
1759 g_return_if_fail (entries != NULL);
1761 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1763 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1765 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1767 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1769 gchar c = group->entries[i].short_name;
1773 if (c == '-' || !g_ascii_isprint (c))
1775 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1776 group->entries[i].short_name = 0;
1781 group->n_entries += n_entries;
1785 * g_option_group_set_parse_hooks:
1786 * @group: a #GOptionGroup
1787 * @pre_parse_func: a function to call before parsing, or %NULL
1788 * @post_parse_func: a function to call after parsing, or %NULL
1790 * Associates two functions with @group which will be called
1791 * from g_option_context_parse() before the first option is parsed
1792 * and after the last option has been parsed, respectively.
1794 * Note that the user data to be passed to @pre_parse_func and
1795 * @post_parse_func can be specified when constructing the group
1796 * with g_option_group_new().
1801 g_option_group_set_parse_hooks (GOptionGroup *group,
1802 GOptionParseFunc pre_parse_func,
1803 GOptionParseFunc post_parse_func)
1805 g_return_if_fail (group != NULL);
1807 group->pre_parse_func = pre_parse_func;
1808 group->post_parse_func = post_parse_func;
1812 * g_option_group_set_error_hook:
1813 * @group: a #GOptionGroup
1814 * @error_func: a function to call when an error occurs
1816 * Associates a function with @group which will be called
1817 * from g_option_context_parse() when an error occurs.
1819 * Note that the user data to be passed to @pre_parse_func and
1820 * @post_parse_func can be specified when constructing the group
1821 * with g_option_group_new().
1826 g_option_group_set_error_hook (GOptionGroup *group,
1827 GOptionErrorFunc error_func)
1829 g_return_if_fail (group != NULL);
1831 group->error_func = error_func;
1836 * g_option_group_set_translate_func:
1837 * @group: a #GOptionGroup
1838 * @func: the #GTranslateFunc, or %NULL
1839 * @data: user data to pass to @func, or %NULL
1840 * @destroy_notify: a function which gets called to free @data, or %NULL
1842 * Sets the function which is used to translate user-visible
1843 * strings, for <option>--help</option> output. Different
1844 * groups can use different #GTranslateFunc<!-- -->s. If @func
1845 * is %NULL, strings are not translated.
1847 * If you are using gettext(), you only need to set the translation
1848 * domain, see g_option_group_set_translation_domain().
1853 g_option_group_set_translate_func (GOptionGroup *group,
1854 GTranslateFunc func,
1856 GDestroyNotify destroy_notify)
1858 g_return_if_fail (group != NULL);
1860 if (group->translate_notify)
1861 group->translate_notify (group->translate_data);
1863 group->translate_func = func;
1864 group->translate_data = data;
1865 group->translate_notify = destroy_notify;
1869 dgettext_swapped (const gchar *msgid,
1870 const gchar *domainname)
1872 return dgettext (domainname, msgid);
1876 * g_option_group_set_translation_domain:
1877 * @group: a #GOptionGroup
1878 * @domain: the domain to use
1880 * A convenience function to use gettext() for translating
1881 * user-visible strings.
1886 g_option_group_set_translation_domain (GOptionGroup *group,
1887 const gchar *domain)
1889 g_return_if_fail (group != NULL);
1891 g_option_group_set_translate_func (group,
1892 (GTranslateFunc)dgettext_swapped,
1897 #define __G_OPTION_C__
1898 #include "galiasdef.c"