1 /* Unit tests for GOptionContext
2 * Copyright (C) 2007 Openismus GmbH
3 * Authors: Mathias Hasselmann
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
30 static GOptionEntry main_entries[] = {
31 { "main-switch", 0, 0,
32 G_OPTION_ARG_NONE, NULL,
33 "A switch that is in the main group", NULL },
37 static GOptionEntry group_entries[] = {
38 { "test-switch", 0, 0,
39 G_OPTION_ARG_NONE, NULL,
40 "A switch that is in the test group", NULL },
44 static GOptionContext *
45 make_options (int test_number)
47 GOptionContext *options;
48 GOptionGroup *group = NULL;
49 gboolean have_main_entries = (0 != (test_number & 1));
50 gboolean have_test_entries = (0 != (test_number & 2));
52 options = g_option_context_new (NULL);
54 if (have_main_entries)
55 g_option_context_add_main_entries (options, main_entries, NULL);
56 if (have_test_entries)
58 group = g_option_group_new ("test", "Test Options",
59 "Show all test options",
61 g_option_context_add_group (options, group);
62 g_option_group_add_entries (group, group_entries);
69 print_help (GOptionContext *options, gchar **argv)
74 g_option_context_parse (options, &argc, &argv, &error);
75 g_option_context_free (options);
80 test_group_captions_help (gconstpointer test_number)
82 GOptionContext *options;
83 gchar *argv[] = { __FILE__, "--help", NULL };
85 options = make_options (GPOINTER_TO_INT (test_number));
86 print_help (options, argv);
90 test_group_captions_help_all (gconstpointer test_number)
92 GOptionContext *options;
93 gchar *argv[] = { __FILE__, "--help-all", NULL };
95 options = make_options (GPOINTER_TO_INT (test_number));
96 print_help (options, argv);
100 test_group_captions_help_test (gconstpointer test_number)
102 GOptionContext *options;
103 gchar *argv[] = { __FILE__, "--help-test", NULL };
105 options = make_options (GPOINTER_TO_INT (test_number));
106 print_help (options, argv);
110 test_group_captions (void)
112 const gchar *test_name_base[] = { "help", "help-all", "help-test" };
117 g_test_bug ("504142");
119 for (i = 0; i < 4; ++i)
121 gboolean have_main_entries = (0 != (i & 1));
122 gboolean have_test_entries = (0 != (i & 2));
124 for (j = 0; j < G_N_ELEMENTS (test_name_base); ++j)
126 GTestSubprocessFlags trap_flags = 0;
127 gboolean expect_main_description = FALSE;
128 gboolean expect_main_switch = FALSE;
129 gboolean expect_test_description = FALSE;
130 gboolean expect_test_switch = FALSE;
131 gboolean expect_test_group = FALSE;
133 if (g_test_verbose ())
134 trap_flags |= G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR;
136 test_name = g_strdup_printf ("/option/group/captions/subprocess/%s-%u",
137 test_name_base[j], i);
138 g_test_trap_subprocess (test_name, 0, trap_flags);
140 g_test_trap_assert_passed ();
141 g_test_trap_assert_stderr ("");
146 g_assert_cmpstr ("help", ==, test_name_base[j]);
147 expect_main_switch = have_main_entries;
148 expect_test_group = have_test_entries;
152 g_assert_cmpstr ("help-all", ==, test_name_base[j]);
153 expect_main_switch = have_main_entries;
154 expect_test_switch = have_test_entries;
155 expect_test_group = have_test_entries;
159 g_assert_cmpstr ("help-test", ==, test_name_base[j]);
160 expect_test_switch = have_test_entries;
164 g_assert_not_reached ();
168 expect_main_description |= expect_main_switch;
169 expect_test_description |= expect_test_switch;
171 if (expect_main_description)
172 g_test_trap_assert_stdout ("*Application Options*");
174 g_test_trap_assert_stdout_unmatched ("*Application Options*");
175 if (expect_main_switch)
176 g_test_trap_assert_stdout ("*--main-switch*");
178 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
180 if (expect_test_description)
181 g_test_trap_assert_stdout ("*Test Options*");
183 g_test_trap_assert_stdout_unmatched ("*Test Options*");
184 if (expect_test_switch)
185 g_test_trap_assert_stdout ("*--test-switch*");
187 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
189 if (expect_test_group)
190 g_test_trap_assert_stdout ("*--help-test*");
192 g_test_trap_assert_stdout_unmatched ("*--help-test*");
198 char *error_test2_string;
199 gboolean error_test3_boolean;
202 gchar *arg_test2_string;
203 gchar *arg_test3_filename;
204 gdouble arg_test4_double;
205 gdouble arg_test5_double;
206 gint64 arg_test6_int64;
207 gint64 arg_test6_int64_2;
209 gchar *callback_test1_string;
210 int callback_test2_int;
212 gchar *callback_test_optional_string;
213 gboolean callback_test_optional_boolean;
215 gchar **array_test1_array;
217 gboolean ignore_test1_boolean;
218 gboolean ignore_test2_boolean;
219 gchar *ignore_test3_string;
222 split_string (const char *str, int *argc)
227 argv = g_strsplit (str, " ", 0);
229 for (len = 0; argv[len] != NULL; len++);
238 join_stringv (int argc, char **argv)
243 str = g_string_new (NULL);
245 for (i = 0; i < argc; i++)
247 g_string_append (str, argv[i]);
250 g_string_append_c (str, ' ');
253 return g_string_free (str, FALSE);
256 /* Performs a shallow copy */
258 copy_stringv (char **argv, int argc)
260 return g_memdup (argv, sizeof (char *) * (argc + 1));
264 check_identical_stringv (gchar **before, gchar **after)
268 /* Not only is it the same string... */
269 for (i = 0; before[i] != NULL; i++)
270 g_assert_cmpstr (before[i], ==, after[i]);
272 /* ... it is actually the same pointer */
273 for (i = 0; before[i] != NULL; i++)
274 g_assert (before[i] == after[i]);
276 g_assert (after[i] == NULL);
281 error_test1_pre_parse (GOptionContext *context,
286 g_assert (error_test1_int == 0x12345678);
292 error_test1_post_parse (GOptionContext *context,
297 g_assert (error_test1_int == 20);
299 /* Set an error in the post hook */
300 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
308 GOptionContext *context;
310 GError *error = NULL;
314 GOptionGroup *main_group;
315 GOptionEntry entries [] =
316 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
319 error_test1_int = 0x12345678;
321 context = g_option_context_new (NULL);
322 g_option_context_add_main_entries (context, entries, NULL);
324 /* Set pre and post parse hooks */
325 main_group = g_option_context_get_main_group (context);
326 g_option_group_set_parse_hooks (main_group,
327 error_test1_pre_parse, error_test1_post_parse);
329 /* Now try parsing */
330 argv = split_string ("program --test 20", &argc);
331 argv_copy = copy_stringv (argv, argc);
333 retval = g_option_context_parse (context, &argc, &argv, &error);
334 g_assert (retval == FALSE);
335 g_assert (error != NULL);
336 /* An error occurred, so argv has not been changed */
337 check_identical_stringv (argv_copy, argv);
338 g_clear_error (&error);
340 /* On failure, values should be reset */
341 g_assert (error_test1_int == 0x12345678);
343 g_strfreev (argv_copy);
345 g_option_context_free (context);
349 error_test2_pre_parse (GOptionContext *context,
354 g_assert (strcmp (error_test2_string, "foo") == 0);
360 error_test2_post_parse (GOptionContext *context,
365 g_assert (strcmp (error_test2_string, "bar") == 0);
367 /* Set an error in the post hook */
368 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
376 GOptionContext *context;
378 GError *error = NULL;
382 GOptionGroup *main_group;
383 GOptionEntry entries [] =
384 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
387 error_test2_string = "foo";
389 context = g_option_context_new (NULL);
390 g_option_context_add_main_entries (context, entries, NULL);
392 /* Set pre and post parse hooks */
393 main_group = g_option_context_get_main_group (context);
394 g_option_group_set_parse_hooks (main_group,
395 error_test2_pre_parse, error_test2_post_parse);
397 /* Now try parsing */
398 argv = split_string ("program --test bar", &argc);
399 argv_copy = copy_stringv (argv, argc);
400 retval = g_option_context_parse (context, &argc, &argv, &error);
402 g_assert (retval == FALSE);
403 g_assert (error != NULL);
404 check_identical_stringv (argv_copy, argv);
405 g_clear_error (&error);
407 g_assert (strcmp (error_test2_string, "foo") == 0);
409 g_strfreev (argv_copy);
411 g_option_context_free (context);
415 error_test3_pre_parse (GOptionContext *context,
420 g_assert (!error_test3_boolean);
426 error_test3_post_parse (GOptionContext *context,
431 g_assert (error_test3_boolean);
433 /* Set an error in the post hook */
434 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
442 GOptionContext *context;
444 GError *error = NULL;
448 GOptionGroup *main_group;
449 GOptionEntry entries [] =
450 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
453 error_test3_boolean = FALSE;
455 context = g_option_context_new (NULL);
456 g_option_context_add_main_entries (context, entries, NULL);
458 /* Set pre and post parse hooks */
459 main_group = g_option_context_get_main_group (context);
460 g_option_group_set_parse_hooks (main_group,
461 error_test3_pre_parse, error_test3_post_parse);
463 /* Now try parsing */
464 argv = split_string ("program --test", &argc);
465 argv_copy = copy_stringv (argv, argc);
466 retval = g_option_context_parse (context, &argc, &argv, &error);
468 g_assert (retval == FALSE);
469 g_assert (error != NULL);
470 check_identical_stringv (argv_copy, argv);
471 g_clear_error (&error);
473 g_assert (!error_test3_boolean);
475 g_strfreev (argv_copy);
477 g_option_context_free (context);
483 GOptionContext *context;
485 GError *error = NULL;
489 GOptionEntry entries [] =
490 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
493 context = g_option_context_new (NULL);
494 g_option_context_add_main_entries (context, entries, NULL);
496 /* Now try parsing */
497 argv = split_string ("program --test 20 --test 30", &argc);
498 argv_copy = copy_stringv (argv, argc);
500 retval = g_option_context_parse (context, &argc, &argv, &error);
501 g_assert_no_error (error);
504 /* Last arg specified is the one that should be stored */
505 g_assert (arg_test1_int == 30);
507 /* We free all of the strings in a copy of argv, because now argv is a
508 * subset - some have been removed in-place
510 g_strfreev (argv_copy);
512 g_option_context_free (context);
518 GOptionContext *context;
520 GError *error = NULL;
524 GOptionEntry entries [] =
525 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
528 context = g_option_context_new (NULL);
529 g_option_context_add_main_entries (context, entries, NULL);
531 /* Now try parsing */
532 argv = split_string ("program --test foo --test bar", &argc);
533 argv_copy = copy_stringv (argv, argc);
535 retval = g_option_context_parse (context, &argc, &argv, &error);
536 g_assert_no_error (error);
539 /* Last arg specified is the one that should be stored */
540 g_assert (strcmp (arg_test2_string, "bar") == 0);
542 g_free (arg_test2_string);
544 g_strfreev (argv_copy);
546 g_option_context_free (context);
552 GOptionContext *context;
554 GError *error = NULL;
558 GOptionEntry entries [] =
559 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
562 context = g_option_context_new (NULL);
563 g_option_context_add_main_entries (context, entries, NULL);
565 /* Now try parsing */
566 argv = split_string ("program --test foo.txt", &argc);
567 argv_copy = copy_stringv (argv, argc);
569 retval = g_option_context_parse (context, &argc, &argv, &error);
570 g_assert_no_error (error);
573 /* Last arg specified is the one that should be stored */
574 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
576 g_free (arg_test3_filename);
578 g_strfreev (argv_copy);
580 g_option_context_free (context);
587 GOptionContext *context;
589 GError *error = NULL;
593 GOptionEntry entries [] =
594 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, NULL, NULL },
597 context = g_option_context_new (NULL);
598 g_option_context_add_main_entries (context, entries, NULL);
600 /* Now try parsing */
601 argv = split_string ("program --test 20.0 --test 30.03", &argc);
602 argv_copy = copy_stringv (argv, argc);
604 retval = g_option_context_parse (context, &argc, &argv, &error);
605 g_assert_no_error (error);
608 /* Last arg specified is the one that should be stored */
609 g_assert (arg_test4_double == 30.03);
611 g_strfreev (argv_copy);
613 g_option_context_free (context);
619 GOptionContext *context;
621 GError *error = NULL;
625 char *old_locale, *current_locale;
626 const char *locale = "de_DE.UTF-8";
627 GOptionEntry entries [] =
628 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test5_double, NULL, NULL },
631 context = g_option_context_new (NULL);
632 g_option_context_add_main_entries (context, entries, NULL);
634 /* Now try parsing */
635 argv = split_string ("program --test 20,0 --test 30,03", &argc);
636 argv_copy = copy_stringv (argv, argc);
638 /* set it to some locale that uses commas instead of decimal points */
640 old_locale = g_strdup (setlocale (LC_NUMERIC, locale));
641 current_locale = setlocale (LC_NUMERIC, NULL);
642 if (strcmp (current_locale, locale) != 0)
644 fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
648 retval = g_option_context_parse (context, &argc, &argv, &error);
649 g_assert_no_error (error);
652 /* Last arg specified is the one that should be stored */
653 g_assert (arg_test5_double == 30.03);
656 setlocale (LC_NUMERIC, old_locale);
659 g_strfreev (argv_copy);
661 g_option_context_free (context);
667 GOptionContext *context;
669 GError *error = NULL;
673 GOptionEntry entries [] =
674 { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL },
675 { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, NULL, NULL },
678 context = g_option_context_new (NULL);
679 g_option_context_add_main_entries (context, entries, NULL);
681 /* Now try parsing */
682 argv = split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc);
683 argv_copy = copy_stringv (argv, argc);
685 retval = g_option_context_parse (context, &argc, &argv, &error);
686 g_assert_no_error (error);
689 /* Last arg specified is the one that should be stored */
690 g_assert (arg_test6_int64 == G_GINT64_CONSTANT(4294967296));
691 g_assert (arg_test6_int64_2 == G_GINT64_CONSTANT(0xfffffffff));
693 g_strfreev (argv_copy);
695 g_option_context_free (context);
699 callback_parse1 (const gchar *option_name, const gchar *value,
700 gpointer data, GError **error)
702 callback_test1_string = g_strdup (value);
707 callback_test1 (void)
709 GOptionContext *context;
711 GError *error = NULL;
715 GOptionEntry entries [] =
716 { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL },
719 context = g_option_context_new (NULL);
720 g_option_context_add_main_entries (context, entries, NULL);
722 /* Now try parsing */
723 argv = split_string ("program --test foo.txt", &argc);
724 argv_copy = copy_stringv (argv, argc);
726 retval = g_option_context_parse (context, &argc, &argv, &error);
727 g_assert_no_error (error);
730 g_assert (strcmp (callback_test1_string, "foo.txt") == 0);
732 g_free (callback_test1_string);
734 g_strfreev (argv_copy);
736 g_option_context_free (context);
740 callback_parse2 (const gchar *option_name, const gchar *value,
741 gpointer data, GError **error)
743 callback_test2_int++;
748 callback_test2 (void)
750 GOptionContext *context;
752 GError *error = NULL;
756 GOptionEntry entries [] =
757 { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL },
760 context = g_option_context_new (NULL);
761 g_option_context_add_main_entries (context, entries, NULL);
763 /* Now try parsing */
764 argv = split_string ("program --test --test", &argc);
765 argv_copy = copy_stringv (argv, argc);
767 retval = g_option_context_parse (context, &argc, &argv, &error);
768 g_assert_no_error (error);
771 g_assert (callback_test2_int == 2);
773 g_strfreev (argv_copy);
775 g_option_context_free (context);
779 callback_parse_optional (const gchar *option_name, const gchar *value,
780 gpointer data, GError **error)
782 callback_test_optional_boolean = TRUE;
784 callback_test_optional_string = g_strdup (value);
786 callback_test_optional_string = NULL;
791 callback_test_optional_1 (void)
793 GOptionContext *context;
795 GError *error = NULL;
799 GOptionEntry entries [] =
800 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
801 callback_parse_optional, NULL, NULL },
804 context = g_option_context_new (NULL);
805 g_option_context_add_main_entries (context, entries, NULL);
807 /* Now try parsing */
808 argv = split_string ("program --test foo.txt", &argc);
809 argv_copy = copy_stringv (argv, argc);
811 retval = g_option_context_parse (context, &argc, &argv, &error);
812 g_assert_no_error (error);
815 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
817 g_assert (callback_test_optional_boolean);
819 g_free (callback_test_optional_string);
821 g_strfreev (argv_copy);
823 g_option_context_free (context);
827 callback_test_optional_2 (void)
829 GOptionContext *context;
831 GError *error = NULL;
835 GOptionEntry entries [] =
836 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
837 callback_parse_optional, NULL, NULL },
840 context = g_option_context_new (NULL);
841 g_option_context_add_main_entries (context, entries, NULL);
843 /* Now try parsing */
844 argv = split_string ("program --test", &argc);
845 argv_copy = copy_stringv (argv, argc);
847 retval = g_option_context_parse (context, &argc, &argv, &error);
848 g_assert_no_error (error);
851 g_assert (callback_test_optional_string == NULL);
853 g_assert (callback_test_optional_boolean);
855 g_free (callback_test_optional_string);
857 g_strfreev (argv_copy);
859 g_option_context_free (context);
863 callback_test_optional_3 (void)
865 GOptionContext *context;
867 GError *error = NULL;
871 GOptionEntry entries [] =
872 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
873 callback_parse_optional, NULL, NULL },
876 context = g_option_context_new (NULL);
877 g_option_context_add_main_entries (context, entries, NULL);
879 /* Now try parsing */
880 argv = split_string ("program -t foo.txt", &argc);
881 argv_copy = copy_stringv (argv, argc);
883 retval = g_option_context_parse (context, &argc, &argv, &error);
884 g_assert_no_error (error);
887 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
889 g_assert (callback_test_optional_boolean);
891 g_free (callback_test_optional_string);
893 g_strfreev (argv_copy);
895 g_option_context_free (context);
900 callback_test_optional_4 (void)
902 GOptionContext *context;
904 GError *error = NULL;
908 GOptionEntry entries [] =
909 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
910 callback_parse_optional, NULL, NULL },
913 context = g_option_context_new (NULL);
914 g_option_context_add_main_entries (context, entries, NULL);
916 /* Now try parsing */
917 argv = split_string ("program -t", &argc);
918 argv_copy = copy_stringv (argv, argc);
920 retval = g_option_context_parse (context, &argc, &argv, &error);
921 g_assert_no_error (error);
924 g_assert (callback_test_optional_string == NULL);
926 g_assert (callback_test_optional_boolean);
928 g_free (callback_test_optional_string);
930 g_strfreev (argv_copy);
932 g_option_context_free (context);
936 callback_test_optional_5 (void)
938 GOptionContext *context;
941 GError *error = NULL;
945 GOptionEntry entries [] =
946 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL, NULL },
947 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
948 callback_parse_optional, NULL, NULL },
951 context = g_option_context_new (NULL);
952 g_option_context_add_main_entries (context, entries, NULL);
954 /* Now try parsing */
955 argv = split_string ("program --test --dummy", &argc);
956 argv_copy = copy_stringv (argv, argc);
958 retval = g_option_context_parse (context, &argc, &argv, &error);
959 g_assert_no_error (error);
962 g_assert (callback_test_optional_string == NULL);
964 g_assert (callback_test_optional_boolean);
966 g_free (callback_test_optional_string);
968 g_strfreev (argv_copy);
970 g_option_context_free (context);
974 callback_test_optional_6 (void)
976 GOptionContext *context;
979 GError *error = NULL;
983 GOptionEntry entries [] =
984 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL, NULL },
985 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
986 callback_parse_optional, NULL, NULL },
989 context = g_option_context_new (NULL);
990 g_option_context_add_main_entries (context, entries, NULL);
992 /* Now try parsing */
993 argv = split_string ("program -t -d", &argc);
994 argv_copy = copy_stringv (argv, argc);
996 retval = g_option_context_parse (context, &argc, &argv, &error);
997 g_assert_no_error (error);
1000 g_assert (callback_test_optional_string == NULL);
1002 g_assert (callback_test_optional_boolean);
1004 g_free (callback_test_optional_string);
1006 g_strfreev (argv_copy);
1008 g_option_context_free (context);
1012 callback_test_optional_7 (void)
1014 GOptionContext *context;
1017 GError *error = NULL;
1021 GOptionEntry entries [] =
1022 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL, NULL },
1023 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
1024 callback_parse_optional, NULL, NULL },
1027 context = g_option_context_new (NULL);
1028 g_option_context_add_main_entries (context, entries, NULL);
1030 /* Now try parsing */
1031 argv = split_string ("program -td", &argc);
1032 argv_copy = copy_stringv (argv, argc);
1034 retval = g_option_context_parse (context, &argc, &argv, &error);
1035 g_assert_no_error (error);
1038 g_assert (callback_test_optional_string == NULL);
1040 g_assert (callback_test_optional_boolean);
1042 g_free (callback_test_optional_string);
1044 g_strfreev (argv_copy);
1046 g_option_context_free (context);
1050 callback_test_optional_8 (void)
1052 GOptionContext *context;
1055 GError *error = NULL;
1059 GOptionEntry entries [] =
1060 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL, NULL },
1061 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
1062 callback_parse_optional, NULL, NULL },
1065 context = g_option_context_new (NULL);
1066 g_option_context_add_main_entries (context, entries, NULL);
1068 /* Now try parsing */
1069 argv = split_string ("program -dt foo.txt", &argc);
1070 argv_copy = copy_stringv (argv, argc);
1072 retval = g_option_context_parse (context, &argc, &argv, &error);
1073 g_assert_no_error (error);
1076 g_assert (callback_test_optional_string);
1078 g_assert (callback_test_optional_boolean);
1080 g_free (callback_test_optional_string);
1082 g_strfreev (argv_copy);
1084 g_option_context_free (context);
1087 static GPtrArray *callback_remaining_args;
1089 callback_remaining_test1_callback (const gchar *option_name, const gchar *value,
1090 gpointer data, GError **error)
1092 g_ptr_array_add (callback_remaining_args, g_strdup (value));
1097 callback_remaining_test1 (void)
1099 GOptionContext *context;
1101 GError *error = NULL;
1105 GOptionEntry entries [] =
1106 { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL },
1109 callback_remaining_args = g_ptr_array_new ();
1110 context = g_option_context_new (NULL);
1111 g_option_context_add_main_entries (context, entries, NULL);
1113 /* Now try parsing */
1114 argv = split_string ("program foo.txt blah.txt", &argc);
1115 argv_copy = copy_stringv (argv, argc);
1117 retval = g_option_context_parse (context, &argc, &argv, &error);
1118 g_assert_no_error (error);
1121 g_assert (callback_remaining_args->len == 2);
1122 g_assert (strcmp (callback_remaining_args->pdata[0], "foo.txt") == 0);
1123 g_assert (strcmp (callback_remaining_args->pdata[1], "blah.txt") == 0);
1125 g_ptr_array_foreach (callback_remaining_args, (GFunc) g_free, NULL);
1126 g_ptr_array_free (callback_remaining_args, TRUE);
1128 g_strfreev (argv_copy);
1130 g_option_context_free (context);
1134 callback_error (const gchar *option_name, const gchar *value,
1135 gpointer data, GError **error)
1137 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "42");
1142 callback_returns_false (void)
1144 GOptionContext *context;
1146 GError *error = NULL;
1150 GOptionEntry entries [] =
1151 { { "error", 0, 0, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1152 { "error-no-arg", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1153 { "error-optional-arg", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1156 context = g_option_context_new (NULL);
1157 g_option_context_add_main_entries (context, entries, NULL);
1159 /* Now try parsing */
1160 argv = split_string ("program --error value", &argc);
1161 argv_copy = copy_stringv (argv, argc);
1163 retval = g_option_context_parse (context, &argc, &argv, &error);
1164 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1165 g_assert (retval == FALSE);
1166 check_identical_stringv (argv_copy, argv);
1168 g_option_context_free (context);
1169 g_clear_error (&error);
1170 g_strfreev (argv_copy);
1173 /* And again, this time with a no-arg variant */
1174 context = g_option_context_new (NULL);
1175 g_option_context_add_main_entries (context, entries, NULL);
1177 argv = split_string ("program --error-no-arg", &argc);
1178 argv_copy = copy_stringv (argv, argc);
1180 retval = g_option_context_parse (context, &argc, &argv, &error);
1181 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1182 g_assert (retval == FALSE);
1183 check_identical_stringv (argv_copy, argv);
1185 g_option_context_free (context);
1186 g_clear_error (&error);
1187 g_strfreev (argv_copy);
1190 /* And again, this time with an optional arg variant, with argument */
1191 context = g_option_context_new (NULL);
1192 g_option_context_add_main_entries (context, entries, NULL);
1194 argv = split_string ("program --error-optional-arg value", &argc);
1195 argv_copy = copy_stringv (argv, argc);
1197 retval = g_option_context_parse (context, &argc, &argv, &error);
1198 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1199 g_assert (retval == FALSE);
1200 check_identical_stringv (argv_copy, argv);
1202 g_option_context_free (context);
1203 g_clear_error (&error);
1204 g_strfreev (argv_copy);
1207 /* And again, this time with an optional arg variant, without argument */
1208 context = g_option_context_new (NULL);
1209 g_option_context_add_main_entries (context, entries, NULL);
1211 argv = split_string ("program --error-optional-arg", &argc);
1212 argv_copy = copy_stringv (argv, argc);
1214 retval = g_option_context_parse (context, &argc, &argv, &error);
1215 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1216 g_assert (retval == FALSE);
1217 check_identical_stringv (argv_copy, argv);
1219 g_option_context_free (context);
1220 g_clear_error (&error);
1221 g_strfreev (argv_copy);
1229 GOptionContext *context;
1231 GError *error = NULL;
1232 gchar **argv, **argv_copy;
1235 GOptionEntry entries [] =
1236 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1239 context = g_option_context_new (NULL);
1240 g_option_context_set_ignore_unknown_options (context, TRUE);
1241 g_option_context_add_main_entries (context, entries, NULL);
1243 /* Now try parsing */
1244 argv = split_string ("program --test --hello", &argc);
1245 argv_copy = copy_stringv (argv, argc);
1247 retval = g_option_context_parse (context, &argc, &argv, &error);
1248 g_assert_no_error (error);
1252 arg = join_stringv (argc, argv);
1253 g_assert (strcmp (arg, "program --hello") == 0);
1256 g_strfreev (argv_copy);
1258 g_option_context_free (context);
1264 GOptionContext *context;
1266 GError *error = NULL;
1271 GOptionEntry entries [] =
1272 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
1275 context = g_option_context_new (NULL);
1276 g_option_context_set_ignore_unknown_options (context, TRUE);
1277 g_option_context_add_main_entries (context, entries, NULL);
1279 /* Now try parsing */
1280 argv = split_string ("program -test", &argc);
1281 argv_copy = copy_stringv (argv, argc);
1283 retval = g_option_context_parse (context, &argc, &argv, &error);
1284 g_assert_no_error (error);
1288 arg = join_stringv (argc, argv);
1289 g_assert (strcmp (arg, "program -es") == 0);
1292 g_strfreev (argv_copy);
1294 g_option_context_free (context);
1300 GOptionContext *context;
1302 GError *error = NULL;
1303 gchar **argv, **argv_copy;
1306 GOptionEntry entries [] =
1307 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
1310 context = g_option_context_new (NULL);
1311 g_option_context_set_ignore_unknown_options (context, TRUE);
1312 g_option_context_add_main_entries (context, entries, NULL);
1314 /* Now try parsing */
1315 argv = split_string ("program --test foo --hello", &argc);
1316 argv_copy = copy_stringv (argv, argc);
1318 retval = g_option_context_parse (context, &argc, &argv, &error);
1319 g_assert_no_error (error);
1323 arg = join_stringv (argc, argv);
1324 g_assert (strcmp (arg, "program --hello") == 0);
1326 g_assert (strcmp (ignore_test3_string, "foo") == 0);
1327 g_free (ignore_test3_string);
1330 g_strfreev (argv_copy);
1332 g_option_context_free (context);
1338 GOptionContext *context;
1340 GError *error = NULL;
1344 GOptionEntry entries [] =
1345 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1348 context = g_option_context_new (NULL);
1349 g_option_context_add_main_entries (context, entries, NULL);
1351 /* Now try parsing */
1352 argv = split_string ("program --test foo --test bar", &argc);
1353 argv_copy = copy_stringv (argv, argc);
1355 retval = g_option_context_parse (context, &argc, &argv, &error);
1356 g_assert_no_error (error);
1360 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1361 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1362 g_assert (array_test1_array[2] == NULL);
1364 g_strfreev (array_test1_array);
1366 g_strfreev (argv_copy);
1368 g_option_context_free (context);
1374 GOptionContext *context;
1376 GOptionEntry entries1 [] =
1377 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1379 GOptionEntry entries2 [] =
1380 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1383 context = g_option_context_new (NULL);
1384 g_option_context_add_main_entries (context, entries1, NULL);
1385 g_option_context_add_main_entries (context, entries2, NULL);
1387 g_option_context_free (context);
1393 GOptionContext *context;
1395 context = g_option_context_new (NULL);
1396 g_option_context_parse (context, NULL, NULL, NULL);
1398 g_option_context_free (context);
1404 GOptionContext *context;
1411 context = g_option_context_new (NULL);
1412 g_option_context_parse (context, &argc, &argv, NULL);
1414 g_option_context_free (context);
1417 /* check that non-option arguments are left in argv by default */
1421 GOptionContext *context;
1423 GError *error = NULL;
1427 GOptionEntry entries [] = {
1428 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1432 context = g_option_context_new (NULL);
1433 g_option_context_add_main_entries (context, entries, NULL);
1435 /* Now try parsing */
1436 argv = split_string ("program foo --test bar", &argc);
1437 argv_copy = copy_stringv (argv, argc);
1439 retval = g_option_context_parse (context, &argc, &argv, &error);
1440 g_assert_no_error (error);
1444 g_assert (ignore_test1_boolean);
1445 g_assert (strcmp (argv[0], "program") == 0);
1446 g_assert (strcmp (argv[1], "foo") == 0);
1447 g_assert (strcmp (argv[2], "bar") == 0);
1448 g_assert (argv[3] == NULL);
1450 g_strfreev (argv_copy);
1452 g_option_context_free (context);
1455 /* check that -- works */
1459 GOptionContext *context;
1461 GError *error = NULL;
1465 GOptionEntry entries [] = {
1466 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1470 context = g_option_context_new (NULL);
1471 g_option_context_add_main_entries (context, entries, NULL);
1473 /* Now try parsing */
1474 argv = split_string ("program foo --test -- -bar", &argc);
1475 argv_copy = copy_stringv (argv, argc);
1477 retval = g_option_context_parse (context, &argc, &argv, &error);
1478 g_assert_no_error (error);
1482 g_assert (ignore_test1_boolean);
1483 g_assert (strcmp (argv[0], "program") == 0);
1484 g_assert (strcmp (argv[1], "foo") == 0);
1485 g_assert (strcmp (argv[2], "--") == 0);
1486 g_assert (strcmp (argv[3], "-bar") == 0);
1487 g_assert (argv[4] == NULL);
1489 g_strfreev (argv_copy);
1491 g_option_context_free (context);
1494 /* check that -- stripping works */
1498 GOptionContext *context;
1500 GError *error = NULL;
1504 GOptionEntry entries [] = {
1505 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1509 context = g_option_context_new (NULL);
1510 g_option_context_add_main_entries (context, entries, NULL);
1512 /* Now try parsing */
1513 argv = split_string ("program foo --test -- bar", &argc);
1514 argv_copy = copy_stringv (argv, argc);
1516 retval = g_option_context_parse (context, &argc, &argv, &error);
1517 g_assert_no_error (error);
1521 g_assert (ignore_test1_boolean);
1522 g_assert (strcmp (argv[0], "program") == 0);
1523 g_assert (strcmp (argv[1], "foo") == 0);
1524 g_assert (strcmp (argv[2], "bar") == 0);
1525 g_assert (argv[3] == NULL);
1527 g_strfreev (argv_copy);
1529 g_option_context_free (context);
1535 GOptionContext *context;
1537 GError *error = NULL;
1541 GOptionEntry entries [] = {
1542 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1546 context = g_option_context_new (NULL);
1547 g_option_context_set_ignore_unknown_options (context, TRUE);
1548 g_option_context_add_main_entries (context, entries, NULL);
1550 /* Now try parsing */
1551 argv = split_string ("program foo --test -bar --", &argc);
1552 argv_copy = copy_stringv (argv, argc);
1554 retval = g_option_context_parse (context, &argc, &argv, &error);
1555 g_assert_no_error (error);
1559 g_assert (ignore_test1_boolean);
1560 g_assert (strcmp (argv[0], "program") == 0);
1561 g_assert (strcmp (argv[1], "foo") == 0);
1562 g_assert (strcmp (argv[2], "-bar") == 0);
1563 g_assert (argv[3] == NULL);
1565 g_strfreev (argv_copy);
1567 g_option_context_free (context);
1573 GOptionContext *context;
1575 GError *error = NULL;
1579 GOptionEntry entries [] = {
1580 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1584 context = g_option_context_new (NULL);
1585 g_option_context_add_main_entries (context, entries, NULL);
1587 /* Now try parsing */
1588 argv = split_string ("program --test foo -- bar", &argc);
1589 argv_copy = copy_stringv (argv, argc);
1591 retval = g_option_context_parse (context, &argc, &argv, &error);
1592 g_assert_no_error (error);
1596 g_assert (ignore_test1_boolean);
1597 g_assert (strcmp (argv[0], "program") == 0);
1598 g_assert (strcmp (argv[1], "foo") == 0);
1599 g_assert (strcmp (argv[2], "bar") == 0);
1600 g_assert (argv[3] == NULL);
1602 g_strfreev (argv_copy);
1604 g_option_context_free (context);
1610 GOptionContext *context;
1612 GError *error = NULL;
1616 GOptionEntry entries [] = {
1617 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1621 context = g_option_context_new (NULL);
1622 g_option_context_add_main_entries (context, entries, NULL);
1624 /* Now try parsing */
1625 argv = split_string ("program --test -- -bar", &argc);
1626 argv_copy = copy_stringv (argv, argc);
1628 retval = g_option_context_parse (context, &argc, &argv, &error);
1629 g_assert_no_error (error);
1633 g_assert (ignore_test1_boolean);
1634 g_assert (strcmp (argv[0], "program") == 0);
1635 g_assert (strcmp (argv[1], "--") == 0);
1636 g_assert (strcmp (argv[2], "-bar") == 0);
1637 g_assert (argv[3] == NULL);
1639 g_strfreev (argv_copy);
1641 g_option_context_free (context);
1645 /* check that G_OPTION_REMAINING collects non-option arguments */
1649 GOptionContext *context;
1651 GError *error = NULL;
1655 GOptionEntry entries [] = {
1656 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1657 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1661 context = g_option_context_new (NULL);
1662 g_option_context_add_main_entries (context, entries, NULL);
1664 /* Now try parsing */
1665 argv = split_string ("program foo --test bar", &argc);
1666 argv_copy = copy_stringv (argv, argc);
1668 retval = g_option_context_parse (context, &argc, &argv, &error);
1669 g_assert_no_error (error);
1673 g_assert (ignore_test1_boolean);
1674 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1675 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1676 g_assert (array_test1_array[2] == NULL);
1678 g_strfreev (array_test1_array);
1680 g_strfreev (argv_copy);
1682 g_option_context_free (context);
1686 /* check that G_OPTION_REMAINING and -- work together */
1690 GOptionContext *context;
1692 GError *error = NULL;
1696 GOptionEntry entries [] = {
1697 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1698 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1702 context = g_option_context_new (NULL);
1703 g_option_context_add_main_entries (context, entries, NULL);
1705 /* Now try parsing */
1706 argv = split_string ("program foo --test -- -bar", &argc);
1707 argv_copy = copy_stringv (argv, argc);
1709 retval = g_option_context_parse (context, &argc, &argv, &error);
1710 g_assert_no_error (error);
1714 g_assert (ignore_test1_boolean);
1715 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1716 g_assert (strcmp (array_test1_array[1], "-bar") == 0);
1717 g_assert (array_test1_array[2] == NULL);
1719 g_strfreev (array_test1_array);
1721 g_strfreev (argv_copy);
1723 g_option_context_free (context);
1726 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1730 GOptionContext *context;
1732 GError *error = NULL;
1736 GOptionEntry entries [] = {
1737 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1738 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
1742 context = g_option_context_new (NULL);
1743 g_option_context_add_main_entries (context, entries, NULL);
1745 /* Now try parsing */
1746 argv = split_string ("program foo --test bar", &argc);
1747 argv_copy = copy_stringv (argv, argc);
1749 retval = g_option_context_parse (context, &argc, &argv, &error);
1750 g_assert_no_error (error);
1754 g_assert (ignore_test1_boolean);
1755 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1756 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1757 g_assert (array_test1_array[2] == NULL);
1759 g_strfreev (array_test1_array);
1761 g_strfreev (argv_copy);
1763 g_option_context_free (context);
1767 unknown_short_test (void)
1769 GOptionContext *context;
1771 GError *error = NULL;
1775 GOptionEntry entries [] = { { NULL } };
1777 g_test_bug ("166609");
1779 context = g_option_context_new (NULL);
1780 g_option_context_add_main_entries (context, entries, NULL);
1782 /* Now try parsing */
1783 argv = split_string ("program -0", &argc);
1784 argv_copy = copy_stringv (argv, argc);
1786 retval = g_option_context_parse (context, &argc, &argv, &error);
1788 g_assert (error != NULL);
1789 g_clear_error (&error);
1791 g_strfreev (argv_copy);
1793 g_option_context_free (context);
1796 /* test that lone dashes are treated as non-options */
1798 lonely_dash_test (void)
1800 GOptionContext *context;
1802 GError *error = NULL;
1807 g_test_bug ("168008");
1809 context = g_option_context_new (NULL);
1811 /* Now try parsing */
1812 argv = split_string ("program -", &argc);
1813 argv_copy = copy_stringv (argv, argc);
1815 retval = g_option_context_parse (context, &argc, &argv, &error);
1816 g_assert_no_error (error);
1819 g_assert (argv[1] && strcmp (argv[1], "-") == 0);
1821 g_strfreev (argv_copy);
1823 g_option_context_free (context);
1826 /* test that three dashes are treated as non-options */
1828 triple_dash_test (void)
1830 GOptionContext *context;
1831 GOptionGroup *group;
1833 GError *error = NULL;
1838 GOptionEntry entries [] =
1839 { { "foo", 0, 0, G_OPTION_ARG_INT, &arg1, NULL, NULL},
1842 GOptionEntry group_entries [] =
1843 { { "test", 0, 0, G_OPTION_ARG_INT, &arg2, NULL, NULL},
1847 context = g_option_context_new (NULL);
1848 g_option_context_add_main_entries (context, entries, NULL);
1850 group = g_option_group_new ("group", "Group description", "Group help", NULL, NULL);
1851 g_option_group_add_entries (group, group_entries);
1853 g_option_context_add_group (context, group);
1855 /* Now try parsing */
1856 argv = split_string ("program ---test 42", &argc);
1857 argv_copy = copy_stringv (argv, argc);
1859 retval = g_option_context_parse (context, &argc, &argv, &error);
1860 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION);
1861 g_assert (retval == FALSE);
1863 g_option_context_free (context);
1864 g_clear_error (&error);
1865 g_strfreev (argv_copy);
1870 missing_arg_test (void)
1872 GOptionContext *context;
1874 GError *error = NULL;
1879 GOptionEntry entries [] =
1880 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1883 g_test_bug ("305576");
1885 context = g_option_context_new (NULL);
1886 g_option_context_add_main_entries (context, entries, NULL);
1888 /* Now try parsing */
1889 argv = split_string ("program --test", &argc);
1890 argv_copy = copy_stringv (argv, argc);
1892 retval = g_option_context_parse (context, &argc, &argv, &error);
1893 g_assert (retval == FALSE);
1894 g_assert (error != NULL);
1895 /* An error occurred, so argv has not been changed */
1896 check_identical_stringv (argv_copy, argv);
1897 g_clear_error (&error);
1899 g_strfreev (argv_copy);
1902 /* Try parsing again */
1903 argv = split_string ("program -t", &argc);
1904 argv_copy = copy_stringv (argv, argc);
1906 retval = g_option_context_parse (context, &argc, &argv, &error);
1907 g_assert (retval == FALSE);
1908 g_assert (error != NULL);
1909 /* An error occurred, so argv has not been changed */
1910 check_identical_stringv (argv_copy, argv);
1911 g_clear_error (&error);
1913 g_strfreev (argv_copy);
1915 g_option_context_free (context);
1917 /* Checking g_option_context_parse_strv on NULL args */
1918 context = g_option_context_new (NULL);
1919 g_assert_true (g_option_context_parse_strv (context, NULL, NULL));
1920 g_option_context_free (context);
1923 static gchar *test_arg;
1925 static gboolean cb (const gchar *option_name,
1930 test_arg = g_strdup (value);
1935 dash_arg_test (void)
1937 GOptionContext *context;
1939 GError *error = NULL;
1943 gboolean argb = FALSE;
1944 GOptionEntry entries [] =
1945 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, cb, NULL, NULL },
1946 { "three", '3', 0, G_OPTION_ARG_NONE, &argb, NULL, NULL },
1949 g_test_bug ("577638");
1951 context = g_option_context_new (NULL);
1952 g_option_context_add_main_entries (context, entries, NULL);
1954 /* Now try parsing */
1955 argv = split_string ("program --test=-3", &argc);
1956 argv_copy = copy_stringv (argv, argc);
1960 retval = g_option_context_parse (context, &argc, &argv, &error);
1962 g_assert_no_error (error);
1963 g_assert_cmpstr (test_arg, ==, "-3");
1965 g_strfreev (argv_copy);
1970 /* Try parsing again */
1971 argv = split_string ("program --test -3", &argc);
1972 argv_copy = copy_stringv (argv, argc);
1975 retval = g_option_context_parse (context, &argc, &argv, &error);
1976 g_assert_no_error (error);
1978 g_assert_cmpstr (test_arg, ==, NULL);
1980 g_option_context_free (context);
1981 g_strfreev (argv_copy);
1988 GOptionContext *context;
1990 GOptionEntry entries [] =
1991 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1994 context = g_option_context_new (NULL);
1995 g_option_context_add_main_entries (context, entries, NULL);
1997 g_assert (g_option_context_get_help_enabled (context));
1998 g_assert (!g_option_context_get_ignore_unknown_options (context));
1999 g_assert_cmpstr (g_option_context_get_summary (context), ==, NULL);
2000 g_assert_cmpstr (g_option_context_get_description (context), ==, NULL);
2002 g_option_context_set_help_enabled (context, FALSE);
2003 g_option_context_set_ignore_unknown_options (context, TRUE);
2004 g_option_context_set_summary (context, "summary");
2005 g_option_context_set_description(context, "description");
2007 g_assert (!g_option_context_get_help_enabled (context));
2008 g_assert (g_option_context_get_ignore_unknown_options (context));
2009 g_assert_cmpstr (g_option_context_get_summary (context), ==, "summary");
2010 g_assert_cmpstr (g_option_context_get_description (context), ==, "description");
2012 g_option_context_free (context);
2016 gboolean parameter_seen;
2017 gboolean summary_seen;
2018 gboolean description_seen;
2022 static const gchar *
2023 translate_func (const gchar *str,
2026 TranslateData *d = data;
2028 if (strcmp (str, "parameter") == 0)
2029 d->parameter_seen = TRUE;
2030 else if (strcmp (str, "summary") == 0)
2031 d->summary_seen = TRUE;
2032 else if (strcmp (str, "description") == 0)
2033 d->description_seen = TRUE;
2039 destroy_notify (gpointer data)
2041 TranslateData *d = data;
2043 d->destroyed = TRUE;
2047 test_translate (void)
2049 GOptionContext *context;
2051 GOptionEntry entries [] =
2052 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2054 TranslateData data = { 0, };
2057 context = g_option_context_new ("parameter");
2058 g_option_context_add_main_entries (context, entries, NULL);
2059 g_option_context_set_summary (context, "summary");
2060 g_option_context_set_description (context, "description");
2062 g_option_context_set_translate_func (context, translate_func, &data, destroy_notify);
2064 str = g_option_context_get_help (context, FALSE, NULL);
2066 g_option_context_free (context);
2068 g_assert (data.parameter_seen);
2069 g_assert (data.summary_seen);
2070 g_assert (data.description_seen);
2071 g_assert (data.destroyed);
2077 GOptionContext *context;
2078 GOptionGroup *group;
2081 gchar **sarr = NULL;
2082 GOptionEntry entries[] = {
2083 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Test tests", "Argument to use in test" },
2084 { "test2", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, NULL, "Tests also", NULL },
2085 { "frob", 0, 0, G_OPTION_ARG_NONE, NULL, "Main frob", NULL },
2086 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
2089 GOptionEntry group_entries[] = {
2090 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Group test", "Group test arg" },
2091 { "frob", 0, G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_NONE, NULL, "Group frob", NULL },
2095 context = g_option_context_new ("blabla");
2096 g_option_context_add_main_entries (context, entries, NULL);
2097 g_option_context_set_summary (context, "Summary");
2098 g_option_context_set_description (context, "Description");
2100 group = g_option_group_new ("group1", "Group1-description", "Group1-help", NULL, NULL);
2101 g_option_group_add_entries (group, group_entries);
2103 g_option_context_add_group (context, group);
2105 str = g_option_context_get_help (context, FALSE, NULL);
2106 g_assert (strstr (str, "blabla") != NULL);
2107 g_assert (strstr (str, "Test tests") != NULL);
2108 g_assert (strstr (str, "Argument to use in test") != NULL);
2109 g_assert (strstr (str, "Tests also") == NULL);
2110 g_assert (strstr (str, "REST") != NULL);
2111 g_assert (strstr (str, "Summary") != NULL);
2112 g_assert (strstr (str, "Description") != NULL);
2113 g_assert (strstr (str, "--help") != NULL);
2114 g_assert (strstr (str, "--help-all") != NULL);
2115 g_assert (strstr (str, "--help-group1") != NULL);
2116 g_assert (strstr (str, "Group1-description") != NULL);
2117 g_assert (strstr (str, "Group1-help") != NULL);
2118 g_assert (strstr (str, "Group test arg") != NULL);
2119 g_assert (strstr (str, "Group frob") != NULL);
2120 g_assert (strstr (str, "Main frob") != NULL);
2121 g_assert (strstr (str, "--frob") != NULL);
2122 g_assert (strstr (str, "--group1-test") != NULL);
2123 g_assert (strstr (str, "--group1-frob") == NULL);
2126 g_option_context_free (context);
2130 test_help_no_options (void)
2132 GOptionContext *context;
2133 gchar **sarr = NULL;
2134 GOptionEntry entries[] = {
2135 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
2140 context = g_option_context_new ("blabla");
2141 g_option_context_add_main_entries (context, entries, NULL);
2143 str = g_option_context_get_help (context, FALSE, NULL);
2144 g_assert (strstr (str, "blabla") != NULL);
2145 g_assert (strstr (str, "REST") != NULL);
2146 g_assert (strstr (str, "Help Options") != NULL);
2147 g_assert (strstr (str, "Application Options") == NULL);
2150 g_option_context_free (context);
2154 test_help_no_help_options (void)
2156 GOptionContext *context;
2157 GOptionGroup *group;
2160 gchar **sarr = NULL;
2161 GOptionEntry entries[] = {
2162 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Test tests", "Argument to use in test" },
2163 { "test2", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, NULL, "Tests also", NULL },
2164 { "frob", 0, 0, G_OPTION_ARG_NONE, NULL, "Main frob", NULL },
2165 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
2168 GOptionEntry group_entries[] = {
2169 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Group test", "Group test arg" },
2170 { "frob", 0, G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_NONE, NULL, "Group frob", NULL },
2174 g_test_bug ("697652");
2176 context = g_option_context_new ("blabla");
2177 g_option_context_add_main_entries (context, entries, NULL);
2178 g_option_context_set_summary (context, "Summary");
2179 g_option_context_set_description (context, "Description");
2180 g_option_context_set_help_enabled (context, FALSE);
2182 group = g_option_group_new ("group1", "Group1-description", "Group1-help", NULL, NULL);
2183 g_option_group_add_entries (group, group_entries);
2185 g_option_context_add_group (context, group);
2187 str = g_option_context_get_help (context, FALSE, NULL);
2188 g_assert (strstr (str, "blabla") != NULL);
2189 g_assert (strstr (str, "Test tests") != NULL);
2190 g_assert (strstr (str, "Argument to use in test") != NULL);
2191 g_assert (strstr (str, "Tests also") == NULL);
2192 g_assert (strstr (str, "REST") != NULL);
2193 g_assert (strstr (str, "Summary") != NULL);
2194 g_assert (strstr (str, "Description") != NULL);
2195 g_assert (strstr (str, "Help Options") == NULL);
2196 g_assert (strstr (str, "--help") == NULL);
2197 g_assert (strstr (str, "--help-all") == NULL);
2198 g_assert (strstr (str, "--help-group1") == NULL);
2199 g_assert (strstr (str, "Group1-description") != NULL);
2200 g_assert (strstr (str, "Group1-help") == NULL);
2201 g_assert (strstr (str, "Group test arg") != NULL);
2202 g_assert (strstr (str, "Group frob") != NULL);
2203 g_assert (strstr (str, "Main frob") != NULL);
2204 g_assert (strstr (str, "--frob") != NULL);
2205 g_assert (strstr (str, "--group1-test") != NULL);
2206 g_assert (strstr (str, "--group1-frob") == NULL);
2209 g_option_context_free (context);
2213 set_bool (gpointer data)
2221 test_main_group (void)
2223 GOptionContext *context;
2224 GOptionGroup *group;
2227 context = g_option_context_new (NULL);
2228 g_assert (g_option_context_get_main_group (context) == NULL);
2229 group = g_option_group_new ("name", "description", "hlep", &b, set_bool);
2230 g_option_context_add_group (context, group);
2231 group = g_option_group_new ("name2", "description", "hlep", NULL, NULL);
2232 g_option_context_add_group (context, group);
2233 g_assert (g_option_context_get_main_group (context) == NULL);
2234 group = g_option_group_new ("name", "description", "hlep", NULL, NULL);
2235 g_option_context_set_main_group (context, group);
2236 g_assert (g_option_context_get_main_group (context) == group);
2238 g_option_context_free (context);
2243 static gboolean error_func_called = FALSE;
2246 error_func (GOptionContext *context,
2247 GOptionGroup *group,
2251 g_assert_cmpint (GPOINTER_TO_INT(data), ==, 1234);
2252 error_func_called = TRUE;
2256 test_error_hook (void)
2258 GOptionContext *context;
2260 GOptionEntry entries [] =
2261 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2263 GOptionGroup *group;
2268 GError *error = NULL;
2270 context = g_option_context_new (NULL);
2271 group = g_option_group_new ("name", "description", "hlep", GINT_TO_POINTER(1234), NULL);
2272 g_option_group_add_entries (group, entries);
2273 g_option_context_set_main_group (context, group);
2274 g_option_group_set_error_hook (g_option_context_get_main_group (context),
2277 argv = split_string ("program --test", &argc);
2278 argv_copy = copy_stringv (argv, argc);
2280 retval = g_option_context_parse (context, &argc, &argv, &error);
2281 g_assert (retval == FALSE);
2282 g_assert (error != NULL);
2283 /* An error occurred, so argv has not been changed */
2284 check_identical_stringv (argv_copy, argv);
2285 g_clear_error (&error);
2287 g_assert (error_func_called);
2289 g_strfreev (argv_copy);
2291 g_option_context_free (context);
2295 test_group_parse (void)
2297 GOptionContext *context;
2298 GOptionGroup *group;
2304 GOptionEntry entries[] = {
2305 { "test", 't', 0, G_OPTION_ARG_STRING, &arg1, NULL, NULL },
2306 { "faz", 'f', 0, G_OPTION_ARG_STRING, &arg2, NULL, NULL },
2309 GOptionEntry group_entries[] = {
2310 { "test", 0, 0, G_OPTION_ARG_STRING, &arg3, NULL, NULL },
2311 { "frob", 'f', 0, G_OPTION_ARG_STRING, &arg4, NULL, NULL },
2312 { "faz", 'z', 0, G_OPTION_ARG_STRING, &arg5, NULL, NULL },
2315 gchar **argv, **orig_argv;
2317 GError *error = NULL;
2320 context = g_option_context_new (NULL);
2321 g_option_context_add_main_entries (context, entries, NULL);
2322 group = g_option_group_new ("group", "A group", "help for group", NULL, NULL);
2323 g_option_group_add_entries (group, group_entries);
2324 g_option_context_add_group (context, group);
2326 argv = split_string ("program --test arg1 -f arg2 --group-test arg3 --frob arg4 -z arg5", &argc);
2327 orig_argv = g_memdup (argv, (argc + 1) * sizeof (char *));
2329 retval = g_option_context_parse (context, &argc, &argv, &error);
2331 g_assert_no_error (error);
2333 g_assert_cmpstr (arg1, ==, "arg1");
2334 g_assert_cmpstr (arg2, ==, "arg2");
2335 g_assert_cmpstr (arg3, ==, "arg3");
2336 g_assert_cmpstr (arg4, ==, "arg4");
2337 g_assert_cmpstr (arg5, ==, "arg5");
2346 g_strfreev (orig_argv);
2347 g_option_context_free (context);
2351 option_context_parse_command_line (GOptionContext *context,
2352 const gchar *command_line)
2355 guint argv_len, argv_new_len;
2358 argv = split_string (command_line, NULL);
2359 argv_len = g_strv_length (argv);
2361 success = g_option_context_parse_strv (context, &argv, NULL);
2362 argv_new_len = g_strv_length (argv);
2365 return success ? (gint) (argv_len - argv_new_len) : -1;
2369 test_strict_posix (void)
2371 GOptionContext *context;
2374 GOptionEntry entries[] = {
2375 { "foo", 'f', 0, G_OPTION_ARG_NONE, &foo, NULL, NULL },
2376 { "bar", 'b', 0, G_OPTION_ARG_NONE, &bar, NULL, NULL },
2381 context = g_option_context_new (NULL);
2382 g_option_context_add_main_entries (context, entries, NULL);
2385 g_option_context_set_strict_posix (context, FALSE);
2386 n_parsed = option_context_parse_command_line (context, "program --foo command --bar");
2387 g_assert_cmpint (n_parsed, ==, 2);
2388 g_assert (foo == TRUE);
2389 g_assert (bar == TRUE);
2392 g_option_context_set_strict_posix (context, TRUE);
2393 n_parsed = option_context_parse_command_line (context, "program --foo command --bar");
2394 g_assert_cmpint (n_parsed, ==, 1);
2395 g_assert (foo == TRUE);
2396 g_assert (bar == FALSE);
2399 g_option_context_set_strict_posix (context, TRUE);
2400 n_parsed = option_context_parse_command_line (context, "program --foo --bar command");
2401 g_assert_cmpint (n_parsed, ==, 2);
2402 g_assert (foo == TRUE);
2403 g_assert (bar == TRUE);
2406 g_option_context_set_strict_posix (context, TRUE);
2407 n_parsed = option_context_parse_command_line (context, "program command --foo --bar");
2408 g_assert_cmpint (n_parsed, ==, 0);
2409 g_assert (foo == FALSE);
2410 g_assert (bar == FALSE);
2412 g_option_context_free (context);
2416 flag_reverse_string (void)
2418 GOptionContext *context;
2420 GOptionEntry entries [] =
2421 { { "test", 't', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2426 GError *error = NULL;
2428 if (!g_test_undefined ())
2431 context = g_option_context_new (NULL);
2433 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
2434 "*ignoring reverse flag*");
2435 g_option_context_add_main_entries (context, entries, NULL);
2436 g_test_assert_expected_messages ();
2438 argv = split_string ("program --test bla", &argc);
2440 retval = g_option_context_parse_strv (context, &argv, &error);
2441 g_assert (retval == TRUE);
2442 g_assert_no_error (error);
2444 g_option_context_free (context);
2449 flag_optional_int (void)
2451 GOptionContext *context;
2453 GOptionEntry entries [] =
2454 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &arg, NULL, NULL },
2459 GError *error = NULL;
2461 if (!g_test_undefined ())
2464 context = g_option_context_new (NULL);
2466 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
2467 "*ignoring no-arg, optional-arg or filename flags*");
2468 g_option_context_add_main_entries (context, entries, NULL);
2469 g_test_assert_expected_messages ();
2471 argv = split_string ("program --test 5", &argc);
2473 retval = g_option_context_parse_strv (context, &argv, &error);
2474 g_assert (retval == TRUE);
2475 g_assert_no_error (error);
2477 g_option_context_free (context);
2481 short_remaining (void)
2483 gboolean ignore = FALSE;
2484 gboolean remaining = FALSE;
2487 gchar** files = NULL;
2488 GError* error = NULL;
2489 GOptionEntry entries[] =
2491 { "ignore", 'i', 0, G_OPTION_ARG_NONE, &ignore, NULL, NULL },
2492 { "remaining", 'r', 0, G_OPTION_ARG_NONE, &remaining, NULL, NULL },
2493 { "number", 'n', 0, G_OPTION_ARG_INT, &number, NULL, NULL },
2494 { "text", 't', 0, G_OPTION_ARG_STRING, &text, NULL, NULL },
2495 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, NULL, NULL },
2498 GOptionContext* context;
2499 gchar **argv, **argv_copy;
2502 g_test_bug ("729563");
2504 argv = split_string ("program -ri -n 4 -t hello file1 file2", &argc);
2505 argv_copy = copy_stringv (argv, argc);
2507 context = g_option_context_new (NULL);
2509 g_option_context_add_main_entries (context, entries, NULL);
2510 g_option_context_set_ignore_unknown_options (context, TRUE);
2512 g_option_context_parse (context, &argc, &argv, &error);
2513 g_assert_no_error (error);
2516 g_assert (remaining);
2517 g_assert_cmpint (number, ==, 4);
2518 g_assert_cmpstr (text, ==, "hello");
2519 g_assert_cmpstr (files[0], ==, "file1");
2520 g_assert_cmpstr (files[1], ==, "file2");
2521 g_assert (files[2] == NULL);
2525 g_strfreev (argv_copy);
2527 g_option_context_free (context);
2534 GOptionEntry entries[] =
2536 { "known", 0, 0, G_OPTION_ARG_STRING, &text, NULL, NULL },
2539 GOptionContext* context;
2542 GError *error = NULL;
2544 g_test_bug ("646926");
2546 argv = split_string ("program --known=foo --known=bar --unknown=baz", &argc);
2548 context = g_option_context_new (NULL);
2550 g_option_context_add_main_entries (context, entries, NULL);
2551 g_option_context_set_ignore_unknown_options (context, FALSE);
2552 g_option_context_parse (context, &argc, &argv, &error);
2554 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION);
2555 g_assert_null (text);
2557 g_option_context_free (context);
2558 g_clear_error (&error);
2570 g_setenv ("LC_ALL", "C", TRUE);
2571 g_test_init (&argc, &argv, NULL);
2573 g_test_bug_base ("http://bugzilla.gnome.org/");
2575 g_test_add_func ("/option/help/options", test_help);
2576 g_test_add_func ("/option/help/no-options", test_help_no_options);
2577 g_test_add_func ("/option/help/no-help-options", test_help_no_help_options);
2579 g_test_add_func ("/option/basic", test_basic);
2580 g_test_add_func ("/option/translate", test_translate);
2582 g_test_add_func ("/option/group/captions", test_group_captions);
2583 for (i = 0; i < 4; i++)
2585 test_name = g_strdup_printf ("/option/group/captions/subprocess/help-%d", i);
2586 g_test_add_data_func (test_name, GINT_TO_POINTER (i),
2587 test_group_captions_help);
2589 test_name = g_strdup_printf ("/option/group/captions/subprocess/help-all-%d", i);
2590 g_test_add_data_func (test_name, GINT_TO_POINTER (i),
2591 test_group_captions_help_all);
2593 test_name = g_strdup_printf ("/option/group/captions/subprocess/help-test-%d", i);
2594 g_test_add_data_func (test_name, GINT_TO_POINTER (i),
2595 test_group_captions_help_test);
2600 g_test_add_func ("/option/group/main", test_main_group);
2601 g_test_add_func ("/option/group/error-hook", test_error_hook);
2602 g_test_add_func ("/option/group/parse", test_group_parse);
2603 g_test_add_func ("/option/strict-posix", test_strict_posix);
2605 /* Test that restoration on failure works */
2606 g_test_add_func ("/option/restoration/int", error_test1);
2607 g_test_add_func ("/option/restoration/string", error_test2);
2608 g_test_add_func ("/option/restoration/boolean", error_test3);
2610 /* Test that special argument parsing works */
2611 g_test_add_func ("/option/arg/repetition/int", arg_test1);
2612 g_test_add_func ("/option/arg/repetition/string", arg_test2);
2613 g_test_add_func ("/option/arg/repetition/filename", arg_test3);
2614 g_test_add_func ("/option/arg/repetition/double", arg_test4);
2615 g_test_add_func ("/option/arg/repetition/locale", arg_test5);
2616 g_test_add_func ("/option/arg/repetition/int64", arg_test6);
2618 /* Test string arrays */
2619 g_test_add_func ("/option/arg/array/string", array_test1);
2621 /* Test callback args */
2622 g_test_add_func ("/option/arg/callback/string", callback_test1);
2623 g_test_add_func ("/option/arg/callback/count", callback_test2);
2625 /* Test optional arg flag for callback */
2626 g_test_add_func ("/option/arg/callback/optional1", callback_test_optional_1);
2627 g_test_add_func ("/option/arg/callback/optional2", callback_test_optional_2);
2628 g_test_add_func ("/option/arg/callback/optional3", callback_test_optional_3);
2629 g_test_add_func ("/option/arg/callback/optional4", callback_test_optional_4);
2630 g_test_add_func ("/option/arg/callback/optional5", callback_test_optional_5);
2631 g_test_add_func ("/option/arg/callback/optional6", callback_test_optional_6);
2632 g_test_add_func ("/option/arg/callback/optional7", callback_test_optional_7);
2633 g_test_add_func ("/option/arg/callback/optional8", callback_test_optional_8);
2635 /* Test callback with G_OPTION_REMAINING */
2636 g_test_add_func ("/option/arg/remaining/callback", callback_remaining_test1);
2638 /* Test callbacks which return FALSE */
2639 g_test_add_func ("/option/arg/remaining/callback-false", callback_returns_false);
2641 /* Test ignoring options */
2642 g_test_add_func ("/option/arg/ignore/long", ignore_test1);
2643 g_test_add_func ("/option/arg/ignore/short", ignore_test2);
2644 g_test_add_func ("/option/arg/ignore/arg", ignore_test3);
2645 g_test_add_func ("/option/context/add", add_test1);
2647 /* Test parsing empty args */
2648 /* Note there used to be an empty1 here, but it effectively moved
2649 * to option-argv0.c.
2651 g_test_add_func ("/option/context/empty2", empty_test2);
2652 g_test_add_func ("/option/context/empty3", empty_test3);
2654 /* Test handling of rest args */
2655 g_test_add_func ("/option/arg/rest/non-option", rest_test1);
2656 g_test_add_func ("/option/arg/rest/separator1", rest_test2);
2657 g_test_add_func ("/option/arg/rest/separator2", rest_test2a);
2658 g_test_add_func ("/option/arg/rest/separator3", rest_test2b);
2659 g_test_add_func ("/option/arg/rest/separator4", rest_test2c);
2660 g_test_add_func ("/option/arg/rest/separator5", rest_test2d);
2661 g_test_add_func ("/option/arg/remaining/non-option", rest_test3);
2662 g_test_add_func ("/option/arg/remaining/separator", rest_test4);
2663 g_test_add_func ("/option/arg/remaining/array", rest_test5);
2665 /* Test some invalid flag combinations */
2666 g_test_add_func ("/option/arg/reverse-string", flag_reverse_string);
2667 g_test_add_func ("/option/arg/optional-int", flag_optional_int);
2669 /* regression tests for individual bugs */
2670 g_test_add_func ("/option/bug/unknown-short", unknown_short_test);
2671 g_test_add_func ("/option/bug/lonely-dash", lonely_dash_test);
2672 g_test_add_func ("/option/bug/triple-dash", triple_dash_test);
2673 g_test_add_func ("/option/bug/missing-arg", missing_arg_test);
2674 g_test_add_func ("/option/bug/dash-arg", dash_arg_test);
2675 g_test_add_func ("/option/bug/short-remaining", short_remaining);
2676 g_test_add_func ("/option/bug/double-free", double_free);
2678 return g_test_run();