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[] = {
36 { "main-switch", 0, 0,
37 G_OPTION_ARG_NONE, NULL,
38 "A switch that is in the main group", NULL },
42 GOptionEntry group_entries[] = {
43 { "test-switch", 0, 0,
44 G_OPTION_ARG_NONE, NULL,
45 "A switch that is in the test group", NULL },
51 g_test_bug ("504142");
53 for (i = 0; i < 4; ++i)
55 gboolean have_main_entries = (0 != (i & 1));
56 gboolean have_test_entries = (0 != (i & 2));
58 GOptionContext *options;
59 GOptionGroup *group = NULL;
61 options = g_option_context_new (NULL);
63 if (have_main_entries)
64 g_option_context_add_main_entries (options, main_entries, NULL);
65 if (have_test_entries)
67 group = g_option_group_new ("test", "Test Options",
68 "Show all test options",
70 g_option_context_add_group (options, group);
71 g_option_group_add_entries (group, group_entries);
74 for (j = 0; j < G_N_ELEMENTS (help_variants); ++j)
76 GTestTrapFlags trap_flags = 0;
80 args[1] = help_variants[j];
83 if (!g_test_verbose ())
84 trap_flags |= G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR;
86 g_test_message ("test setup: args='%s', main-entries=%d, test-entries=%d",
87 args[1], have_main_entries, have_test_entries);
89 if (g_test_trap_fork (0, trap_flags))
95 g_setenv ("LANG", "C", TRUE);
97 g_option_context_parse (options, &argc, &argv, &error);
98 g_option_context_free (options);
103 gboolean expect_main_description = FALSE;
104 gboolean expect_main_switch = FALSE;
106 gboolean expect_test_description = FALSE;
107 gboolean expect_test_switch = FALSE;
108 gboolean expect_test_group = FALSE;
110 g_test_trap_assert_passed ();
111 g_test_trap_assert_stderr ("");
116 g_assert_cmpstr ("--help", ==, args[1]);
117 expect_main_switch = have_main_entries;
118 expect_test_group = have_test_entries;
122 g_assert_cmpstr ("--help-all", ==, args[1]);
123 expect_main_switch = have_main_entries;
124 expect_test_switch = have_test_entries;
125 expect_test_group = have_test_entries;
129 g_assert_cmpstr ("--help-test", ==, args[1]);
130 expect_test_switch = have_test_entries;
134 g_assert_not_reached ();
138 expect_main_description |= expect_main_switch;
139 expect_test_description |= expect_test_switch;
141 if (expect_main_description)
142 g_test_trap_assert_stdout ("*Application Options*");
144 g_test_trap_assert_stdout_unmatched ("*Application Options*");
145 if (expect_main_switch)
146 g_test_trap_assert_stdout ("*--main-switch*");
148 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
150 if (expect_test_description)
151 g_test_trap_assert_stdout ("*Test Options*");
153 g_test_trap_assert_stdout_unmatched ("*Test Options*");
154 if (expect_test_switch)
155 g_test_trap_assert_stdout ("*--test-switch*");
157 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
159 if (expect_test_group)
160 g_test_trap_assert_stdout ("*--help-test*");
162 g_test_trap_assert_stdout_unmatched ("*--help-test*");
166 g_option_context_free (options);
171 char *error_test2_string;
172 gboolean error_test3_boolean;
175 gchar *arg_test2_string;
176 gchar *arg_test3_filename;
177 gdouble arg_test4_double;
178 gdouble arg_test5_double;
179 gint64 arg_test6_int64;
180 gint64 arg_test6_int64_2;
182 gchar *callback_test1_string;
183 int callback_test2_int;
185 gchar *callback_test_optional_string;
186 gboolean callback_test_optional_boolean;
188 gchar **array_test1_array;
190 gboolean ignore_test1_boolean;
191 gboolean ignore_test2_boolean;
192 gchar *ignore_test3_string;
195 split_string (const char *str, int *argc)
200 argv = g_strsplit (str, " ", 0);
202 for (len = 0; argv[len] != NULL; len++);
211 join_stringv (int argc, char **argv)
216 str = g_string_new (NULL);
218 for (i = 0; i < argc; i++)
220 g_string_append (str, argv[i]);
223 g_string_append_c (str, ' ');
226 return g_string_free (str, FALSE);
229 /* Performs a shallow copy */
231 copy_stringv (char **argv, int argc)
233 return g_memdup (argv, sizeof (char *) * (argc + 1));
237 check_identical_stringv (gchar **before, gchar **after)
241 /* Not only is it the same string... */
242 for (i = 0; before[i] != NULL; i++)
243 g_assert_cmpstr (before[i], ==, after[i]);
245 /* ... it is actually the same pointer */
246 for (i = 0; before[i] != NULL; i++)
247 g_assert (before[i] == after[i]);
249 g_assert (after[i] == NULL);
254 error_test1_pre_parse (GOptionContext *context,
259 g_assert (error_test1_int == 0x12345678);
265 error_test1_post_parse (GOptionContext *context,
270 g_assert (error_test1_int == 20);
272 /* Set an error in the post hook */
273 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
281 GOptionContext *context;
283 GError *error = NULL;
287 GOptionGroup *main_group;
288 GOptionEntry entries [] =
289 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
292 error_test1_int = 0x12345678;
294 context = g_option_context_new (NULL);
295 g_option_context_add_main_entries (context, entries, NULL);
297 /* Set pre and post parse hooks */
298 main_group = g_option_context_get_main_group (context);
299 g_option_group_set_parse_hooks (main_group,
300 error_test1_pre_parse, error_test1_post_parse);
302 /* Now try parsing */
303 argv = split_string ("program --test 20", &argc);
304 argv_copy = copy_stringv (argv, argc);
306 retval = g_option_context_parse (context, &argc, &argv, &error);
307 g_assert (retval == FALSE);
308 g_assert (error != NULL);
309 /* An error occurred, so argv has not been changed */
310 check_identical_stringv (argv_copy, argv);
311 g_clear_error (&error);
313 /* On failure, values should be reset */
314 g_assert (error_test1_int == 0x12345678);
316 g_strfreev (argv_copy);
318 g_option_context_free (context);
322 error_test2_pre_parse (GOptionContext *context,
327 g_assert (strcmp (error_test2_string, "foo") == 0);
333 error_test2_post_parse (GOptionContext *context,
338 g_assert (strcmp (error_test2_string, "bar") == 0);
340 /* Set an error in the post hook */
341 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
349 GOptionContext *context;
351 GError *error = NULL;
355 GOptionGroup *main_group;
356 GOptionEntry entries [] =
357 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
360 error_test2_string = "foo";
362 context = g_option_context_new (NULL);
363 g_option_context_add_main_entries (context, entries, NULL);
365 /* Set pre and post parse hooks */
366 main_group = g_option_context_get_main_group (context);
367 g_option_group_set_parse_hooks (main_group,
368 error_test2_pre_parse, error_test2_post_parse);
370 /* Now try parsing */
371 argv = split_string ("program --test bar", &argc);
372 argv_copy = copy_stringv (argv, argc);
373 retval = g_option_context_parse (context, &argc, &argv, &error);
375 g_assert (retval == FALSE);
376 g_assert (error != NULL);
377 check_identical_stringv (argv_copy, argv);
378 g_clear_error (&error);
380 g_assert (strcmp (error_test2_string, "foo") == 0);
382 g_strfreev (argv_copy);
384 g_option_context_free (context);
388 error_test3_pre_parse (GOptionContext *context,
393 g_assert (!error_test3_boolean);
399 error_test3_post_parse (GOptionContext *context,
404 g_assert (error_test3_boolean);
406 /* Set an error in the post hook */
407 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
415 GOptionContext *context;
417 GError *error = NULL;
421 GOptionGroup *main_group;
422 GOptionEntry entries [] =
423 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
426 error_test3_boolean = FALSE;
428 context = g_option_context_new (NULL);
429 g_option_context_add_main_entries (context, entries, NULL);
431 /* Set pre and post parse hooks */
432 main_group = g_option_context_get_main_group (context);
433 g_option_group_set_parse_hooks (main_group,
434 error_test3_pre_parse, error_test3_post_parse);
436 /* Now try parsing */
437 argv = split_string ("program --test", &argc);
438 argv_copy = copy_stringv (argv, argc);
439 retval = g_option_context_parse (context, &argc, &argv, &error);
441 g_assert (retval == FALSE);
442 g_assert (error != NULL);
443 check_identical_stringv (argv_copy, argv);
444 g_clear_error (&error);
446 g_assert (!error_test3_boolean);
448 g_strfreev (argv_copy);
450 g_option_context_free (context);
456 GOptionContext *context;
458 GError *error = NULL;
462 GOptionEntry entries [] =
463 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
466 context = g_option_context_new (NULL);
467 g_option_context_add_main_entries (context, entries, NULL);
469 /* Now try parsing */
470 argv = split_string ("program --test 20 --test 30", &argc);
471 argv_copy = copy_stringv (argv, argc);
473 retval = g_option_context_parse (context, &argc, &argv, &error);
474 g_assert_no_error (error);
477 /* Last arg specified is the one that should be stored */
478 g_assert (arg_test1_int == 30);
480 /* We free all of the strings in a copy of argv, because now argv is a
481 * subset - some have been removed in-place
483 g_strfreev (argv_copy);
485 g_option_context_free (context);
491 GOptionContext *context;
493 GError *error = NULL;
497 GOptionEntry entries [] =
498 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, 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 --test bar", &argc);
506 argv_copy = copy_stringv (argv, argc);
508 retval = g_option_context_parse (context, &argc, &argv, &error);
509 g_assert_no_error (error);
512 /* Last arg specified is the one that should be stored */
513 g_assert (strcmp (arg_test2_string, "bar") == 0);
515 g_free (arg_test2_string);
517 g_strfreev (argv_copy);
519 g_option_context_free (context);
525 GOptionContext *context;
527 GError *error = NULL;
531 GOptionEntry entries [] =
532 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
535 context = g_option_context_new (NULL);
536 g_option_context_add_main_entries (context, entries, NULL);
538 /* Now try parsing */
539 argv = split_string ("program --test foo.txt", &argc);
540 argv_copy = copy_stringv (argv, argc);
542 retval = g_option_context_parse (context, &argc, &argv, &error);
543 g_assert_no_error (error);
546 /* Last arg specified is the one that should be stored */
547 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
549 g_free (arg_test3_filename);
551 g_strfreev (argv_copy);
553 g_option_context_free (context);
560 GOptionContext *context;
562 GError *error = NULL;
566 GOptionEntry entries [] =
567 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, NULL, NULL },
570 context = g_option_context_new (NULL);
571 g_option_context_add_main_entries (context, entries, NULL);
573 /* Now try parsing */
574 argv = split_string ("program --test 20.0 --test 30.03", &argc);
575 argv_copy = copy_stringv (argv, argc);
577 retval = g_option_context_parse (context, &argc, &argv, &error);
578 g_assert_no_error (error);
581 /* Last arg specified is the one that should be stored */
582 g_assert (arg_test4_double == 30.03);
584 g_strfreev (argv_copy);
586 g_option_context_free (context);
592 GOptionContext *context;
594 GError *error = NULL;
598 char *old_locale, *current_locale;
599 const char *locale = "de_DE";
600 GOptionEntry entries [] =
601 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test5_double, NULL, NULL },
604 context = g_option_context_new (NULL);
605 g_option_context_add_main_entries (context, entries, NULL);
607 /* Now try parsing */
608 argv = split_string ("program --test 20,0 --test 30,03", &argc);
609 argv_copy = copy_stringv (argv, argc);
611 /* set it to some locale that uses commas instead of decimal points */
613 old_locale = g_strdup (setlocale (LC_NUMERIC, locale));
614 current_locale = setlocale (LC_NUMERIC, NULL);
615 if (strcmp (current_locale, locale) != 0)
617 fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
621 retval = g_option_context_parse (context, &argc, &argv, &error);
622 g_assert_no_error (error);
625 /* Last arg specified is the one that should be stored */
626 g_assert (arg_test5_double == 30.03);
629 setlocale (LC_NUMERIC, old_locale);
632 g_strfreev (argv_copy);
634 g_option_context_free (context);
640 GOptionContext *context;
642 GError *error = NULL;
646 GOptionEntry entries [] =
647 { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL },
648 { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, NULL, NULL },
651 context = g_option_context_new (NULL);
652 g_option_context_add_main_entries (context, entries, NULL);
654 /* Now try parsing */
655 argv = split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc);
656 argv_copy = copy_stringv (argv, argc);
658 retval = g_option_context_parse (context, &argc, &argv, &error);
659 g_assert_no_error (error);
662 /* Last arg specified is the one that should be stored */
663 g_assert (arg_test6_int64 == G_GINT64_CONSTANT(4294967296));
664 g_assert (arg_test6_int64_2 == G_GINT64_CONSTANT(0xfffffffff));
666 g_strfreev (argv_copy);
668 g_option_context_free (context);
672 callback_parse1 (const gchar *option_name, const gchar *value,
673 gpointer data, GError **error)
675 callback_test1_string = g_strdup (value);
680 callback_test1 (void)
682 GOptionContext *context;
684 GError *error = NULL;
688 GOptionEntry entries [] =
689 { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL },
692 context = g_option_context_new (NULL);
693 g_option_context_add_main_entries (context, entries, NULL);
695 /* Now try parsing */
696 argv = split_string ("program --test foo.txt", &argc);
697 argv_copy = copy_stringv (argv, argc);
699 retval = g_option_context_parse (context, &argc, &argv, &error);
700 g_assert_no_error (error);
703 g_assert (strcmp (callback_test1_string, "foo.txt") == 0);
705 g_free (callback_test1_string);
707 g_strfreev (argv_copy);
709 g_option_context_free (context);
713 callback_parse2 (const gchar *option_name, const gchar *value,
714 gpointer data, GError **error)
716 callback_test2_int++;
721 callback_test2 (void)
723 GOptionContext *context;
725 GError *error = NULL;
729 GOptionEntry entries [] =
730 { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL },
733 context = g_option_context_new (NULL);
734 g_option_context_add_main_entries (context, entries, NULL);
736 /* Now try parsing */
737 argv = split_string ("program --test --test", &argc);
738 argv_copy = copy_stringv (argv, argc);
740 retval = g_option_context_parse (context, &argc, &argv, &error);
741 g_assert_no_error (error);
744 g_assert (callback_test2_int == 2);
746 g_strfreev (argv_copy);
748 g_option_context_free (context);
752 callback_parse_optional (const gchar *option_name, const gchar *value,
753 gpointer data, GError **error)
755 callback_test_optional_boolean = TRUE;
757 callback_test_optional_string = g_strdup (value);
759 callback_test_optional_string = NULL;
764 callback_test_optional_1 (void)
766 GOptionContext *context;
768 GError *error = NULL;
772 GOptionEntry entries [] =
773 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
774 callback_parse_optional, NULL, NULL },
777 context = g_option_context_new (NULL);
778 g_option_context_add_main_entries (context, entries, NULL);
780 /* Now try parsing */
781 argv = split_string ("program --test foo.txt", &argc);
782 argv_copy = copy_stringv (argv, argc);
784 retval = g_option_context_parse (context, &argc, &argv, &error);
785 g_assert_no_error (error);
788 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
790 g_assert (callback_test_optional_boolean);
792 g_free (callback_test_optional_string);
794 g_strfreev (argv_copy);
796 g_option_context_free (context);
800 callback_test_optional_2 (void)
802 GOptionContext *context;
804 GError *error = NULL;
808 GOptionEntry entries [] =
809 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
810 callback_parse_optional, NULL, NULL },
813 context = g_option_context_new (NULL);
814 g_option_context_add_main_entries (context, entries, NULL);
816 /* Now try parsing */
817 argv = split_string ("program --test", &argc);
818 argv_copy = copy_stringv (argv, argc);
820 retval = g_option_context_parse (context, &argc, &argv, &error);
821 g_assert_no_error (error);
824 g_assert (callback_test_optional_string == NULL);
826 g_assert (callback_test_optional_boolean);
828 g_free (callback_test_optional_string);
830 g_strfreev (argv_copy);
832 g_option_context_free (context);
836 callback_test_optional_3 (void)
838 GOptionContext *context;
840 GError *error = NULL;
844 GOptionEntry entries [] =
845 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
846 callback_parse_optional, NULL, NULL },
849 context = g_option_context_new (NULL);
850 g_option_context_add_main_entries (context, entries, NULL);
852 /* Now try parsing */
853 argv = split_string ("program -t foo.txt", &argc);
854 argv_copy = copy_stringv (argv, argc);
856 retval = g_option_context_parse (context, &argc, &argv, &error);
857 g_assert_no_error (error);
860 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
862 g_assert (callback_test_optional_boolean);
864 g_free (callback_test_optional_string);
866 g_strfreev (argv_copy);
868 g_option_context_free (context);
873 callback_test_optional_4 (void)
875 GOptionContext *context;
877 GError *error = NULL;
881 GOptionEntry entries [] =
882 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
883 callback_parse_optional, NULL, NULL },
886 context = g_option_context_new (NULL);
887 g_option_context_add_main_entries (context, entries, NULL);
889 /* Now try parsing */
890 argv = split_string ("program -t", &argc);
891 argv_copy = copy_stringv (argv, argc);
893 retval = g_option_context_parse (context, &argc, &argv, &error);
894 g_assert_no_error (error);
897 g_assert (callback_test_optional_string == NULL);
899 g_assert (callback_test_optional_boolean);
901 g_free (callback_test_optional_string);
903 g_strfreev (argv_copy);
905 g_option_context_free (context);
909 callback_test_optional_5 (void)
911 GOptionContext *context;
914 GError *error = NULL;
918 GOptionEntry entries [] =
919 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
920 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
921 callback_parse_optional, NULL, NULL },
924 context = g_option_context_new (NULL);
925 g_option_context_add_main_entries (context, entries, NULL);
927 /* Now try parsing */
928 argv = split_string ("program --test --dummy", &argc);
929 argv_copy = copy_stringv (argv, argc);
931 retval = g_option_context_parse (context, &argc, &argv, &error);
932 g_assert_no_error (error);
935 g_assert (callback_test_optional_string == NULL);
937 g_assert (callback_test_optional_boolean);
939 g_free (callback_test_optional_string);
941 g_strfreev (argv_copy);
943 g_option_context_free (context);
947 callback_test_optional_6 (void)
949 GOptionContext *context;
952 GError *error = NULL;
956 GOptionEntry entries [] =
957 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
958 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
959 callback_parse_optional, NULL, NULL },
962 context = g_option_context_new (NULL);
963 g_option_context_add_main_entries (context, entries, NULL);
965 /* Now try parsing */
966 argv = split_string ("program -t -d", &argc);
967 argv_copy = copy_stringv (argv, argc);
969 retval = g_option_context_parse (context, &argc, &argv, &error);
970 g_assert_no_error (error);
973 g_assert (callback_test_optional_string == NULL);
975 g_assert (callback_test_optional_boolean);
977 g_free (callback_test_optional_string);
979 g_strfreev (argv_copy);
981 g_option_context_free (context);
985 callback_test_optional_7 (void)
987 GOptionContext *context;
990 GError *error = NULL;
994 GOptionEntry entries [] =
995 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
996 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
997 callback_parse_optional, NULL, NULL },
1000 context = g_option_context_new (NULL);
1001 g_option_context_add_main_entries (context, entries, NULL);
1003 /* Now try parsing */
1004 argv = split_string ("program -td", &argc);
1005 argv_copy = copy_stringv (argv, argc);
1007 retval = g_option_context_parse (context, &argc, &argv, &error);
1008 g_assert_no_error (error);
1011 g_assert (callback_test_optional_string == NULL);
1013 g_assert (callback_test_optional_boolean);
1015 g_free (callback_test_optional_string);
1017 g_strfreev (argv_copy);
1019 g_option_context_free (context);
1023 callback_test_optional_8 (void)
1025 GOptionContext *context;
1028 GError *error = NULL;
1032 GOptionEntry entries [] =
1033 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
1034 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
1035 callback_parse_optional, NULL, NULL },
1038 context = g_option_context_new (NULL);
1039 g_option_context_add_main_entries (context, entries, NULL);
1041 /* Now try parsing */
1042 argv = split_string ("program -dt foo.txt", &argc);
1043 argv_copy = copy_stringv (argv, argc);
1045 retval = g_option_context_parse (context, &argc, &argv, &error);
1046 g_assert_no_error (error);
1049 g_assert (callback_test_optional_string);
1051 g_assert (callback_test_optional_boolean);
1053 g_free (callback_test_optional_string);
1055 g_strfreev (argv_copy);
1057 g_option_context_free (context);
1060 static GPtrArray *callback_remaining_args;
1062 callback_remaining_test1_callback (const gchar *option_name, const gchar *value,
1063 gpointer data, GError **error)
1065 g_ptr_array_add (callback_remaining_args, g_strdup (value));
1070 callback_remaining_test1 (void)
1072 GOptionContext *context;
1074 GError *error = NULL;
1078 GOptionEntry entries [] =
1079 { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL },
1082 callback_remaining_args = g_ptr_array_new ();
1083 context = g_option_context_new (NULL);
1084 g_option_context_add_main_entries (context, entries, NULL);
1086 /* Now try parsing */
1087 argv = split_string ("program foo.txt blah.txt", &argc);
1088 argv_copy = copy_stringv (argv, argc);
1090 retval = g_option_context_parse (context, &argc, &argv, &error);
1091 g_assert_no_error (error);
1094 g_assert (callback_remaining_args->len == 2);
1095 g_assert (strcmp (callback_remaining_args->pdata[0], "foo.txt") == 0);
1096 g_assert (strcmp (callback_remaining_args->pdata[1], "blah.txt") == 0);
1098 g_ptr_array_foreach (callback_remaining_args, (GFunc) g_free, NULL);
1099 g_ptr_array_free (callback_remaining_args, TRUE);
1101 g_strfreev (argv_copy);
1103 g_option_context_free (context);
1107 callback_error (const gchar *option_name, const gchar *value,
1108 gpointer data, GError **error)
1110 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "42");
1115 callback_returns_false (void)
1117 GOptionContext *context;
1119 GError *error = NULL;
1123 GOptionEntry entries [] =
1124 { { "error", 0, 0, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1125 { "error-no-arg", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1126 { "error-optional-arg", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1129 context = g_option_context_new (NULL);
1130 g_option_context_add_main_entries (context, entries, NULL);
1132 /* Now try parsing */
1133 argv = split_string ("program --error value", &argc);
1134 argv_copy = copy_stringv (argv, argc);
1136 retval = g_option_context_parse (context, &argc, &argv, &error);
1137 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1138 g_assert (retval == FALSE);
1139 check_identical_stringv (argv_copy, argv);
1141 g_option_context_free (context);
1142 g_clear_error (&error);
1143 g_strfreev (argv_copy);
1146 /* And again, this time with a no-arg variant */
1147 context = g_option_context_new (NULL);
1148 g_option_context_add_main_entries (context, entries, NULL);
1150 argv = split_string ("program --error-no-arg", &argc);
1151 argv_copy = copy_stringv (argv, argc);
1153 retval = g_option_context_parse (context, &argc, &argv, &error);
1154 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1155 g_assert (retval == FALSE);
1156 check_identical_stringv (argv_copy, argv);
1158 g_option_context_free (context);
1159 g_clear_error (&error);
1160 g_strfreev (argv_copy);
1163 /* And again, this time with a optional arg variant, with argument */
1164 context = g_option_context_new (NULL);
1165 g_option_context_add_main_entries (context, entries, NULL);
1167 argv = split_string ("program --error-optional-arg value", &argc);
1168 argv_copy = copy_stringv (argv, argc);
1170 retval = g_option_context_parse (context, &argc, &argv, &error);
1171 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1172 g_assert (retval == FALSE);
1173 check_identical_stringv (argv_copy, argv);
1175 g_option_context_free (context);
1176 g_clear_error (&error);
1177 g_strfreev (argv_copy);
1180 /* And again, this time with a optional arg variant, without argument */
1181 context = g_option_context_new (NULL);
1182 g_option_context_add_main_entries (context, entries, NULL);
1184 argv = split_string ("program --error-optional-arg", &argc);
1185 argv_copy = copy_stringv (argv, argc);
1187 retval = g_option_context_parse (context, &argc, &argv, &error);
1188 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1189 g_assert (retval == FALSE);
1190 check_identical_stringv (argv_copy, argv);
1192 g_option_context_free (context);
1193 g_clear_error (&error);
1194 g_strfreev (argv_copy);
1202 GOptionContext *context;
1204 GError *error = NULL;
1205 gchar **argv, **argv_copy;
1208 GOptionEntry entries [] =
1209 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1212 context = g_option_context_new (NULL);
1213 g_option_context_set_ignore_unknown_options (context, TRUE);
1214 g_option_context_add_main_entries (context, entries, NULL);
1216 /* Now try parsing */
1217 argv = split_string ("program --test --hello", &argc);
1218 argv_copy = copy_stringv (argv, argc);
1220 retval = g_option_context_parse (context, &argc, &argv, &error);
1221 g_assert_no_error (error);
1225 arg = join_stringv (argc, argv);
1226 g_assert (strcmp (arg, "program --hello") == 0);
1229 g_strfreev (argv_copy);
1231 g_option_context_free (context);
1237 GOptionContext *context;
1239 GError *error = NULL;
1244 GOptionEntry entries [] =
1245 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
1248 context = g_option_context_new (NULL);
1249 g_option_context_set_ignore_unknown_options (context, TRUE);
1250 g_option_context_add_main_entries (context, entries, NULL);
1252 /* Now try parsing */
1253 argv = split_string ("program -test", &argc);
1254 argv_copy = copy_stringv (argv, argc);
1256 retval = g_option_context_parse (context, &argc, &argv, &error);
1257 g_assert_no_error (error);
1261 arg = join_stringv (argc, argv);
1262 g_assert (strcmp (arg, "program -es") == 0);
1265 g_strfreev (argv_copy);
1267 g_option_context_free (context);
1273 GOptionContext *context;
1275 GError *error = NULL;
1276 gchar **argv, **argv_copy;
1279 GOptionEntry entries [] =
1280 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
1283 context = g_option_context_new (NULL);
1284 g_option_context_set_ignore_unknown_options (context, TRUE);
1285 g_option_context_add_main_entries (context, entries, NULL);
1287 /* Now try parsing */
1288 argv = split_string ("program --test foo --hello", &argc);
1289 argv_copy = copy_stringv (argv, argc);
1291 retval = g_option_context_parse (context, &argc, &argv, &error);
1292 g_assert_no_error (error);
1296 arg = join_stringv (argc, argv);
1297 g_assert (strcmp (arg, "program --hello") == 0);
1299 g_assert (strcmp (ignore_test3_string, "foo") == 0);
1300 g_free (ignore_test3_string);
1303 g_strfreev (argv_copy);
1305 g_option_context_free (context);
1309 static array_test1 (void)
1311 GOptionContext *context;
1313 GError *error = NULL;
1317 GOptionEntry entries [] =
1318 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1321 context = g_option_context_new (NULL);
1322 g_option_context_add_main_entries (context, entries, NULL);
1324 /* Now try parsing */
1325 argv = split_string ("program --test foo --test bar", &argc);
1326 argv_copy = copy_stringv (argv, argc);
1328 retval = g_option_context_parse (context, &argc, &argv, &error);
1329 g_assert_no_error (error);
1333 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1334 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1335 g_assert (array_test1_array[2] == NULL);
1337 g_strfreev (array_test1_array);
1339 g_strfreev (argv_copy);
1341 g_option_context_free (context);
1347 GOptionContext *context;
1349 GOptionEntry entries1 [] =
1350 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1352 GOptionEntry entries2 [] =
1353 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1356 context = g_option_context_new (NULL);
1357 g_option_context_add_main_entries (context, entries1, NULL);
1358 g_option_context_add_main_entries (context, entries2, NULL);
1360 g_option_context_free (context);
1366 GOptionContext *context;
1368 context = g_option_context_new (NULL);
1369 g_option_context_parse (context, NULL, NULL, NULL);
1371 g_option_context_free (context);
1377 GOptionContext *context;
1384 context = g_option_context_new (NULL);
1385 g_option_context_parse (context, &argc, &argv, NULL);
1387 g_option_context_free (context);
1390 /* check that non-option arguments are left in argv by default */
1394 GOptionContext *context;
1396 GError *error = NULL;
1400 GOptionEntry entries [] = {
1401 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1405 context = g_option_context_new (NULL);
1406 g_option_context_add_main_entries (context, entries, NULL);
1408 /* Now try parsing */
1409 argv = split_string ("program foo --test bar", &argc);
1410 argv_copy = copy_stringv (argv, argc);
1412 retval = g_option_context_parse (context, &argc, &argv, &error);
1413 g_assert_no_error (error);
1417 g_assert (ignore_test1_boolean);
1418 g_assert (strcmp (argv[0], "program") == 0);
1419 g_assert (strcmp (argv[1], "foo") == 0);
1420 g_assert (strcmp (argv[2], "bar") == 0);
1421 g_assert (argv[3] == NULL);
1423 g_strfreev (argv_copy);
1425 g_option_context_free (context);
1428 /* check that -- works */
1432 GOptionContext *context;
1434 GError *error = NULL;
1438 GOptionEntry entries [] = {
1439 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1443 context = g_option_context_new (NULL);
1444 g_option_context_add_main_entries (context, entries, NULL);
1446 /* Now try parsing */
1447 argv = split_string ("program foo --test -- -bar", &argc);
1448 argv_copy = copy_stringv (argv, argc);
1450 retval = g_option_context_parse (context, &argc, &argv, &error);
1451 g_assert_no_error (error);
1455 g_assert (ignore_test1_boolean);
1456 g_assert (strcmp (argv[0], "program") == 0);
1457 g_assert (strcmp (argv[1], "foo") == 0);
1458 g_assert (strcmp (argv[2], "--") == 0);
1459 g_assert (strcmp (argv[3], "-bar") == 0);
1460 g_assert (argv[4] == NULL);
1462 g_strfreev (argv_copy);
1464 g_option_context_free (context);
1467 /* check that -- stripping works */
1471 GOptionContext *context;
1473 GError *error = NULL;
1477 GOptionEntry entries [] = {
1478 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1482 context = g_option_context_new (NULL);
1483 g_option_context_add_main_entries (context, entries, NULL);
1485 /* Now try parsing */
1486 argv = split_string ("program foo --test -- bar", &argc);
1487 argv_copy = copy_stringv (argv, argc);
1489 retval = g_option_context_parse (context, &argc, &argv, &error);
1490 g_assert_no_error (error);
1494 g_assert (ignore_test1_boolean);
1495 g_assert (strcmp (argv[0], "program") == 0);
1496 g_assert (strcmp (argv[1], "foo") == 0);
1497 g_assert (strcmp (argv[2], "bar") == 0);
1498 g_assert (argv[3] == NULL);
1500 g_strfreev (argv_copy);
1502 g_option_context_free (context);
1508 GOptionContext *context;
1510 GError *error = NULL;
1514 GOptionEntry entries [] = {
1515 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1519 context = g_option_context_new (NULL);
1520 g_option_context_set_ignore_unknown_options (context, TRUE);
1521 g_option_context_add_main_entries (context, entries, NULL);
1523 /* Now try parsing */
1524 argv = split_string ("program foo --test -bar --", &argc);
1525 argv_copy = copy_stringv (argv, argc);
1527 retval = g_option_context_parse (context, &argc, &argv, &error);
1528 g_assert_no_error (error);
1532 g_assert (ignore_test1_boolean);
1533 g_assert (strcmp (argv[0], "program") == 0);
1534 g_assert (strcmp (argv[1], "foo") == 0);
1535 g_assert (strcmp (argv[2], "-bar") == 0);
1536 g_assert (argv[3] == NULL);
1538 g_strfreev (argv_copy);
1540 g_option_context_free (context);
1546 GOptionContext *context;
1548 GError *error = NULL;
1552 GOptionEntry entries [] = {
1553 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1557 context = g_option_context_new (NULL);
1558 g_option_context_add_main_entries (context, entries, NULL);
1560 /* Now try parsing */
1561 argv = split_string ("program --test foo -- bar", &argc);
1562 argv_copy = copy_stringv (argv, argc);
1564 retval = g_option_context_parse (context, &argc, &argv, &error);
1565 g_assert_no_error (error);
1569 g_assert (ignore_test1_boolean);
1570 g_assert (strcmp (argv[0], "program") == 0);
1571 g_assert (strcmp (argv[1], "foo") == 0);
1572 g_assert (strcmp (argv[2], "bar") == 0);
1573 g_assert (argv[3] == NULL);
1575 g_strfreev (argv_copy);
1577 g_option_context_free (context);
1583 GOptionContext *context;
1585 GError *error = NULL;
1589 GOptionEntry entries [] = {
1590 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1594 context = g_option_context_new (NULL);
1595 g_option_context_add_main_entries (context, entries, NULL);
1597 /* Now try parsing */
1598 argv = split_string ("program --test -- -bar", &argc);
1599 argv_copy = copy_stringv (argv, argc);
1601 retval = g_option_context_parse (context, &argc, &argv, &error);
1602 g_assert_no_error (error);
1606 g_assert (ignore_test1_boolean);
1607 g_assert (strcmp (argv[0], "program") == 0);
1608 g_assert (strcmp (argv[1], "--") == 0);
1609 g_assert (strcmp (argv[2], "-bar") == 0);
1610 g_assert (argv[3] == NULL);
1612 g_strfreev (argv_copy);
1614 g_option_context_free (context);
1618 /* check that G_OPTION_REMAINING collects non-option arguments */
1622 GOptionContext *context;
1624 GError *error = NULL;
1628 GOptionEntry entries [] = {
1629 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1630 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1634 context = g_option_context_new (NULL);
1635 g_option_context_add_main_entries (context, entries, NULL);
1637 /* Now try parsing */
1638 argv = split_string ("program foo --test bar", &argc);
1639 argv_copy = copy_stringv (argv, argc);
1641 retval = g_option_context_parse (context, &argc, &argv, &error);
1642 g_assert_no_error (error);
1646 g_assert (ignore_test1_boolean);
1647 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1648 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1649 g_assert (array_test1_array[2] == NULL);
1651 g_strfreev (array_test1_array);
1653 g_strfreev (argv_copy);
1655 g_option_context_free (context);
1659 /* check that G_OPTION_REMAINING and -- work together */
1663 GOptionContext *context;
1665 GError *error = NULL;
1669 GOptionEntry entries [] = {
1670 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1671 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1675 context = g_option_context_new (NULL);
1676 g_option_context_add_main_entries (context, entries, NULL);
1678 /* Now try parsing */
1679 argv = split_string ("program foo --test -- -bar", &argc);
1680 argv_copy = copy_stringv (argv, argc);
1682 retval = g_option_context_parse (context, &argc, &argv, &error);
1683 g_assert_no_error (error);
1687 g_assert (ignore_test1_boolean);
1688 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1689 g_assert (strcmp (array_test1_array[1], "-bar") == 0);
1690 g_assert (array_test1_array[2] == NULL);
1692 g_strfreev (array_test1_array);
1694 g_strfreev (argv_copy);
1696 g_option_context_free (context);
1699 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1703 GOptionContext *context;
1705 GError *error = NULL;
1709 GOptionEntry entries [] = {
1710 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1711 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
1715 context = g_option_context_new (NULL);
1716 g_option_context_add_main_entries (context, entries, NULL);
1718 /* Now try parsing */
1719 argv = split_string ("program foo --test bar", &argc);
1720 argv_copy = copy_stringv (argv, argc);
1722 retval = g_option_context_parse (context, &argc, &argv, &error);
1723 g_assert_no_error (error);
1727 g_assert (ignore_test1_boolean);
1728 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1729 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1730 g_assert (array_test1_array[2] == NULL);
1732 g_strfreev (array_test1_array);
1734 g_strfreev (argv_copy);
1736 g_option_context_free (context);
1740 unknown_short_test (void)
1742 GOptionContext *context;
1744 GError *error = NULL;
1748 GOptionEntry entries [] = { { NULL } };
1750 g_test_bug ("166609");
1752 context = g_option_context_new (NULL);
1753 g_option_context_add_main_entries (context, entries, NULL);
1755 /* Now try parsing */
1756 argv = split_string ("program -0", &argc);
1757 argv_copy = copy_stringv (argv, argc);
1759 retval = g_option_context_parse (context, &argc, &argv, &error);
1761 g_assert (error != NULL);
1762 g_clear_error (&error);
1764 g_strfreev (argv_copy);
1766 g_option_context_free (context);
1769 /* test that lone dashes are treated as non-options */
1771 lonely_dash_test (void)
1773 GOptionContext *context;
1775 GError *error = NULL;
1780 g_test_bug ("168008");
1782 context = g_option_context_new (NULL);
1784 /* Now try parsing */
1785 argv = split_string ("program -", &argc);
1786 argv_copy = copy_stringv (argv, argc);
1788 retval = g_option_context_parse (context, &argc, &argv, &error);
1789 g_assert_no_error (error);
1792 g_assert (argv[1] && strcmp (argv[1], "-") == 0);
1794 g_strfreev (argv_copy);
1796 g_option_context_free (context);
1800 missing_arg_test (void)
1802 GOptionContext *context;
1804 GError *error = NULL;
1809 GOptionEntry entries [] =
1810 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1813 g_test_bug ("305576");
1815 context = g_option_context_new (NULL);
1816 g_option_context_add_main_entries (context, entries, NULL);
1818 /* Now try parsing */
1819 argv = split_string ("program --test", &argc);
1820 argv_copy = copy_stringv (argv, argc);
1822 retval = g_option_context_parse (context, &argc, &argv, &error);
1823 g_assert (retval == FALSE);
1824 g_assert (error != NULL);
1825 /* An error occurred, so argv has not been changed */
1826 check_identical_stringv (argv_copy, argv);
1827 g_clear_error (&error);
1829 g_strfreev (argv_copy);
1832 /* Try parsing again */
1833 argv = split_string ("program -t", &argc);
1834 argv_copy = copy_stringv (argv, argc);
1836 retval = g_option_context_parse (context, &argc, &argv, &error);
1837 g_assert (retval == FALSE);
1838 g_assert (error != NULL);
1839 /* An error occurred, so argv has not been changed */
1840 check_identical_stringv (argv_copy, argv);
1841 g_clear_error (&error);
1843 g_strfreev (argv_copy);
1845 g_option_context_free (context);
1848 static gchar *test_arg;
1850 static gboolean cb (const gchar *option_name,
1855 test_arg = g_strdup (value);
1860 dash_arg_test (void)
1862 GOptionContext *context;
1864 GError *error = NULL;
1868 gboolean argb = FALSE;
1869 GOptionEntry entries [] =
1870 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, cb, NULL, NULL },
1871 { "three", '3', 0, G_OPTION_ARG_NONE, &argb, NULL, NULL },
1874 g_test_bug ("577638");
1876 context = g_option_context_new (NULL);
1877 g_option_context_add_main_entries (context, entries, NULL);
1879 /* Now try parsing */
1880 argv = split_string ("program --test=-3", &argc);
1881 argv_copy = copy_stringv (argv, argc);
1885 retval = g_option_context_parse (context, &argc, &argv, &error);
1887 g_assert_no_error (error);
1888 g_assert_cmpstr (test_arg, ==, "-3");
1890 g_strfreev (argv_copy);
1895 /* Try parsing again */
1896 argv = split_string ("program --test -3", &argc);
1897 argv_copy = copy_stringv (argv, argc);
1900 retval = g_option_context_parse (context, &argc, &argv, &error);
1901 g_assert_no_error (error);
1903 g_assert_cmpstr (test_arg, ==, NULL);
1905 g_option_context_free (context);
1906 g_strfreev (argv_copy);
1913 GOptionContext *context;
1915 GOptionEntry entries [] =
1916 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1919 context = g_option_context_new (NULL);
1920 g_option_context_add_main_entries (context, entries, NULL);
1922 g_assert (g_option_context_get_help_enabled (context));
1923 g_assert (!g_option_context_get_ignore_unknown_options (context));
1924 g_assert_cmpstr (g_option_context_get_summary (context), ==, NULL);
1925 g_assert_cmpstr (g_option_context_get_description (context), ==, NULL);
1927 g_option_context_set_help_enabled (context, FALSE);
1928 g_option_context_set_ignore_unknown_options (context, TRUE);
1929 g_option_context_set_summary (context, "summary");
1930 g_option_context_set_description(context, "description");
1932 g_assert (!g_option_context_get_help_enabled (context));
1933 g_assert (g_option_context_get_ignore_unknown_options (context));
1934 g_assert_cmpstr (g_option_context_get_summary (context), ==, "summary");
1935 g_assert_cmpstr (g_option_context_get_description (context), ==, "description");
1937 g_option_context_free (context);
1941 test_main_group (void)
1943 GOptionContext *context;
1944 GOptionGroup *group;
1946 context = g_option_context_new (NULL);
1947 g_assert (g_option_context_get_main_group (context) == NULL);
1948 group = g_option_group_new ("name", "description", "hlep", NULL, NULL);
1949 g_option_context_add_group (context, group);
1950 g_assert (g_option_context_get_main_group (context) == NULL);
1951 group = g_option_group_new ("name", "description", "hlep", NULL, NULL);
1952 g_option_context_set_main_group (context, group);
1953 g_assert (g_option_context_get_main_group (context) == group);
1955 g_option_context_free (context);
1958 static gboolean error_func_called = FALSE;
1961 error_func (GOptionContext *context,
1962 GOptionGroup *group,
1966 g_assert_cmpint (GPOINTER_TO_INT(data), ==, 1234);
1967 error_func_called = TRUE;
1971 test_error_hook (void)
1973 GOptionContext *context;
1975 GOptionEntry entries [] =
1976 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1978 GOptionGroup *group;
1983 GError *error = NULL;
1985 context = g_option_context_new (NULL);
1986 group = g_option_group_new ("name", "description", "hlep", GINT_TO_POINTER(1234), NULL);
1987 g_option_group_add_entries (group, entries);
1988 g_option_context_set_main_group (context, group);
1989 g_option_group_set_error_hook (g_option_context_get_main_group (context),
1992 argv = split_string ("program --test", &argc);
1993 argv_copy = copy_stringv (argv, argc);
1995 retval = g_option_context_parse (context, &argc, &argv, &error);
1996 g_assert (retval == FALSE);
1997 g_assert (error != NULL);
1998 /* An error occurred, so argv has not been changed */
1999 check_identical_stringv (argv_copy, argv);
2000 g_clear_error (&error);
2002 g_assert (error_func_called);
2004 g_strfreev (argv_copy);
2006 g_option_context_free (context);
2010 flag_reverse_string (void)
2012 if (!g_test_undefined ())
2015 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
2017 GOptionContext *context;
2019 GOptionEntry entries [] =
2020 { { "test", 't', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2025 GError *error = NULL;
2027 context = g_option_context_new (NULL);
2028 g_option_context_add_main_entries (context, entries, NULL);
2030 argv = split_string ("program --test bla", &argc);
2032 retval = g_option_context_parse (context, &argc, &argv, &error);
2033 g_assert (retval == FALSE);
2034 g_clear_error (&error);
2036 g_option_context_free (context);
2039 g_test_trap_assert_failed ();
2040 g_test_trap_assert_stderr ("*ignoring reverse flag*");
2044 flag_optional_int (void)
2046 if (!g_test_undefined ())
2049 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
2051 GOptionContext *context;
2053 GOptionEntry entries [] =
2054 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &arg, NULL, NULL },
2059 GError *error = NULL;
2061 context = g_option_context_new (NULL);
2062 g_option_context_add_main_entries (context, entries, NULL);
2064 argv = split_string ("program --test 5", &argc);
2066 retval = g_option_context_parse (context, &argc, &argv, &error);
2067 g_assert (retval == FALSE);
2068 g_clear_error (&error);
2070 g_option_context_free (context);
2073 g_test_trap_assert_failed ();
2074 g_test_trap_assert_stderr ("*ignoring no-arg, optional-arg or filename flags*");
2080 g_test_init (&argc, &argv, NULL);
2082 g_test_bug_base ("http://bugzilla.gnome.org/");
2084 g_test_add_func ("/option/basic", test_basic);
2085 g_test_add_func ("/option/group/captions", group_captions);
2086 g_test_add_func ("/option/group/main", test_main_group);
2087 g_test_add_func ("/option/group/error-hook", test_error_hook);
2089 /* Test that restoration on failure works */
2090 g_test_add_func ("/option/restoration/int", error_test1);
2091 g_test_add_func ("/option/restoration/string", error_test2);
2092 g_test_add_func ("/option/restoration/boolean", error_test3);
2094 /* Test that special argument parsing works */
2095 g_test_add_func ("/option/arg/repetition/int", arg_test1);
2096 g_test_add_func ("/option/arg/repetition/string", arg_test2);
2097 g_test_add_func ("/option/arg/repetition/filename", arg_test3);
2098 g_test_add_func ("/option/arg/repetition/double", arg_test4);
2099 g_test_add_func ("/option/arg/repetition/locale", arg_test5);
2100 g_test_add_func ("/option/arg/repetition/int64", arg_test6);
2102 /* Test string arrays */
2103 g_test_add_func ("/option/arg/array/string", array_test1);
2105 /* Test callback args */
2106 g_test_add_func ("/option/arg/callback/string", callback_test1);
2107 g_test_add_func ("/option/arg/callback/count", callback_test2);
2109 /* Test optional arg flag for callback */
2110 g_test_add_func ("/option/arg/callback/optional1", callback_test_optional_1);
2111 g_test_add_func ("/option/arg/callback/optional2", callback_test_optional_2);
2112 g_test_add_func ("/option/arg/callback/optional3", callback_test_optional_3);
2113 g_test_add_func ("/option/arg/callback/optional4", callback_test_optional_4);
2114 g_test_add_func ("/option/arg/callback/optional5", callback_test_optional_5);
2115 g_test_add_func ("/option/arg/callback/optional6", callback_test_optional_6);
2116 g_test_add_func ("/option/arg/callback/optional7", callback_test_optional_7);
2117 g_test_add_func ("/option/arg/callback/optional8", callback_test_optional_8);
2119 /* Test callback with G_OPTION_REMAINING */
2120 g_test_add_func ("/option/arg/remaining/callback", callback_remaining_test1);
2122 /* Test callbacks which return FALSE */
2123 g_test_add_func ("/option/arg/remaining/callback-false", callback_returns_false);
2125 /* Test ignoring options */
2126 g_test_add_func ("/option/arg/ignore/long", ignore_test1);
2127 g_test_add_func ("/option/arg/ignore/short", ignore_test2);
2128 g_test_add_func ("/option/arg/ignore/arg", ignore_test3);
2129 g_test_add_func ("/option/context/add", add_test1);
2131 /* Test parsing empty args */
2132 /* Note there used to be an empty1 here, but it effectively moved
2133 * to option-argv0.c.
2135 g_test_add_func ("/option/context/empty2", empty_test2);
2136 g_test_add_func ("/option/context/empty3", empty_test3);
2138 /* Test handling of rest args */
2139 g_test_add_func ("/option/arg/rest/non-option", rest_test1);
2140 g_test_add_func ("/option/arg/rest/separator1", rest_test2);
2141 g_test_add_func ("/option/arg/rest/separator2", rest_test2a);
2142 g_test_add_func ("/option/arg/rest/separator3", rest_test2b);
2143 g_test_add_func ("/option/arg/rest/separator4", rest_test2c);
2144 g_test_add_func ("/option/arg/rest/separator5", rest_test2d);
2145 g_test_add_func ("/option/arg/remaining/non-option", rest_test3);
2146 g_test_add_func ("/option/arg/remaining/separator", rest_test4);
2147 g_test_add_func ("/option/arg/remaining/array", rest_test5);
2149 /* Test some invalid flag combinations */
2150 g_test_add_func ("/option/arg/reverse-string", flag_reverse_string);
2151 g_test_add_func ("/option/arg/optional-int", flag_optional_int);
2153 /* regression tests for individual bugs */
2154 g_test_add_func ("/option/bug/unknown-short", unknown_short_test);
2155 g_test_add_func ("/option/bug/lonely-dash", lonely_dash_test);
2156 g_test_add_func ("/option/bug/missing-arg", missing_arg_test);
2157 g_test_add_func ("/option/bug/dash-arg", dash_arg_test);
2159 return g_test_run();