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).
229 g_option_context_set_ignore_unknown_options (GOptionContext *context,
230 gboolean ignore_unknown)
232 g_return_if_fail (context != NULL);
234 context->ignore_unknown = ignore_unknown;
238 * g_option_context_get_ignore_unknown_options:
239 * @context: a #GOptionContext
241 * Returns whether unknown options are ignored or not. See
242 * g_option_context_set_ignore_unknown_options().
244 * Returns: %TRUE if unknown options are ignored.
249 g_option_context_get_ignore_unknown_options (GOptionContext *context)
251 g_return_val_if_fail (context != NULL, FALSE);
253 return context->ignore_unknown;
257 * g_option_context_add_group:
258 * @context: a #GOptionContext
259 * @group: the group to add
261 * Adds a #GOptionGroup to the @context, so that parsing with @context
262 * will recognize the options in the group. Note that the group will
263 * be freed together with the context when g_option_context_free() is
264 * called, so you must not free the group yourself after adding it
270 g_option_context_add_group (GOptionContext *context,
273 g_return_if_fail (context != NULL);
274 g_return_if_fail (group != NULL);
275 g_return_if_fail (group->name != NULL);
276 g_return_if_fail (group->description != NULL);
277 g_return_if_fail (group->help_description != NULL);
279 context->groups = g_list_prepend (context->groups, group);
283 * g_option_context_set_main_group:
284 * @context: a #GOptionContext
285 * @group: the group to set as main group
287 * Sets a #GOptionGroup as main group of the @context.
288 * This has the same effect as calling g_option_context_add_group(),
289 * the only difference is that the options in the main group are
290 * treated differently when generating <option>--help</option> output.
295 g_option_context_set_main_group (GOptionContext *context,
298 g_return_if_fail (context != NULL);
299 g_return_if_fail (group != NULL);
301 context->main_group = group;
305 * g_option_context_get_main_group:
306 * @context: a #GOptionContext
308 * Returns a pointer to the main group of @context.
310 * Return value: the main group of @context, or %NULL if @context doesn't
311 * have a main group. Note that group belongs to @context and should
312 * not be modified or freed.
317 g_option_context_get_main_group (GOptionContext *context)
319 g_return_val_if_fail (context != NULL, NULL);
321 return context->main_group;
325 * g_option_context_add_main_entries:
326 * @context: a #GOptionContext
327 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
328 * @translation_domain: a translation domain to use for translating
329 * the <option>--help</option> output for the options in @entries
330 * with gettext(), or %NULL
332 * A convenience function which creates a main group if it doesn't
333 * exist, adds the @entries to it and sets the translation domain.
338 g_option_context_add_main_entries (GOptionContext *context,
339 const GOptionEntry *entries,
340 const gchar *translation_domain)
342 g_return_if_fail (entries != NULL);
344 if (!context->main_group)
345 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
347 g_option_group_add_entries (context->main_group, entries);
348 g_option_group_set_translation_domain (context->main_group, translation_domain);
352 print_entry (GOptionGroup *group,
354 const GOptionEntry *entry)
358 if (entry->flags & G_OPTION_FLAG_HIDDEN)
361 str = g_string_new (NULL);
363 if (entry->short_name)
364 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
366 g_string_append_printf (str, " --%s", entry->long_name);
368 if (entry->arg_description)
369 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
371 g_print ("%-*s %s\n", max_length + 4, str->str,
372 entry->description ? TRANSLATE (group, entry->description) : "");
373 g_string_free (str, TRUE);
377 print_help (GOptionContext *context,
382 gint max_length, len;
385 g_print ("%s\n %s %s %s\n\n",
386 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
387 context->parameter_string ? context->parameter_string : "");
389 list = context->groups;
391 max_length = g_utf8_strlen ("--help, -?", -1);
395 len = g_utf8_strlen ("--help-all", -1);
396 max_length = MAX (max_length, len);
401 GOptionGroup *group = list->data;
403 /* First, we check the --help-<groupname> options */
404 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
405 max_length = MAX (max_length, len);
407 /* Then we go through the entries */
408 for (i = 0; i < group->n_entries; i++)
410 if (group->entries[i].flags & G_OPTION_FLAG_HIDDEN)
413 len = g_utf8_strlen (group->entries[i].long_name, -1);
415 if (group->entries[i].short_name)
418 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
419 group->entries[i].arg_description)
420 len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
422 max_length = MAX (max_length, len);
428 /* Add a bit of padding */
431 list = context->groups;
433 g_print ("%s\n --%-*s %s\n",
434 _("Help Options:"), max_length, "help", _("Show help options"));
436 /* We only want --help-all when there are groups */
438 g_print (" --%-*s %s\n", max_length, "help-all", _("Show all help options"));
442 GOptionGroup *group = list->data;
444 g_print (" --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
453 /* Print a certain group */
455 g_print ("%s\n", TRANSLATE (group, group->description));
456 for (i = 0; i < group->n_entries; i++)
457 print_entry (group, max_length, &group->entries[i]);
462 /* Print all groups */
464 list = context->groups;
468 GOptionGroup *group = list->data;
470 g_print ("%s\n", group->description);
472 for (i = 0; i < group->n_entries; i++)
473 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
474 print_entry (group, max_length, &group->entries[i]);
481 /* Print application options if --help or --help-all has been specified */
482 if (main_help || !group)
484 list = context->groups;
486 g_print ("%s\n", _("Application Options:"));
488 if (context->main_group)
489 for (i = 0; i < context->main_group->n_entries; i++)
490 print_entry (context->main_group, max_length, &context->main_group->entries[i]);
494 GOptionGroup *group = list->data;
496 /* Print main entries from other groups */
497 for (i = 0; i < group->n_entries; i++)
498 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
499 print_entry (group, max_length, &group->entries[i]);
512 parse_int (const gchar *arg_name,
518 glong tmp = strtol (arg, &end, 0);
522 if (*arg == '\0' || *end != '\0')
525 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
526 _("Cannot parse integer value '%s' for --%s"),
532 if (*result != tmp || errno == ERANGE)
535 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
536 _("Integer value '%s' for %s out of range"),
545 get_change (GOptionContext *context,
550 Change *change = NULL;
552 for (list = context->changes; list != NULL; list = list->next)
556 if (change->arg_data == arg_data)
560 change = g_new0 (Change, 1);
561 change->arg_type = arg_type;
562 change->arg_data = arg_data;
564 context->changes = g_list_prepend (context->changes, change);
572 add_pending_null (GOptionContext *context,
578 n = g_new0 (PendingNull, 1);
582 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
586 parse_arg (GOptionContext *context,
590 const gchar *option_name,
598 case G_OPTION_ARG_NONE:
600 change = get_change (context, G_OPTION_ARG_NONE,
603 *(gboolean *)entry->arg_data = TRUE;
606 case G_OPTION_ARG_STRING:
610 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
615 change = get_change (context, G_OPTION_ARG_STRING,
617 g_free (change->allocated.str);
619 change->prev.str = *(gchar **)entry->arg_data;
620 change->allocated.str = data;
622 *(gchar **)entry->arg_data = data;
625 case G_OPTION_ARG_STRING_ARRAY:
629 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
634 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
637 if (change->allocated.array.len == 0)
639 change->prev.array = entry->arg_data;
640 change->allocated.array.data = g_new (gchar *, 2);
643 change->allocated.array.data =
644 g_renew (gchar *, change->allocated.array.data,
645 change->allocated.array.len + 2);
647 change->allocated.array.data[change->allocated.array.len] = data;
648 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
650 change->allocated.array.len ++;
652 *(gchar ***)entry->arg_data = change->allocated.array.data;
657 case G_OPTION_ARG_FILENAME:
661 data = g_strdup (value);
663 change = get_change (context, G_OPTION_ARG_FILENAME,
665 g_free (change->allocated.str);
667 change->prev.str = *(gchar **)entry->arg_data;
668 change->allocated.str = data;
670 *(gchar **)entry->arg_data = data;
674 case G_OPTION_ARG_FILENAME_ARRAY:
678 data = g_strdup (value);
680 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
683 if (change->allocated.array.len == 0)
685 change->prev.array = entry->arg_data;
686 change->allocated.array.data = g_new (gchar *, 2);
689 change->allocated.array.data =
690 g_renew (gchar *, change->allocated.array.data,
691 change->allocated.array.len + 2);
693 change->allocated.array.data[change->allocated.array.len] = data;
694 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
696 change->allocated.array.len ++;
698 *(gchar ***)entry->arg_data = change->allocated.array.data;
703 case G_OPTION_ARG_INT:
707 if (!parse_int (option_name, value,
712 change = get_change (context, G_OPTION_ARG_INT,
714 change->prev.integer = *(gint *)entry->arg_data;
715 *(gint *)entry->arg_data = data;
718 case G_OPTION_ARG_CALLBACK:
723 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
728 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
737 g_assert_not_reached ();
744 parse_short_option (GOptionContext *context,
756 for (j = 0; j < group->n_entries; j++)
758 if (arg == group->entries[j].short_name)
760 if (group->entries[j].arg == G_OPTION_ARG_NONE)
762 parse_arg (context, group, &group->entries[j],
771 if (*new_index > index)
773 g_warning ("FIXME: figure out the correct error here");
778 if (index < *argc - 1)
780 value = (*argv)[index + 1];
781 add_pending_null (context, &((*argv)[index + 1]), NULL);
782 *new_index = index + 1;
788 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
790 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
792 g_free (option_name);
796 g_free (option_name);
806 parse_long_option (GOptionContext *context,
817 for (j = 0; j < group->n_entries; j++)
822 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
823 strcmp (arg, group->entries[j].long_name) == 0)
825 parse_arg (context, group, &group->entries[j],
828 add_pending_null (context, &((*argv)[*index]), NULL);
833 gint len = strlen (group->entries[j].long_name);
835 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
836 (arg[len] == '=' || arg[len] == 0))
841 add_pending_null (context, &((*argv)[*index]), NULL);
844 value = arg + len + 1;
845 else if (*index < *argc - 1)
847 value = (*argv)[*index + 1];
848 add_pending_null (context, &((*argv)[*index + 1]), NULL);
854 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
856 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
858 g_free (option_name);
862 g_free (option_name);
872 parse_remaining_arg (GOptionContext *context,
882 for (j = 0; j < group->n_entries; j++)
887 if (group->entries[j].long_name[0])
890 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
891 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
893 add_pending_null (context, &((*argv)[*index]), NULL);
895 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
906 free_changes_list (GOptionContext *context,
911 for (list = context->changes; list != NULL; list = list->next)
913 Change *change = list->data;
917 switch (change->arg_type)
919 case G_OPTION_ARG_NONE:
920 *(gboolean *)change->arg_data = change->prev.bool;
922 case G_OPTION_ARG_INT:
923 *(gint *)change->arg_data = change->prev.integer;
925 case G_OPTION_ARG_STRING:
926 case G_OPTION_ARG_FILENAME:
927 g_free (change->allocated.str);
928 *(gchar **)change->arg_data = change->prev.str;
930 case G_OPTION_ARG_STRING_ARRAY:
931 case G_OPTION_ARG_FILENAME_ARRAY:
932 g_strfreev (change->allocated.array.data);
933 *(gchar ***)change->arg_data = change->prev.array;
936 g_assert_not_reached ();
943 g_list_free (context->changes);
944 context->changes = NULL;
948 free_pending_nulls (GOptionContext *context,
949 gboolean perform_nulls)
953 for (list = context->pending_nulls; list != NULL; list = list->next)
955 PendingNull *n = list->data;
961 /* Copy back the short options */
963 strcpy (*n->ptr + 1, n->value);
973 g_list_free (context->pending_nulls);
974 context->pending_nulls = NULL;
978 * g_option_context_parse:
979 * @context: a #GOptionContext
980 * @argc: a pointer to the number of command line arguments.
981 * @argv: a pointer to the array of command line arguments.
982 * @error: a return location for errors
984 * Parses the command line arguments, recognizing options
985 * which have been added to @context. A side-effect of
986 * calling this function is that g_set_prgname() will be
989 * If the parsing is successful, any parsed arguments are
990 * removed from the array and @argc and @argv are updated
991 * accordingly. In case of an error, @argc and @argv are
994 * Return value: %TRUE if the parsing was successful,
995 * %FALSE if an error occurred
1000 g_option_context_parse (GOptionContext *context,
1008 /* Set program name */
1009 if (argc && argv && *argc)
1013 prgname = g_path_get_basename ((*argv)[0]);
1014 g_set_prgname (prgname);
1019 g_set_prgname ("<unknown>");
1022 /* Call pre-parse hooks */
1023 list = context->groups;
1026 GOptionGroup *group = list->data;
1028 if (group->pre_parse_func)
1030 if (!(* group->pre_parse_func) (context, group,
1031 group->user_data, error))
1038 if (context->main_group && context->main_group->pre_parse_func)
1040 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1041 context->main_group->user_data, error))
1047 gboolean stop_parsing = FALSE;
1049 for (i = 1; i < *argc; i++)
1052 gboolean parsed = FALSE;
1054 if ((*argv)[i][0] == '-' && !stop_parsing)
1056 if ((*argv)[i][1] == '-')
1060 arg = (*argv)[i] + 2;
1062 /* '--' terminates list of arguments */
1065 add_pending_null (context, &((*argv)[i]), NULL);
1066 stop_parsing = TRUE;
1070 /* Handle help options */
1071 if (context->help_enabled)
1073 if (strcmp (arg, "help") == 0)
1074 print_help (context, TRUE, NULL);
1075 else if (strcmp (arg, "help-all") == 0)
1076 print_help (context, FALSE, NULL);
1077 else if (strncmp (arg, "help-", 5) == 0)
1081 list = context->groups;
1085 GOptionGroup *group = list->data;
1087 if (strcmp (arg + 5, group->name) == 0)
1088 print_help (context, FALSE, group);
1095 if (context->main_group &&
1096 !parse_long_option (context, context->main_group, &i, arg,
1097 argc, argv, error, &parsed))
1103 /* Try the groups */
1104 list = context->groups;
1107 GOptionGroup *group = list->data;
1109 if (!parse_long_option (context, group, &i, arg,
1110 argc, argv, error, &parsed))
1119 if (context->ignore_unknown)
1125 gboolean *nulled_out = NULL;
1127 arg = (*argv)[i] + 1;
1131 if (context->ignore_unknown)
1132 nulled_out = g_new0 (gboolean, strlen (arg));
1134 for (j = 0; j < strlen (arg); j++)
1138 if (context->main_group &&
1139 !parse_short_option (context, context->main_group,
1141 argc, argv, error, &parsed))
1144 g_free (nulled_out);
1150 /* Try the groups */
1151 list = context->groups;
1154 GOptionGroup *group = list->data;
1156 if (!parse_short_option (context, group, i, &new_i, arg[j],
1157 argc, argv, error, &parsed))
1167 if (context->ignore_unknown)
1170 nulled_out[j] = TRUE;
1179 if (context->ignore_unknown)
1181 gchar *new_arg = NULL;
1184 for (j = 0; j < strlen (arg); j++)
1189 new_arg = g_malloc (strlen (arg));
1190 new_arg[arg_index++] = arg[j];
1194 new_arg[arg_index] = '\0';
1196 add_pending_null (context, &((*argv)[i]), new_arg);
1200 add_pending_null (context, &((*argv)[i]), NULL);
1205 if (!parsed && !context->ignore_unknown)
1208 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1209 _("Unknown option %s"), (*argv)[i]);
1215 /* Collect remaining args */
1216 if (context->main_group &&
1217 !parse_remaining_arg (context, context->main_group, &i,
1218 argc, argv, error, &parsed))
1224 /* Call post-parse hooks */
1225 list = context->groups;
1228 GOptionGroup *group = list->data;
1230 if (group->post_parse_func)
1232 if (!(* group->post_parse_func) (context, group,
1233 group->user_data, error))
1240 if (context->main_group && context->main_group->post_parse_func)
1242 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1243 context->main_group->user_data, error))
1247 free_pending_nulls (context, TRUE);
1249 for (i = 1; i < *argc; i++)
1251 for (k = i; k < *argc; k++)
1252 if ((*argv)[k] != NULL)
1258 for (j = i + k; j < *argc; j++)
1260 (*argv)[j-k] = (*argv)[j];
1272 /* Call error hooks */
1273 list = context->groups;
1276 GOptionGroup *group = list->data;
1278 if (group->error_func)
1279 (* group->error_func) (context, group,
1280 group->user_data, error);
1285 if (context->main_group && context->main_group->error_func)
1286 (* context->main_group->error_func) (context, context->main_group,
1287 context->main_group->user_data, error);
1289 free_changes_list (context, TRUE);
1290 free_pending_nulls (context, FALSE);
1296 * g_option_group_new:
1297 * @name: the name for the option group, this is used to provide
1298 * help for the options in this group with <option>--help-</option>@name
1299 * @description: a description for this group to be shown in
1300 * <option>--help</option>. This string is translated using the translation
1301 * domain or translation function of the group
1302 * @help_description: a description for the <option>--help-</option>@name option.
1303 * This string is translated using the translation domain or translation function
1305 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1306 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1307 * @destroy: a function that will be called to free @user_data, or %NULL
1309 * Creates a new #GOptionGroup.
1311 * Return value: a newly created option group. It should be added
1312 * to a #GOptionContext or freed with g_option_group_free().
1317 g_option_group_new (const gchar *name,
1318 const gchar *description,
1319 const gchar *help_description,
1321 GDestroyNotify destroy)
1324 GOptionGroup *group;
1326 group = g_new0 (GOptionGroup, 1);
1327 group->name = g_strdup (name);
1328 group->description = g_strdup (description);
1329 group->help_description = g_strdup (help_description);
1330 group->user_data = user_data;
1331 group->destroy_notify = destroy;
1337 * g_option_group_free:
1338 * @group: a #GOptionGroup
1340 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1341 * free groups which have been added to a #GOptionContext.
1346 g_option_group_free (GOptionGroup *group)
1348 g_return_if_fail (group != NULL);
1350 g_free (group->name);
1351 g_free (group->description);
1352 g_free (group->help_description);
1354 g_free (group->entries);
1356 if (group->destroy_notify)
1357 (* group->destroy_notify) (group->user_data);
1359 if (group->translate_notify)
1360 (* group->translate_notify) (group->translate_data);
1367 * g_option_group_add_entries:
1368 * @group: a #GOptionGroup
1369 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1371 * Adds the options specified in @entries to @group.
1376 g_option_group_add_entries (GOptionGroup *group,
1377 const GOptionEntry *entries)
1381 g_return_if_fail (entries != NULL);
1383 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1385 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1387 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1389 group->n_entries += n_entries;
1393 * g_option_group_set_parse_hooks:
1394 * @group: a #GOptionGroup
1395 * @pre_parse_func: a function to call before parsing, or %NULL
1396 * @post_parse_func: a function to call after parsing, or %NULL
1398 * Associates two functions with @group which will be called
1399 * from g_option_context_parse() before the first option is parsed
1400 * and after the last option has been parsed, respectively.
1402 * Note that the user data to be passed to @pre_parse_func and
1403 * @post_parse_func can be specified when constructing the group
1404 * with g_option_group_new().
1409 g_option_group_set_parse_hooks (GOptionGroup *group,
1410 GOptionParseFunc pre_parse_func,
1411 GOptionParseFunc post_parse_func)
1413 g_return_if_fail (group != NULL);
1415 group->pre_parse_func = pre_parse_func;
1416 group->post_parse_func = post_parse_func;
1420 * g_option_group_set_error_hook:
1421 * @group: a #GOptionGroup
1422 * @error_func: a function to call when an error occurs
1424 * Associates a function with @group which will be called
1425 * from g_option_context_parse() when an error occurs.
1427 * Note that the user data to be passed to @pre_parse_func and
1428 * @post_parse_func can be specified when constructing the group
1429 * with g_option_group_new().
1434 g_option_group_set_error_hook (GOptionGroup *group,
1435 GOptionErrorFunc error_func)
1437 g_return_if_fail (group != NULL);
1439 group->error_func = error_func;
1444 * g_option_group_set_translate_func:
1445 * @group: a #GOptionGroup
1446 * @func: the #GTranslateFunc, or %NULL
1447 * @data: user data to pass to @func, or %NULL
1448 * @destroy_notify: a function which gets called to free @data, or %NULL
1450 * Sets the function which is used to translate user-visible
1451 * strings, for <option>--help</option> output. Different
1452 * groups can use different #GTranslateFunc<!-- -->s. If @func
1453 * is %NULL, strings are not translated.
1455 * If you are using gettext(), you only need to set the translation
1456 * domain, see g_option_group_set_translation_domain().
1461 g_option_group_set_translate_func (GOptionGroup *group,
1462 GTranslateFunc func,
1464 GDestroyNotify destroy_notify)
1466 g_return_if_fail (group != NULL);
1468 if (group->translate_notify)
1469 group->translate_notify (group->translate_data);
1471 group->translate_func = func;
1472 group->translate_data = data;
1473 group->translate_notify = destroy_notify;
1477 dgettext_swapped (const gchar *msgid,
1478 const gchar *domainname)
1480 return dgettext (domainname, msgid);
1484 * g_option_group_set_translation_domain:
1485 * @group: a #GOptionGroup
1486 * @domain: the domain to use
1488 * A convenience function to use gettext() for translating
1489 * user-visible strings.
1494 g_option_group_set_translation_domain (GOptionGroup *group,
1495 const gchar *domain)
1497 g_return_if_fail (group != NULL);
1499 g_option_group_set_translate_func (group,
1500 (GTranslateFunc)dgettext_swapped,