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.
31 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
57 struct _GOptionContext
61 gchar *parameter_string;
63 gboolean help_enabled;
64 gboolean ignore_unknown;
66 GOptionGroup *main_group;
68 /* We keep a list of change so we can revert them */
71 /* We also keep track of all argv elements that should be NULLed or
81 gchar *help_description;
83 GDestroyNotify destroy_notify;
86 GTranslateFunc translate_func;
87 GDestroyNotify translate_notify;
88 gpointer translate_data;
90 GOptionEntry *entries;
93 GOptionParseFunc pre_parse_func;
94 GOptionParseFunc post_parse_func;
95 GOptionErrorFunc error_func;
98 static void free_changes_list (GOptionContext *context,
100 static void free_pending_nulls (GOptionContext *context,
101 gboolean perform_nulls);
104 g_option_context_error_quark (void)
109 q = g_quark_from_static_string ("g-option-context-error-quark");
115 g_option_context_new (const gchar *parameter_string)
118 GOptionContext *context;
120 context = g_new0 (GOptionContext, 1);
122 context->parameter_string = g_strdup (parameter_string);
123 context->help_enabled = TRUE;
129 g_option_context_free (GOptionContext *context)
131 g_return_if_fail (context != NULL);
133 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
134 g_list_free (context->groups);
136 g_option_group_free (context->main_group);
138 free_changes_list (context, FALSE);
139 free_pending_nulls (context, FALSE);
141 g_free (context->parameter_string);
147 g_option_context_set_help_enabled (GOptionContext *context,
148 gboolean help_enabled)
151 g_return_if_fail (context != NULL);
153 context->help_enabled = help_enabled;
157 g_option_context_get_help_enabled (GOptionContext *context)
159 g_return_val_if_fail (context != NULL, FALSE);
161 return context->help_enabled;
165 g_option_context_set_ignore_unknown_options (GOptionContext *context,
166 gboolean ignore_unknown)
168 g_return_if_fail (context != NULL);
170 context->ignore_unknown = ignore_unknown;
174 g_option_context_get_ignore_unknown_options (GOptionContext *context)
176 g_return_val_if_fail (context != NULL, FALSE);
178 return context->ignore_unknown;
182 g_option_context_add_group (GOptionContext *context,
185 g_return_if_fail (context != NULL);
186 g_return_if_fail (group != NULL);
187 g_return_if_fail (group->name != NULL);
188 g_return_if_fail (group->description != NULL);
189 g_return_if_fail (group->help_description != NULL);
191 context->groups = g_list_prepend (context->groups, group);
195 g_option_context_set_main_group (GOptionContext *context,
198 g_return_if_fail (context != NULL);
199 g_return_if_fail (group != NULL);
201 context->main_group = group;
205 g_option_context_get_main_group (GOptionContext *context)
207 g_return_val_if_fail (context != NULL, NULL);
209 return context->main_group;
213 g_option_context_add_main_entries (GOptionContext *context,
214 const GOptionEntry *entries,
215 const gchar *translation_domain)
217 g_return_if_fail (entries != NULL);
219 if (!context->main_group)
220 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
222 g_option_group_add_entries (context->main_group, entries);
223 g_option_group_set_translation_domain (context->main_group, translation_domain);
227 print_entry (GOptionGroup *group,
229 const GOptionEntry *entry)
233 if (entry->flags & G_OPTION_FLAG_HIDDEN)
236 str = g_string_new (NULL);
238 if (entry->short_name)
239 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
241 g_string_append_printf (str, " --%s", entry->long_name);
243 if (entry->arg_description)
244 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
246 g_print ("%-*s %s\n", max_length + 4, str->str,
247 entry->description ? TRANSLATE (group, entry->description) : "");
248 g_string_free (str, TRUE);
252 print_help (GOptionContext *context,
257 gint max_length, len;
260 g_print ("Usage:\n");
261 g_print (" %s [OPTION...] %s\n\n", g_get_prgname (),
262 context->parameter_string ? context->parameter_string : "");
264 list = context->groups;
266 max_length = g_utf8_strlen ("--help, -?", -1);
270 len = g_utf8_strlen ("--help-all", -1);
271 max_length = MAX (max_length, len);
276 GOptionGroup *group = list->data;
278 /* First, we check the --help-<groupname> options */
279 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
280 max_length = MAX (max_length, len);
282 /* Then we go through the entries */
283 for (i = 0; i < group->n_entries; i++)
285 len = g_utf8_strlen (group->entries[i].long_name, -1);
287 if (group->entries[i].short_name)
290 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
291 group->entries[i].arg_description)
292 len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
294 max_length = MAX (max_length, len);
300 /* Add a bit of padding */
303 list = context->groups;
305 g_print ("Help Options:\n");
306 g_print (" --%-*s %s\n", max_length, "help", "Show help options");
308 /* We only want --help-all when there are groups */
310 g_print (" --%-*s %s\n", max_length, "help-all", "Show all help options");
314 GOptionGroup *group = list->data;
316 g_print (" --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
325 /* Print a certain group */
327 g_print ("%s\n", TRANSLATE (group, group->description));
328 for (i = 0; i < group->n_entries; i++)
329 print_entry (group, max_length, &group->entries[i]);
334 /* Print all groups */
336 list = context->groups;
340 GOptionGroup *group = list->data;
342 g_print ("%s\n", group->description);
344 for (i = 0; i < group->n_entries; i++)
345 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
346 print_entry (group, max_length, &group->entries[i]);
353 /* Print application options if --help or --help-all has been specified */
354 if (main_help || !group)
356 list = context->groups;
358 g_print ("Application Options\n");
360 for (i = 0; i < context->main_group->n_entries; i++)
361 print_entry (context->main_group, max_length, &context->main_group->entries[i]);
365 GOptionGroup *group = list->data;
367 /* Print main entries from other groups */
368 for (i = 0; i < group->n_entries; i++)
369 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
370 print_entry (group, max_length, &group->entries[i]);
383 parse_int (const char *arg_name,
389 glong tmp = strtol (arg, &end, 0);
393 if (*arg == '\0' || *end != '\0')
396 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
397 "Cannot parse integer value '%s' for --%s",
403 if (*result != tmp || errno == ERANGE)
406 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
407 "Integer value '%s' for %s out of range",
416 get_change (GOptionContext *context,
421 Change *change = NULL;
423 for (list = context->changes; list != NULL; list = list->next)
427 if (change->arg_data == arg_data)
433 change = g_new0 (Change, 1);
434 change->arg_type = arg_type;
435 change->arg_data = arg_data;
437 context->changes = g_list_prepend (context->changes, change);
444 add_pending_null (GOptionContext *context,
450 n = g_new0 (PendingNull, 1);
454 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
458 parse_arg (GOptionContext *context,
462 const gchar *option_name,
470 case G_OPTION_ARG_NONE:
472 change = get_change (context, G_OPTION_ARG_NONE,
475 *(gboolean *)entry->arg_data = TRUE;
478 case G_OPTION_ARG_STRING:
482 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
487 change = get_change (context, G_OPTION_ARG_STRING,
489 g_free (change->allocated.str);
491 change->prev.str = *(gchar **)entry->arg_data;
492 change->allocated.str = data;
494 *(gchar **)entry->arg_data = data;
497 case G_OPTION_ARG_STRING_ARRAY:
501 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
506 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
509 if (change->allocated.array.len == 0)
511 change->prev.array = entry->arg_data;
512 change->allocated.array.data = g_new (gchar *, 2);
515 change->allocated.array.data =
516 g_renew (gchar *, change->allocated.array.data,
517 change->allocated.array.len + 2);
519 change->allocated.array.data[change->allocated.array.len] = data;
520 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
522 change->allocated.array.len ++;
524 *(gchar ***)entry->arg_data = change->allocated.array.data;
529 case G_OPTION_ARG_FILENAME:
533 data = g_strdup (value);
535 change = get_change (context, G_OPTION_ARG_FILENAME,
537 g_free (change->allocated.str);
539 change->prev.str = *(gchar **)entry->arg_data;
540 change->allocated.str = data;
545 case G_OPTION_ARG_FILENAME_ARRAY:
549 data = g_strdup (value);
551 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
554 if (change->allocated.array.len == 0)
556 change->prev.array = entry->arg_data;
557 change->allocated.array.data = g_new (gchar *, 2);
560 change->allocated.array.data =
561 g_renew (gchar *, change->allocated.array.data,
562 change->allocated.array.len + 2);
564 change->allocated.array.data[change->allocated.array.len] = data;
565 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
567 change->allocated.array.len ++;
569 *(gchar ***)entry->arg_data = change->allocated.array.data;
574 case G_OPTION_ARG_INT:
578 if (!parse_int (option_name, value,
583 change = get_change (context, G_OPTION_ARG_INT,
585 change->prev.integer = *(gint *)entry->arg_data;
586 *(gint *)entry->arg_data = data;
589 case G_OPTION_ARG_CALLBACK:
594 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
599 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
608 g_assert_not_reached ();
615 parse_short_option (GOptionContext *context,
627 for (j = 0; j < group->n_entries; j++)
629 if (arg == group->entries[j].short_name)
631 if (group->entries[j].arg == G_OPTION_ARG_NONE)
633 parse_arg (context, group, &group->entries[j],
642 if (*new_index > index)
644 g_warning ("FIXME: figure out the correct error here");
649 if (index < *argc - 1)
651 value = (*argv)[index + 1];
652 add_pending_null (context, &((*argv)[index + 1]), NULL);
653 *new_index = index + 1;
659 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
661 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
663 g_free (option_name);
667 g_free (option_name);
677 parse_long_option (GOptionContext *context,
688 for (j = 0; j < group->n_entries; j++)
693 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
694 strcmp (arg, group->entries[j].long_name) == 0)
696 parse_arg (context, group, &group->entries[j],
699 add_pending_null (context, &((*argv)[*index]), NULL);
703 gint len = strlen (group->entries[j].long_name);
705 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
706 (arg[len] == '=' || arg[len] == 0))
711 add_pending_null (context, &((*argv)[*index + 1]), NULL);
714 value = arg + len + 1;
715 else if (*index < *argc - 1)
717 value = (*argv)[*index + 1];
718 add_pending_null (context, &((*argv)[*index + 1]), NULL);
724 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
726 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
728 g_free (option_name);
732 g_free (option_name);
742 free_changes_list (GOptionContext *context,
747 for (list = context->changes; list != NULL; list = list->next)
749 Change *change = list->data;
753 /* Free any allocated data */
754 g_free (change->allocated.str);
755 g_strfreev (change->allocated.array.data);
757 switch (change->arg_type)
759 case G_OPTION_ARG_NONE:
760 *(gboolean *)change->arg_data = change->prev.bool;
762 case G_OPTION_ARG_INT:
763 *(gint *)change->arg_data = change->prev.integer;
765 case G_OPTION_ARG_STRING:
766 case G_OPTION_ARG_FILENAME:
767 *(gchar **)change->arg_data = change->prev.str;
769 case G_OPTION_ARG_STRING_ARRAY:
770 case G_OPTION_ARG_FILENAME_ARRAY:
771 *(gchar ***)change->arg_data = change->prev.array;
773 g_assert_not_reached ();
780 g_list_free (context->changes);
781 context->changes = NULL;
785 free_pending_nulls (GOptionContext *context,
786 gboolean perform_nulls)
790 for (list = context->pending_nulls; list != NULL; list = list->next)
792 PendingNull *n = list->data;
798 /* Copy back the short options */
800 strcpy (*n->ptr + 1, n->value);
810 g_list_free (context->pending_nulls);
811 context->pending_nulls = NULL;
815 g_option_context_parse (GOptionContext *context,
823 /* Call pre-parse hooks */
824 list = context->groups;
827 GOptionGroup *group = list->data;
829 if (group->pre_parse_func)
831 if (!(* group->pre_parse_func) (context, group,
832 group->user_data, error))
839 if (context->main_group->pre_parse_func)
841 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
842 context->main_group->user_data, error))
848 for (i = 1; i < *argc; i++)
851 gboolean parsed = FALSE;
853 if ((*argv)[i][0] == '-')
855 if ((*argv)[i][1] == '-')
859 arg = (*argv)[i] + 2;
861 /* '--' terminates list of arguments */
864 add_pending_null (context, &((*argv)[i]), NULL);
868 /* Handle help options */
869 if (context->help_enabled)
871 if (strcmp (arg, "help") == 0)
872 print_help (context, TRUE, NULL);
873 else if (strcmp (arg, "help-all") == 0)
874 print_help (context, FALSE, NULL);
875 else if (strncmp (arg, "help-", 5) == 0)
879 list = context->groups;
883 GOptionGroup *group = list->data;
885 if (strcmp (arg + 5, group->name) == 0)
886 print_help (context, FALSE, group);
893 if (!parse_long_option (context, context->main_group, &i, arg,
894 argc, argv, error, &parsed))
901 list = context->groups;
904 GOptionGroup *group = list->data;
906 if (!parse_long_option (context, group, &i, arg,
907 argc, argv, error, &parsed))
916 if (context->ignore_unknown)
922 gboolean *nulled_out = NULL;
924 arg = (*argv)[i] + 1;
928 if (context->ignore_unknown)
929 nulled_out = g_new0 (gboolean, strlen (arg));
931 for (j = 0; j < strlen (arg); j++)
935 if (!parse_short_option (context, context->main_group,
937 argc, argv, error, &parsed))
947 list = context->groups;
950 GOptionGroup *group = list->data;
952 if (!parse_short_option (context, group, i, &new_i, arg[j],
953 argc, argv, error, &parsed))
963 if (context->ignore_unknown)
966 nulled_out[j] = TRUE;
975 if (context->ignore_unknown)
977 gchar *new_arg = NULL;
980 for (j = 0; j < strlen (arg); j++)
985 new_arg = g_malloc (strlen (arg));
986 new_arg[arg_index++] = arg[j];
990 new_arg[arg_index] = '\0';
992 add_pending_null (context, &((*argv)[i]), new_arg);
996 add_pending_null (context, &((*argv)[i]), NULL);
1001 if (!parsed && !context->ignore_unknown)
1004 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1005 _("Unknown option %s"), (*argv)[i]);
1011 /* Call post-parse hooks */
1012 list = context->groups;
1015 GOptionGroup *group = list->data;
1017 if (group->post_parse_func)
1019 if (!(* group->post_parse_func) (context, group,
1020 group->user_data, error))
1027 if (context->main_group->post_parse_func)
1029 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1030 context->main_group->user_data, error))
1034 free_pending_nulls (context, TRUE);
1036 for (i = 1; i < *argc; i++)
1038 for (k = i; k < *argc; k++)
1039 if ((*argv)[k] != NULL)
1045 for (j = i + k; j < *argc; j++)
1046 (*argv)[j-k] = (*argv)[j];
1057 /* Call error hooks */
1058 list = context->groups;
1061 GOptionGroup *group = list->data;
1063 if (group->error_func)
1064 (* group->post_parse_func) (context, group,
1065 group->user_data, error);
1070 if (context->main_group->error_func)
1071 (* context->main_group->error_func) (context, context->main_group,
1072 context->main_group->user_data, error);
1074 free_changes_list (context, TRUE);
1075 free_pending_nulls (context, FALSE);
1082 g_option_group_new (const gchar *name,
1083 const gchar *description,
1084 const gchar *help_description,
1086 GDestroyNotify destroy)
1089 GOptionGroup *group;
1091 group = g_new0 (GOptionGroup, 1);
1092 group->name = g_strdup (name);
1093 group->description = g_strdup (description);
1094 group->help_description = g_strdup (help_description);
1095 group->user_data = user_data;
1096 group->destroy_notify = destroy;
1102 g_option_group_free (GOptionGroup *group)
1104 g_return_if_fail (group != NULL);
1106 g_free (group->name);
1107 g_free (group->description);
1108 g_free (group->help_description);
1110 g_free (group->entries);
1112 if (group->destroy_notify)
1113 (* group->destroy_notify) (group->user_data);
1115 if (group->translate_notify)
1116 (* group->translate_notify) (group->translate_data);
1123 g_option_group_add_entries (GOptionGroup *group,
1124 const GOptionEntry *entries)
1129 g_return_if_fail (entries != NULL);
1131 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1133 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1135 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1137 group->n_entries += n_entries;
1141 g_option_group_set_parse_hooks (GOptionGroup *group,
1142 GOptionParseFunc pre_parse_func,
1143 GOptionParseFunc post_parse_func)
1145 g_return_if_fail (group != NULL);
1147 group->pre_parse_func = pre_parse_func;
1148 group->post_parse_func = post_parse_func;
1152 g_option_group_set_error_hook (GOptionGroup *group,
1153 GOptionErrorFunc error_func)
1155 g_return_if_fail (group != NULL);
1157 group->error_func = error_func;
1162 g_option_group_set_translate_func (GOptionGroup *group,
1163 GTranslateFunc func,
1165 GDestroyNotify notify)
1167 g_return_if_fail (group != NULL);
1169 if (group->translate_notify)
1170 group->translate_notify (group->translate_data);
1172 group->translate_func = func;
1173 group->translate_data = data;
1174 group->translate_notify = notify;
1178 dgettext_swapped (const gchar *msgid,
1179 const gchar *domainname)
1181 return dgettext (domainname, msgid);
1185 g_option_group_set_translation_domain (GOptionGroup *group,
1186 const gchar *domain)
1188 g_return_if_fail (group != NULL);
1190 g_option_group_set_translate_func (group,
1191 (GTranslateFunc)dgettext_swapped,