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:
662 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
667 data = g_strdup (value);
669 change = get_change (context, G_OPTION_ARG_FILENAME,
671 g_free (change->allocated.str);
673 change->prev.str = *(gchar **)entry->arg_data;
674 change->allocated.str = data;
676 *(gchar **)entry->arg_data = data;
680 case G_OPTION_ARG_FILENAME_ARRAY:
685 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
690 data = g_strdup (value);
692 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
695 if (change->allocated.array.len == 0)
697 change->prev.array = entry->arg_data;
698 change->allocated.array.data = g_new (gchar *, 2);
701 change->allocated.array.data =
702 g_renew (gchar *, change->allocated.array.data,
703 change->allocated.array.len + 2);
705 change->allocated.array.data[change->allocated.array.len] = data;
706 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
708 change->allocated.array.len ++;
710 *(gchar ***)entry->arg_data = change->allocated.array.data;
715 case G_OPTION_ARG_INT:
719 if (!parse_int (option_name, value,
724 change = get_change (context, G_OPTION_ARG_INT,
726 change->prev.integer = *(gint *)entry->arg_data;
727 *(gint *)entry->arg_data = data;
730 case G_OPTION_ARG_CALLBACK:
735 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
740 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
749 g_assert_not_reached ();
756 parse_short_option (GOptionContext *context,
768 for (j = 0; j < group->n_entries; j++)
770 if (arg == group->entries[j].short_name)
772 if (group->entries[j].arg == G_OPTION_ARG_NONE)
774 parse_arg (context, group, &group->entries[j],
783 if (*new_index > index)
785 g_warning ("FIXME: figure out the correct error here");
790 if (index < *argc - 1)
792 value = (*argv)[index + 1];
793 add_pending_null (context, &((*argv)[index + 1]), NULL);
794 *new_index = index + 1;
800 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
802 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
804 g_free (option_name);
808 g_free (option_name);
818 parse_long_option (GOptionContext *context,
829 for (j = 0; j < group->n_entries; j++)
834 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
835 strcmp (arg, group->entries[j].long_name) == 0)
837 parse_arg (context, group, &group->entries[j],
840 add_pending_null (context, &((*argv)[*index]), NULL);
845 gint len = strlen (group->entries[j].long_name);
847 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
848 (arg[len] == '=' || arg[len] == 0))
853 add_pending_null (context, &((*argv)[*index]), NULL);
856 value = arg + len + 1;
857 else if (*index < *argc - 1)
859 value = (*argv)[*index + 1];
860 add_pending_null (context, &((*argv)[*index + 1]), NULL);
866 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
868 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
870 g_free (option_name);
874 g_free (option_name);
884 parse_remaining_arg (GOptionContext *context,
894 for (j = 0; j < group->n_entries; j++)
899 if (group->entries[j].long_name[0])
902 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
903 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
905 add_pending_null (context, &((*argv)[*index]), NULL);
907 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
918 free_changes_list (GOptionContext *context,
923 for (list = context->changes; list != NULL; list = list->next)
925 Change *change = list->data;
929 switch (change->arg_type)
931 case G_OPTION_ARG_NONE:
932 *(gboolean *)change->arg_data = change->prev.bool;
934 case G_OPTION_ARG_INT:
935 *(gint *)change->arg_data = change->prev.integer;
937 case G_OPTION_ARG_STRING:
938 case G_OPTION_ARG_FILENAME:
939 g_free (change->allocated.str);
940 *(gchar **)change->arg_data = change->prev.str;
942 case G_OPTION_ARG_STRING_ARRAY:
943 case G_OPTION_ARG_FILENAME_ARRAY:
944 g_strfreev (change->allocated.array.data);
945 *(gchar ***)change->arg_data = change->prev.array;
948 g_assert_not_reached ();
955 g_list_free (context->changes);
956 context->changes = NULL;
960 free_pending_nulls (GOptionContext *context,
961 gboolean perform_nulls)
965 for (list = context->pending_nulls; list != NULL; list = list->next)
967 PendingNull *n = list->data;
973 /* Copy back the short options */
975 strcpy (*n->ptr + 1, n->value);
985 g_list_free (context->pending_nulls);
986 context->pending_nulls = NULL;
990 * g_option_context_parse:
991 * @context: a #GOptionContext
992 * @argc: a pointer to the number of command line arguments.
993 * @argv: a pointer to the array of command line arguments.
994 * @error: a return location for errors
996 * Parses the command line arguments, recognizing options
997 * which have been added to @context. A side-effect of
998 * calling this function is that g_set_prgname() will be
1001 * If the parsing is successful, any parsed arguments are
1002 * removed from the array and @argc and @argv are updated
1003 * accordingly. In case of an error, @argc and @argv are
1006 * Return value: %TRUE if the parsing was successful,
1007 * %FALSE if an error occurred
1012 g_option_context_parse (GOptionContext *context,
1020 /* Set program name */
1021 if (argc && argv && *argc)
1025 prgname = g_path_get_basename ((*argv)[0]);
1026 g_set_prgname (prgname);
1031 g_set_prgname ("<unknown>");
1034 /* Call pre-parse hooks */
1035 list = context->groups;
1038 GOptionGroup *group = list->data;
1040 if (group->pre_parse_func)
1042 if (!(* group->pre_parse_func) (context, group,
1043 group->user_data, error))
1050 if (context->main_group && context->main_group->pre_parse_func)
1052 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1053 context->main_group->user_data, error))
1059 gboolean stop_parsing = FALSE;
1061 for (i = 1; i < *argc; i++)
1064 gboolean parsed = FALSE;
1066 if ((*argv)[i][0] == '-' && !stop_parsing)
1068 if ((*argv)[i][1] == '-')
1072 arg = (*argv)[i] + 2;
1074 /* '--' terminates list of arguments */
1077 add_pending_null (context, &((*argv)[i]), NULL);
1078 stop_parsing = TRUE;
1082 /* Handle help options */
1083 if (context->help_enabled)
1085 if (strcmp (arg, "help") == 0)
1086 print_help (context, TRUE, NULL);
1087 else if (strcmp (arg, "help-all") == 0)
1088 print_help (context, FALSE, NULL);
1089 else if (strncmp (arg, "help-", 5) == 0)
1093 list = context->groups;
1097 GOptionGroup *group = list->data;
1099 if (strcmp (arg + 5, group->name) == 0)
1100 print_help (context, FALSE, group);
1107 if (context->main_group &&
1108 !parse_long_option (context, context->main_group, &i, arg,
1109 argc, argv, error, &parsed))
1115 /* Try the groups */
1116 list = context->groups;
1119 GOptionGroup *group = list->data;
1121 if (!parse_long_option (context, group, &i, arg,
1122 argc, argv, error, &parsed))
1131 if (context->ignore_unknown)
1137 gboolean *nulled_out = NULL;
1139 arg = (*argv)[i] + 1;
1143 if (context->ignore_unknown)
1144 nulled_out = g_new0 (gboolean, strlen (arg));
1146 for (j = 0; j < strlen (arg); j++)
1150 if (context->main_group &&
1151 !parse_short_option (context, context->main_group,
1153 argc, argv, error, &parsed))
1156 g_free (nulled_out);
1162 /* Try the groups */
1163 list = context->groups;
1166 GOptionGroup *group = list->data;
1168 if (!parse_short_option (context, group, i, &new_i, arg[j],
1169 argc, argv, error, &parsed))
1179 if (context->ignore_unknown)
1182 nulled_out[j] = TRUE;
1191 if (context->ignore_unknown)
1193 gchar *new_arg = NULL;
1196 for (j = 0; j < strlen (arg); j++)
1201 new_arg = g_malloc (strlen (arg));
1202 new_arg[arg_index++] = arg[j];
1206 new_arg[arg_index] = '\0';
1208 add_pending_null (context, &((*argv)[i]), new_arg);
1212 add_pending_null (context, &((*argv)[i]), NULL);
1217 if (!parsed && !context->ignore_unknown)
1220 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1221 _("Unknown option %s"), (*argv)[i]);
1227 /* Collect remaining args */
1228 if (context->main_group &&
1229 !parse_remaining_arg (context, context->main_group, &i,
1230 argc, argv, error, &parsed))
1236 /* Call post-parse hooks */
1237 list = context->groups;
1240 GOptionGroup *group = list->data;
1242 if (group->post_parse_func)
1244 if (!(* group->post_parse_func) (context, group,
1245 group->user_data, error))
1252 if (context->main_group && context->main_group->post_parse_func)
1254 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1255 context->main_group->user_data, error))
1259 free_pending_nulls (context, TRUE);
1261 for (i = 1; i < *argc; i++)
1263 for (k = i; k < *argc; k++)
1264 if ((*argv)[k] != NULL)
1270 for (j = i + k; j < *argc; j++)
1272 (*argv)[j-k] = (*argv)[j];
1284 /* Call error hooks */
1285 list = context->groups;
1288 GOptionGroup *group = list->data;
1290 if (group->error_func)
1291 (* group->error_func) (context, group,
1292 group->user_data, error);
1297 if (context->main_group && context->main_group->error_func)
1298 (* context->main_group->error_func) (context, context->main_group,
1299 context->main_group->user_data, error);
1301 free_changes_list (context, TRUE);
1302 free_pending_nulls (context, FALSE);
1308 * g_option_group_new:
1309 * @name: the name for the option group, this is used to provide
1310 * help for the options in this group with <option>--help-</option>@name
1311 * @description: a description for this group to be shown in
1312 * <option>--help</option>. This string is translated using the translation
1313 * domain or translation function of the group
1314 * @help_description: a description for the <option>--help-</option>@name option.
1315 * This string is translated using the translation domain or translation function
1317 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1318 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1319 * @destroy: a function that will be called to free @user_data, or %NULL
1321 * Creates a new #GOptionGroup.
1323 * Return value: a newly created option group. It should be added
1324 * to a #GOptionContext or freed with g_option_group_free().
1329 g_option_group_new (const gchar *name,
1330 const gchar *description,
1331 const gchar *help_description,
1333 GDestroyNotify destroy)
1336 GOptionGroup *group;
1338 group = g_new0 (GOptionGroup, 1);
1339 group->name = g_strdup (name);
1340 group->description = g_strdup (description);
1341 group->help_description = g_strdup (help_description);
1342 group->user_data = user_data;
1343 group->destroy_notify = destroy;
1349 * g_option_group_free:
1350 * @group: a #GOptionGroup
1352 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1353 * free groups which have been added to a #GOptionContext.
1358 g_option_group_free (GOptionGroup *group)
1360 g_return_if_fail (group != NULL);
1362 g_free (group->name);
1363 g_free (group->description);
1364 g_free (group->help_description);
1366 g_free (group->entries);
1368 if (group->destroy_notify)
1369 (* group->destroy_notify) (group->user_data);
1371 if (group->translate_notify)
1372 (* group->translate_notify) (group->translate_data);
1379 * g_option_group_add_entries:
1380 * @group: a #GOptionGroup
1381 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1383 * Adds the options specified in @entries to @group.
1388 g_option_group_add_entries (GOptionGroup *group,
1389 const GOptionEntry *entries)
1393 g_return_if_fail (entries != NULL);
1395 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1397 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1399 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1401 group->n_entries += n_entries;
1405 * g_option_group_set_parse_hooks:
1406 * @group: a #GOptionGroup
1407 * @pre_parse_func: a function to call before parsing, or %NULL
1408 * @post_parse_func: a function to call after parsing, or %NULL
1410 * Associates two functions with @group which will be called
1411 * from g_option_context_parse() before the first option is parsed
1412 * and after the last option has been parsed, respectively.
1414 * Note that the user data to be passed to @pre_parse_func and
1415 * @post_parse_func can be specified when constructing the group
1416 * with g_option_group_new().
1421 g_option_group_set_parse_hooks (GOptionGroup *group,
1422 GOptionParseFunc pre_parse_func,
1423 GOptionParseFunc post_parse_func)
1425 g_return_if_fail (group != NULL);
1427 group->pre_parse_func = pre_parse_func;
1428 group->post_parse_func = post_parse_func;
1432 * g_option_group_set_error_hook:
1433 * @group: a #GOptionGroup
1434 * @error_func: a function to call when an error occurs
1436 * Associates a function with @group which will be called
1437 * from g_option_context_parse() when an error occurs.
1439 * Note that the user data to be passed to @pre_parse_func and
1440 * @post_parse_func can be specified when constructing the group
1441 * with g_option_group_new().
1446 g_option_group_set_error_hook (GOptionGroup *group,
1447 GOptionErrorFunc error_func)
1449 g_return_if_fail (group != NULL);
1451 group->error_func = error_func;
1456 * g_option_group_set_translate_func:
1457 * @group: a #GOptionGroup
1458 * @func: the #GTranslateFunc, or %NULL
1459 * @data: user data to pass to @func, or %NULL
1460 * @destroy_notify: a function which gets called to free @data, or %NULL
1462 * Sets the function which is used to translate user-visible
1463 * strings, for <option>--help</option> output. Different
1464 * groups can use different #GTranslateFunc<!-- -->s. If @func
1465 * is %NULL, strings are not translated.
1467 * If you are using gettext(), you only need to set the translation
1468 * domain, see g_option_group_set_translation_domain().
1473 g_option_group_set_translate_func (GOptionGroup *group,
1474 GTranslateFunc func,
1476 GDestroyNotify destroy_notify)
1478 g_return_if_fail (group != NULL);
1480 if (group->translate_notify)
1481 group->translate_notify (group->translate_data);
1483 group->translate_func = func;
1484 group->translate_data = data;
1485 group->translate_notify = destroy_notify;
1489 dgettext_swapped (const gchar *msgid,
1490 const gchar *domainname)
1492 return dgettext (domainname, msgid);
1496 * g_option_group_set_translation_domain:
1497 * @group: a #GOptionGroup
1498 * @domain: the domain to use
1500 * A convenience function to use gettext() for translating
1501 * user-visible strings.
1506 g_option_group_set_translation_domain (GOptionGroup *group,
1507 const gchar *domain)
1509 g_return_if_fail (group != NULL);
1511 g_option_group_set_translate_func (group,
1512 (GTranslateFunc)dgettext_swapped,