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)
72 struct _GOptionContext
76 gchar *parameter_string;
78 gboolean help_enabled;
79 gboolean ignore_unknown;
81 GOptionGroup *main_group;
83 /* We keep a list of change so we can revert them */
86 /* We also keep track of all argv elements that should be NULLed or
96 gchar *help_description;
98 GDestroyNotify destroy_notify;
101 GTranslateFunc translate_func;
102 GDestroyNotify translate_notify;
103 gpointer translate_data;
105 GOptionEntry *entries;
108 GOptionParseFunc pre_parse_func;
109 GOptionParseFunc post_parse_func;
110 GOptionErrorFunc error_func;
113 static void free_changes_list (GOptionContext *context,
115 static void free_pending_nulls (GOptionContext *context,
116 gboolean perform_nulls);
119 g_option_error_quark (void)
121 return g_quark_from_static_string ("g-option-context-error-quark");
125 * g_option_context_new:
126 * @parameter_string: a string which is displayed in
127 * the first line of <option>--help</option> output, after the
129 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
131 * Creates a new option context.
133 * The @parameter_text can serve multiple purposes. It can be used
134 * to add descriptions for "rest" arguments, which are not parsed by
135 * the #GOptionContext, typically something like "FILES" or
136 * "FILE1 FILE2...". (If you are using #G_OPTION_REMAINING for
137 * collecting "rest" arguments, GLib handles this automatically by
138 * using the @arg_description of the corresponding #GOptionEntry in
139 * the usage summary.)
141 * Another common usage is to give a summary of the program
142 * functionality. This can be a short summary on the same line,
143 * like " - frob the strings", or a longer description in a paragraph
144 * below the usage summary. In this case, @parameter_string should start
145 * with two newlines, to separate the description from the usage summary:
146 * "\n\nA program to frob strings, which will..."
148 * Returns: a newly created #GOptionContext, which must be
149 * freed with g_option_context_free() after use.
154 g_option_context_new (const gchar *parameter_string)
157 GOptionContext *context;
159 context = g_new0 (GOptionContext, 1);
161 context->parameter_string = g_strdup (parameter_string);
162 context->help_enabled = TRUE;
163 context->ignore_unknown = FALSE;
169 * g_option_context_free:
170 * @context: a #GOptionContext
172 * Frees context and all the groups which have been
177 void g_option_context_free (GOptionContext *context)
179 g_return_if_fail (context != NULL);
181 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
182 g_list_free (context->groups);
184 if (context->main_group)
185 g_option_group_free (context->main_group);
187 free_changes_list (context, FALSE);
188 free_pending_nulls (context, FALSE);
190 g_free (context->parameter_string);
197 * g_option_context_set_help_enabled:
198 * @context: a #GOptionContext
199 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
201 * Enables or disables automatic generation of <option>--help</option>
202 * output. By default, g_option_context_parse() recognizes
203 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
204 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
205 * suitable output to stdout.
209 void g_option_context_set_help_enabled (GOptionContext *context,
210 gboolean help_enabled)
213 g_return_if_fail (context != NULL);
215 context->help_enabled = help_enabled;
219 * g_option_context_get_help_enabled:
220 * @context: a #GOptionContext
222 * Returns whether automatic <option>--help</option> generation
223 * is turned on for @context. See g_option_context_set_help_enabled().
225 * Returns: %TRUE if automatic help generation is turned on.
230 g_option_context_get_help_enabled (GOptionContext *context)
232 g_return_val_if_fail (context != NULL, FALSE);
234 return context->help_enabled;
238 * g_option_context_set_ignore_unknown_options:
239 * @context: a #GOptionContext
240 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
241 * an error when unknown options are met
243 * Sets whether to ignore unknown options or not. If an argument is
244 * ignored, it is left in the @argv array after parsing. By default,
245 * g_option_context_parse() treats unknown options as error.
247 * This setting does not affect non-option arguments (i.e. arguments
248 * which don't start with a dash). But note that GOption cannot reliably
249 * determine whether a non-option belongs to a preceding unknown option.
254 g_option_context_set_ignore_unknown_options (GOptionContext *context,
255 gboolean ignore_unknown)
257 g_return_if_fail (context != NULL);
259 context->ignore_unknown = ignore_unknown;
263 * g_option_context_get_ignore_unknown_options:
264 * @context: a #GOptionContext
266 * Returns whether unknown options are ignored or not. See
267 * g_option_context_set_ignore_unknown_options().
269 * Returns: %TRUE if unknown options are ignored.
274 g_option_context_get_ignore_unknown_options (GOptionContext *context)
276 g_return_val_if_fail (context != NULL, FALSE);
278 return context->ignore_unknown;
282 * g_option_context_add_group:
283 * @context: a #GOptionContext
284 * @group: the group to add
286 * Adds a #GOptionGroup to the @context, so that parsing with @context
287 * will recognize the options in the group. Note that the group will
288 * be freed together with the context when g_option_context_free() is
289 * called, so you must not free the group yourself after adding it
295 g_option_context_add_group (GOptionContext *context,
300 g_return_if_fail (context != NULL);
301 g_return_if_fail (group != NULL);
302 g_return_if_fail (group->name != NULL);
303 g_return_if_fail (group->description != NULL);
304 g_return_if_fail (group->help_description != NULL);
306 for (list = context->groups; list; list = list->next)
308 GOptionGroup *g = (GOptionGroup *)list->data;
310 if ((group->name == NULL && g->name == NULL) ||
311 (group->name && g->name && strcmp (group->name, g->name) == 0))
312 g_warning ("A group named \"%s\" is already part of this GOptionContext",
316 context->groups = g_list_append (context->groups, group);
320 * g_option_context_set_main_group:
321 * @context: a #GOptionContext
322 * @group: the group to set as main group
324 * Sets a #GOptionGroup as main group of the @context.
325 * This has the same effect as calling g_option_context_add_group(),
326 * the only difference is that the options in the main group are
327 * treated differently when generating <option>--help</option> output.
332 g_option_context_set_main_group (GOptionContext *context,
335 g_return_if_fail (context != NULL);
336 g_return_if_fail (group != NULL);
338 if (context->main_group)
340 g_warning ("This GOptionContext already has a main group");
345 context->main_group = group;
349 * g_option_context_get_main_group:
350 * @context: a #GOptionContext
352 * Returns a pointer to the main group of @context.
354 * Return value: the main group of @context, or %NULL if @context doesn't
355 * have a main group. Note that group belongs to @context and should
356 * not be modified or freed.
361 g_option_context_get_main_group (GOptionContext *context)
363 g_return_val_if_fail (context != NULL, NULL);
365 return context->main_group;
369 * g_option_context_add_main_entries:
370 * @context: a #GOptionContext
371 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
372 * @translation_domain: a translation domain to use for translating
373 * the <option>--help</option> output for the options in @entries
374 * with gettext(), or %NULL
376 * A convenience function which creates a main group if it doesn't
377 * exist, adds the @entries to it and sets the translation domain.
382 g_option_context_add_main_entries (GOptionContext *context,
383 const GOptionEntry *entries,
384 const gchar *translation_domain)
386 g_return_if_fail (entries != NULL);
388 if (!context->main_group)
389 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
391 g_option_group_add_entries (context->main_group, entries);
392 g_option_group_set_translation_domain (context->main_group, translation_domain);
396 calculate_max_length (GOptionGroup *group)
399 gint i, len, max_length;
403 for (i = 0; i < group->n_entries; i++)
405 entry = &group->entries[i];
407 if (entry->flags & G_OPTION_FLAG_HIDDEN)
410 len = g_utf8_strlen (entry->long_name, -1);
412 if (entry->short_name)
415 if (!NO_ARG (entry) && entry->arg_description)
416 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
418 max_length = MAX (max_length, len);
425 print_entry (GOptionGroup *group,
427 const GOptionEntry *entry)
431 if (entry->flags & G_OPTION_FLAG_HIDDEN)
434 if (entry->long_name[0] == 0)
437 str = g_string_new (NULL);
439 if (entry->short_name)
440 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
442 g_string_append_printf (str, " --%s", entry->long_name);
444 if (entry->arg_description)
445 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
447 g_print ("%-*s %s\n", max_length + 4, str->str,
448 entry->description ? TRANSLATE (group, entry->description) : "");
449 g_string_free (str, TRUE);
453 print_help (GOptionContext *context,
458 gint max_length, len;
461 GHashTable *shadow_map;
463 const gchar *rest_description;
465 rest_description = NULL;
466 if (context->main_group)
468 for (i = 0; i < context->main_group->n_entries; i++)
470 entry = &context->main_group->entries[i];
471 if (entry->long_name[0] == 0)
473 rest_description = entry->arg_description;
479 g_print ("%s\n %s %s%s%s%s%s\n\n",
480 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
481 rest_description ? " " : "",
482 rest_description ? rest_description : "",
483 context->parameter_string ? " " : "",
484 context->parameter_string ? context->parameter_string : "");
486 memset (seen, 0, sizeof (gboolean) * 256);
487 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
489 if (context->main_group)
491 for (i = 0; i < context->main_group->n_entries; i++)
493 entry = &context->main_group->entries[i];
494 g_hash_table_insert (shadow_map,
495 (gpointer)entry->long_name,
498 if (seen[(guchar)entry->short_name])
499 entry->short_name = 0;
501 seen[(guchar)entry->short_name] = TRUE;
505 list = context->groups;
508 GOptionGroup *group = list->data;
509 for (i = 0; i < group->n_entries; i++)
511 entry = &group->entries[i];
512 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
513 !(entry->flags && G_OPTION_FLAG_NOALIAS))
514 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
516 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
518 if (seen[(guchar)entry->short_name] &&
519 !(entry->flags && G_OPTION_FLAG_NOALIAS))
520 entry->short_name = 0;
522 seen[(guchar)entry->short_name] = TRUE;
527 g_hash_table_destroy (shadow_map);
529 list = context->groups;
531 max_length = g_utf8_strlen ("-?, --help", -1);
535 len = g_utf8_strlen ("--help-all", -1);
536 max_length = MAX (max_length, len);
539 if (context->main_group)
541 len = calculate_max_length (context->main_group);
542 max_length = MAX (max_length, len);
547 GOptionGroup *group = list->data;
549 /* First, we check the --help-<groupname> options */
550 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
551 max_length = MAX (max_length, len);
553 /* Then we go through the entries */
554 len = calculate_max_length (group);
555 max_length = MAX (max_length, len);
560 /* Add a bit of padding */
565 list = context->groups;
567 g_print ("%s\n -%c, --%-*s %s\n",
568 _("Help Options:"), '?', max_length - 4, "help",
569 _("Show help options"));
571 /* We only want --help-all when there are groups */
573 g_print (" --%-*s %s\n", max_length, "help-all",
574 _("Show all help options"));
578 GOptionGroup *group = list->data;
580 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
581 TRANSLATE (group, group->help_description));
591 /* Print a certain group */
593 g_print ("%s\n", TRANSLATE (group, group->description));
594 for (i = 0; i < group->n_entries; i++)
595 print_entry (group, max_length, &group->entries[i]);
600 /* Print all groups */
602 list = context->groups;
606 GOptionGroup *group = list->data;
608 g_print ("%s\n", group->description);
610 for (i = 0; i < group->n_entries; i++)
611 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
612 print_entry (group, max_length, &group->entries[i]);
619 /* Print application options if --help or --help-all has been specified */
620 if (main_help || !group)
622 list = context->groups;
624 g_print ("%s\n", _("Application Options:"));
626 if (context->main_group)
627 for (i = 0; i < context->main_group->n_entries; i++)
628 print_entry (context->main_group, max_length,
629 &context->main_group->entries[i]);
633 GOptionGroup *group = list->data;
635 /* Print main entries from other groups */
636 for (i = 0; i < group->n_entries; i++)
637 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
638 print_entry (group, max_length, &group->entries[i]);
650 parse_int (const gchar *arg_name,
659 tmp = strtol (arg, &end, 0);
661 if (*arg == '\0' || *end != '\0')
664 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
665 _("Cannot parse integer value '%s' for %s"),
671 if (*result != tmp || errno == ERANGE)
674 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
675 _("Integer value '%s' for %s out of range"),
685 parse_double (const gchar *arg_name,
694 tmp = g_strtod (arg, &end);
696 if (*arg == '\0' || *end != '\0')
699 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
700 _("Cannot parse double value '%s' for %s"),
707 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
708 _("Double value '%s' for %s out of range"),
720 get_change (GOptionContext *context,
725 Change *change = NULL;
727 for (list = context->changes; list != NULL; list = list->next)
731 if (change->arg_data == arg_data)
735 change = g_new0 (Change, 1);
736 change->arg_type = arg_type;
737 change->arg_data = arg_data;
739 context->changes = g_list_prepend (context->changes, change);
747 add_pending_null (GOptionContext *context,
753 n = g_new0 (PendingNull, 1);
757 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
761 parse_arg (GOptionContext *context,
765 const gchar *option_name,
771 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
775 case G_OPTION_ARG_NONE:
777 change = get_change (context, G_OPTION_ARG_NONE,
780 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
783 case G_OPTION_ARG_STRING:
787 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
792 change = get_change (context, G_OPTION_ARG_STRING,
794 g_free (change->allocated.str);
796 change->prev.str = *(gchar **)entry->arg_data;
797 change->allocated.str = data;
799 *(gchar **)entry->arg_data = data;
802 case G_OPTION_ARG_STRING_ARRAY:
806 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
811 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
814 if (change->allocated.array.len == 0)
816 change->prev.array = *(gchar ***)entry->arg_data;
817 change->allocated.array.data = g_new (gchar *, 2);
820 change->allocated.array.data =
821 g_renew (gchar *, change->allocated.array.data,
822 change->allocated.array.len + 2);
824 change->allocated.array.data[change->allocated.array.len] = data;
825 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
827 change->allocated.array.len ++;
829 *(gchar ***)entry->arg_data = change->allocated.array.data;
834 case G_OPTION_ARG_FILENAME:
839 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
844 data = g_strdup (value);
846 change = get_change (context, G_OPTION_ARG_FILENAME,
848 g_free (change->allocated.str);
850 change->prev.str = *(gchar **)entry->arg_data;
851 change->allocated.str = data;
853 *(gchar **)entry->arg_data = data;
857 case G_OPTION_ARG_FILENAME_ARRAY:
862 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
867 data = g_strdup (value);
869 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
872 if (change->allocated.array.len == 0)
874 change->prev.array = *(gchar ***)entry->arg_data;
875 change->allocated.array.data = g_new (gchar *, 2);
878 change->allocated.array.data =
879 g_renew (gchar *, change->allocated.array.data,
880 change->allocated.array.len + 2);
882 change->allocated.array.data[change->allocated.array.len] = data;
883 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
885 change->allocated.array.len ++;
887 *(gchar ***)entry->arg_data = change->allocated.array.data;
892 case G_OPTION_ARG_INT:
896 if (!parse_int (option_name, value,
901 change = get_change (context, G_OPTION_ARG_INT,
903 change->prev.integer = *(gint *)entry->arg_data;
904 *(gint *)entry->arg_data = data;
907 case G_OPTION_ARG_CALLBACK:
912 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
914 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
916 else if (entry->flags & G_OPTION_FLAG_FILENAME)
919 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
921 data = g_strdup (value);
925 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
927 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
931 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
939 case G_OPTION_ARG_DOUBLE:
943 if (!parse_double (option_name, value,
950 change = get_change (context, G_OPTION_ARG_DOUBLE,
952 change->prev.dbl = *(gdouble *)entry->arg_data;
953 *(gdouble *)entry->arg_data = data;
957 g_assert_not_reached ();
964 parse_short_option (GOptionContext *context,
976 for (j = 0; j < group->n_entries; j++)
978 if (arg == group->entries[j].short_name)
983 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
985 if (NO_ARG (&group->entries[j]))
989 if (*new_index > index)
992 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
993 _("Error parsing option %s"), option_name);
994 g_free (option_name);
998 if (index < *argc - 1)
1000 if (!OPTIONAL_ARG (&group->entries[j]))
1002 value = (*argv)[index + 1];
1003 add_pending_null (context, &((*argv)[index + 1]), NULL);
1004 *new_index = index+1;
1008 if ((*argv)[index + 1][0] == '-')
1012 value = (*argv)[index + 1];
1013 add_pending_null (context, &((*argv)[index + 1]), NULL);
1014 *new_index = index + 1;
1018 else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1023 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1024 _("Missing argument for %s"), option_name);
1025 g_free (option_name);
1030 if (!parse_arg (context, group, &group->entries[j],
1031 value, option_name, error))
1033 g_free (option_name);
1037 g_free (option_name);
1046 parse_long_option (GOptionContext *context,
1047 GOptionGroup *group,
1058 for (j = 0; j < group->n_entries; j++)
1060 if (*index >= *argc)
1063 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1066 if (NO_ARG (&group->entries[j]) &&
1067 strcmp (arg, group->entries[j].long_name) == 0)
1071 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1072 parse_arg (context, group, &group->entries[j],
1073 NULL, option_name, error);
1074 g_free(option_name);
1076 add_pending_null (context, &((*argv)[*index]), NULL);
1081 gint len = strlen (group->entries[j].long_name);
1083 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1084 (arg[len] == '=' || arg[len] == 0))
1086 gchar *value = NULL;
1089 add_pending_null (context, &((*argv)[*index]), NULL);
1090 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1092 if (arg[len] == '=')
1093 value = arg + len + 1;
1094 else if (*index < *argc - 1)
1096 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1098 value = (*argv)[*index + 1];
1099 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1104 if ((*argv)[*index + 1][0] == '-')
1107 retval = parse_arg (context, group, &group->entries[j],
1108 NULL, option_name, error);
1110 g_free (option_name);
1115 value = (*argv)[*index + 1];
1116 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1121 else if (*index >= *argc - 1 &&
1122 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1125 retval = parse_arg (context, group, &group->entries[j],
1126 NULL, option_name, error);
1128 g_free (option_name);
1134 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1135 _("Missing argument for %s"), option_name);
1136 g_free (option_name);
1140 if (!parse_arg (context, group, &group->entries[j],
1141 value, option_name, error))
1143 g_free (option_name);
1147 g_free (option_name);
1157 parse_remaining_arg (GOptionContext *context,
1158 GOptionGroup *group,
1167 for (j = 0; j < group->n_entries; j++)
1169 if (*index >= *argc)
1172 if (group->entries[j].long_name[0])
1175 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1176 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1178 add_pending_null (context, &((*argv)[*index]), NULL);
1180 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1191 free_changes_list (GOptionContext *context,
1196 for (list = context->changes; list != NULL; list = list->next)
1198 Change *change = list->data;
1202 switch (change->arg_type)
1204 case G_OPTION_ARG_NONE:
1205 *(gboolean *)change->arg_data = change->prev.bool;
1207 case G_OPTION_ARG_INT:
1208 *(gint *)change->arg_data = change->prev.integer;
1210 case G_OPTION_ARG_STRING:
1211 case G_OPTION_ARG_FILENAME:
1212 g_free (change->allocated.str);
1213 *(gchar **)change->arg_data = change->prev.str;
1215 case G_OPTION_ARG_STRING_ARRAY:
1216 case G_OPTION_ARG_FILENAME_ARRAY:
1217 g_strfreev (change->allocated.array.data);
1218 *(gchar ***)change->arg_data = change->prev.array;
1220 case G_OPTION_ARG_DOUBLE:
1221 *(gdouble *)change->arg_data = change->prev.dbl;
1224 g_assert_not_reached ();
1231 g_list_free (context->changes);
1232 context->changes = NULL;
1236 free_pending_nulls (GOptionContext *context,
1237 gboolean perform_nulls)
1241 for (list = context->pending_nulls; list != NULL; list = list->next)
1243 PendingNull *n = list->data;
1249 /* Copy back the short options */
1251 strcpy (*n->ptr + 1, n->value);
1261 g_list_free (context->pending_nulls);
1262 context->pending_nulls = NULL;
1266 * g_option_context_parse:
1267 * @context: a #GOptionContext
1268 * @argc: a pointer to the number of command line arguments.
1269 * @argv: a pointer to the array of command line arguments.
1270 * @error: a return location for errors
1272 * Parses the command line arguments, recognizing options
1273 * which have been added to @context. A side-effect of
1274 * calling this function is that g_set_prgname() will be
1277 * If the parsing is successful, any parsed arguments are
1278 * removed from the array and @argc and @argv are updated
1279 * accordingly. A '--' option is stripped from @argv
1280 * unless there are unparsed options before and after it,
1281 * or some of the options after it start with '-'. In case
1282 * of an error, @argc and @argv are left unmodified.
1284 * If automatic <option>--help</option> support is enabled
1285 * (see g_option_context_set_help_enabled()), and the
1286 * @argv array contains one of the recognized help options,
1287 * this function will produce help output to stdout and
1288 * call <literal>exit (0)</literal>.
1290 * Return value: %TRUE if the parsing was successful,
1291 * %FALSE if an error occurred
1296 g_option_context_parse (GOptionContext *context,
1304 /* Set program name */
1305 if (!g_get_prgname())
1307 if (argc && argv && *argc)
1311 prgname = g_path_get_basename ((*argv)[0]);
1312 g_set_prgname (prgname);
1316 g_set_prgname ("<unknown>");
1319 /* Call pre-parse hooks */
1320 list = context->groups;
1323 GOptionGroup *group = list->data;
1325 if (group->pre_parse_func)
1327 if (!(* group->pre_parse_func) (context, group,
1328 group->user_data, error))
1335 if (context->main_group && context->main_group->pre_parse_func)
1337 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1338 context->main_group->user_data, error))
1344 gboolean stop_parsing = FALSE;
1345 gboolean has_unknown = FALSE;
1346 gint separator_pos = 0;
1348 for (i = 1; i < *argc; i++)
1351 gboolean parsed = FALSE;
1353 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1355 if ((*argv)[i][1] == '-')
1359 arg = (*argv)[i] + 2;
1361 /* '--' terminates list of arguments */
1365 stop_parsing = TRUE;
1369 /* Handle help options */
1370 if (context->help_enabled)
1372 if (strcmp (arg, "help") == 0)
1373 print_help (context, TRUE, NULL);
1374 else if (strcmp (arg, "help-all") == 0)
1375 print_help (context, FALSE, NULL);
1376 else if (strncmp (arg, "help-", 5) == 0)
1380 list = context->groups;
1384 GOptionGroup *group = list->data;
1386 if (strcmp (arg + 5, group->name) == 0)
1387 print_help (context, FALSE, group);
1394 if (context->main_group &&
1395 !parse_long_option (context, context->main_group, &i, arg,
1396 FALSE, argc, argv, error, &parsed))
1402 /* Try the groups */
1403 list = context->groups;
1406 GOptionGroup *group = list->data;
1408 if (!parse_long_option (context, group, &i, arg,
1409 FALSE, argc, argv, error, &parsed))
1421 /* Now look for --<group>-<option> */
1422 dash = strchr (arg, '-');
1425 /* Try the groups */
1426 list = context->groups;
1429 GOptionGroup *group = list->data;
1431 if (strncmp (group->name, arg, dash - arg) == 0)
1433 if (!parse_long_option (context, group, &i, dash + 1,
1434 TRUE, argc, argv, error, &parsed))
1445 if (context->ignore_unknown)
1453 gboolean *nulled_out = NULL;
1455 arg = (*argv)[i] + 1;
1459 if (context->ignore_unknown)
1460 nulled_out = g_new0 (gboolean, strlen (arg));
1462 for (j = 0; j < strlen (arg); j++)
1464 if (context->help_enabled && arg[j] == '?')
1465 print_help (context, TRUE, NULL);
1469 if (context->main_group &&
1470 !parse_short_option (context, context->main_group,
1472 argc, argv, error, &parsed))
1475 g_free (nulled_out);
1481 /* Try the groups */
1482 list = context->groups;
1485 GOptionGroup *group = list->data;
1487 if (!parse_short_option (context, group, i, &new_i, arg[j],
1488 argc, argv, error, &parsed))
1498 if (context->ignore_unknown)
1501 nulled_out[j] = TRUE;
1510 if (context->ignore_unknown)
1512 gchar *new_arg = NULL;
1515 for (j = 0; j < strlen (arg); j++)
1520 new_arg = g_malloc (strlen (arg) + 1);
1521 new_arg[arg_index++] = arg[j];
1525 new_arg[arg_index] = '\0';
1527 add_pending_null (context, &((*argv)[i]), new_arg);
1531 add_pending_null (context, &((*argv)[i]), NULL);
1539 if (!parsed && !context->ignore_unknown)
1542 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1543 _("Unknown option %s"), (*argv)[i]);
1549 /* Collect remaining args */
1550 if (context->main_group &&
1551 !parse_remaining_arg (context, context->main_group, &i,
1552 argc, argv, error, &parsed))
1555 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1560 if (separator_pos > 0)
1561 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1565 /* Call post-parse hooks */
1566 list = context->groups;
1569 GOptionGroup *group = list->data;
1571 if (group->post_parse_func)
1573 if (!(* group->post_parse_func) (context, group,
1574 group->user_data, error))
1581 if (context->main_group && context->main_group->post_parse_func)
1583 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1584 context->main_group->user_data, error))
1590 free_pending_nulls (context, TRUE);
1592 for (i = 1; i < *argc; i++)
1594 for (k = i; k < *argc; k++)
1595 if ((*argv)[k] != NULL)
1601 for (j = i + k; j < *argc; j++)
1603 (*argv)[j-k] = (*argv)[j];
1615 /* Call error hooks */
1616 list = context->groups;
1619 GOptionGroup *group = list->data;
1621 if (group->error_func)
1622 (* group->error_func) (context, group,
1623 group->user_data, error);
1628 if (context->main_group && context->main_group->error_func)
1629 (* context->main_group->error_func) (context, context->main_group,
1630 context->main_group->user_data, error);
1632 free_changes_list (context, TRUE);
1633 free_pending_nulls (context, FALSE);
1639 * g_option_group_new:
1640 * @name: the name for the option group, this is used to provide
1641 * help for the options in this group with <option>--help-</option>@name
1642 * @description: a description for this group to be shown in
1643 * <option>--help</option>. This string is translated using the translation
1644 * domain or translation function of the group
1645 * @help_description: a description for the <option>--help-</option>@name option.
1646 * This string is translated using the translation domain or translation function
1648 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1649 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1650 * @destroy: a function that will be called to free @user_data, or %NULL
1652 * Creates a new #GOptionGroup.
1654 * Return value: a newly created option group. It should be added
1655 * to a #GOptionContext or freed with g_option_group_free().
1660 g_option_group_new (const gchar *name,
1661 const gchar *description,
1662 const gchar *help_description,
1664 GDestroyNotify destroy)
1667 GOptionGroup *group;
1669 group = g_new0 (GOptionGroup, 1);
1670 group->name = g_strdup (name);
1671 group->description = g_strdup (description);
1672 group->help_description = g_strdup (help_description);
1673 group->user_data = user_data;
1674 group->destroy_notify = destroy;
1681 * g_option_group_free:
1682 * @group: a #GOptionGroup
1684 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1685 * free groups which have been added to a #GOptionContext.
1690 g_option_group_free (GOptionGroup *group)
1692 g_return_if_fail (group != NULL);
1694 g_free (group->name);
1695 g_free (group->description);
1696 g_free (group->help_description);
1698 g_free (group->entries);
1700 if (group->destroy_notify)
1701 (* group->destroy_notify) (group->user_data);
1703 if (group->translate_notify)
1704 (* group->translate_notify) (group->translate_data);
1711 * g_option_group_add_entries:
1712 * @group: a #GOptionGroup
1713 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1715 * Adds the options specified in @entries to @group.
1720 g_option_group_add_entries (GOptionGroup *group,
1721 const GOptionEntry *entries)
1725 g_return_if_fail (entries != NULL);
1727 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1729 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1731 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1733 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1735 gchar c = group->entries[i].short_name;
1739 if (c == '-' || !g_ascii_isprint (c))
1741 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1742 group->entries[i].short_name = 0;
1747 group->n_entries += n_entries;
1751 * g_option_group_set_parse_hooks:
1752 * @group: a #GOptionGroup
1753 * @pre_parse_func: a function to call before parsing, or %NULL
1754 * @post_parse_func: a function to call after parsing, or %NULL
1756 * Associates two functions with @group which will be called
1757 * from g_option_context_parse() before the first option is parsed
1758 * and after the last option has been parsed, respectively.
1760 * Note that the user data to be passed to @pre_parse_func and
1761 * @post_parse_func can be specified when constructing the group
1762 * with g_option_group_new().
1767 g_option_group_set_parse_hooks (GOptionGroup *group,
1768 GOptionParseFunc pre_parse_func,
1769 GOptionParseFunc post_parse_func)
1771 g_return_if_fail (group != NULL);
1773 group->pre_parse_func = pre_parse_func;
1774 group->post_parse_func = post_parse_func;
1778 * g_option_group_set_error_hook:
1779 * @group: a #GOptionGroup
1780 * @error_func: a function to call when an error occurs
1782 * Associates a function with @group which will be called
1783 * from g_option_context_parse() when an error occurs.
1785 * Note that the user data to be passed to @pre_parse_func and
1786 * @post_parse_func can be specified when constructing the group
1787 * with g_option_group_new().
1792 g_option_group_set_error_hook (GOptionGroup *group,
1793 GOptionErrorFunc error_func)
1795 g_return_if_fail (group != NULL);
1797 group->error_func = error_func;
1802 * g_option_group_set_translate_func:
1803 * @group: a #GOptionGroup
1804 * @func: the #GTranslateFunc, or %NULL
1805 * @data: user data to pass to @func, or %NULL
1806 * @destroy_notify: a function which gets called to free @data, or %NULL
1808 * Sets the function which is used to translate user-visible
1809 * strings, for <option>--help</option> output. Different
1810 * groups can use different #GTranslateFunc<!-- -->s. If @func
1811 * is %NULL, strings are not translated.
1813 * If you are using gettext(), you only need to set the translation
1814 * domain, see g_option_group_set_translation_domain().
1819 g_option_group_set_translate_func (GOptionGroup *group,
1820 GTranslateFunc func,
1822 GDestroyNotify destroy_notify)
1824 g_return_if_fail (group != NULL);
1826 if (group->translate_notify)
1827 group->translate_notify (group->translate_data);
1829 group->translate_func = func;
1830 group->translate_data = data;
1831 group->translate_notify = destroy_notify;
1835 dgettext_swapped (const gchar *msgid,
1836 const gchar *domainname)
1838 return dgettext (domainname, msgid);
1842 * g_option_group_set_translation_domain:
1843 * @group: a #GOptionGroup
1844 * @domain: the domain to use
1846 * A convenience function to use gettext() for translating
1847 * user-visible strings.
1852 g_option_group_set_translation_domain (GOptionGroup *group,
1853 const gchar *domain)
1855 g_return_if_fail (group != NULL);
1857 g_option_group_set_translate_func (group,
1858 (GTranslateFunc)dgettext_swapped,
1863 #define __G_OPTION_C__
1864 #include "galiasdef.c"