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 if (context->main_group)
137 g_option_group_free (context->main_group);
139 free_changes_list (context, FALSE);
140 free_pending_nulls (context, FALSE);
142 g_free (context->parameter_string);
148 g_option_context_set_help_enabled (GOptionContext *context,
149 gboolean help_enabled)
152 g_return_if_fail (context != NULL);
154 context->help_enabled = help_enabled;
158 g_option_context_get_help_enabled (GOptionContext *context)
160 g_return_val_if_fail (context != NULL, FALSE);
162 return context->help_enabled;
166 g_option_context_set_ignore_unknown_options (GOptionContext *context,
167 gboolean ignore_unknown)
169 g_return_if_fail (context != NULL);
171 context->ignore_unknown = ignore_unknown;
175 g_option_context_get_ignore_unknown_options (GOptionContext *context)
177 g_return_val_if_fail (context != NULL, FALSE);
179 return context->ignore_unknown;
183 g_option_context_add_group (GOptionContext *context,
186 g_return_if_fail (context != NULL);
187 g_return_if_fail (group != NULL);
188 g_return_if_fail (group->name != NULL);
189 g_return_if_fail (group->description != NULL);
190 g_return_if_fail (group->help_description != NULL);
192 context->groups = g_list_prepend (context->groups, group);
196 g_option_context_set_main_group (GOptionContext *context,
199 g_return_if_fail (context != NULL);
200 g_return_if_fail (group != NULL);
202 context->main_group = group;
206 g_option_context_get_main_group (GOptionContext *context)
208 g_return_val_if_fail (context != NULL, NULL);
210 return context->main_group;
214 g_option_context_add_main_entries (GOptionContext *context,
215 const GOptionEntry *entries,
216 const gchar *translation_domain)
218 g_return_if_fail (entries != NULL);
220 if (!context->main_group)
221 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
223 g_option_group_add_entries (context->main_group, entries);
224 g_option_group_set_translation_domain (context->main_group, translation_domain);
228 print_entry (GOptionGroup *group,
230 const GOptionEntry *entry)
234 if (entry->flags & G_OPTION_FLAG_HIDDEN)
237 str = g_string_new (NULL);
239 if (entry->short_name)
240 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
242 g_string_append_printf (str, " --%s", entry->long_name);
244 if (entry->arg_description)
245 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
247 g_print ("%-*s %s\n", max_length + 4, str->str,
248 entry->description ? TRANSLATE (group, entry->description) : "");
249 g_string_free (str, TRUE);
253 print_help (GOptionContext *context,
258 gint max_length, len;
261 g_print ("%s\n %s %s %s\n\n",
262 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
263 context->parameter_string ? context->parameter_string : "");
265 list = context->groups;
267 max_length = g_utf8_strlen ("--help, -?", -1);
271 len = g_utf8_strlen ("--help-all", -1);
272 max_length = MAX (max_length, len);
277 GOptionGroup *group = list->data;
279 /* First, we check the --help-<groupname> options */
280 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
281 max_length = MAX (max_length, len);
283 /* Then we go through the entries */
284 for (i = 0; i < group->n_entries; i++)
286 len = g_utf8_strlen (group->entries[i].long_name, -1);
288 if (group->entries[i].short_name)
291 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
292 group->entries[i].arg_description)
293 len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
295 max_length = MAX (max_length, len);
301 /* Add a bit of padding */
304 list = context->groups;
306 g_print ("%s\n --%-*s %s\n",
307 _("Help Options:"), max_length, "help", _("Show help options"));
309 /* We only want --help-all when there are groups */
311 g_print (" --%-*s %s\n", max_length, "help-all", _("Show all help options"));
315 GOptionGroup *group = list->data;
317 g_print (" --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
326 /* Print a certain group */
328 g_print ("%s\n", TRANSLATE (group, group->description));
329 for (i = 0; i < group->n_entries; i++)
330 print_entry (group, max_length, &group->entries[i]);
335 /* Print all groups */
337 list = context->groups;
341 GOptionGroup *group = list->data;
343 g_print ("%s\n", group->description);
345 for (i = 0; i < group->n_entries; i++)
346 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
347 print_entry (group, max_length, &group->entries[i]);
354 /* Print application options if --help or --help-all has been specified */
355 if (main_help || !group)
357 list = context->groups;
359 g_print ("%s\n", _("Application Options:"));
361 if (context->main_group)
362 for (i = 0; i < context->main_group->n_entries; i++)
363 print_entry (context->main_group, max_length, &context->main_group->entries[i]);
367 GOptionGroup *group = list->data;
369 /* Print main entries from other groups */
370 for (i = 0; i < group->n_entries; i++)
371 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
372 print_entry (group, max_length, &group->entries[i]);
385 parse_int (const gchar *arg_name,
391 glong tmp = strtol (arg, &end, 0);
395 if (*arg == '\0' || *end != '\0')
398 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
399 _("Cannot parse integer value '%s' for --%s"),
405 if (*result != tmp || errno == ERANGE)
408 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
409 _("Integer value '%s' for %s out of range"),
418 get_change (GOptionContext *context,
423 Change *change = NULL;
425 for (list = context->changes; list != NULL; list = list->next)
429 if (change->arg_data == arg_data)
435 change = g_new0 (Change, 1);
436 change->arg_type = arg_type;
437 change->arg_data = arg_data;
439 context->changes = g_list_prepend (context->changes, change);
446 add_pending_null (GOptionContext *context,
452 n = g_new0 (PendingNull, 1);
456 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
460 parse_arg (GOptionContext *context,
464 const gchar *option_name,
472 case G_OPTION_ARG_NONE:
474 change = get_change (context, G_OPTION_ARG_NONE,
477 *(gboolean *)entry->arg_data = TRUE;
480 case G_OPTION_ARG_STRING:
484 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
489 change = get_change (context, G_OPTION_ARG_STRING,
491 g_free (change->allocated.str);
493 change->prev.str = *(gchar **)entry->arg_data;
494 change->allocated.str = data;
496 *(gchar **)entry->arg_data = data;
499 case G_OPTION_ARG_STRING_ARRAY:
503 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
508 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
511 if (change->allocated.array.len == 0)
513 change->prev.array = entry->arg_data;
514 change->allocated.array.data = g_new (gchar *, 2);
517 change->allocated.array.data =
518 g_renew (gchar *, change->allocated.array.data,
519 change->allocated.array.len + 2);
521 change->allocated.array.data[change->allocated.array.len] = data;
522 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
524 change->allocated.array.len ++;
526 *(gchar ***)entry->arg_data = change->allocated.array.data;
531 case G_OPTION_ARG_FILENAME:
535 data = g_strdup (value);
537 change = get_change (context, G_OPTION_ARG_FILENAME,
539 g_free (change->allocated.str);
541 change->prev.str = *(gchar **)entry->arg_data;
542 change->allocated.str = data;
547 case G_OPTION_ARG_FILENAME_ARRAY:
551 data = g_strdup (value);
553 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
556 if (change->allocated.array.len == 0)
558 change->prev.array = entry->arg_data;
559 change->allocated.array.data = g_new (gchar *, 2);
562 change->allocated.array.data =
563 g_renew (gchar *, change->allocated.array.data,
564 change->allocated.array.len + 2);
566 change->allocated.array.data[change->allocated.array.len] = data;
567 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
569 change->allocated.array.len ++;
571 *(gchar ***)entry->arg_data = change->allocated.array.data;
576 case G_OPTION_ARG_INT:
580 if (!parse_int (option_name, value,
585 change = get_change (context, G_OPTION_ARG_INT,
587 change->prev.integer = *(gint *)entry->arg_data;
588 *(gint *)entry->arg_data = data;
591 case G_OPTION_ARG_CALLBACK:
596 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
601 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
610 g_assert_not_reached ();
617 parse_short_option (GOptionContext *context,
629 for (j = 0; j < group->n_entries; j++)
631 if (arg == group->entries[j].short_name)
633 if (group->entries[j].arg == G_OPTION_ARG_NONE)
635 parse_arg (context, group, &group->entries[j],
644 if (*new_index > index)
646 g_warning ("FIXME: figure out the correct error here");
651 if (index < *argc - 1)
653 value = (*argv)[index + 1];
654 add_pending_null (context, &((*argv)[index + 1]), NULL);
655 *new_index = index + 1;
661 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
663 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
665 g_free (option_name);
669 g_free (option_name);
679 parse_long_option (GOptionContext *context,
690 for (j = 0; j < group->n_entries; j++)
695 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
696 strcmp (arg, group->entries[j].long_name) == 0)
698 parse_arg (context, group, &group->entries[j],
701 add_pending_null (context, &((*argv)[*index]), NULL);
705 gint len = strlen (group->entries[j].long_name);
707 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
708 (arg[len] == '=' || arg[len] == 0))
713 add_pending_null (context, &((*argv)[*index + 1]), NULL);
716 value = arg + len + 1;
717 else if (*index < *argc - 1)
719 value = (*argv)[*index + 1];
720 add_pending_null (context, &((*argv)[*index + 1]), NULL);
726 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
728 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
730 g_free (option_name);
734 g_free (option_name);
744 free_changes_list (GOptionContext *context,
749 for (list = context->changes; list != NULL; list = list->next)
751 Change *change = list->data;
755 /* Free any allocated data */
756 g_free (change->allocated.str);
757 g_strfreev (change->allocated.array.data);
759 switch (change->arg_type)
761 case G_OPTION_ARG_NONE:
762 *(gboolean *)change->arg_data = change->prev.bool;
764 case G_OPTION_ARG_INT:
765 *(gint *)change->arg_data = change->prev.integer;
767 case G_OPTION_ARG_STRING:
768 case G_OPTION_ARG_FILENAME:
769 *(gchar **)change->arg_data = change->prev.str;
771 case G_OPTION_ARG_STRING_ARRAY:
772 case G_OPTION_ARG_FILENAME_ARRAY:
773 *(gchar ***)change->arg_data = change->prev.array;
775 g_assert_not_reached ();
782 g_list_free (context->changes);
783 context->changes = NULL;
787 free_pending_nulls (GOptionContext *context,
788 gboolean perform_nulls)
792 for (list = context->pending_nulls; list != NULL; list = list->next)
794 PendingNull *n = list->data;
800 /* Copy back the short options */
802 strcpy (*n->ptr + 1, n->value);
812 g_list_free (context->pending_nulls);
813 context->pending_nulls = NULL;
817 g_option_context_parse (GOptionContext *context,
825 /* Call pre-parse hooks */
826 list = context->groups;
829 GOptionGroup *group = list->data;
831 if (group->pre_parse_func)
833 if (!(* group->pre_parse_func) (context, group,
834 group->user_data, error))
841 if (context->main_group && context->main_group->pre_parse_func)
843 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
844 context->main_group->user_data, error))
852 prgname = strrchr ((*argv)[0], G_DIR_SEPARATOR);
856 prgname = (*argv)[0];
858 g_set_prgname (prgname);
860 for (i = 1; i < *argc; i++)
863 gboolean parsed = FALSE;
865 if ((*argv)[i][0] == '-')
867 if ((*argv)[i][1] == '-')
871 arg = (*argv)[i] + 2;
873 /* '--' terminates list of arguments */
876 add_pending_null (context, &((*argv)[i]), NULL);
880 /* Handle help options */
881 if (context->help_enabled)
883 if (strcmp (arg, "help") == 0)
884 print_help (context, TRUE, NULL);
885 else if (strcmp (arg, "help-all") == 0)
886 print_help (context, FALSE, NULL);
887 else if (strncmp (arg, "help-", 5) == 0)
891 list = context->groups;
895 GOptionGroup *group = list->data;
897 if (strcmp (arg + 5, group->name) == 0)
898 print_help (context, FALSE, group);
905 if (context->main_group &&
906 !parse_long_option (context, context->main_group, &i, arg,
907 argc, argv, error, &parsed))
914 list = context->groups;
917 GOptionGroup *group = list->data;
919 if (!parse_long_option (context, group, &i, arg,
920 argc, argv, error, &parsed))
929 if (context->ignore_unknown)
935 gboolean *nulled_out = NULL;
937 arg = (*argv)[i] + 1;
941 if (context->ignore_unknown)
942 nulled_out = g_new0 (gboolean, strlen (arg));
944 for (j = 0; j < strlen (arg); j++)
948 if (context->main_group &&
949 !parse_short_option (context, context->main_group,
951 argc, argv, error, &parsed))
961 list = context->groups;
964 GOptionGroup *group = list->data;
966 if (!parse_short_option (context, group, i, &new_i, arg[j],
967 argc, argv, error, &parsed))
977 if (context->ignore_unknown)
980 nulled_out[j] = TRUE;
989 if (context->ignore_unknown)
991 gchar *new_arg = NULL;
994 for (j = 0; j < strlen (arg); j++)
999 new_arg = g_malloc (strlen (arg));
1000 new_arg[arg_index++] = arg[j];
1004 new_arg[arg_index] = '\0';
1006 add_pending_null (context, &((*argv)[i]), new_arg);
1010 add_pending_null (context, &((*argv)[i]), NULL);
1015 if (!parsed && !context->ignore_unknown)
1018 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1019 _("Unknown option %s"), (*argv)[i]);
1025 /* Call post-parse hooks */
1026 list = context->groups;
1029 GOptionGroup *group = list->data;
1031 if (group->post_parse_func)
1033 if (!(* group->post_parse_func) (context, group,
1034 group->user_data, error))
1041 if (context->main_group && context->main_group->post_parse_func)
1043 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1044 context->main_group->user_data, error))
1048 free_pending_nulls (context, TRUE);
1050 for (i = 1; i < *argc; i++)
1052 for (k = i; k < *argc; k++)
1053 if ((*argv)[k] != NULL)
1059 for (j = i + k; j < *argc; j++)
1060 (*argv)[j-k] = (*argv)[j];
1067 g_set_prgname ("<unknown>");
1074 /* Call error hooks */
1075 list = context->groups;
1078 GOptionGroup *group = list->data;
1080 if (group->error_func)
1081 (* group->post_parse_func) (context, group,
1082 group->user_data, error);
1087 if (context->main_group && context->main_group->error_func)
1088 (* context->main_group->error_func) (context, context->main_group,
1089 context->main_group->user_data, error);
1091 free_changes_list (context, TRUE);
1092 free_pending_nulls (context, FALSE);
1099 g_option_group_new (const gchar *name,
1100 const gchar *description,
1101 const gchar *help_description,
1103 GDestroyNotify destroy)
1106 GOptionGroup *group;
1108 group = g_new0 (GOptionGroup, 1);
1109 group->name = g_strdup (name);
1110 group->description = g_strdup (description);
1111 group->help_description = g_strdup (help_description);
1112 group->user_data = user_data;
1113 group->destroy_notify = destroy;
1119 g_option_group_free (GOptionGroup *group)
1121 g_return_if_fail (group != NULL);
1123 g_free (group->name);
1124 g_free (group->description);
1125 g_free (group->help_description);
1127 g_free (group->entries);
1129 if (group->destroy_notify)
1130 (* group->destroy_notify) (group->user_data);
1132 if (group->translate_notify)
1133 (* group->translate_notify) (group->translate_data);
1140 g_option_group_add_entries (GOptionGroup *group,
1141 const GOptionEntry *entries)
1145 g_return_if_fail (entries != NULL);
1147 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1149 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1151 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1153 group->n_entries += n_entries;
1157 g_option_group_set_parse_hooks (GOptionGroup *group,
1158 GOptionParseFunc pre_parse_func,
1159 GOptionParseFunc post_parse_func)
1161 g_return_if_fail (group != NULL);
1163 group->pre_parse_func = pre_parse_func;
1164 group->post_parse_func = post_parse_func;
1168 g_option_group_set_error_hook (GOptionGroup *group,
1169 GOptionErrorFunc error_func)
1171 g_return_if_fail (group != NULL);
1173 group->error_func = error_func;
1178 g_option_group_set_translate_func (GOptionGroup *group,
1179 GTranslateFunc func,
1181 GDestroyNotify notify)
1183 g_return_if_fail (group != NULL);
1185 if (group->translate_notify)
1186 group->translate_notify (group->translate_data);
1188 group->translate_func = func;
1189 group->translate_data = data;
1190 group->translate_notify = notify;
1194 dgettext_swapped (const gchar *msgid,
1195 const gchar *domainname)
1197 return dgettext (domainname, msgid);
1201 g_option_group_set_translation_domain (GOptionGroup *group,
1202 const gchar *domain)
1204 g_return_if_fail (group != NULL);
1206 g_option_group_set_translate_func (group,
1207 (GTranslateFunc)dgettext_swapped,