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.
33 gchar *help_variants[] = { "--help", "--help-all", "--help-test" };
35 GOptionEntry main_entries[] = {
38 G_OPTION_ARG_NONE, NULL,
39 "A switch that is in the main group", NULL },
43 GOptionEntry group_entries[] = {
46 G_OPTION_ARG_NONE, NULL,
47 "A switch that is in the test group", NULL },
53 g_test_bug ("504142");
55 for (i = 0; i < 4; ++i)
57 gboolean have_main_entries = (0 != (i & 1));
58 gboolean have_test_entries = (0 != (i & 2));
60 GOptionContext *options;
61 GOptionGroup *group = NULL;
63 options = g_option_context_new (NULL);
65 if (have_main_entries)
66 g_option_context_add_main_entries (options, main_entries, NULL);
67 if (have_test_entries)
69 group = g_option_group_new ("test", "Test Options",
70 "Show all test options",
72 g_option_context_add_group (options, group);
73 g_option_group_add_entries (group, group_entries);
76 for (j = 0; j < G_N_ELEMENTS (help_variants); ++j)
78 GTestTrapFlags trap_flags = 0;
82 args[1] = help_variants[j];
85 if (!g_test_verbose ())
86 trap_flags |= G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR;
88 g_test_message ("test setup: args='%s', main-entries=%d, test-entries=%d",
89 args[1], have_main_entries, have_test_entries);
91 if (g_test_trap_fork (0, trap_flags))
97 g_setenv ("LANG", "C", TRUE);
99 g_option_context_parse (options, &argc, &argv, &error);
104 gboolean expect_main_description = FALSE;
105 gboolean expect_main_switch = FALSE;
107 gboolean expect_test_description = FALSE;
108 gboolean expect_test_switch = FALSE;
109 gboolean expect_test_group = FALSE;
111 g_test_trap_assert_passed ();
112 g_test_trap_assert_stderr ("");
117 g_assert_cmpstr ("--help", ==, args[1]);
118 expect_main_switch = have_main_entries;
119 expect_test_group = have_test_entries;
123 g_assert_cmpstr ("--help-all", ==, args[1]);
124 expect_main_switch = have_main_entries;
125 expect_test_switch = have_test_entries;
126 expect_test_group = have_test_entries;
130 g_assert_cmpstr ("--help-test", ==, args[1]);
131 expect_test_switch = have_test_entries;
135 g_assert_not_reached ();
139 expect_main_description |= expect_main_switch;
140 expect_test_description |= expect_test_switch;
142 if (expect_main_description)
143 g_test_trap_assert_stdout ("*Application Options*");
145 g_test_trap_assert_stdout_unmatched ("*Application Options*");
146 if (expect_main_switch)
147 g_test_trap_assert_stdout ("*--main-switch*");
149 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
151 if (expect_test_description)
152 g_test_trap_assert_stdout ("*Test Options*");
154 g_test_trap_assert_stdout_unmatched ("*Test Options*");
155 if (expect_test_switch)
156 g_test_trap_assert_stdout ("*--test-switch*");
158 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
160 if (expect_test_group)
161 g_test_trap_assert_stdout ("*--help-test*");
163 g_test_trap_assert_stdout_unmatched ("*--help-test*");
170 char *error_test2_string;
171 gboolean error_test3_boolean;
174 gchar *arg_test2_string;
175 gchar *arg_test3_filename;
176 gdouble arg_test4_double;
177 gdouble arg_test5_double;
178 gint64 arg_test6_int64;
179 gint64 arg_test6_int64_2;
181 gchar *callback_test1_string;
182 int callback_test2_int;
184 gchar *callback_test_optional_string;
185 gboolean callback_test_optional_boolean;
187 gchar **array_test1_array;
189 gboolean ignore_test1_boolean;
190 gboolean ignore_test2_boolean;
191 gchar *ignore_test3_string;
194 split_string (const char *str, int *argc)
199 argv = g_strsplit (str, " ", 0);
201 for (len = 0; argv[len] != NULL; len++);
210 join_stringv (int argc, char **argv)
215 str = g_string_new (NULL);
217 for (i = 0; i < argc; i++)
219 g_string_append (str, argv[i]);
222 g_string_append_c (str, ' ');
225 return g_string_free (str, FALSE);
228 /* Performs a shallow copy */
230 copy_stringv (char **argv, int argc)
232 return g_memdup (argv, sizeof (char *) * (argc + 1));
237 error_test1_pre_parse (GOptionContext *context,
242 g_assert (error_test1_int == 0x12345678);
248 error_test1_post_parse (GOptionContext *context,
253 g_assert (error_test1_int == 20);
255 /* Set an error in the post hook */
256 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
264 GOptionContext *context;
266 GError *error = NULL;
269 GOptionGroup *main_group;
270 GOptionEntry entries [] =
271 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
274 error_test1_int = 0x12345678;
276 context = g_option_context_new (NULL);
277 g_option_context_add_main_entries (context, entries, NULL);
279 /* Set pre and post parse hooks */
280 main_group = g_option_context_get_main_group (context);
281 g_option_group_set_parse_hooks (main_group,
282 error_test1_pre_parse, error_test1_post_parse);
284 /* Now try parsing */
285 argv = split_string ("program --test 20", &argc);
287 retval = g_option_context_parse (context, &argc, &argv, &error);
288 g_assert (retval == FALSE);
290 /* On failure, values should be reset */
291 g_assert (error_test1_int == 0x12345678);
294 g_option_context_free (context);
298 error_test2_pre_parse (GOptionContext *context,
303 g_assert (strcmp (error_test2_string, "foo") == 0);
309 error_test2_post_parse (GOptionContext *context,
314 g_assert (strcmp (error_test2_string, "bar") == 0);
316 /* Set an error in the post hook */
317 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
325 GOptionContext *context;
327 GError *error = NULL;
330 GOptionGroup *main_group;
331 GOptionEntry entries [] =
332 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
335 error_test2_string = "foo";
337 context = g_option_context_new (NULL);
338 g_option_context_add_main_entries (context, entries, NULL);
340 /* Set pre and post parse hooks */
341 main_group = g_option_context_get_main_group (context);
342 g_option_group_set_parse_hooks (main_group,
343 error_test2_pre_parse, error_test2_post_parse);
345 /* Now try parsing */
346 argv = split_string ("program --test bar", &argc);
347 retval = g_option_context_parse (context, &argc, &argv, &error);
349 g_error_free (error);
350 g_assert (retval == FALSE);
352 g_assert (strcmp (error_test2_string, "foo") == 0);
355 g_option_context_free (context);
359 error_test3_pre_parse (GOptionContext *context,
364 g_assert (!error_test3_boolean);
370 error_test3_post_parse (GOptionContext *context,
375 g_assert (error_test3_boolean);
377 /* Set an error in the post hook */
378 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
386 GOptionContext *context;
388 GError *error = NULL;
391 GOptionGroup *main_group;
392 GOptionEntry entries [] =
393 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
396 error_test3_boolean = FALSE;
398 context = g_option_context_new (NULL);
399 g_option_context_add_main_entries (context, entries, NULL);
401 /* Set pre and post parse hooks */
402 main_group = g_option_context_get_main_group (context);
403 g_option_group_set_parse_hooks (main_group,
404 error_test3_pre_parse, error_test3_post_parse);
406 /* Now try parsing */
407 argv = split_string ("program --test", &argc);
408 retval = g_option_context_parse (context, &argc, &argv, &error);
410 g_error_free (error);
411 g_assert (retval == FALSE);
413 g_assert (!error_test3_boolean);
416 g_option_context_free (context);
420 assert_no_error (GError *error)
424 fprintf (stderr, "unexpected error: %s, %d, %s\n", g_quark_to_string (error->domain), error->code, error->message);
432 GOptionContext *context;
434 GError *error = NULL;
437 GOptionEntry entries [] =
438 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
441 context = g_option_context_new (NULL);
442 g_option_context_add_main_entries (context, entries, NULL);
444 /* Now try parsing */
445 argv = split_string ("program --test 20 --test 30", &argc);
447 retval = g_option_context_parse (context, &argc, &argv, &error);
448 assert_no_error (error);
451 /* Last arg specified is the one that should be stored */
452 g_assert (arg_test1_int == 30);
455 g_option_context_free (context);
461 GOptionContext *context;
463 GError *error = NULL;
466 GOptionEntry entries [] =
467 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
470 context = g_option_context_new (NULL);
471 g_option_context_add_main_entries (context, entries, NULL);
473 /* Now try parsing */
474 argv = split_string ("program --test foo --test bar", &argc);
476 retval = g_option_context_parse (context, &argc, &argv, &error);
477 assert_no_error (error);
480 /* Last arg specified is the one that should be stored */
481 g_assert (strcmp (arg_test2_string, "bar") == 0);
483 g_free (arg_test2_string);
486 g_option_context_free (context);
492 GOptionContext *context;
494 GError *error = NULL;
497 GOptionEntry entries [] =
498 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
501 context = g_option_context_new (NULL);
502 g_option_context_add_main_entries (context, entries, NULL);
504 /* Now try parsing */
505 argv = split_string ("program --test foo.txt", &argc);
507 retval = g_option_context_parse (context, &argc, &argv, &error);
508 assert_no_error (error);
511 /* Last arg specified is the one that should be stored */
512 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
514 g_free (arg_test3_filename);
517 g_option_context_free (context);
524 GOptionContext *context;
526 GError *error = NULL;
529 GOptionEntry entries [] =
530 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, NULL, NULL },
533 context = g_option_context_new (NULL);
534 g_option_context_add_main_entries (context, entries, NULL);
536 /* Now try parsing */
537 argv = split_string ("program --test 20.0 --test 30.03", &argc);
539 retval = g_option_context_parse (context, &argc, &argv, &error);
540 assert_no_error (error);
543 /* Last arg specified is the one that should be stored */
544 g_assert (arg_test4_double == 30.03);
547 g_option_context_free (context);
553 GOptionContext *context;
555 GError *error = NULL;
558 char *old_locale, *current_locale;
559 const char *locale = "de_DE";
560 GOptionEntry entries [] =
561 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test5_double, NULL, NULL },
564 context = g_option_context_new (NULL);
565 g_option_context_add_main_entries (context, entries, NULL);
567 /* Now try parsing */
568 argv = split_string ("program --test 20,0 --test 30,03", &argc);
570 /* set it to some locale that uses commas instead of decimal points */
572 old_locale = g_strdup (setlocale (LC_NUMERIC, locale));
573 current_locale = setlocale (LC_NUMERIC, NULL);
574 if (strcmp (current_locale, locale) != 0)
576 fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
580 retval = g_option_context_parse (context, &argc, &argv, &error);
581 assert_no_error (error);
584 /* Last arg specified is the one that should be stored */
585 g_assert (arg_test5_double == 30.03);
588 setlocale (LC_NUMERIC, old_locale);
592 g_option_context_free (context);
598 GOptionContext *context;
600 GError *error = NULL;
603 GOptionEntry entries [] =
604 { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL },
605 { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, NULL, NULL },
608 context = g_option_context_new (NULL);
609 g_option_context_add_main_entries (context, entries, NULL);
611 /* Now try parsing */
612 argv = split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc);
614 retval = g_option_context_parse (context, &argc, &argv, &error);
615 assert_no_error (error);
618 /* Last arg specified is the one that should be stored */
619 g_assert (arg_test6_int64 == G_GINT64_CONSTANT(4294967296));
620 g_assert (arg_test6_int64_2 == G_GINT64_CONSTANT(0xfffffffff));
623 g_option_context_free (context);
627 callback_parse1 (const gchar *option_name, const gchar *value,
628 gpointer data, GError **error)
630 callback_test1_string = g_strdup (value);
635 callback_test1 (void)
637 GOptionContext *context;
639 GError *error = NULL;
642 GOptionEntry entries [] =
643 { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL },
646 context = g_option_context_new (NULL);
647 g_option_context_add_main_entries (context, entries, NULL);
649 /* Now try parsing */
650 argv = split_string ("program --test foo.txt", &argc);
652 retval = g_option_context_parse (context, &argc, &argv, &error);
653 assert_no_error (error);
656 g_assert (strcmp (callback_test1_string, "foo.txt") == 0);
658 g_free (callback_test1_string);
661 g_option_context_free (context);
665 callback_parse2 (const gchar *option_name, const gchar *value,
666 gpointer data, GError **error)
668 callback_test2_int++;
673 callback_test2 (void)
675 GOptionContext *context;
677 GError *error = NULL;
680 GOptionEntry entries [] =
681 { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL },
684 context = g_option_context_new (NULL);
685 g_option_context_add_main_entries (context, entries, NULL);
687 /* Now try parsing */
688 argv = split_string ("program --test --test", &argc);
690 retval = g_option_context_parse (context, &argc, &argv, &error);
691 assert_no_error (error);
694 g_assert (callback_test2_int == 2);
697 g_option_context_free (context);
701 callback_parse_optional (const gchar *option_name, const gchar *value,
702 gpointer data, GError **error)
704 callback_test_optional_boolean = TRUE;
706 callback_test_optional_string = g_strdup (value);
708 callback_test_optional_string = NULL;
713 callback_test_optional_1 (void)
715 GOptionContext *context;
717 GError *error = NULL;
720 GOptionEntry entries [] =
721 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
722 callback_parse_optional, NULL, NULL },
725 context = g_option_context_new (NULL);
726 g_option_context_add_main_entries (context, entries, NULL);
728 /* Now try parsing */
729 argv = split_string ("program --test foo.txt", &argc);
731 retval = g_option_context_parse (context, &argc, &argv, &error);
732 assert_no_error (error);
735 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
737 g_assert (callback_test_optional_boolean);
739 g_free (callback_test_optional_string);
742 g_option_context_free (context);
746 callback_test_optional_2 (void)
748 GOptionContext *context;
750 GError *error = NULL;
753 GOptionEntry entries [] =
754 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
755 callback_parse_optional, NULL, NULL },
758 context = g_option_context_new (NULL);
759 g_option_context_add_main_entries (context, entries, NULL);
761 /* Now try parsing */
762 argv = split_string ("program --test", &argc);
764 retval = g_option_context_parse (context, &argc, &argv, &error);
765 assert_no_error (error);
768 g_assert (callback_test_optional_string == NULL);
770 g_assert (callback_test_optional_boolean);
772 g_free (callback_test_optional_string);
775 g_option_context_free (context);
779 callback_test_optional_3 (void)
781 GOptionContext *context;
783 GError *error = NULL;
786 GOptionEntry entries [] =
787 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
788 callback_parse_optional, NULL, NULL },
791 context = g_option_context_new (NULL);
792 g_option_context_add_main_entries (context, entries, NULL);
794 /* Now try parsing */
795 argv = split_string ("program -t foo.txt", &argc);
797 retval = g_option_context_parse (context, &argc, &argv, &error);
798 assert_no_error (error);
801 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
803 g_assert (callback_test_optional_boolean);
805 g_free (callback_test_optional_string);
808 g_option_context_free (context);
813 callback_test_optional_4 (void)
815 GOptionContext *context;
817 GError *error = NULL;
820 GOptionEntry entries [] =
821 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
822 callback_parse_optional, NULL, NULL },
825 context = g_option_context_new (NULL);
826 g_option_context_add_main_entries (context, entries, NULL);
828 /* Now try parsing */
829 argv = split_string ("program -t", &argc);
831 retval = g_option_context_parse (context, &argc, &argv, &error);
832 assert_no_error (error);
835 g_assert (callback_test_optional_string == NULL);
837 g_assert (callback_test_optional_boolean);
839 g_free (callback_test_optional_string);
842 g_option_context_free (context);
846 callback_test_optional_5 (void)
848 GOptionContext *context;
851 GError *error = NULL;
854 GOptionEntry entries [] =
855 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
856 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
857 callback_parse_optional, NULL, NULL },
860 context = g_option_context_new (NULL);
861 g_option_context_add_main_entries (context, entries, NULL);
863 /* Now try parsing */
864 argv = split_string ("program --test --dummy", &argc);
866 retval = g_option_context_parse (context, &argc, &argv, &error);
867 assert_no_error (error);
870 g_assert (callback_test_optional_string == NULL);
872 g_assert (callback_test_optional_boolean);
874 g_free (callback_test_optional_string);
877 g_option_context_free (context);
881 callback_test_optional_6 (void)
883 GOptionContext *context;
886 GError *error = NULL;
889 GOptionEntry entries [] =
890 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
891 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
892 callback_parse_optional, NULL, NULL },
895 context = g_option_context_new (NULL);
896 g_option_context_add_main_entries (context, entries, NULL);
898 /* Now try parsing */
899 argv = split_string ("program -t -d", &argc);
901 retval = g_option_context_parse (context, &argc, &argv, &error);
902 assert_no_error (error);
905 g_assert (callback_test_optional_string == NULL);
907 g_assert (callback_test_optional_boolean);
909 g_free (callback_test_optional_string);
912 g_option_context_free (context);
916 callback_test_optional_7 (void)
918 GOptionContext *context;
921 GError *error = NULL;
924 GOptionEntry entries [] =
925 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
926 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
927 callback_parse_optional, NULL, NULL },
930 context = g_option_context_new (NULL);
931 g_option_context_add_main_entries (context, entries, NULL);
933 /* Now try parsing */
934 argv = split_string ("program -td", &argc);
936 retval = g_option_context_parse (context, &argc, &argv, &error);
937 assert_no_error (error);
940 g_assert (callback_test_optional_string == NULL);
942 g_assert (callback_test_optional_boolean);
944 g_free (callback_test_optional_string);
947 g_option_context_free (context);
951 callback_test_optional_8 (void)
953 GOptionContext *context;
956 GError *error = NULL;
959 GOptionEntry entries [] =
960 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
961 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
962 callback_parse_optional, NULL, NULL },
965 context = g_option_context_new (NULL);
966 g_option_context_add_main_entries (context, entries, NULL);
968 /* Now try parsing */
969 argv = split_string ("program -dt foo.txt", &argc);
971 retval = g_option_context_parse (context, &argc, &argv, &error);
972 assert_no_error (error);
975 g_assert (callback_test_optional_string);
977 g_assert (callback_test_optional_boolean);
979 g_free (callback_test_optional_string);
982 g_option_context_free (context);
985 static GPtrArray *callback_remaining_args;
987 callback_remaining_test1_callback (const gchar *option_name, const gchar *value,
988 gpointer data, GError **error)
990 g_ptr_array_add (callback_remaining_args, g_strdup (value));
995 callback_remaining_test1 (void)
997 GOptionContext *context;
999 GError *error = NULL;
1002 GOptionEntry entries [] =
1003 { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL },
1006 callback_remaining_args = g_ptr_array_new ();
1007 context = g_option_context_new (NULL);
1008 g_option_context_add_main_entries (context, entries, NULL);
1010 /* Now try parsing */
1011 argv = split_string ("program foo.txt blah.txt", &argc);
1013 retval = g_option_context_parse (context, &argc, &argv, &error);
1014 assert_no_error (error);
1017 g_assert (callback_remaining_args->len == 2);
1018 g_assert (strcmp (callback_remaining_args->pdata[0], "foo.txt") == 0);
1019 g_assert (strcmp (callback_remaining_args->pdata[1], "blah.txt") == 0);
1021 g_ptr_array_foreach (callback_remaining_args, (GFunc) g_free, NULL);
1022 g_ptr_array_free (callback_remaining_args, TRUE);
1025 g_option_context_free (context);
1031 GOptionContext *context;
1033 GError *error = NULL;
1034 gchar **argv, **argv_copy;
1037 GOptionEntry entries [] =
1038 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1041 context = g_option_context_new (NULL);
1042 g_option_context_set_ignore_unknown_options (context, TRUE);
1043 g_option_context_add_main_entries (context, entries, NULL);
1045 /* Now try parsing */
1046 argv = split_string ("program --test --hello", &argc);
1047 argv_copy = copy_stringv (argv, argc);
1049 retval = g_option_context_parse (context, &argc, &argv, &error);
1050 assert_no_error (error);
1054 arg = join_stringv (argc, argv);
1055 g_assert (strcmp (arg, "program --hello") == 0);
1058 g_strfreev (argv_copy);
1060 g_option_context_free (context);
1066 GOptionContext *context;
1068 GError *error = NULL;
1072 GOptionEntry entries [] =
1073 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
1076 context = g_option_context_new (NULL);
1077 g_option_context_set_ignore_unknown_options (context, TRUE);
1078 g_option_context_add_main_entries (context, entries, NULL);
1080 /* Now try parsing */
1081 argv = split_string ("program -test", &argc);
1083 retval = g_option_context_parse (context, &argc, &argv, &error);
1084 assert_no_error (error);
1088 arg = join_stringv (argc, argv);
1089 g_assert (strcmp (arg, "program -es") == 0);
1093 g_option_context_free (context);
1099 GOptionContext *context;
1101 GError *error = NULL;
1102 gchar **argv, **argv_copy;
1105 GOptionEntry entries [] =
1106 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
1109 context = g_option_context_new (NULL);
1110 g_option_context_set_ignore_unknown_options (context, TRUE);
1111 g_option_context_add_main_entries (context, entries, NULL);
1113 /* Now try parsing */
1114 argv = split_string ("program --test foo --hello", &argc);
1115 argv_copy = copy_stringv (argv, argc);
1117 retval = g_option_context_parse (context, &argc, &argv, &error);
1118 assert_no_error (error);
1122 arg = join_stringv (argc, argv);
1123 g_assert (strcmp (arg, "program --hello") == 0);
1125 g_assert (strcmp (ignore_test3_string, "foo") == 0);
1126 g_free (ignore_test3_string);
1129 g_strfreev (argv_copy);
1131 g_option_context_free (context);
1137 GOptionContext *context;
1139 GError *error = NULL;
1142 GOptionEntry entries [] =
1143 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1146 context = g_option_context_new (NULL);
1147 g_option_context_add_main_entries (context, entries, NULL);
1149 /* Now try parsing */
1150 argv = split_string ("program --test foo --test bar", &argc);
1152 retval = g_option_context_parse (context, &argc, &argv, &error);
1153 assert_no_error (error);
1157 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1158 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1159 g_assert (array_test1_array[2] == NULL);
1161 g_strfreev (array_test1_array);
1164 g_option_context_free (context);
1170 GOptionContext *context;
1172 GOptionEntry entries1 [] =
1173 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1175 GOptionEntry entries2 [] =
1176 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1179 context = g_option_context_new (NULL);
1180 g_option_context_add_main_entries (context, entries1, NULL);
1181 g_option_context_add_main_entries (context, entries2, NULL);
1183 g_option_context_free (context);
1189 GOptionContext *context;
1190 GOptionEntry entries [] =
1194 g_set_prgname (NULL);
1195 context = g_option_context_new (NULL);
1197 g_option_context_add_main_entries (context, entries, NULL);
1199 g_option_context_parse (context, NULL, NULL, NULL);
1201 prgname = g_get_prgname ();
1202 g_assert (prgname && strcmp (prgname, "<unknown>") == 0);
1204 g_option_context_free (context);
1210 GOptionContext *context;
1212 context = g_option_context_new (NULL);
1213 g_option_context_parse (context, NULL, NULL, NULL);
1215 g_option_context_free (context);
1221 GOptionContext *context;
1228 context = g_option_context_new (NULL);
1229 g_option_context_parse (context, &argc, &argv, NULL);
1231 g_option_context_free (context);
1234 /* check that non-option arguments are left in argv by default */
1238 GOptionContext *context;
1240 GError *error = NULL;
1243 GOptionEntry entries [] = {
1244 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1248 context = g_option_context_new (NULL);
1249 g_option_context_add_main_entries (context, entries, NULL);
1251 /* Now try parsing */
1252 argv = split_string ("program foo --test bar", &argc);
1254 retval = g_option_context_parse (context, &argc, &argv, &error);
1255 assert_no_error (error);
1259 g_assert (ignore_test1_boolean);
1260 g_assert (strcmp (argv[0], "program") == 0);
1261 g_assert (strcmp (argv[1], "foo") == 0);
1262 g_assert (strcmp (argv[2], "bar") == 0);
1263 g_assert (argv[3] == NULL);
1266 g_option_context_free (context);
1269 /* check that -- works */
1273 GOptionContext *context;
1275 GError *error = NULL;
1278 GOptionEntry entries [] = {
1279 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1283 context = g_option_context_new (NULL);
1284 g_option_context_add_main_entries (context, entries, NULL);
1286 /* Now try parsing */
1287 argv = split_string ("program foo --test -- -bar", &argc);
1289 retval = g_option_context_parse (context, &argc, &argv, &error);
1290 assert_no_error (error);
1294 g_assert (ignore_test1_boolean);
1295 g_assert (strcmp (argv[0], "program") == 0);
1296 g_assert (strcmp (argv[1], "foo") == 0);
1297 g_assert (strcmp (argv[2], "--") == 0);
1298 g_assert (strcmp (argv[3], "-bar") == 0);
1299 g_assert (argv[4] == NULL);
1302 g_option_context_free (context);
1305 /* check that -- stripping works */
1309 GOptionContext *context;
1311 GError *error = NULL;
1314 GOptionEntry entries [] = {
1315 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1319 context = g_option_context_new (NULL);
1320 g_option_context_add_main_entries (context, entries, NULL);
1322 /* Now try parsing */
1323 argv = split_string ("program foo --test -- bar", &argc);
1325 retval = g_option_context_parse (context, &argc, &argv, &error);
1326 assert_no_error (error);
1330 g_assert (ignore_test1_boolean);
1331 g_assert (strcmp (argv[0], "program") == 0);
1332 g_assert (strcmp (argv[1], "foo") == 0);
1333 g_assert (strcmp (argv[2], "bar") == 0);
1334 g_assert (argv[3] == NULL);
1337 g_option_context_free (context);
1343 GOptionContext *context;
1345 GError *error = NULL;
1348 GOptionEntry entries [] = {
1349 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1353 context = g_option_context_new (NULL);
1354 g_option_context_set_ignore_unknown_options (context, TRUE);
1355 g_option_context_add_main_entries (context, entries, NULL);
1357 /* Now try parsing */
1358 argv = split_string ("program foo --test -bar --", &argc);
1360 retval = g_option_context_parse (context, &argc, &argv, &error);
1361 assert_no_error (error);
1365 g_assert (ignore_test1_boolean);
1366 g_assert (strcmp (argv[0], "program") == 0);
1367 g_assert (strcmp (argv[1], "foo") == 0);
1368 g_assert (strcmp (argv[2], "-bar") == 0);
1369 g_assert (argv[3] == NULL);
1372 g_option_context_free (context);
1378 GOptionContext *context;
1380 GError *error = NULL;
1383 GOptionEntry entries [] = {
1384 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1388 context = g_option_context_new (NULL);
1389 g_option_context_add_main_entries (context, entries, NULL);
1391 /* Now try parsing */
1392 argv = split_string ("program --test foo -- bar", &argc);
1394 retval = g_option_context_parse (context, &argc, &argv, &error);
1395 assert_no_error (error);
1399 g_assert (ignore_test1_boolean);
1400 g_assert (strcmp (argv[0], "program") == 0);
1401 g_assert (strcmp (argv[1], "foo") == 0);
1402 g_assert (strcmp (argv[2], "bar") == 0);
1403 g_assert (argv[3] == NULL);
1406 g_option_context_free (context);
1412 GOptionContext *context;
1414 GError *error = NULL;
1417 GOptionEntry entries [] = {
1418 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1422 context = g_option_context_new (NULL);
1423 g_option_context_add_main_entries (context, entries, NULL);
1425 /* Now try parsing */
1426 argv = split_string ("program --test -- -bar", &argc);
1428 retval = g_option_context_parse (context, &argc, &argv, &error);
1429 assert_no_error (error);
1433 g_assert (ignore_test1_boolean);
1434 g_assert (strcmp (argv[0], "program") == 0);
1435 g_assert (strcmp (argv[1], "--") == 0);
1436 g_assert (strcmp (argv[2], "-bar") == 0);
1437 g_assert (argv[3] == NULL);
1440 g_option_context_free (context);
1444 /* check that G_OPTION_REMAINING collects non-option arguments */
1448 GOptionContext *context;
1450 GError *error = NULL;
1453 GOptionEntry entries [] = {
1454 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1455 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1459 context = g_option_context_new (NULL);
1460 g_option_context_add_main_entries (context, entries, NULL);
1462 /* Now try parsing */
1463 argv = split_string ("program foo --test bar", &argc);
1465 retval = g_option_context_parse (context, &argc, &argv, &error);
1466 assert_no_error (error);
1470 g_assert (ignore_test1_boolean);
1471 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1472 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1473 g_assert (array_test1_array[2] == NULL);
1475 g_strfreev (array_test1_array);
1478 g_option_context_free (context);
1482 /* check that G_OPTION_REMAINING and -- work together */
1486 GOptionContext *context;
1488 GError *error = NULL;
1491 GOptionEntry entries [] = {
1492 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1493 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1497 context = g_option_context_new (NULL);
1498 g_option_context_add_main_entries (context, entries, NULL);
1500 /* Now try parsing */
1501 argv = split_string ("program foo --test -- -bar", &argc);
1503 retval = g_option_context_parse (context, &argc, &argv, &error);
1504 assert_no_error (error);
1508 g_assert (ignore_test1_boolean);
1509 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1510 g_assert (strcmp (array_test1_array[1], "-bar") == 0);
1511 g_assert (array_test1_array[2] == NULL);
1513 g_strfreev (array_test1_array);
1516 g_option_context_free (context);
1519 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1523 GOptionContext *context;
1525 GError *error = NULL;
1528 GOptionEntry entries [] = {
1529 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1530 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
1534 context = g_option_context_new (NULL);
1535 g_option_context_add_main_entries (context, entries, NULL);
1537 /* Now try parsing */
1538 argv = split_string ("program foo --test bar", &argc);
1540 retval = g_option_context_parse (context, &argc, &argv, &error);
1541 assert_no_error (error);
1545 g_assert (ignore_test1_boolean);
1546 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1547 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1548 g_assert (array_test1_array[2] == NULL);
1550 g_strfreev (array_test1_array);
1553 g_option_context_free (context);
1557 unknown_short_test (void)
1559 GOptionContext *context;
1561 GError *error = NULL;
1564 GOptionEntry entries [] = { { NULL } };
1566 g_test_bug ("166609");
1568 context = g_option_context_new (NULL);
1569 g_option_context_add_main_entries (context, entries, NULL);
1571 /* Now try parsing */
1572 argv = split_string ("program -0", &argc);
1574 retval = g_option_context_parse (context, &argc, &argv, &error);
1578 g_option_context_free (context);
1581 /* test that lone dashes are treated as non-options */
1582 void lonely_dash_test (void)
1584 GOptionContext *context;
1586 GError *error = NULL;
1590 g_test_bug ("168008");
1592 context = g_option_context_new (NULL);
1594 /* Now try parsing */
1595 argv = split_string ("program -", &argc);
1597 retval = g_option_context_parse (context, &argc, &argv, &error);
1598 assert_no_error (error);
1601 g_assert (argv[1] && strcmp (argv[1], "-") == 0);
1604 g_option_context_free (context);
1608 missing_arg_test (void)
1610 GOptionContext *context;
1612 GError *error = NULL;
1616 GOptionEntry entries [] =
1617 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1620 g_test_bug ("305576");
1622 context = g_option_context_new (NULL);
1623 g_option_context_add_main_entries (context, entries, NULL);
1625 /* Now try parsing */
1626 argv = split_string ("program --test", &argc);
1628 retval = g_option_context_parse (context, &argc, &argv, &error);
1629 g_assert (retval == FALSE);
1630 g_clear_error (&error);
1634 /* Try parsing again */
1635 argv = split_string ("program --t", &argc);
1637 retval = g_option_context_parse (context, &argc, &argv, &error);
1638 g_assert (retval == FALSE);
1641 g_option_context_free (context);
1648 g_test_init (&argc, &argv, NULL);
1650 g_test_bug_base ("http://bugzilla.gnome.org/");
1651 g_test_add_func ("/group/captions", group_captions);
1653 /* Test that restoration on failure works */
1654 g_test_add_func ("/restoration/int", error_test1);
1655 g_test_add_func ("/restoration/string", error_test2);
1656 g_test_add_func ("/restoration/boolean", error_test3);
1658 /* Test that special argument parsing works */
1659 g_test_add_func ("/arg/repetition/int", arg_test1);
1660 g_test_add_func ("/arg/repetition/string", arg_test2);
1661 g_test_add_func ("/arg/repetition/filename", arg_test3);
1662 g_test_add_func ("/arg/repetition/double", arg_test4);
1663 g_test_add_func ("/arg/repetition/locale", arg_test5);
1664 g_test_add_func ("/arg/repetition/int64", arg_test6);
1666 /* Test string arrays */
1667 g_test_add_func ("/arg/array/string", array_test1);
1669 /* Test callback args */
1670 g_test_add_func ("/arg/callback/string", callback_test1);
1671 g_test_add_func ("/arg/callback/count", callback_test2);
1673 /* Test optional arg flag for callback */
1674 g_test_add_func ("/arg/callback/optional1", callback_test_optional_1);
1675 g_test_add_func ("/arg/callback/optional2", callback_test_optional_2);
1676 g_test_add_func ("/arg/callback/optional3", callback_test_optional_3);
1677 g_test_add_func ("/arg/callback/optional4", callback_test_optional_4);
1678 g_test_add_func ("/arg/callback/optional5", callback_test_optional_5);
1679 g_test_add_func ("/arg/callback/optional6", callback_test_optional_6);
1680 g_test_add_func ("/arg/callback/optional7", callback_test_optional_7);
1681 g_test_add_func ("/arg/callback/optional8", callback_test_optional_8);
1683 /* Test callback with G_OPTION_REMAINING */
1684 g_test_add_func ("/arg/remaining/callback", callback_remaining_test1);
1686 /* Test ignoring options */
1687 g_test_add_func ("/arg/ignore/long", ignore_test1);
1688 g_test_add_func ("/arg/ignore/short", ignore_test2);
1689 g_test_add_func ("/arg/ignore/arg", ignore_test3);
1691 g_test_add_func ("/context/add", add_test1);
1693 /* Test parsing empty args */
1694 g_test_add_func ("/context/empty1", empty_test1);
1695 g_test_add_func ("/context/empty2", empty_test2);
1696 g_test_add_func ("/context/empty3", empty_test3);
1698 /* Test handling of rest args */
1699 g_test_add_func ("/arg/rest/non-option", rest_test1);
1700 g_test_add_func ("/arg/rest/separator1", rest_test2);
1701 g_test_add_func ("/arg/rest/separator2", rest_test2a);
1702 g_test_add_func ("/arg/rest/separator3", rest_test2b);
1703 g_test_add_func ("/arg/rest/separator4", rest_test2c);
1704 g_test_add_func ("/arg/rest/separator5", rest_test2d);
1705 g_test_add_func ("/arg/remaining/non-option", rest_test3);
1706 g_test_add_func ("/arg/remaining/separator", rest_test4);
1707 g_test_add_func ("/arg/remaining/array", rest_test5);
1709 /* regression tests for individual bugs */
1710 g_test_add_func ("/bug/unknown-short", unknown_short_test);
1711 g_test_add_func ("/bug/lonely-dash", lonely_dash_test);
1712 g_test_add_func ("/bug/missing-arg", missing_arg_test);
1714 return g_test_run();