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.
32 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
58 struct _GOptionContext
62 gchar *parameter_string;
64 gboolean help_enabled;
65 gboolean ignore_unknown;
67 GOptionGroup *main_group;
69 /* We keep a list of change so we can revert them */
72 /* We also keep track of all argv elements that should be NULLed or
82 gchar *help_description;
84 GDestroyNotify destroy_notify;
87 GTranslateFunc translate_func;
88 GDestroyNotify translate_notify;
89 gpointer translate_data;
91 GOptionEntry *entries;
94 GOptionParseFunc pre_parse_func;
95 GOptionParseFunc post_parse_func;
96 GOptionErrorFunc error_func;
99 static void free_changes_list (GOptionContext *context,
101 static void free_pending_nulls (GOptionContext *context,
102 gboolean perform_nulls);
105 g_option_error_quark (void)
110 q = g_quark_from_static_string ("g-option-context-error-quark");
116 * g_option_context_new:
117 * @parameter_string: the parameter string to be used in
118 * <option>--help</option> output.
120 * Creates a new option context.
122 * Returns: a newly created #GOptionContext, which must be
123 * freed with g_option_context_free() after use.
128 g_option_context_new (const gchar *parameter_string)
131 GOptionContext *context;
133 context = g_new0 (GOptionContext, 1);
135 context->parameter_string = g_strdup (parameter_string);
136 context->help_enabled = TRUE;
137 context->ignore_unknown = FALSE;
143 * g_option_context_free:
144 * @context: a #GOptionContext
146 * Frees context and all the groups which have been
151 void g_option_context_free (GOptionContext *context) {
152 g_return_if_fail (context != NULL);
154 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
155 g_list_free (context->groups);
157 if (context->main_group)
158 g_option_group_free (context->main_group);
160 free_changes_list (context, FALSE);
161 free_pending_nulls (context, FALSE);
163 g_free (context->parameter_string);
170 * g_option_context_set_help_enabled:
171 * @context: a #GOptionContext
172 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
174 * Enables or disables automatic generation of <option>--help</option>
175 * output. By default, g_option_context_parse() recognizes
176 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
177 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
178 * suitable output to stdout.
182 void g_option_context_set_help_enabled (GOptionContext *context,
183 gboolean help_enabled)
186 g_return_if_fail (context != NULL);
188 context->help_enabled = help_enabled;
192 * g_option_context_get_help_enabled:
193 * @context: a #GOptionContext
195 * Returns whether automatic <option>--help</option> generation
196 * is turned on for @context. See g_option_context_set_help_enabled().
198 * Returns: %TRUE if automatic help generation is turned on.
203 g_option_context_get_help_enabled (GOptionContext *context)
205 g_return_val_if_fail (context != NULL, FALSE);
207 return context->help_enabled;
211 * g_option_context_set_ignore_unknown_options:
212 * @context: a #GOptionContext
213 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
214 * an error when unknown options are met
216 * Sets whether to ignore unknown options or not. If an argument is
217 * ignored, it is left in the @argv array after parsing. By default,
218 * g_option_context_parse() treats unknown options as error.
220 * This setting does not affect non-option arguments (i.e. arguments
221 * which don't start with a dash).
226 g_option_context_set_ignore_unknown_options (GOptionContext *context,
227 gboolean ignore_unknown)
229 g_return_if_fail (context != NULL);
231 context->ignore_unknown = ignore_unknown;
235 * g_option_context_get_ignore_unknown_options:
236 * @context: a #GOptionContext
238 * Returns whether unknown options are ignored or not. See
239 * g_option_context_set_ignore_unknown_options().
241 * Returns: %TRUE if unknown options are ignored.
246 g_option_context_get_ignore_unknown_options (GOptionContext *context)
248 g_return_val_if_fail (context != NULL, FALSE);
250 return context->ignore_unknown;
254 * g_option_context_add_group:
255 * @context: a #GOptionContext
256 * @group: the group to add
258 * Adds a #GOptionGroup to the @context, so that parsing with @context
259 * will recognize the options in the group. Note that the group will
260 * be freed together with the context when g_option_context_free() is
261 * called, so you must not free the group yourself after adding it
267 g_option_context_add_group (GOptionContext *context,
270 g_return_if_fail (context != NULL);
271 g_return_if_fail (group != NULL);
272 g_return_if_fail (group->name != NULL);
273 g_return_if_fail (group->description != NULL);
274 g_return_if_fail (group->help_description != NULL);
276 context->groups = g_list_prepend (context->groups, group);
280 * g_option_context_set_main_group:
281 * @context: a #GOptionContext
282 * @group: the group to set as main group
284 * Sets a #GOptionGroup as main group of the @context.
285 * This has the same effect as calling g_option_context_add_group(),
286 * the only difference is that the options in the main group are
287 * treated differently when generating <option>--help</option> output.
292 g_option_context_set_main_group (GOptionContext *context,
295 g_return_if_fail (context != NULL);
296 g_return_if_fail (group != NULL);
298 context->main_group = group;
302 * g_option_context_get_main_group:
303 * @context: a #GOptionContext
305 * Returns a pointer to the main group of @context.
307 * Return value: the main group of @context, or %NULL if @context doesn't
308 * have a main group. Note that group belongs to @context and should
309 * not be modified or freed.
314 g_option_context_get_main_group (GOptionContext *context)
316 g_return_val_if_fail (context != NULL, NULL);
318 return context->main_group;
322 * g_option_context_add_main_entries:
323 * @context: a #GOptionContext
324 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
325 * @translation_domain: a translation domain to use for translating
326 * the <option>--help</option> output for the options in @entries
327 * with gettext(), or %NULL
329 * A convenience function which creates a main group if it doesn't
330 * exist, adds the @entries to it and sets the translation domain.
335 g_option_context_add_main_entries (GOptionContext *context,
336 const GOptionEntry *entries,
337 const gchar *translation_domain)
339 g_return_if_fail (entries != NULL);
341 if (!context->main_group)
342 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
344 g_option_group_add_entries (context->main_group, entries);
345 g_option_group_set_translation_domain (context->main_group, translation_domain);
349 print_entry (GOptionGroup *group,
351 const GOptionEntry *entry)
355 if (entry->flags & G_OPTION_FLAG_HIDDEN)
358 str = g_string_new (NULL);
360 if (entry->short_name)
361 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
363 g_string_append_printf (str, " --%s", entry->long_name);
365 if (entry->arg_description)
366 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
368 g_print ("%-*s %s\n", max_length + 4, str->str,
369 entry->description ? TRANSLATE (group, entry->description) : "");
370 g_string_free (str, TRUE);
374 print_help (GOptionContext *context,
379 gint max_length, len;
382 g_print ("%s\n %s %s %s\n\n",
383 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
384 context->parameter_string ? context->parameter_string : "");
386 list = context->groups;
388 max_length = g_utf8_strlen ("--help, -?", -1);
392 len = g_utf8_strlen ("--help-all", -1);
393 max_length = MAX (max_length, len);
398 GOptionGroup *group = list->data;
400 /* First, we check the --help-<groupname> options */
401 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
402 max_length = MAX (max_length, len);
404 /* Then we go through the entries */
405 for (i = 0; i < group->n_entries; i++)
407 len = g_utf8_strlen (group->entries[i].long_name, -1);
409 if (group->entries[i].short_name)
412 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
413 group->entries[i].arg_description)
414 len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
416 max_length = MAX (max_length, len);
422 /* Add a bit of padding */
425 list = context->groups;
427 g_print ("%s\n --%-*s %s\n",
428 _("Help Options:"), max_length, "help", _("Show help options"));
430 /* We only want --help-all when there are groups */
432 g_print (" --%-*s %s\n", max_length, "help-all", _("Show all help options"));
436 GOptionGroup *group = list->data;
438 g_print (" --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
447 /* Print a certain group */
449 g_print ("%s\n", TRANSLATE (group, group->description));
450 for (i = 0; i < group->n_entries; i++)
451 print_entry (group, max_length, &group->entries[i]);
456 /* Print all groups */
458 list = context->groups;
462 GOptionGroup *group = list->data;
464 g_print ("%s\n", group->description);
466 for (i = 0; i < group->n_entries; i++)
467 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
468 print_entry (group, max_length, &group->entries[i]);
475 /* Print application options if --help or --help-all has been specified */
476 if (main_help || !group)
478 list = context->groups;
480 g_print ("%s\n", _("Application Options:"));
482 if (context->main_group)
483 for (i = 0; i < context->main_group->n_entries; i++)
484 print_entry (context->main_group, max_length, &context->main_group->entries[i]);
488 GOptionGroup *group = list->data;
490 /* Print main entries from other groups */
491 for (i = 0; i < group->n_entries; i++)
492 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
493 print_entry (group, max_length, &group->entries[i]);
506 parse_int (const gchar *arg_name,
512 glong tmp = strtol (arg, &end, 0);
516 if (*arg == '\0' || *end != '\0')
519 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
520 _("Cannot parse integer value '%s' for --%s"),
526 if (*result != tmp || errno == ERANGE)
529 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
530 _("Integer value '%s' for %s out of range"),
539 get_change (GOptionContext *context,
544 Change *change = NULL;
546 for (list = context->changes; list != NULL; list = list->next)
550 if (change->arg_data == arg_data)
554 change = g_new0 (Change, 1);
555 change->arg_type = arg_type;
556 change->arg_data = arg_data;
558 context->changes = g_list_prepend (context->changes, change);
566 add_pending_null (GOptionContext *context,
572 n = g_new0 (PendingNull, 1);
576 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
580 parse_arg (GOptionContext *context,
584 const gchar *option_name,
592 case G_OPTION_ARG_NONE:
594 change = get_change (context, G_OPTION_ARG_NONE,
597 *(gboolean *)entry->arg_data = TRUE;
600 case G_OPTION_ARG_STRING:
604 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
609 change = get_change (context, G_OPTION_ARG_STRING,
611 g_free (change->allocated.str);
613 change->prev.str = *(gchar **)entry->arg_data;
614 change->allocated.str = data;
616 *(gchar **)entry->arg_data = data;
619 case G_OPTION_ARG_STRING_ARRAY:
623 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
628 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
631 if (change->allocated.array.len == 0)
633 change->prev.array = entry->arg_data;
634 change->allocated.array.data = g_new (gchar *, 2);
637 change->allocated.array.data =
638 g_renew (gchar *, change->allocated.array.data,
639 change->allocated.array.len + 2);
641 change->allocated.array.data[change->allocated.array.len] = data;
642 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
644 change->allocated.array.len ++;
646 *(gchar ***)entry->arg_data = change->allocated.array.data;
651 case G_OPTION_ARG_FILENAME:
655 data = g_strdup (value);
657 change = get_change (context, G_OPTION_ARG_FILENAME,
659 g_free (change->allocated.str);
661 change->prev.str = *(gchar **)entry->arg_data;
662 change->allocated.str = data;
664 *(gchar **)entry->arg_data = data;
668 case G_OPTION_ARG_FILENAME_ARRAY:
672 data = g_strdup (value);
674 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
677 if (change->allocated.array.len == 0)
679 change->prev.array = entry->arg_data;
680 change->allocated.array.data = g_new (gchar *, 2);
683 change->allocated.array.data =
684 g_renew (gchar *, change->allocated.array.data,
685 change->allocated.array.len + 2);
687 change->allocated.array.data[change->allocated.array.len] = data;
688 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
690 change->allocated.array.len ++;
692 *(gchar ***)entry->arg_data = change->allocated.array.data;
697 case G_OPTION_ARG_INT:
701 if (!parse_int (option_name, value,
706 change = get_change (context, G_OPTION_ARG_INT,
708 change->prev.integer = *(gint *)entry->arg_data;
709 *(gint *)entry->arg_data = data;
712 case G_OPTION_ARG_CALLBACK:
717 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
722 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
731 g_assert_not_reached ();
738 parse_short_option (GOptionContext *context,
750 for (j = 0; j < group->n_entries; j++)
752 if (arg == group->entries[j].short_name)
754 if (group->entries[j].arg == G_OPTION_ARG_NONE)
756 parse_arg (context, group, &group->entries[j],
765 if (*new_index > index)
767 g_warning ("FIXME: figure out the correct error here");
772 if (index < *argc - 1)
774 value = (*argv)[index + 1];
775 add_pending_null (context, &((*argv)[index + 1]), NULL);
776 *new_index = index + 1;
782 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
784 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
786 g_free (option_name);
790 g_free (option_name);
800 parse_long_option (GOptionContext *context,
811 for (j = 0; j < group->n_entries; j++)
816 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
817 strcmp (arg, group->entries[j].long_name) == 0)
819 parse_arg (context, group, &group->entries[j],
822 add_pending_null (context, &((*argv)[*index]), NULL);
827 gint len = strlen (group->entries[j].long_name);
829 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
830 (arg[len] == '=' || arg[len] == 0))
835 add_pending_null (context, &((*argv)[*index]), NULL);
838 value = arg + len + 1;
839 else if (*index < *argc - 1)
841 value = (*argv)[*index + 1];
842 add_pending_null (context, &((*argv)[*index + 1]), NULL);
848 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
850 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
852 g_free (option_name);
856 g_free (option_name);
866 free_changes_list (GOptionContext *context,
871 for (list = context->changes; list != NULL; list = list->next)
873 Change *change = list->data;
877 switch (change->arg_type)
879 case G_OPTION_ARG_NONE:
880 *(gboolean *)change->arg_data = change->prev.bool;
882 case G_OPTION_ARG_INT:
883 *(gint *)change->arg_data = change->prev.integer;
885 case G_OPTION_ARG_STRING:
886 case G_OPTION_ARG_FILENAME:
887 g_free (change->allocated.str);
888 *(gchar **)change->arg_data = change->prev.str;
890 case G_OPTION_ARG_STRING_ARRAY:
891 case G_OPTION_ARG_FILENAME_ARRAY:
892 g_strfreev (change->allocated.array.data);
893 *(gchar ***)change->arg_data = change->prev.array;
896 g_assert_not_reached ();
903 g_list_free (context->changes);
904 context->changes = NULL;
908 free_pending_nulls (GOptionContext *context,
909 gboolean perform_nulls)
913 for (list = context->pending_nulls; list != NULL; list = list->next)
915 PendingNull *n = list->data;
921 /* Copy back the short options */
923 strcpy (*n->ptr + 1, n->value);
933 g_list_free (context->pending_nulls);
934 context->pending_nulls = NULL;
938 * g_option_context_parse:
939 * @context: a #GOptionContext
940 * @argc: a pointer to the number of command line arguments.
941 * @argv: a pointer to the array of command line arguments.
942 * @error: a return location for errors
944 * Parses the command line arguments, recognizing options
945 * which have been added to @context. A side-effect of
946 * calling this function is that g_set_prgname() will be
949 * If the parsing is successful, any parsed arguments are
950 * removed from the array and @argc and @argv are updated
951 * accordingly. In case of an error, @argc and @argv are
954 * Return value: %TRUE if the parsing was successful,
955 * %FALSE if an error occurred
960 g_option_context_parse (GOptionContext *context,
968 /* Set program name */
969 if (argc && argv && *argc)
973 prgname = g_path_get_basename ((*argv)[0]);
974 g_set_prgname (prgname);
979 g_set_prgname ("<unknown>");
982 /* Call pre-parse hooks */
983 list = context->groups;
986 GOptionGroup *group = list->data;
988 if (group->pre_parse_func)
990 if (!(* group->pre_parse_func) (context, group,
991 group->user_data, error))
998 if (context->main_group && context->main_group->pre_parse_func)
1000 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1001 context->main_group->user_data, error))
1007 for (i = 1; i < *argc; i++)
1010 gboolean parsed = FALSE;
1012 if ((*argv)[i][0] == '-')
1014 if ((*argv)[i][1] == '-')
1018 arg = (*argv)[i] + 2;
1020 /* '--' terminates list of arguments */
1023 add_pending_null (context, &((*argv)[i]), NULL);
1027 /* Handle help options */
1028 if (context->help_enabled)
1030 if (strcmp (arg, "help") == 0)
1031 print_help (context, TRUE, NULL);
1032 else if (strcmp (arg, "help-all") == 0)
1033 print_help (context, FALSE, NULL);
1034 else if (strncmp (arg, "help-", 5) == 0)
1038 list = context->groups;
1042 GOptionGroup *group = list->data;
1044 if (strcmp (arg + 5, group->name) == 0)
1045 print_help (context, FALSE, group);
1052 if (context->main_group &&
1053 !parse_long_option (context, context->main_group, &i, arg,
1054 argc, argv, error, &parsed))
1060 /* Try the groups */
1061 list = context->groups;
1064 GOptionGroup *group = list->data;
1066 if (!parse_long_option (context, group, &i, arg,
1067 argc, argv, error, &parsed))
1076 if (context->ignore_unknown)
1082 gboolean *nulled_out = NULL;
1084 arg = (*argv)[i] + 1;
1088 if (context->ignore_unknown)
1089 nulled_out = g_new0 (gboolean, strlen (arg));
1091 for (j = 0; j < strlen (arg); j++)
1095 if (context->main_group &&
1096 !parse_short_option (context, context->main_group,
1098 argc, argv, error, &parsed))
1101 g_free (nulled_out);
1107 /* Try the groups */
1108 list = context->groups;
1111 GOptionGroup *group = list->data;
1113 if (!parse_short_option (context, group, i, &new_i, arg[j],
1114 argc, argv, error, &parsed))
1124 if (context->ignore_unknown)
1127 nulled_out[j] = TRUE;
1136 if (context->ignore_unknown)
1138 gchar *new_arg = NULL;
1141 for (j = 0; j < strlen (arg); j++)
1146 new_arg = g_malloc (strlen (arg));
1147 new_arg[arg_index++] = arg[j];
1151 new_arg[arg_index] = '\0';
1153 add_pending_null (context, &((*argv)[i]), new_arg);
1157 add_pending_null (context, &((*argv)[i]), NULL);
1162 if (!parsed && !context->ignore_unknown)
1165 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1166 _("Unknown option %s"), (*argv)[i]);
1172 /* Call post-parse hooks */
1173 list = context->groups;
1176 GOptionGroup *group = list->data;
1178 if (group->post_parse_func)
1180 if (!(* group->post_parse_func) (context, group,
1181 group->user_data, error))
1188 if (context->main_group && context->main_group->post_parse_func)
1190 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1191 context->main_group->user_data, error))
1195 free_pending_nulls (context, TRUE);
1197 for (i = 1; i < *argc; i++)
1199 for (k = i; k < *argc; k++)
1200 if ((*argv)[k] != NULL)
1206 for (j = i + k; j < *argc; j++)
1207 (*argv)[j-k] = (*argv)[j];
1217 /* Call error hooks */
1218 list = context->groups;
1221 GOptionGroup *group = list->data;
1223 if (group->error_func)
1224 (* group->error_func) (context, group,
1225 group->user_data, error);
1230 if (context->main_group && context->main_group->error_func)
1231 (* context->main_group->error_func) (context, context->main_group,
1232 context->main_group->user_data, error);
1234 free_changes_list (context, TRUE);
1235 free_pending_nulls (context, FALSE);
1241 * g_option_group_new:
1242 * @name: the name for the option group, this is used to provide
1243 * help for the options in this group with <option>--help-</option>@name
1244 * @description: a description for this group to be shown in
1245 * <option>--help</option>. This string is translated using the translation
1246 * domain or translation function of the group
1247 * @help_description: a description for the <option>--help-</option>@name option.
1248 * This string is translated using the translation domain or translation function
1250 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1251 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1252 * @destroy: a function that will be called to free @user_data, or %NULL
1254 * Creates a new #GOptionGroup.
1256 * Return value: a newly created option group. It should be added
1257 * to a #GOptionContext or freed with g_option_group_free().
1262 g_option_group_new (const gchar *name,
1263 const gchar *description,
1264 const gchar *help_description,
1266 GDestroyNotify destroy)
1269 GOptionGroup *group;
1271 group = g_new0 (GOptionGroup, 1);
1272 group->name = g_strdup (name);
1273 group->description = g_strdup (description);
1274 group->help_description = g_strdup (help_description);
1275 group->user_data = user_data;
1276 group->destroy_notify = destroy;
1282 * g_option_group_free:
1283 * @group: a #GOptionGroup
1285 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1286 * free groups which have been added to a #GOptionContext.
1291 g_option_group_free (GOptionGroup *group)
1293 g_return_if_fail (group != NULL);
1295 g_free (group->name);
1296 g_free (group->description);
1297 g_free (group->help_description);
1299 g_free (group->entries);
1301 if (group->destroy_notify)
1302 (* group->destroy_notify) (group->user_data);
1304 if (group->translate_notify)
1305 (* group->translate_notify) (group->translate_data);
1312 * g_option_group_add_entries:
1313 * @group: a #GOptionGroup
1314 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1316 * Adds the options specified in @entries to @group.
1321 g_option_group_add_entries (GOptionGroup *group,
1322 const GOptionEntry *entries)
1326 g_return_if_fail (entries != NULL);
1328 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1330 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1332 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1334 group->n_entries += n_entries;
1338 * g_option_group_set_parse_hooks:
1339 * @group: a #GOptionGroup
1340 * @pre_parse_func: a function to call before parsing, or %NULL
1341 * @post_parse_func: a function to call after parsing, or %NULL
1343 * Associates two functions with @group which will be called
1344 * from g_option_context_parse() before the first option is parsed
1345 * and after the last option has been parsed, respectively.
1347 * Note that the user data to be passed to @pre_parse_func and
1348 * @post_parse_func can be specified when constructing the group
1349 * with g_option_group_new().
1354 g_option_group_set_parse_hooks (GOptionGroup *group,
1355 GOptionParseFunc pre_parse_func,
1356 GOptionParseFunc post_parse_func)
1358 g_return_if_fail (group != NULL);
1360 group->pre_parse_func = pre_parse_func;
1361 group->post_parse_func = post_parse_func;
1365 * g_option_group_set_error_hook:
1366 * @group: a #GOptionGroup
1367 * @error_func: a function to call when an error occurs
1369 * Associates a function with @group which will be called
1370 * from g_option_context_parse() when an error occurs.
1372 * Note that the user data to be passed to @pre_parse_func and
1373 * @post_parse_func can be specified when constructing the group
1374 * with g_option_group_new().
1379 g_option_group_set_error_hook (GOptionGroup *group,
1380 GOptionErrorFunc error_func)
1382 g_return_if_fail (group != NULL);
1384 group->error_func = error_func;
1389 * g_option_group_set_translate_func:
1390 * @group: a #GOptionGroup
1391 * @func: the #GTranslateFunc, or %NULL
1392 * @data: user data to pass to @func, or %NULL
1393 * @destroy_notify: a function which gets called to free @data, or %NULL
1395 * Sets the function which is used to translate user-visible
1396 * strings, for <option>--help</option> output. Different
1397 * groups can use different #GTranslateFunc<!-- -->s. If @func
1398 * is %NULL, strings are not translated.
1400 * If you are using gettext(), you only need to set the translation
1401 * domain, see g_option_group_set_translation_domain().
1406 g_option_group_set_translate_func (GOptionGroup *group,
1407 GTranslateFunc func,
1409 GDestroyNotify destroy_notify)
1411 g_return_if_fail (group != NULL);
1413 if (group->translate_notify)
1414 group->translate_notify (group->translate_data);
1416 group->translate_func = func;
1417 group->translate_data = data;
1418 group->translate_notify = notify;
1422 dgettext_swapped (const gchar *msgid,
1423 const gchar *domainname)
1425 return dgettext (domainname, msgid);
1429 * g_option_group_set_translation_domain:
1430 * @group: a #GOptionGroup
1431 * @domain: the domain to use
1433 * A convenience function to use gettext() for translating
1434 * user-visible strings.
1439 g_option_group_set_translation_domain (GOptionGroup *group,
1440 const gchar *domain)
1442 g_return_if_fail (group != NULL);
1444 g_option_group_set_translate_func (group,
1445 (GTranslateFunc)dgettext_swapped,