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)))
36 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
37 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
38 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
40 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
41 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
71 struct _GOptionContext
75 gchar *parameter_string;
77 gboolean help_enabled;
78 gboolean ignore_unknown;
80 GOptionGroup *main_group;
82 /* We keep a list of change so we can revert them */
85 /* We also keep track of all argv elements that should be NULLed or
95 gchar *help_description;
97 GDestroyNotify destroy_notify;
100 GTranslateFunc translate_func;
101 GDestroyNotify translate_notify;
102 gpointer translate_data;
104 GOptionEntry *entries;
107 GOptionParseFunc pre_parse_func;
108 GOptionParseFunc post_parse_func;
109 GOptionErrorFunc error_func;
112 static void free_changes_list (GOptionContext *context,
114 static void free_pending_nulls (GOptionContext *context,
115 gboolean perform_nulls);
118 g_option_error_quark (void)
123 q = g_quark_from_static_string ("g-option-context-error-quark");
129 * g_option_context_new:
130 * @parameter_string: a string which is displayed in
131 * the first line of <option>--help</option> output, after
132 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
134 * Creates a new option context.
136 * Returns: a newly created #GOptionContext, which must be
137 * freed with g_option_context_free() after use.
142 g_option_context_new (const gchar *parameter_string)
145 GOptionContext *context;
147 context = g_new0 (GOptionContext, 1);
149 context->parameter_string = g_strdup (parameter_string);
150 context->help_enabled = TRUE;
151 context->ignore_unknown = FALSE;
157 * g_option_context_free:
158 * @context: a #GOptionContext
160 * Frees context and all the groups which have been
165 void g_option_context_free (GOptionContext *context)
167 g_return_if_fail (context != NULL);
169 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
170 g_list_free (context->groups);
172 if (context->main_group)
173 g_option_group_free (context->main_group);
175 free_changes_list (context, FALSE);
176 free_pending_nulls (context, FALSE);
178 g_free (context->parameter_string);
185 * g_option_context_set_help_enabled:
186 * @context: a #GOptionContext
187 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
189 * Enables or disables automatic generation of <option>--help</option>
190 * output. By default, g_option_context_parse() recognizes
191 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
192 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
193 * suitable output to stdout.
197 void g_option_context_set_help_enabled (GOptionContext *context,
198 gboolean help_enabled)
201 g_return_if_fail (context != NULL);
203 context->help_enabled = help_enabled;
207 * g_option_context_get_help_enabled:
208 * @context: a #GOptionContext
210 * Returns whether automatic <option>--help</option> generation
211 * is turned on for @context. See g_option_context_set_help_enabled().
213 * Returns: %TRUE if automatic help generation is turned on.
218 g_option_context_get_help_enabled (GOptionContext *context)
220 g_return_val_if_fail (context != NULL, FALSE);
222 return context->help_enabled;
226 * g_option_context_set_ignore_unknown_options:
227 * @context: a #GOptionContext
228 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
229 * an error when unknown options are met
231 * Sets whether to ignore unknown options or not. If an argument is
232 * ignored, it is left in the @argv array after parsing. By default,
233 * g_option_context_parse() treats unknown options as error.
235 * This setting does not affect non-option arguments (i.e. arguments
236 * which don't start with a dash). But note that GOption cannot reliably
237 * determine whether a non-option belongs to a preceding unknown option.
242 g_option_context_set_ignore_unknown_options (GOptionContext *context,
243 gboolean ignore_unknown)
245 g_return_if_fail (context != NULL);
247 context->ignore_unknown = ignore_unknown;
251 * g_option_context_get_ignore_unknown_options:
252 * @context: a #GOptionContext
254 * Returns whether unknown options are ignored or not. See
255 * g_option_context_set_ignore_unknown_options().
257 * Returns: %TRUE if unknown options are ignored.
262 g_option_context_get_ignore_unknown_options (GOptionContext *context)
264 g_return_val_if_fail (context != NULL, FALSE);
266 return context->ignore_unknown;
270 * g_option_context_add_group:
271 * @context: a #GOptionContext
272 * @group: the group to add
274 * Adds a #GOptionGroup to the @context, so that parsing with @context
275 * will recognize the options in the group. Note that the group will
276 * be freed together with the context when g_option_context_free() is
277 * called, so you must not free the group yourself after adding it
283 g_option_context_add_group (GOptionContext *context,
288 g_return_if_fail (context != NULL);
289 g_return_if_fail (group != NULL);
290 g_return_if_fail (group->name != NULL);
291 g_return_if_fail (group->description != NULL);
292 g_return_if_fail (group->help_description != NULL);
294 for (list = context->groups; list; list = list->next)
296 GOptionGroup *g = (GOptionGroup *)list->data;
298 if ((group->name == NULL && g->name == NULL) ||
299 (group->name && g->name && strcmp (group->name, g->name) == 0))
300 g_warning ("A group named \"%s\" is already part of this GOptionContext",
304 context->groups = g_list_append (context->groups, group);
308 * g_option_context_set_main_group:
309 * @context: a #GOptionContext
310 * @group: the group to set as main group
312 * Sets a #GOptionGroup as main group of the @context.
313 * This has the same effect as calling g_option_context_add_group(),
314 * the only difference is that the options in the main group are
315 * treated differently when generating <option>--help</option> output.
320 g_option_context_set_main_group (GOptionContext *context,
323 g_return_if_fail (context != NULL);
324 g_return_if_fail (group != NULL);
326 if (context->main_group)
328 g_warning ("This GOptionContext already has a main group");
333 context->main_group = group;
337 * g_option_context_get_main_group:
338 * @context: a #GOptionContext
340 * Returns a pointer to the main group of @context.
342 * Return value: the main group of @context, or %NULL if @context doesn't
343 * have a main group. Note that group belongs to @context and should
344 * not be modified or freed.
349 g_option_context_get_main_group (GOptionContext *context)
351 g_return_val_if_fail (context != NULL, NULL);
353 return context->main_group;
357 * g_option_context_add_main_entries:
358 * @context: a #GOptionContext
359 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
360 * @translation_domain: a translation domain to use for translating
361 * the <option>--help</option> output for the options in @entries
362 * with gettext(), or %NULL
364 * A convenience function which creates a main group if it doesn't
365 * exist, adds the @entries to it and sets the translation domain.
370 g_option_context_add_main_entries (GOptionContext *context,
371 const GOptionEntry *entries,
372 const gchar *translation_domain)
374 g_return_if_fail (entries != NULL);
376 if (!context->main_group)
377 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
379 g_option_group_add_entries (context->main_group, entries);
380 g_option_group_set_translation_domain (context->main_group, translation_domain);
384 calculate_max_length (GOptionGroup *group)
387 gint i, len, max_length;
391 for (i = 0; i < group->n_entries; i++)
393 entry = &group->entries[i];
395 if (entry->flags & G_OPTION_FLAG_HIDDEN)
398 len = g_utf8_strlen (entry->long_name, -1);
400 if (entry->short_name)
403 if (!NO_ARG (entry) && entry->arg_description)
404 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
406 max_length = MAX (max_length, len);
413 print_entry (GOptionGroup *group,
415 const GOptionEntry *entry)
419 if (entry->flags & G_OPTION_FLAG_HIDDEN)
422 if (entry->long_name[0] == 0)
425 str = g_string_new (NULL);
427 if (entry->short_name)
428 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
430 g_string_append_printf (str, " --%s", entry->long_name);
432 if (entry->arg_description)
433 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
435 g_print ("%-*s %s\n", max_length + 4, str->str,
436 entry->description ? TRANSLATE (group, entry->description) : "");
437 g_string_free (str, TRUE);
441 print_help (GOptionContext *context,
446 gint max_length, len;
449 GHashTable *shadow_map;
451 const gchar *rest_description;
453 rest_description = NULL;
454 if (context->main_group)
456 for (i = 0; i < context->main_group->n_entries; i++)
458 entry = &context->main_group->entries[i];
459 if (entry->long_name[0] == 0)
461 rest_description = entry->arg_description;
467 g_print ("%s\n %s %s%s%s%s%s\n\n",
468 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
469 rest_description ? " " : "",
470 rest_description ? rest_description : "",
471 context->parameter_string ? " " : "",
472 context->parameter_string ? context->parameter_string : "");
474 memset (seen, 0, sizeof (gboolean) * 256);
475 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
477 if (context->main_group)
479 for (i = 0; i < context->main_group->n_entries; i++)
481 entry = &context->main_group->entries[i];
482 g_hash_table_insert (shadow_map,
483 (gpointer)entry->long_name,
486 if (seen[(guchar)entry->short_name])
487 entry->short_name = 0;
489 seen[(guchar)entry->short_name] = TRUE;
493 list = context->groups;
496 GOptionGroup *group = list->data;
497 for (i = 0; i < group->n_entries; i++)
499 entry = &group->entries[i];
500 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
501 !(entry->flags && G_OPTION_FLAG_NOALIAS))
502 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
504 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
506 if (seen[(guchar)entry->short_name] &&
507 !(entry->flags && G_OPTION_FLAG_NOALIAS))
508 entry->short_name = 0;
510 seen[(guchar)entry->short_name] = TRUE;
515 g_hash_table_destroy (shadow_map);
517 list = context->groups;
519 max_length = g_utf8_strlen ("-?, --help", -1);
523 len = g_utf8_strlen ("--help-all", -1);
524 max_length = MAX (max_length, len);
527 if (context->main_group)
529 len = calculate_max_length (context->main_group);
530 max_length = MAX (max_length, len);
535 GOptionGroup *group = list->data;
537 /* First, we check the --help-<groupname> options */
538 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
539 max_length = MAX (max_length, len);
541 /* Then we go through the entries */
542 len = calculate_max_length (group);
543 max_length = MAX (max_length, len);
548 /* Add a bit of padding */
553 list = context->groups;
555 g_print ("%s\n -%c, --%-*s %s\n",
556 _("Help Options:"), '?', max_length - 4, "help",
557 _("Show help options"));
559 /* We only want --help-all when there are groups */
561 g_print (" --%-*s %s\n", max_length, "help-all",
562 _("Show all help options"));
566 GOptionGroup *group = list->data;
568 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
569 TRANSLATE (group, group->help_description));
579 /* Print a certain group */
581 g_print ("%s\n", TRANSLATE (group, group->description));
582 for (i = 0; i < group->n_entries; i++)
583 print_entry (group, max_length, &group->entries[i]);
588 /* Print all groups */
590 list = context->groups;
594 GOptionGroup *group = list->data;
596 g_print ("%s\n", group->description);
598 for (i = 0; i < group->n_entries; i++)
599 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
600 print_entry (group, max_length, &group->entries[i]);
607 /* Print application options if --help or --help-all has been specified */
608 if (main_help || !group)
610 list = context->groups;
612 g_print ("%s\n", _("Application Options:"));
614 if (context->main_group)
615 for (i = 0; i < context->main_group->n_entries; i++)
616 print_entry (context->main_group, max_length,
617 &context->main_group->entries[i]);
621 GOptionGroup *group = list->data;
623 /* Print main entries from other groups */
624 for (i = 0; i < group->n_entries; i++)
625 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
626 print_entry (group, max_length, &group->entries[i]);
638 parse_int (const gchar *arg_name,
647 tmp = strtol (arg, &end, 0);
649 if (*arg == '\0' || *end != '\0')
652 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
653 _("Cannot parse integer value '%s' for %s"),
659 if (*result != tmp || errno == ERANGE)
662 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
663 _("Integer value '%s' for %s out of range"),
672 get_change (GOptionContext *context,
677 Change *change = NULL;
679 for (list = context->changes; list != NULL; list = list->next)
683 if (change->arg_data == arg_data)
687 change = g_new0 (Change, 1);
688 change->arg_type = arg_type;
689 change->arg_data = arg_data;
691 context->changes = g_list_prepend (context->changes, change);
699 add_pending_null (GOptionContext *context,
705 n = g_new0 (PendingNull, 1);
709 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
713 parse_arg (GOptionContext *context,
717 const gchar *option_name,
725 case G_OPTION_ARG_NONE:
727 change = get_change (context, G_OPTION_ARG_NONE,
730 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
733 case G_OPTION_ARG_STRING:
737 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
742 change = get_change (context, G_OPTION_ARG_STRING,
744 g_free (change->allocated.str);
746 change->prev.str = *(gchar **)entry->arg_data;
747 change->allocated.str = data;
749 *(gchar **)entry->arg_data = data;
752 case G_OPTION_ARG_STRING_ARRAY:
756 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
761 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
764 if (change->allocated.array.len == 0)
766 change->prev.array = *(gchar ***)entry->arg_data;
767 change->allocated.array.data = g_new (gchar *, 2);
770 change->allocated.array.data =
771 g_renew (gchar *, change->allocated.array.data,
772 change->allocated.array.len + 2);
774 change->allocated.array.data[change->allocated.array.len] = data;
775 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
777 change->allocated.array.len ++;
779 *(gchar ***)entry->arg_data = change->allocated.array.data;
784 case G_OPTION_ARG_FILENAME:
789 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
794 data = g_strdup (value);
796 change = get_change (context, G_OPTION_ARG_FILENAME,
798 g_free (change->allocated.str);
800 change->prev.str = *(gchar **)entry->arg_data;
801 change->allocated.str = data;
803 *(gchar **)entry->arg_data = data;
807 case G_OPTION_ARG_FILENAME_ARRAY:
812 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
817 data = g_strdup (value);
819 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
822 if (change->allocated.array.len == 0)
824 change->prev.array = *(gchar ***)entry->arg_data;
825 change->allocated.array.data = g_new (gchar *, 2);
828 change->allocated.array.data =
829 g_renew (gchar *, change->allocated.array.data,
830 change->allocated.array.len + 2);
832 change->allocated.array.data[change->allocated.array.len] = data;
833 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
835 change->allocated.array.len ++;
837 *(gchar ***)entry->arg_data = change->allocated.array.data;
842 case G_OPTION_ARG_INT:
846 if (!parse_int (option_name, value,
851 change = get_change (context, G_OPTION_ARG_INT,
853 change->prev.integer = *(gint *)entry->arg_data;
854 *(gint *)entry->arg_data = data;
857 case G_OPTION_ARG_CALLBACK:
862 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
864 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
866 else if (entry->flags & G_OPTION_FLAG_FILENAME)
869 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
871 data = g_strdup (value);
875 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
877 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
881 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
890 g_assert_not_reached ();
897 parse_short_option (GOptionContext *context,
909 for (j = 0; j < group->n_entries; j++)
911 if (arg == group->entries[j].short_name)
916 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
918 if (NO_ARG (&group->entries[j]))
922 if (*new_index > index)
925 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
926 _("Error parsing option %s"), option_name);
927 g_free (option_name);
931 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
933 if (index < *argc - 1)
935 if (!OPTIONAL_ARG (&group->entries[j]))
937 value = (*argv)[index + 1];
938 add_pending_null (context, &((*argv)[index + 1]), NULL);
939 *new_index = index+1;
943 if ((*argv)[index + 1][0] == '-')
947 value = (*argv)[index + 1];
948 add_pending_null (context, &((*argv)[index + 1]), NULL);
949 *new_index = index + 1;
953 else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
958 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
959 _("Missing argument for %s"), option_name);
960 g_free (option_name);
965 if (!parse_arg (context, group, &group->entries[j],
966 value, option_name, error))
968 g_free (option_name);
972 g_free (option_name);
981 parse_long_option (GOptionContext *context,
993 for (j = 0; j < group->n_entries; j++)
998 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1001 if (NO_ARG (&group->entries[j]) &&
1002 strcmp (arg, group->entries[j].long_name) == 0)
1006 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1007 parse_arg (context, group, &group->entries[j],
1008 NULL, option_name, error);
1009 g_free(option_name);
1011 add_pending_null (context, &((*argv)[*index]), NULL);
1016 gint len = strlen (group->entries[j].long_name);
1018 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1019 (arg[len] == '=' || arg[len] == 0))
1021 gchar *value = NULL;
1024 add_pending_null (context, &((*argv)[*index]), NULL);
1025 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1027 if (arg[len] == '=')
1028 value = arg + len + 1;
1029 else if (*index < *argc - 1)
1031 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1033 value = (*argv)[*index + 1];
1034 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1039 if ((*argv)[*index + 1][0] == '-')
1042 retval = parse_arg (context, group, &group->entries[j],
1043 NULL, option_name, error);
1045 g_free (option_name);
1050 value = (*argv)[*index + 1];
1051 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1056 else if (*index >= *argc - 1 &&
1057 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1060 retval = parse_arg (context, group, &group->entries[j],
1061 NULL, option_name, error);
1063 g_free (option_name);
1069 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1070 _("Missing argument for %s"), option_name);
1071 g_free (option_name);
1075 if (!parse_arg (context, group, &group->entries[j],
1076 value, option_name, error))
1078 g_free (option_name);
1082 g_free (option_name);
1092 parse_remaining_arg (GOptionContext *context,
1093 GOptionGroup *group,
1102 for (j = 0; j < group->n_entries; j++)
1104 if (*index >= *argc)
1107 if (group->entries[j].long_name[0])
1110 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1111 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1113 add_pending_null (context, &((*argv)[*index]), NULL);
1115 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1126 free_changes_list (GOptionContext *context,
1131 for (list = context->changes; list != NULL; list = list->next)
1133 Change *change = list->data;
1137 switch (change->arg_type)
1139 case G_OPTION_ARG_NONE:
1140 *(gboolean *)change->arg_data = change->prev.bool;
1142 case G_OPTION_ARG_INT:
1143 *(gint *)change->arg_data = change->prev.integer;
1145 case G_OPTION_ARG_STRING:
1146 case G_OPTION_ARG_FILENAME:
1147 g_free (change->allocated.str);
1148 *(gchar **)change->arg_data = change->prev.str;
1150 case G_OPTION_ARG_STRING_ARRAY:
1151 case G_OPTION_ARG_FILENAME_ARRAY:
1152 g_strfreev (change->allocated.array.data);
1153 *(gchar ***)change->arg_data = change->prev.array;
1156 g_assert_not_reached ();
1163 g_list_free (context->changes);
1164 context->changes = NULL;
1168 free_pending_nulls (GOptionContext *context,
1169 gboolean perform_nulls)
1173 for (list = context->pending_nulls; list != NULL; list = list->next)
1175 PendingNull *n = list->data;
1181 /* Copy back the short options */
1183 strcpy (*n->ptr + 1, n->value);
1193 g_list_free (context->pending_nulls);
1194 context->pending_nulls = NULL;
1198 * g_option_context_parse:
1199 * @context: a #GOptionContext
1200 * @argc: a pointer to the number of command line arguments.
1201 * @argv: a pointer to the array of command line arguments.
1202 * @error: a return location for errors
1204 * Parses the command line arguments, recognizing options
1205 * which have been added to @context. A side-effect of
1206 * calling this function is that g_set_prgname() will be
1209 * If the parsing is successful, any parsed arguments are
1210 * removed from the array and @argc and @argv are updated
1211 * accordingly. A '--' option is stripped from @argv
1212 * unless there are unparsed options before and after it,
1213 * or some of the options after it start with '-'. In case
1214 * of an error, @argc and @argv are left unmodified.
1216 * If automatic <option>--help</option> support is enabled
1217 * (see g_option_context_set_help_enabled()), and the
1218 * @argv array contains one of the recognized help options,
1219 * this function will produce help output to stdout and
1220 * call <literal>exit (0)</literal>.
1222 * Return value: %TRUE if the parsing was successful,
1223 * %FALSE if an error occurred
1228 g_option_context_parse (GOptionContext *context,
1236 /* Set program name */
1237 if (argc && argv && *argc)
1241 prgname = g_path_get_basename ((*argv)[0]);
1242 g_set_prgname (prgname);
1247 g_set_prgname ("<unknown>");
1250 /* Call pre-parse hooks */
1251 list = context->groups;
1254 GOptionGroup *group = list->data;
1256 if (group->pre_parse_func)
1258 if (!(* group->pre_parse_func) (context, group,
1259 group->user_data, error))
1266 if (context->main_group && context->main_group->pre_parse_func)
1268 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1269 context->main_group->user_data, error))
1275 gboolean stop_parsing = FALSE;
1276 gboolean has_unknown = FALSE;
1277 gint separator_pos = 0;
1279 for (i = 1; i < *argc; i++)
1282 gboolean parsed = FALSE;
1284 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1286 if ((*argv)[i][1] == '-')
1290 arg = (*argv)[i] + 2;
1292 /* '--' terminates list of arguments */
1296 stop_parsing = TRUE;
1300 /* Handle help options */
1301 if (context->help_enabled)
1303 if (strcmp (arg, "help") == 0)
1304 print_help (context, TRUE, NULL);
1305 else if (strcmp (arg, "help-all") == 0)
1306 print_help (context, FALSE, NULL);
1307 else if (strncmp (arg, "help-", 5) == 0)
1311 list = context->groups;
1315 GOptionGroup *group = list->data;
1317 if (strcmp (arg + 5, group->name) == 0)
1318 print_help (context, FALSE, group);
1325 if (context->main_group &&
1326 !parse_long_option (context, context->main_group, &i, arg,
1327 FALSE, argc, argv, error, &parsed))
1333 /* Try the groups */
1334 list = context->groups;
1337 GOptionGroup *group = list->data;
1339 if (!parse_long_option (context, group, &i, arg,
1340 FALSE, argc, argv, error, &parsed))
1352 /* Now look for --<group>-<option> */
1353 dash = strchr (arg, '-');
1356 /* Try the groups */
1357 list = context->groups;
1360 GOptionGroup *group = list->data;
1362 if (strncmp (group->name, arg, dash - arg) == 0)
1364 if (!parse_long_option (context, group, &i, dash + 1,
1365 TRUE, argc, argv, error, &parsed))
1376 if (context->ignore_unknown)
1384 gboolean *nulled_out = NULL;
1386 arg = (*argv)[i] + 1;
1390 if (context->ignore_unknown)
1391 nulled_out = g_new0 (gboolean, strlen (arg));
1393 for (j = 0; j < strlen (arg); j++)
1395 if (context->help_enabled && arg[j] == '?')
1396 print_help (context, TRUE, NULL);
1400 if (context->main_group &&
1401 !parse_short_option (context, context->main_group,
1403 argc, argv, error, &parsed))
1406 g_free (nulled_out);
1412 /* Try the groups */
1413 list = context->groups;
1416 GOptionGroup *group = list->data;
1418 if (!parse_short_option (context, group, i, &new_i, arg[j],
1419 argc, argv, error, &parsed))
1429 if (context->ignore_unknown)
1432 nulled_out[j] = TRUE;
1441 if (context->ignore_unknown)
1443 gchar *new_arg = NULL;
1446 for (j = 0; j < strlen (arg); j++)
1451 new_arg = g_malloc (strlen (arg) + 1);
1452 new_arg[arg_index++] = arg[j];
1456 new_arg[arg_index] = '\0';
1458 add_pending_null (context, &((*argv)[i]), new_arg);
1462 add_pending_null (context, &((*argv)[i]), NULL);
1470 if (!parsed && !context->ignore_unknown)
1473 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1474 _("Unknown option %s"), (*argv)[i]);
1480 /* Collect remaining args */
1481 if (context->main_group &&
1482 !parse_remaining_arg (context, context->main_group, &i,
1483 argc, argv, error, &parsed))
1486 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1491 if (separator_pos > 0)
1492 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1496 /* Call post-parse hooks */
1497 list = context->groups;
1500 GOptionGroup *group = list->data;
1502 if (group->post_parse_func)
1504 if (!(* group->post_parse_func) (context, group,
1505 group->user_data, error))
1512 if (context->main_group && context->main_group->post_parse_func)
1514 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1515 context->main_group->user_data, error))
1521 free_pending_nulls (context, TRUE);
1523 for (i = 1; i < *argc; i++)
1525 for (k = i; k < *argc; k++)
1526 if ((*argv)[k] != NULL)
1532 for (j = i + k; j < *argc; j++)
1534 (*argv)[j-k] = (*argv)[j];
1546 /* Call error hooks */
1547 list = context->groups;
1550 GOptionGroup *group = list->data;
1552 if (group->error_func)
1553 (* group->error_func) (context, group,
1554 group->user_data, error);
1559 if (context->main_group && context->main_group->error_func)
1560 (* context->main_group->error_func) (context, context->main_group,
1561 context->main_group->user_data, error);
1563 free_changes_list (context, TRUE);
1564 free_pending_nulls (context, FALSE);
1570 * g_option_group_new:
1571 * @name: the name for the option group, this is used to provide
1572 * help for the options in this group with <option>--help-</option>@name
1573 * @description: a description for this group to be shown in
1574 * <option>--help</option>. This string is translated using the translation
1575 * domain or translation function of the group
1576 * @help_description: a description for the <option>--help-</option>@name option.
1577 * This string is translated using the translation domain or translation function
1579 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1580 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1581 * @destroy: a function that will be called to free @user_data, or %NULL
1583 * Creates a new #GOptionGroup.
1585 * Return value: a newly created option group. It should be added
1586 * to a #GOptionContext or freed with g_option_group_free().
1591 g_option_group_new (const gchar *name,
1592 const gchar *description,
1593 const gchar *help_description,
1595 GDestroyNotify destroy)
1598 GOptionGroup *group;
1600 group = g_new0 (GOptionGroup, 1);
1601 group->name = g_strdup (name);
1602 group->description = g_strdup (description);
1603 group->help_description = g_strdup (help_description);
1604 group->user_data = user_data;
1605 group->destroy_notify = destroy;
1612 * g_option_group_free:
1613 * @group: a #GOptionGroup
1615 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1616 * free groups which have been added to a #GOptionContext.
1621 g_option_group_free (GOptionGroup *group)
1623 g_return_if_fail (group != NULL);
1625 g_free (group->name);
1626 g_free (group->description);
1627 g_free (group->help_description);
1629 g_free (group->entries);
1631 if (group->destroy_notify)
1632 (* group->destroy_notify) (group->user_data);
1634 if (group->translate_notify)
1635 (* group->translate_notify) (group->translate_data);
1642 * g_option_group_add_entries:
1643 * @group: a #GOptionGroup
1644 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1646 * Adds the options specified in @entries to @group.
1651 g_option_group_add_entries (GOptionGroup *group,
1652 const GOptionEntry *entries)
1656 g_return_if_fail (entries != NULL);
1658 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1660 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1662 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1664 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1666 gchar c = group->entries[i].short_name;
1670 if (c == '-' || !g_ascii_isprint (c))
1672 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1673 group->entries[i].short_name = 0;
1678 group->n_entries += n_entries;
1682 * g_option_group_set_parse_hooks:
1683 * @group: a #GOptionGroup
1684 * @pre_parse_func: a function to call before parsing, or %NULL
1685 * @post_parse_func: a function to call after parsing, or %NULL
1687 * Associates two functions with @group which will be called
1688 * from g_option_context_parse() before the first option is parsed
1689 * and after the last option has been parsed, respectively.
1691 * Note that the user data to be passed to @pre_parse_func and
1692 * @post_parse_func can be specified when constructing the group
1693 * with g_option_group_new().
1698 g_option_group_set_parse_hooks (GOptionGroup *group,
1699 GOptionParseFunc pre_parse_func,
1700 GOptionParseFunc post_parse_func)
1702 g_return_if_fail (group != NULL);
1704 group->pre_parse_func = pre_parse_func;
1705 group->post_parse_func = post_parse_func;
1709 * g_option_group_set_error_hook:
1710 * @group: a #GOptionGroup
1711 * @error_func: a function to call when an error occurs
1713 * Associates a function with @group which will be called
1714 * from g_option_context_parse() when an error occurs.
1716 * Note that the user data to be passed to @pre_parse_func and
1717 * @post_parse_func can be specified when constructing the group
1718 * with g_option_group_new().
1723 g_option_group_set_error_hook (GOptionGroup *group,
1724 GOptionErrorFunc error_func)
1726 g_return_if_fail (group != NULL);
1728 group->error_func = error_func;
1733 * g_option_group_set_translate_func:
1734 * @group: a #GOptionGroup
1735 * @func: the #GTranslateFunc, or %NULL
1736 * @data: user data to pass to @func, or %NULL
1737 * @destroy_notify: a function which gets called to free @data, or %NULL
1739 * Sets the function which is used to translate user-visible
1740 * strings, for <option>--help</option> output. Different
1741 * groups can use different #GTranslateFunc<!-- -->s. If @func
1742 * is %NULL, strings are not translated.
1744 * If you are using gettext(), you only need to set the translation
1745 * domain, see g_option_group_set_translation_domain().
1750 g_option_group_set_translate_func (GOptionGroup *group,
1751 GTranslateFunc func,
1753 GDestroyNotify destroy_notify)
1755 g_return_if_fail (group != NULL);
1757 if (group->translate_notify)
1758 group->translate_notify (group->translate_data);
1760 group->translate_func = func;
1761 group->translate_data = data;
1762 group->translate_notify = destroy_notify;
1766 dgettext_swapped (const gchar *msgid,
1767 const gchar *domainname)
1769 return dgettext (domainname, msgid);
1773 * g_option_group_set_translation_domain:
1774 * @group: a #GOptionGroup
1775 * @domain: the domain to use
1777 * A convenience function to use gettext() for translating
1778 * user-visible strings.
1783 g_option_group_set_translation_domain (GOptionGroup *group,
1784 const gchar *domain)
1786 g_return_if_fail (group != NULL);
1788 g_option_group_set_translate_func (group,
1789 (GTranslateFunc)dgettext_swapped,
1794 #define __G_OPTION_C__
1795 #include "galiasdef.c"