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)))
60 struct _GOptionContext
64 gchar *parameter_string;
66 gboolean help_enabled;
67 gboolean ignore_unknown;
69 GOptionGroup *main_group;
71 /* We keep a list of change so we can revert them */
74 /* We also keep track of all argv elements that should be NULLed or
84 gchar *help_description;
86 GDestroyNotify destroy_notify;
89 GTranslateFunc translate_func;
90 GDestroyNotify translate_notify;
91 gpointer translate_data;
93 GOptionEntry *entries;
96 GOptionParseFunc pre_parse_func;
97 GOptionParseFunc post_parse_func;
98 GOptionErrorFunc error_func;
101 static void free_changes_list (GOptionContext *context,
103 static void free_pending_nulls (GOptionContext *context,
104 gboolean perform_nulls);
107 g_option_error_quark (void)
112 q = g_quark_from_static_string ("g-option-context-error-quark");
118 * g_option_context_new:
119 * @parameter_string: the parameter string to be used in
120 * <option>--help</option> output.
122 * Creates a new option context.
124 * Returns: a newly created #GOptionContext, which must be
125 * freed with g_option_context_free() after use.
130 g_option_context_new (const gchar *parameter_string)
133 GOptionContext *context;
135 context = g_new0 (GOptionContext, 1);
137 context->parameter_string = g_strdup (parameter_string);
138 context->help_enabled = TRUE;
139 context->ignore_unknown = FALSE;
145 * g_option_context_free:
146 * @context: a #GOptionContext
148 * Frees context and all the groups which have been
153 void g_option_context_free (GOptionContext *context)
155 g_return_if_fail (context != NULL);
157 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
158 g_list_free (context->groups);
160 if (context->main_group)
161 g_option_group_free (context->main_group);
163 free_changes_list (context, FALSE);
164 free_pending_nulls (context, FALSE);
166 g_free (context->parameter_string);
173 * g_option_context_set_help_enabled:
174 * @context: a #GOptionContext
175 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
177 * Enables or disables automatic generation of <option>--help</option>
178 * output. By default, g_option_context_parse() recognizes
179 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
180 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
181 * suitable output to stdout.
185 void g_option_context_set_help_enabled (GOptionContext *context,
186 gboolean help_enabled)
189 g_return_if_fail (context != NULL);
191 context->help_enabled = help_enabled;
195 * g_option_context_get_help_enabled:
196 * @context: a #GOptionContext
198 * Returns whether automatic <option>--help</option> generation
199 * is turned on for @context. See g_option_context_set_help_enabled().
201 * Returns: %TRUE if automatic help generation is turned on.
206 g_option_context_get_help_enabled (GOptionContext *context)
208 g_return_val_if_fail (context != NULL, FALSE);
210 return context->help_enabled;
214 * g_option_context_set_ignore_unknown_options:
215 * @context: a #GOptionContext
216 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
217 * an error when unknown options are met
219 * Sets whether to ignore unknown options or not. If an argument is
220 * ignored, it is left in the @argv array after parsing. By default,
221 * g_option_context_parse() treats unknown options as error.
223 * This setting does not affect non-option arguments (i.e. arguments
224 * which don't start with a dash). But note that GOption cannot reliably
225 * determine whether a non-option belongs to a preceding unknown option.
230 g_option_context_set_ignore_unknown_options (GOptionContext *context,
231 gboolean ignore_unknown)
233 g_return_if_fail (context != NULL);
235 context->ignore_unknown = ignore_unknown;
239 * g_option_context_get_ignore_unknown_options:
240 * @context: a #GOptionContext
242 * Returns whether unknown options are ignored or not. See
243 * g_option_context_set_ignore_unknown_options().
245 * Returns: %TRUE if unknown options are ignored.
250 g_option_context_get_ignore_unknown_options (GOptionContext *context)
252 g_return_val_if_fail (context != NULL, FALSE);
254 return context->ignore_unknown;
258 * g_option_context_add_group:
259 * @context: a #GOptionContext
260 * @group: the group to add
262 * Adds a #GOptionGroup to the @context, so that parsing with @context
263 * will recognize the options in the group. Note that the group will
264 * be freed together with the context when g_option_context_free() is
265 * called, so you must not free the group yourself after adding it
271 g_option_context_add_group (GOptionContext *context,
276 g_return_if_fail (context != NULL);
277 g_return_if_fail (group != NULL);
278 g_return_if_fail (group->name != NULL);
279 g_return_if_fail (group->description != NULL);
280 g_return_if_fail (group->help_description != NULL);
282 for (list = context->groups; list; list = list->next)
284 GOptionGroup *g = (GOptionGroup *)list->data;
286 if ((group->name == NULL && g->name == NULL) ||
287 (group->name && g->name && strcmp (group->name, g->name) == 0))
288 g_warning ("A group named \"%s\" is already part of this GOptionContext",
292 context->groups = g_list_append (context->groups, group);
296 * g_option_context_set_main_group:
297 * @context: a #GOptionContext
298 * @group: the group to set as main group
300 * Sets a #GOptionGroup as main group of the @context.
301 * This has the same effect as calling g_option_context_add_group(),
302 * the only difference is that the options in the main group are
303 * treated differently when generating <option>--help</option> output.
308 g_option_context_set_main_group (GOptionContext *context,
311 g_return_if_fail (context != NULL);
312 g_return_if_fail (group != NULL);
314 context->main_group = group;
318 * g_option_context_get_main_group:
319 * @context: a #GOptionContext
321 * Returns a pointer to the main group of @context.
323 * Return value: the main group of @context, or %NULL if @context doesn't
324 * have a main group. Note that group belongs to @context and should
325 * not be modified or freed.
330 g_option_context_get_main_group (GOptionContext *context)
332 g_return_val_if_fail (context != NULL, NULL);
334 return context->main_group;
338 * g_option_context_add_main_entries:
339 * @context: a #GOptionContext
340 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
341 * @translation_domain: a translation domain to use for translating
342 * the <option>--help</option> output for the options in @entries
343 * with gettext(), or %NULL
345 * A convenience function which creates a main group if it doesn't
346 * exist, adds the @entries to it and sets the translation domain.
351 g_option_context_add_main_entries (GOptionContext *context,
352 const GOptionEntry *entries,
353 const gchar *translation_domain)
355 g_return_if_fail (entries != NULL);
357 if (!context->main_group)
358 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
360 g_option_group_add_entries (context->main_group, entries);
361 g_option_group_set_translation_domain (context->main_group, translation_domain);
365 print_entry (GOptionGroup *group,
367 const GOptionEntry *entry)
371 if (entry->flags & G_OPTION_FLAG_HIDDEN)
374 str = g_string_new (NULL);
376 if (entry->short_name)
377 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
379 g_string_append_printf (str, " --%s", entry->long_name);
381 if (entry->arg_description)
382 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
384 g_print ("%-*s %s\n", max_length + 4, str->str,
385 entry->description ? TRANSLATE (group, entry->description) : "");
386 g_string_free (str, TRUE);
390 print_help (GOptionContext *context,
395 gint max_length, len;
397 GHashTable *shadow_map;
400 g_print ("%s\n %s %s %s\n\n",
401 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
402 context->parameter_string ? context->parameter_string : "");
404 memset (seen, 0, sizeof (gboolean) * 256);
405 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
407 if (context->main_group)
409 for (i = 0; i < context->main_group->n_entries; i++)
411 g_hash_table_insert (shadow_map,
412 (gpointer)context->main_group->entries[i].long_name,
413 context->main_group->entries + i);
415 if (seen[(guchar)context->main_group->entries[i].short_name])
416 context->main_group->entries[i].short_name = 0;
418 seen[(guchar)context->main_group->entries[i].short_name] = TRUE;
422 list = context->groups;
425 GOptionGroup *group = list->data;
426 for (i = 0; i < group->n_entries; i++)
428 if (g_hash_table_lookup (shadow_map, group->entries[i].long_name))
429 group->entries[i].long_name = g_strdup_printf ("%s-%s", group->name, group->entries[i].long_name);
431 g_hash_table_insert (shadow_map, (gpointer)group->entries[i].long_name, group->entries + i);
433 if (seen[(guchar)group->entries[i].short_name])
434 group->entries[i].short_name = 0;
436 seen[(guchar)group->entries[i].short_name] = TRUE;
441 g_hash_table_destroy (shadow_map);
443 list = context->groups;
445 max_length = g_utf8_strlen ("--help, -?", -1);
449 len = g_utf8_strlen ("--help-all", -1);
450 max_length = MAX (max_length, len);
455 GOptionGroup *group = list->data;
457 /* First, we check the --help-<groupname> options */
458 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
459 max_length = MAX (max_length, len);
461 /* Then we go through the entries */
462 for (i = 0; i < group->n_entries; i++)
464 if (group->entries[i].flags & G_OPTION_FLAG_HIDDEN)
467 len = g_utf8_strlen (group->entries[i].long_name, -1);
469 if (group->entries[i].short_name)
472 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
473 group->entries[i].arg_description)
474 len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
476 max_length = MAX (max_length, len);
482 /* Add a bit of padding */
485 list = context->groups;
487 g_print ("%s\n --%-*s %s\n",
488 _("Help Options:"), max_length, "help", _("Show help options"));
490 /* We only want --help-all when there are groups */
492 g_print (" --%-*s %s\n", max_length, "help-all", _("Show all help options"));
496 GOptionGroup *group = list->data;
498 g_print (" --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
507 /* Print a certain group */
509 g_print ("%s\n", TRANSLATE (group, group->description));
510 for (i = 0; i < group->n_entries; i++)
511 print_entry (group, max_length, &group->entries[i]);
516 /* Print all groups */
518 list = context->groups;
522 GOptionGroup *group = list->data;
524 g_print ("%s\n", group->description);
526 for (i = 0; i < group->n_entries; i++)
527 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
528 print_entry (group, max_length, &group->entries[i]);
535 /* Print application options if --help or --help-all has been specified */
536 if (main_help || !group)
538 list = context->groups;
540 g_print ("%s\n", _("Application Options:"));
542 if (context->main_group)
543 for (i = 0; i < context->main_group->n_entries; i++)
544 print_entry (context->main_group, max_length, &context->main_group->entries[i]);
548 GOptionGroup *group = list->data;
550 /* Print main entries from other groups */
551 for (i = 0; i < group->n_entries; i++)
552 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
553 print_entry (group, max_length, &group->entries[i]);
565 parse_int (const gchar *arg_name,
571 glong tmp = strtol (arg, &end, 0);
575 if (*arg == '\0' || *end != '\0')
578 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
579 _("Cannot parse integer value '%s' for --%s"),
585 if (*result != tmp || errno == ERANGE)
588 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
589 _("Integer value '%s' for %s out of range"),
598 get_change (GOptionContext *context,
603 Change *change = NULL;
605 for (list = context->changes; list != NULL; list = list->next)
609 if (change->arg_data == arg_data)
613 change = g_new0 (Change, 1);
614 change->arg_type = arg_type;
615 change->arg_data = arg_data;
617 context->changes = g_list_prepend (context->changes, change);
625 add_pending_null (GOptionContext *context,
631 n = g_new0 (PendingNull, 1);
635 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
639 parse_arg (GOptionContext *context,
643 const gchar *option_name,
651 case G_OPTION_ARG_NONE:
653 change = get_change (context, G_OPTION_ARG_NONE,
656 *(gboolean *)entry->arg_data = TRUE;
659 case G_OPTION_ARG_STRING:
663 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
668 change = get_change (context, G_OPTION_ARG_STRING,
670 g_free (change->allocated.str);
672 change->prev.str = *(gchar **)entry->arg_data;
673 change->allocated.str = data;
675 *(gchar **)entry->arg_data = data;
678 case G_OPTION_ARG_STRING_ARRAY:
682 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
687 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
690 if (change->allocated.array.len == 0)
692 change->prev.array = entry->arg_data;
693 change->allocated.array.data = g_new (gchar *, 2);
696 change->allocated.array.data =
697 g_renew (gchar *, change->allocated.array.data,
698 change->allocated.array.len + 2);
700 change->allocated.array.data[change->allocated.array.len] = data;
701 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
703 change->allocated.array.len ++;
705 *(gchar ***)entry->arg_data = change->allocated.array.data;
710 case G_OPTION_ARG_FILENAME:
715 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
720 data = g_strdup (value);
722 change = get_change (context, G_OPTION_ARG_FILENAME,
724 g_free (change->allocated.str);
726 change->prev.str = *(gchar **)entry->arg_data;
727 change->allocated.str = data;
729 *(gchar **)entry->arg_data = data;
733 case G_OPTION_ARG_FILENAME_ARRAY:
738 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
743 data = g_strdup (value);
745 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
748 if (change->allocated.array.len == 0)
750 change->prev.array = entry->arg_data;
751 change->allocated.array.data = g_new (gchar *, 2);
754 change->allocated.array.data =
755 g_renew (gchar *, change->allocated.array.data,
756 change->allocated.array.len + 2);
758 change->allocated.array.data[change->allocated.array.len] = data;
759 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
761 change->allocated.array.len ++;
763 *(gchar ***)entry->arg_data = change->allocated.array.data;
768 case G_OPTION_ARG_INT:
772 if (!parse_int (option_name, value,
777 change = get_change (context, G_OPTION_ARG_INT,
779 change->prev.integer = *(gint *)entry->arg_data;
780 *(gint *)entry->arg_data = data;
783 case G_OPTION_ARG_CALLBACK:
788 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
793 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
802 g_assert_not_reached ();
809 parse_short_option (GOptionContext *context,
821 for (j = 0; j < group->n_entries; j++)
823 if (arg == group->entries[j].short_name)
825 if (group->entries[j].arg == G_OPTION_ARG_NONE)
827 parse_arg (context, group, &group->entries[j],
836 if (*new_index > index)
838 g_warning ("FIXME: figure out the correct error here");
843 if (index < *argc - 1)
845 value = (*argv)[index + 1];
846 add_pending_null (context, &((*argv)[index + 1]), NULL);
847 *new_index = index + 1;
853 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
855 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
857 g_free (option_name);
861 g_free (option_name);
871 parse_long_option (GOptionContext *context,
882 for (j = 0; j < group->n_entries; j++)
887 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
888 strcmp (arg, group->entries[j].long_name) == 0)
890 parse_arg (context, group, &group->entries[j],
893 add_pending_null (context, &((*argv)[*index]), NULL);
898 gint len = strlen (group->entries[j].long_name);
900 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
901 (arg[len] == '=' || arg[len] == 0))
906 add_pending_null (context, &((*argv)[*index]), NULL);
909 value = arg + len + 1;
910 else if (*index < *argc - 1)
912 value = (*argv)[*index + 1];
913 add_pending_null (context, &((*argv)[*index + 1]), NULL);
919 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
921 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
923 g_free (option_name);
927 g_free (option_name);
937 parse_remaining_arg (GOptionContext *context,
947 for (j = 0; j < group->n_entries; j++)
952 if (group->entries[j].long_name[0])
955 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
956 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
958 add_pending_null (context, &((*argv)[*index]), NULL);
960 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
971 free_changes_list (GOptionContext *context,
976 for (list = context->changes; list != NULL; list = list->next)
978 Change *change = list->data;
982 switch (change->arg_type)
984 case G_OPTION_ARG_NONE:
985 *(gboolean *)change->arg_data = change->prev.bool;
987 case G_OPTION_ARG_INT:
988 *(gint *)change->arg_data = change->prev.integer;
990 case G_OPTION_ARG_STRING:
991 case G_OPTION_ARG_FILENAME:
992 g_free (change->allocated.str);
993 *(gchar **)change->arg_data = change->prev.str;
995 case G_OPTION_ARG_STRING_ARRAY:
996 case G_OPTION_ARG_FILENAME_ARRAY:
997 g_strfreev (change->allocated.array.data);
998 *(gchar ***)change->arg_data = change->prev.array;
1001 g_assert_not_reached ();
1008 g_list_free (context->changes);
1009 context->changes = NULL;
1013 free_pending_nulls (GOptionContext *context,
1014 gboolean perform_nulls)
1018 for (list = context->pending_nulls; list != NULL; list = list->next)
1020 PendingNull *n = list->data;
1026 /* Copy back the short options */
1028 strcpy (*n->ptr + 1, n->value);
1038 g_list_free (context->pending_nulls);
1039 context->pending_nulls = NULL;
1043 * g_option_context_parse:
1044 * @context: a #GOptionContext
1045 * @argc: a pointer to the number of command line arguments.
1046 * @argv: a pointer to the array of command line arguments.
1047 * @error: a return location for errors
1049 * Parses the command line arguments, recognizing options
1050 * which have been added to @context. A side-effect of
1051 * calling this function is that g_set_prgname() will be
1054 * If the parsing is successful, any parsed arguments are
1055 * removed from the array and @argc and @argv are updated
1056 * accordingly. In case of an error, @argc and @argv are
1059 * Return value: %TRUE if the parsing was successful,
1060 * %FALSE if an error occurred
1065 g_option_context_parse (GOptionContext *context,
1073 /* Set program name */
1074 if (argc && argv && *argc)
1078 prgname = g_path_get_basename ((*argv)[0]);
1079 g_set_prgname (prgname);
1084 g_set_prgname ("<unknown>");
1087 /* Call pre-parse hooks */
1088 list = context->groups;
1091 GOptionGroup *group = list->data;
1093 if (group->pre_parse_func)
1095 if (!(* group->pre_parse_func) (context, group,
1096 group->user_data, error))
1103 if (context->main_group && context->main_group->pre_parse_func)
1105 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1106 context->main_group->user_data, error))
1112 gboolean stop_parsing = FALSE;
1114 for (i = 1; i < *argc; i++)
1117 gboolean parsed = FALSE;
1119 if ((*argv)[i][0] == '-' && !stop_parsing)
1121 if ((*argv)[i][1] == '-')
1125 arg = (*argv)[i] + 2;
1127 /* '--' terminates list of arguments */
1130 add_pending_null (context, &((*argv)[i]), NULL);
1131 stop_parsing = TRUE;
1135 /* Handle help options */
1136 if (context->help_enabled)
1138 if (strcmp (arg, "help") == 0)
1139 print_help (context, TRUE, NULL);
1140 else if (strcmp (arg, "help-all") == 0)
1141 print_help (context, FALSE, NULL);
1142 else if (strncmp (arg, "help-", 5) == 0)
1146 list = context->groups;
1150 GOptionGroup *group = list->data;
1152 if (strcmp (arg + 5, group->name) == 0)
1153 print_help (context, FALSE, group);
1160 if (context->main_group &&
1161 !parse_long_option (context, context->main_group, &i, arg,
1162 argc, argv, error, &parsed))
1168 /* Try the groups */
1169 list = context->groups;
1172 GOptionGroup *group = list->data;
1174 if (!parse_long_option (context, group, &i, arg,
1175 argc, argv, error, &parsed))
1187 /* Now look for --<group>-<option> */
1188 dash = strchr (arg, '-');
1191 /* Try the groups */
1192 list = context->groups;
1195 GOptionGroup *group = list->data;
1197 if (strncmp (group->name, arg, dash - arg) == 0)
1199 if (!parse_long_option (context, group, &i, dash + 1,
1200 argc, argv, error, &parsed))
1211 if (context->ignore_unknown)
1219 gboolean *nulled_out = NULL;
1221 arg = (*argv)[i] + 1;
1225 if (context->ignore_unknown)
1226 nulled_out = g_new0 (gboolean, strlen (arg));
1228 for (j = 0; j < strlen (arg); j++)
1232 if (context->main_group &&
1233 !parse_short_option (context, context->main_group,
1235 argc, argv, error, &parsed))
1238 g_free (nulled_out);
1244 /* Try the groups */
1245 list = context->groups;
1248 GOptionGroup *group = list->data;
1250 if (!parse_short_option (context, group, i, &new_i, arg[j],
1251 argc, argv, error, &parsed))
1261 if (context->ignore_unknown)
1264 nulled_out[j] = TRUE;
1273 if (context->ignore_unknown)
1275 gchar *new_arg = NULL;
1278 for (j = 0; j < strlen (arg); j++)
1283 new_arg = g_malloc (strlen (arg));
1284 new_arg[arg_index++] = arg[j];
1288 new_arg[arg_index] = '\0';
1290 add_pending_null (context, &((*argv)[i]), new_arg);
1294 add_pending_null (context, &((*argv)[i]), NULL);
1299 if (!parsed && !context->ignore_unknown)
1302 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1303 _("Unknown option %s"), (*argv)[i]);
1309 /* Collect remaining args */
1310 if (context->main_group &&
1311 !parse_remaining_arg (context, context->main_group, &i,
1312 argc, argv, error, &parsed))
1318 /* Call post-parse hooks */
1319 list = context->groups;
1322 GOptionGroup *group = list->data;
1324 if (group->post_parse_func)
1326 if (!(* group->post_parse_func) (context, group,
1327 group->user_data, error))
1334 if (context->main_group && context->main_group->post_parse_func)
1336 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1337 context->main_group->user_data, error))
1341 free_pending_nulls (context, TRUE);
1343 for (i = 1; i < *argc; i++)
1345 for (k = i; k < *argc; k++)
1346 if ((*argv)[k] != NULL)
1352 for (j = i + k; j < *argc; j++)
1354 (*argv)[j-k] = (*argv)[j];
1366 /* Call error hooks */
1367 list = context->groups;
1370 GOptionGroup *group = list->data;
1372 if (group->error_func)
1373 (* group->error_func) (context, group,
1374 group->user_data, error);
1379 if (context->main_group && context->main_group->error_func)
1380 (* context->main_group->error_func) (context, context->main_group,
1381 context->main_group->user_data, error);
1383 free_changes_list (context, TRUE);
1384 free_pending_nulls (context, FALSE);
1390 * g_option_group_new:
1391 * @name: the name for the option group, this is used to provide
1392 * help for the options in this group with <option>--help-</option>@name
1393 * @description: a description for this group to be shown in
1394 * <option>--help</option>. This string is translated using the translation
1395 * domain or translation function of the group
1396 * @help_description: a description for the <option>--help-</option>@name option.
1397 * This string is translated using the translation domain or translation function
1399 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1400 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1401 * @destroy: a function that will be called to free @user_data, or %NULL
1403 * Creates a new #GOptionGroup.
1405 * Return value: a newly created option group. It should be added
1406 * to a #GOptionContext or freed with g_option_group_free().
1411 g_option_group_new (const gchar *name,
1412 const gchar *description,
1413 const gchar *help_description,
1415 GDestroyNotify destroy)
1418 GOptionGroup *group;
1420 group = g_new0 (GOptionGroup, 1);
1421 group->name = g_strdup (name);
1422 group->description = g_strdup (description);
1423 group->help_description = g_strdup (help_description);
1424 group->user_data = user_data;
1425 group->destroy_notify = destroy;
1432 * g_option_group_free:
1433 * @group: a #GOptionGroup
1435 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1436 * free groups which have been added to a #GOptionContext.
1441 g_option_group_free (GOptionGroup *group)
1443 g_return_if_fail (group != NULL);
1445 g_free (group->name);
1446 g_free (group->description);
1447 g_free (group->help_description);
1449 g_free (group->entries);
1451 if (group->destroy_notify)
1452 (* group->destroy_notify) (group->user_data);
1454 if (group->translate_notify)
1455 (* group->translate_notify) (group->translate_data);
1462 * g_option_group_add_entries:
1463 * @group: a #GOptionGroup
1464 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1466 * Adds the options specified in @entries to @group.
1471 g_option_group_add_entries (GOptionGroup *group,
1472 const GOptionEntry *entries)
1476 g_return_if_fail (entries != NULL);
1478 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1480 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1482 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1484 group->n_entries += n_entries;
1488 * g_option_group_set_parse_hooks:
1489 * @group: a #GOptionGroup
1490 * @pre_parse_func: a function to call before parsing, or %NULL
1491 * @post_parse_func: a function to call after parsing, or %NULL
1493 * Associates two functions with @group which will be called
1494 * from g_option_context_parse() before the first option is parsed
1495 * and after the last option has been parsed, respectively.
1497 * Note that the user data to be passed to @pre_parse_func and
1498 * @post_parse_func can be specified when constructing the group
1499 * with g_option_group_new().
1504 g_option_group_set_parse_hooks (GOptionGroup *group,
1505 GOptionParseFunc pre_parse_func,
1506 GOptionParseFunc post_parse_func)
1508 g_return_if_fail (group != NULL);
1510 group->pre_parse_func = pre_parse_func;
1511 group->post_parse_func = post_parse_func;
1515 * g_option_group_set_error_hook:
1516 * @group: a #GOptionGroup
1517 * @error_func: a function to call when an error occurs
1519 * Associates a function with @group which will be called
1520 * from g_option_context_parse() when an error occurs.
1522 * Note that the user data to be passed to @pre_parse_func and
1523 * @post_parse_func can be specified when constructing the group
1524 * with g_option_group_new().
1529 g_option_group_set_error_hook (GOptionGroup *group,
1530 GOptionErrorFunc error_func)
1532 g_return_if_fail (group != NULL);
1534 group->error_func = error_func;
1539 * g_option_group_set_translate_func:
1540 * @group: a #GOptionGroup
1541 * @func: the #GTranslateFunc, or %NULL
1542 * @data: user data to pass to @func, or %NULL
1543 * @destroy_notify: a function which gets called to free @data, or %NULL
1545 * Sets the function which is used to translate user-visible
1546 * strings, for <option>--help</option> output. Different
1547 * groups can use different #GTranslateFunc<!-- -->s. If @func
1548 * is %NULL, strings are not translated.
1550 * If you are using gettext(), you only need to set the translation
1551 * domain, see g_option_group_set_translation_domain().
1556 g_option_group_set_translate_func (GOptionGroup *group,
1557 GTranslateFunc func,
1559 GDestroyNotify destroy_notify)
1561 g_return_if_fail (group != NULL);
1563 if (group->translate_notify)
1564 group->translate_notify (group->translate_data);
1566 group->translate_func = func;
1567 group->translate_data = data;
1568 group->translate_notify = destroy_notify;
1572 dgettext_swapped (const gchar *msgid,
1573 const gchar *domainname)
1575 return dgettext (domainname, msgid);
1579 * g_option_group_set_translation_domain:
1580 * @group: a #GOptionGroup
1581 * @domain: the domain to use
1583 * A convenience function to use gettext() for translating
1584 * user-visible strings.
1589 g_option_group_set_translation_domain (GOptionGroup *group,
1590 const gchar *domain)
1592 g_return_if_fail (group != NULL);
1594 g_option_group_set_translate_func (group,
1595 (GTranslateFunc)dgettext_swapped,