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,
274 g_return_if_fail (context != NULL);
275 g_return_if_fail (group != NULL);
276 g_return_if_fail (group->name != NULL);
277 g_return_if_fail (group->description != NULL);
278 g_return_if_fail (group->help_description != NULL);
280 context->groups = g_list_prepend (context->groups, group);
284 * g_option_context_set_main_group:
285 * @context: a #GOptionContext
286 * @group: the group to set as main group
288 * Sets a #GOptionGroup as main group of the @context.
289 * This has the same effect as calling g_option_context_add_group(),
290 * the only difference is that the options in the main group are
291 * treated differently when generating <option>--help</option> output.
296 g_option_context_set_main_group (GOptionContext *context,
299 g_return_if_fail (context != NULL);
300 g_return_if_fail (group != NULL);
302 context->main_group = group;
306 * g_option_context_get_main_group:
307 * @context: a #GOptionContext
309 * Returns a pointer to the main group of @context.
311 * Return value: the main group of @context, or %NULL if @context doesn't
312 * have a main group. Note that group belongs to @context and should
313 * not be modified or freed.
318 g_option_context_get_main_group (GOptionContext *context)
320 g_return_val_if_fail (context != NULL, NULL);
322 return context->main_group;
326 * g_option_context_add_main_entries:
327 * @context: a #GOptionContext
328 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
329 * @translation_domain: a translation domain to use for translating
330 * the <option>--help</option> output for the options in @entries
331 * with gettext(), or %NULL
333 * A convenience function which creates a main group if it doesn't
334 * exist, adds the @entries to it and sets the translation domain.
339 g_option_context_add_main_entries (GOptionContext *context,
340 const GOptionEntry *entries,
341 const gchar *translation_domain)
343 g_return_if_fail (entries != NULL);
345 if (!context->main_group)
346 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
348 g_option_group_add_entries (context->main_group, entries);
349 g_option_group_set_translation_domain (context->main_group, translation_domain);
353 print_entry (GOptionGroup *group,
355 const GOptionEntry *entry)
359 if (entry->flags & G_OPTION_FLAG_HIDDEN)
362 str = g_string_new (NULL);
364 if (entry->short_name)
365 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
367 g_string_append_printf (str, " --%s", entry->long_name);
369 if (entry->arg_description)
370 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
372 g_print ("%-*s %s\n", max_length + 4, str->str,
373 entry->description ? TRANSLATE (group, entry->description) : "");
374 g_string_free (str, TRUE);
378 print_help (GOptionContext *context,
383 gint max_length, len;
386 g_print ("%s\n %s %s %s\n\n",
387 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
388 context->parameter_string ? context->parameter_string : "");
390 list = context->groups;
392 max_length = g_utf8_strlen ("--help, -?", -1);
396 len = g_utf8_strlen ("--help-all", -1);
397 max_length = MAX (max_length, len);
402 GOptionGroup *group = list->data;
404 /* First, we check the --help-<groupname> options */
405 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
406 max_length = MAX (max_length, len);
408 /* Then we go through the entries */
409 for (i = 0; i < group->n_entries; i++)
411 if (group->entries[i].flags & G_OPTION_FLAG_HIDDEN)
414 len = g_utf8_strlen (group->entries[i].long_name, -1);
416 if (group->entries[i].short_name)
419 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
420 group->entries[i].arg_description)
421 len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
423 max_length = MAX (max_length, len);
429 /* Add a bit of padding */
432 list = context->groups;
434 g_print ("%s\n --%-*s %s\n",
435 _("Help Options:"), max_length, "help", _("Show help options"));
437 /* We only want --help-all when there are groups */
439 g_print (" --%-*s %s\n", max_length, "help-all", _("Show all help options"));
443 GOptionGroup *group = list->data;
445 g_print (" --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
454 /* Print a certain group */
456 g_print ("%s\n", TRANSLATE (group, group->description));
457 for (i = 0; i < group->n_entries; i++)
458 print_entry (group, max_length, &group->entries[i]);
463 /* Print all groups */
465 list = context->groups;
469 GOptionGroup *group = list->data;
471 g_print ("%s\n", group->description);
473 for (i = 0; i < group->n_entries; i++)
474 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
475 print_entry (group, max_length, &group->entries[i]);
482 /* Print application options if --help or --help-all has been specified */
483 if (main_help || !group)
485 list = context->groups;
487 g_print ("%s\n", _("Application Options:"));
489 if (context->main_group)
490 for (i = 0; i < context->main_group->n_entries; i++)
491 print_entry (context->main_group, max_length, &context->main_group->entries[i]);
495 GOptionGroup *group = list->data;
497 /* Print main entries from other groups */
498 for (i = 0; i < group->n_entries; i++)
499 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
500 print_entry (group, max_length, &group->entries[i]);
513 parse_int (const gchar *arg_name,
519 glong tmp = strtol (arg, &end, 0);
523 if (*arg == '\0' || *end != '\0')
526 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
527 _("Cannot parse integer value '%s' for --%s"),
533 if (*result != tmp || errno == ERANGE)
536 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
537 _("Integer value '%s' for %s out of range"),
546 get_change (GOptionContext *context,
551 Change *change = NULL;
553 for (list = context->changes; list != NULL; list = list->next)
557 if (change->arg_data == arg_data)
561 change = g_new0 (Change, 1);
562 change->arg_type = arg_type;
563 change->arg_data = arg_data;
565 context->changes = g_list_prepend (context->changes, change);
573 add_pending_null (GOptionContext *context,
579 n = g_new0 (PendingNull, 1);
583 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
587 parse_arg (GOptionContext *context,
591 const gchar *option_name,
599 case G_OPTION_ARG_NONE:
601 change = get_change (context, G_OPTION_ARG_NONE,
604 *(gboolean *)entry->arg_data = TRUE;
607 case G_OPTION_ARG_STRING:
611 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
616 change = get_change (context, G_OPTION_ARG_STRING,
618 g_free (change->allocated.str);
620 change->prev.str = *(gchar **)entry->arg_data;
621 change->allocated.str = data;
623 *(gchar **)entry->arg_data = data;
626 case G_OPTION_ARG_STRING_ARRAY:
630 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
635 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
638 if (change->allocated.array.len == 0)
640 change->prev.array = entry->arg_data;
641 change->allocated.array.data = g_new (gchar *, 2);
644 change->allocated.array.data =
645 g_renew (gchar *, change->allocated.array.data,
646 change->allocated.array.len + 2);
648 change->allocated.array.data[change->allocated.array.len] = data;
649 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
651 change->allocated.array.len ++;
653 *(gchar ***)entry->arg_data = change->allocated.array.data;
658 case G_OPTION_ARG_FILENAME:
663 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
668 data = g_strdup (value);
670 change = get_change (context, G_OPTION_ARG_FILENAME,
672 g_free (change->allocated.str);
674 change->prev.str = *(gchar **)entry->arg_data;
675 change->allocated.str = data;
677 *(gchar **)entry->arg_data = data;
681 case G_OPTION_ARG_FILENAME_ARRAY:
686 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
691 data = g_strdup (value);
693 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
696 if (change->allocated.array.len == 0)
698 change->prev.array = entry->arg_data;
699 change->allocated.array.data = g_new (gchar *, 2);
702 change->allocated.array.data =
703 g_renew (gchar *, change->allocated.array.data,
704 change->allocated.array.len + 2);
706 change->allocated.array.data[change->allocated.array.len] = data;
707 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
709 change->allocated.array.len ++;
711 *(gchar ***)entry->arg_data = change->allocated.array.data;
716 case G_OPTION_ARG_INT:
720 if (!parse_int (option_name, value,
725 change = get_change (context, G_OPTION_ARG_INT,
727 change->prev.integer = *(gint *)entry->arg_data;
728 *(gint *)entry->arg_data = data;
731 case G_OPTION_ARG_CALLBACK:
736 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
741 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
750 g_assert_not_reached ();
757 parse_short_option (GOptionContext *context,
769 for (j = 0; j < group->n_entries; j++)
771 if (arg == group->entries[j].short_name)
773 if (group->entries[j].arg == G_OPTION_ARG_NONE)
775 parse_arg (context, group, &group->entries[j],
784 if (*new_index > index)
786 g_warning ("FIXME: figure out the correct error here");
791 if (index < *argc - 1)
793 value = (*argv)[index + 1];
794 add_pending_null (context, &((*argv)[index + 1]), NULL);
795 *new_index = index + 1;
801 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
803 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
805 g_free (option_name);
809 g_free (option_name);
819 parse_long_option (GOptionContext *context,
830 for (j = 0; j < group->n_entries; j++)
835 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
836 strcmp (arg, group->entries[j].long_name) == 0)
838 parse_arg (context, group, &group->entries[j],
841 add_pending_null (context, &((*argv)[*index]), NULL);
846 gint len = strlen (group->entries[j].long_name);
848 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
849 (arg[len] == '=' || arg[len] == 0))
854 add_pending_null (context, &((*argv)[*index]), NULL);
857 value = arg + len + 1;
858 else if (*index < *argc - 1)
860 value = (*argv)[*index + 1];
861 add_pending_null (context, &((*argv)[*index + 1]), NULL);
867 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
869 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
871 g_free (option_name);
875 g_free (option_name);
885 parse_remaining_arg (GOptionContext *context,
895 for (j = 0; j < group->n_entries; j++)
900 if (group->entries[j].long_name[0])
903 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
904 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
906 add_pending_null (context, &((*argv)[*index]), NULL);
908 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
919 free_changes_list (GOptionContext *context,
924 for (list = context->changes; list != NULL; list = list->next)
926 Change *change = list->data;
930 switch (change->arg_type)
932 case G_OPTION_ARG_NONE:
933 *(gboolean *)change->arg_data = change->prev.bool;
935 case G_OPTION_ARG_INT:
936 *(gint *)change->arg_data = change->prev.integer;
938 case G_OPTION_ARG_STRING:
939 case G_OPTION_ARG_FILENAME:
940 g_free (change->allocated.str);
941 *(gchar **)change->arg_data = change->prev.str;
943 case G_OPTION_ARG_STRING_ARRAY:
944 case G_OPTION_ARG_FILENAME_ARRAY:
945 g_strfreev (change->allocated.array.data);
946 *(gchar ***)change->arg_data = change->prev.array;
949 g_assert_not_reached ();
956 g_list_free (context->changes);
957 context->changes = NULL;
961 free_pending_nulls (GOptionContext *context,
962 gboolean perform_nulls)
966 for (list = context->pending_nulls; list != NULL; list = list->next)
968 PendingNull *n = list->data;
974 /* Copy back the short options */
976 strcpy (*n->ptr + 1, n->value);
986 g_list_free (context->pending_nulls);
987 context->pending_nulls = NULL;
991 * g_option_context_parse:
992 * @context: a #GOptionContext
993 * @argc: a pointer to the number of command line arguments.
994 * @argv: a pointer to the array of command line arguments.
995 * @error: a return location for errors
997 * Parses the command line arguments, recognizing options
998 * which have been added to @context. A side-effect of
999 * calling this function is that g_set_prgname() will be
1002 * If the parsing is successful, any parsed arguments are
1003 * removed from the array and @argc and @argv are updated
1004 * accordingly. In case of an error, @argc and @argv are
1007 * Return value: %TRUE if the parsing was successful,
1008 * %FALSE if an error occurred
1013 g_option_context_parse (GOptionContext *context,
1021 /* Set program name */
1022 if (argc && argv && *argc)
1026 prgname = g_path_get_basename ((*argv)[0]);
1027 g_set_prgname (prgname);
1032 g_set_prgname ("<unknown>");
1035 /* Call pre-parse hooks */
1036 list = context->groups;
1039 GOptionGroup *group = list->data;
1041 if (group->pre_parse_func)
1043 if (!(* group->pre_parse_func) (context, group,
1044 group->user_data, error))
1051 if (context->main_group && context->main_group->pre_parse_func)
1053 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1054 context->main_group->user_data, error))
1060 gboolean stop_parsing = FALSE;
1062 for (i = 1; i < *argc; i++)
1065 gboolean parsed = FALSE;
1067 if ((*argv)[i][0] == '-' && !stop_parsing)
1069 if ((*argv)[i][1] == '-')
1073 arg = (*argv)[i] + 2;
1075 /* '--' terminates list of arguments */
1078 add_pending_null (context, &((*argv)[i]), NULL);
1079 stop_parsing = TRUE;
1083 /* Handle help options */
1084 if (context->help_enabled)
1086 if (strcmp (arg, "help") == 0)
1087 print_help (context, TRUE, NULL);
1088 else if (strcmp (arg, "help-all") == 0)
1089 print_help (context, FALSE, NULL);
1090 else if (strncmp (arg, "help-", 5) == 0)
1094 list = context->groups;
1098 GOptionGroup *group = list->data;
1100 if (strcmp (arg + 5, group->name) == 0)
1101 print_help (context, FALSE, group);
1108 if (context->main_group &&
1109 !parse_long_option (context, context->main_group, &i, arg,
1110 argc, argv, error, &parsed))
1116 /* Try the groups */
1117 list = context->groups;
1120 GOptionGroup *group = list->data;
1122 if (!parse_long_option (context, group, &i, arg,
1123 argc, argv, error, &parsed))
1132 if (context->ignore_unknown)
1138 gboolean *nulled_out = NULL;
1140 arg = (*argv)[i] + 1;
1144 if (context->ignore_unknown)
1145 nulled_out = g_new0 (gboolean, strlen (arg));
1147 for (j = 0; j < strlen (arg); j++)
1151 if (context->main_group &&
1152 !parse_short_option (context, context->main_group,
1154 argc, argv, error, &parsed))
1157 g_free (nulled_out);
1163 /* Try the groups */
1164 list = context->groups;
1167 GOptionGroup *group = list->data;
1169 if (!parse_short_option (context, group, i, &new_i, arg[j],
1170 argc, argv, error, &parsed))
1180 if (context->ignore_unknown)
1183 nulled_out[j] = TRUE;
1192 if (context->ignore_unknown)
1194 gchar *new_arg = NULL;
1197 for (j = 0; j < strlen (arg); j++)
1202 new_arg = g_malloc (strlen (arg));
1203 new_arg[arg_index++] = arg[j];
1207 new_arg[arg_index] = '\0';
1209 add_pending_null (context, &((*argv)[i]), new_arg);
1213 add_pending_null (context, &((*argv)[i]), NULL);
1218 if (!parsed && !context->ignore_unknown)
1221 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1222 _("Unknown option %s"), (*argv)[i]);
1228 /* Collect remaining args */
1229 if (context->main_group &&
1230 !parse_remaining_arg (context, context->main_group, &i,
1231 argc, argv, error, &parsed))
1237 /* Call post-parse hooks */
1238 list = context->groups;
1241 GOptionGroup *group = list->data;
1243 if (group->post_parse_func)
1245 if (!(* group->post_parse_func) (context, group,
1246 group->user_data, error))
1253 if (context->main_group && context->main_group->post_parse_func)
1255 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1256 context->main_group->user_data, error))
1260 free_pending_nulls (context, TRUE);
1262 for (i = 1; i < *argc; i++)
1264 for (k = i; k < *argc; k++)
1265 if ((*argv)[k] != NULL)
1271 for (j = i + k; j < *argc; j++)
1273 (*argv)[j-k] = (*argv)[j];
1285 /* Call error hooks */
1286 list = context->groups;
1289 GOptionGroup *group = list->data;
1291 if (group->error_func)
1292 (* group->error_func) (context, group,
1293 group->user_data, error);
1298 if (context->main_group && context->main_group->error_func)
1299 (* context->main_group->error_func) (context, context->main_group,
1300 context->main_group->user_data, error);
1302 free_changes_list (context, TRUE);
1303 free_pending_nulls (context, FALSE);
1309 * g_option_group_new:
1310 * @name: the name for the option group, this is used to provide
1311 * help for the options in this group with <option>--help-</option>@name
1312 * @description: a description for this group to be shown in
1313 * <option>--help</option>. This string is translated using the translation
1314 * domain or translation function of the group
1315 * @help_description: a description for the <option>--help-</option>@name option.
1316 * This string is translated using the translation domain or translation function
1318 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1319 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1320 * @destroy: a function that will be called to free @user_data, or %NULL
1322 * Creates a new #GOptionGroup.
1324 * Return value: a newly created option group. It should be added
1325 * to a #GOptionContext or freed with g_option_group_free().
1330 g_option_group_new (const gchar *name,
1331 const gchar *description,
1332 const gchar *help_description,
1334 GDestroyNotify destroy)
1337 GOptionGroup *group;
1339 group = g_new0 (GOptionGroup, 1);
1340 group->name = g_strdup (name);
1341 group->description = g_strdup (description);
1342 group->help_description = g_strdup (help_description);
1343 group->user_data = user_data;
1344 group->destroy_notify = destroy;
1350 * g_option_group_free:
1351 * @group: a #GOptionGroup
1353 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1354 * free groups which have been added to a #GOptionContext.
1359 g_option_group_free (GOptionGroup *group)
1361 g_return_if_fail (group != NULL);
1363 g_free (group->name);
1364 g_free (group->description);
1365 g_free (group->help_description);
1367 g_free (group->entries);
1369 if (group->destroy_notify)
1370 (* group->destroy_notify) (group->user_data);
1372 if (group->translate_notify)
1373 (* group->translate_notify) (group->translate_data);
1380 * g_option_group_add_entries:
1381 * @group: a #GOptionGroup
1382 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1384 * Adds the options specified in @entries to @group.
1389 g_option_group_add_entries (GOptionGroup *group,
1390 const GOptionEntry *entries)
1394 g_return_if_fail (entries != NULL);
1396 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1398 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1400 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1402 group->n_entries += n_entries;
1406 * g_option_group_set_parse_hooks:
1407 * @group: a #GOptionGroup
1408 * @pre_parse_func: a function to call before parsing, or %NULL
1409 * @post_parse_func: a function to call after parsing, or %NULL
1411 * Associates two functions with @group which will be called
1412 * from g_option_context_parse() before the first option is parsed
1413 * and after the last option has been parsed, respectively.
1415 * Note that the user data to be passed to @pre_parse_func and
1416 * @post_parse_func can be specified when constructing the group
1417 * with g_option_group_new().
1422 g_option_group_set_parse_hooks (GOptionGroup *group,
1423 GOptionParseFunc pre_parse_func,
1424 GOptionParseFunc post_parse_func)
1426 g_return_if_fail (group != NULL);
1428 group->pre_parse_func = pre_parse_func;
1429 group->post_parse_func = post_parse_func;
1433 * g_option_group_set_error_hook:
1434 * @group: a #GOptionGroup
1435 * @error_func: a function to call when an error occurs
1437 * Associates a function with @group which will be called
1438 * from g_option_context_parse() when an error occurs.
1440 * Note that the user data to be passed to @pre_parse_func and
1441 * @post_parse_func can be specified when constructing the group
1442 * with g_option_group_new().
1447 g_option_group_set_error_hook (GOptionGroup *group,
1448 GOptionErrorFunc error_func)
1450 g_return_if_fail (group != NULL);
1452 group->error_func = error_func;
1457 * g_option_group_set_translate_func:
1458 * @group: a #GOptionGroup
1459 * @func: the #GTranslateFunc, or %NULL
1460 * @data: user data to pass to @func, or %NULL
1461 * @destroy_notify: a function which gets called to free @data, or %NULL
1463 * Sets the function which is used to translate user-visible
1464 * strings, for <option>--help</option> output. Different
1465 * groups can use different #GTranslateFunc<!-- -->s. If @func
1466 * is %NULL, strings are not translated.
1468 * If you are using gettext(), you only need to set the translation
1469 * domain, see g_option_group_set_translation_domain().
1474 g_option_group_set_translate_func (GOptionGroup *group,
1475 GTranslateFunc func,
1477 GDestroyNotify destroy_notify)
1479 g_return_if_fail (group != NULL);
1481 if (group->translate_notify)
1482 group->translate_notify (group->translate_data);
1484 group->translate_func = func;
1485 group->translate_data = data;
1486 group->translate_notify = destroy_notify;
1490 dgettext_swapped (const gchar *msgid,
1491 const gchar *domainname)
1493 return dgettext (domainname, msgid);
1497 * g_option_group_set_translation_domain:
1498 * @group: a #GOptionGroup
1499 * @domain: the domain to use
1501 * A convenience function to use gettext() for translating
1502 * user-visible strings.
1507 g_option_group_set_translation_domain (GOptionGroup *group,
1508 const gchar *domain)
1510 g_return_if_fail (group != NULL);
1512 g_option_group_set_translate_func (group,
1513 (GTranslateFunc)dgettext_swapped,