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_setenv ("LANG", "C", TRUE);
76 g_option_context_parse (options, &argc, &argv, &error);
77 g_option_context_free (options);
82 test_group_captions_help (gconstpointer test_number)
84 GOptionContext *options;
85 gchar *argv[] = { __FILE__, "--help", NULL };
87 options = make_options (GPOINTER_TO_INT (test_number));
88 print_help (options, argv);
92 test_group_captions_help_all (gconstpointer test_number)
94 GOptionContext *options;
95 gchar *argv[] = { __FILE__, "--help-all", NULL };
97 options = make_options (GPOINTER_TO_INT (test_number));
98 print_help (options, argv);
102 test_group_captions_help_test (gconstpointer test_number)
104 GOptionContext *options;
105 gchar *argv[] = { __FILE__, "--help-test", NULL };
107 options = make_options (GPOINTER_TO_INT (test_number));
108 print_help (options, argv);
112 test_group_captions (void)
114 const gchar *test_name_base[] = { "help", "help-all", "help-test" };
118 g_test_bug ("504142");
120 for (i = 0; i < 4; ++i)
122 gboolean have_main_entries = (0 != (i & 1));
123 gboolean have_test_entries = (0 != (i & 2));
125 for (j = 0; j < G_N_ELEMENTS (test_name_base); ++j)
127 GTestSubprocessFlags trap_flags = 0;
128 gboolean expect_main_description = FALSE;
129 gboolean expect_main_switch = FALSE;
130 gboolean expect_test_description = FALSE;
131 gboolean expect_test_switch = FALSE;
132 gboolean expect_test_group = FALSE;
134 if (g_test_verbose ())
135 trap_flags |= G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR;
137 test_name = g_strdup_printf ("/option/group/captions/subprocess/%s-%d",
138 test_name_base[j], i);
139 g_test_trap_subprocess (test_name, 0, trap_flags);
141 g_test_trap_assert_passed ();
142 g_test_trap_assert_stderr ("");
147 g_assert_cmpstr ("help", ==, test_name_base[j]);
148 expect_main_switch = have_main_entries;
149 expect_test_group = have_test_entries;
153 g_assert_cmpstr ("help-all", ==, test_name_base[j]);
154 expect_main_switch = have_main_entries;
155 expect_test_switch = have_test_entries;
156 expect_test_group = have_test_entries;
160 g_assert_cmpstr ("help-test", ==, test_name_base[j]);
161 expect_test_switch = have_test_entries;
165 g_assert_not_reached ();
169 expect_main_description |= expect_main_switch;
170 expect_test_description |= expect_test_switch;
172 if (expect_main_description)
173 g_test_trap_assert_stdout ("*Application Options*");
175 g_test_trap_assert_stdout_unmatched ("*Application Options*");
176 if (expect_main_switch)
177 g_test_trap_assert_stdout ("*--main-switch*");
179 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
181 if (expect_test_description)
182 g_test_trap_assert_stdout ("*Test Options*");
184 g_test_trap_assert_stdout_unmatched ("*Test Options*");
185 if (expect_test_switch)
186 g_test_trap_assert_stdout ("*--test-switch*");
188 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
190 if (expect_test_group)
191 g_test_trap_assert_stdout ("*--help-test*");
193 g_test_trap_assert_stdout_unmatched ("*--help-test*");
199 char *error_test2_string;
200 gboolean error_test3_boolean;
203 gchar *arg_test2_string;
204 gchar *arg_test3_filename;
205 gdouble arg_test4_double;
206 gdouble arg_test5_double;
207 gint64 arg_test6_int64;
208 gint64 arg_test6_int64_2;
210 gchar *callback_test1_string;
211 int callback_test2_int;
213 gchar *callback_test_optional_string;
214 gboolean callback_test_optional_boolean;
216 gchar **array_test1_array;
218 gboolean ignore_test1_boolean;
219 gboolean ignore_test2_boolean;
220 gchar *ignore_test3_string;
223 split_string (const char *str, int *argc)
228 argv = g_strsplit (str, " ", 0);
230 for (len = 0; argv[len] != NULL; len++);
239 join_stringv (int argc, char **argv)
244 str = g_string_new (NULL);
246 for (i = 0; i < argc; i++)
248 g_string_append (str, argv[i]);
251 g_string_append_c (str, ' ');
254 return g_string_free (str, FALSE);
257 /* Performs a shallow copy */
259 copy_stringv (char **argv, int argc)
261 return g_memdup (argv, sizeof (char *) * (argc + 1));
265 check_identical_stringv (gchar **before, gchar **after)
269 /* Not only is it the same string... */
270 for (i = 0; before[i] != NULL; i++)
271 g_assert_cmpstr (before[i], ==, after[i]);
273 /* ... it is actually the same pointer */
274 for (i = 0; before[i] != NULL; i++)
275 g_assert (before[i] == after[i]);
277 g_assert (after[i] == NULL);
282 error_test1_pre_parse (GOptionContext *context,
287 g_assert (error_test1_int == 0x12345678);
293 error_test1_post_parse (GOptionContext *context,
298 g_assert (error_test1_int == 20);
300 /* Set an error in the post hook */
301 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
309 GOptionContext *context;
311 GError *error = NULL;
315 GOptionGroup *main_group;
316 GOptionEntry entries [] =
317 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
320 error_test1_int = 0x12345678;
322 context = g_option_context_new (NULL);
323 g_option_context_add_main_entries (context, entries, NULL);
325 /* Set pre and post parse hooks */
326 main_group = g_option_context_get_main_group (context);
327 g_option_group_set_parse_hooks (main_group,
328 error_test1_pre_parse, error_test1_post_parse);
330 /* Now try parsing */
331 argv = split_string ("program --test 20", &argc);
332 argv_copy = copy_stringv (argv, argc);
334 retval = g_option_context_parse (context, &argc, &argv, &error);
335 g_assert (retval == FALSE);
336 g_assert (error != NULL);
337 /* An error occurred, so argv has not been changed */
338 check_identical_stringv (argv_copy, argv);
339 g_clear_error (&error);
341 /* On failure, values should be reset */
342 g_assert (error_test1_int == 0x12345678);
344 g_strfreev (argv_copy);
346 g_option_context_free (context);
350 error_test2_pre_parse (GOptionContext *context,
355 g_assert (strcmp (error_test2_string, "foo") == 0);
361 error_test2_post_parse (GOptionContext *context,
366 g_assert (strcmp (error_test2_string, "bar") == 0);
368 /* Set an error in the post hook */
369 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
377 GOptionContext *context;
379 GError *error = NULL;
383 GOptionGroup *main_group;
384 GOptionEntry entries [] =
385 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
388 error_test2_string = "foo";
390 context = g_option_context_new (NULL);
391 g_option_context_add_main_entries (context, entries, NULL);
393 /* Set pre and post parse hooks */
394 main_group = g_option_context_get_main_group (context);
395 g_option_group_set_parse_hooks (main_group,
396 error_test2_pre_parse, error_test2_post_parse);
398 /* Now try parsing */
399 argv = split_string ("program --test bar", &argc);
400 argv_copy = copy_stringv (argv, argc);
401 retval = g_option_context_parse (context, &argc, &argv, &error);
403 g_assert (retval == FALSE);
404 g_assert (error != NULL);
405 check_identical_stringv (argv_copy, argv);
406 g_clear_error (&error);
408 g_assert (strcmp (error_test2_string, "foo") == 0);
410 g_strfreev (argv_copy);
412 g_option_context_free (context);
416 error_test3_pre_parse (GOptionContext *context,
421 g_assert (!error_test3_boolean);
427 error_test3_post_parse (GOptionContext *context,
432 g_assert (error_test3_boolean);
434 /* Set an error in the post hook */
435 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
443 GOptionContext *context;
445 GError *error = NULL;
449 GOptionGroup *main_group;
450 GOptionEntry entries [] =
451 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
454 error_test3_boolean = FALSE;
456 context = g_option_context_new (NULL);
457 g_option_context_add_main_entries (context, entries, NULL);
459 /* Set pre and post parse hooks */
460 main_group = g_option_context_get_main_group (context);
461 g_option_group_set_parse_hooks (main_group,
462 error_test3_pre_parse, error_test3_post_parse);
464 /* Now try parsing */
465 argv = split_string ("program --test", &argc);
466 argv_copy = copy_stringv (argv, argc);
467 retval = g_option_context_parse (context, &argc, &argv, &error);
469 g_assert (retval == FALSE);
470 g_assert (error != NULL);
471 check_identical_stringv (argv_copy, argv);
472 g_clear_error (&error);
474 g_assert (!error_test3_boolean);
476 g_strfreev (argv_copy);
478 g_option_context_free (context);
484 GOptionContext *context;
486 GError *error = NULL;
490 GOptionEntry entries [] =
491 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
494 context = g_option_context_new (NULL);
495 g_option_context_add_main_entries (context, entries, NULL);
497 /* Now try parsing */
498 argv = split_string ("program --test 20 --test 30", &argc);
499 argv_copy = copy_stringv (argv, argc);
501 retval = g_option_context_parse (context, &argc, &argv, &error);
502 g_assert_no_error (error);
505 /* Last arg specified is the one that should be stored */
506 g_assert (arg_test1_int == 30);
508 /* We free all of the strings in a copy of argv, because now argv is a
509 * subset - some have been removed in-place
511 g_strfreev (argv_copy);
513 g_option_context_free (context);
519 GOptionContext *context;
521 GError *error = NULL;
525 GOptionEntry entries [] =
526 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
529 context = g_option_context_new (NULL);
530 g_option_context_add_main_entries (context, entries, NULL);
532 /* Now try parsing */
533 argv = split_string ("program --test foo --test bar", &argc);
534 argv_copy = copy_stringv (argv, argc);
536 retval = g_option_context_parse (context, &argc, &argv, &error);
537 g_assert_no_error (error);
540 /* Last arg specified is the one that should be stored */
541 g_assert (strcmp (arg_test2_string, "bar") == 0);
543 g_free (arg_test2_string);
545 g_strfreev (argv_copy);
547 g_option_context_free (context);
553 GOptionContext *context;
555 GError *error = NULL;
559 GOptionEntry entries [] =
560 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
563 context = g_option_context_new (NULL);
564 g_option_context_add_main_entries (context, entries, NULL);
566 /* Now try parsing */
567 argv = split_string ("program --test foo.txt", &argc);
568 argv_copy = copy_stringv (argv, argc);
570 retval = g_option_context_parse (context, &argc, &argv, &error);
571 g_assert_no_error (error);
574 /* Last arg specified is the one that should be stored */
575 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
577 g_free (arg_test3_filename);
579 g_strfreev (argv_copy);
581 g_option_context_free (context);
588 GOptionContext *context;
590 GError *error = NULL;
594 GOptionEntry entries [] =
595 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, NULL, NULL },
598 context = g_option_context_new (NULL);
599 g_option_context_add_main_entries (context, entries, NULL);
601 /* Now try parsing */
602 argv = split_string ("program --test 20.0 --test 30.03", &argc);
603 argv_copy = copy_stringv (argv, argc);
605 retval = g_option_context_parse (context, &argc, &argv, &error);
606 g_assert_no_error (error);
609 /* Last arg specified is the one that should be stored */
610 g_assert (arg_test4_double == 30.03);
612 g_strfreev (argv_copy);
614 g_option_context_free (context);
620 GOptionContext *context;
622 GError *error = NULL;
626 char *old_locale, *current_locale;
627 const char *locale = "de_DE";
628 GOptionEntry entries [] =
629 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test5_double, NULL, NULL },
632 context = g_option_context_new (NULL);
633 g_option_context_add_main_entries (context, entries, NULL);
635 /* Now try parsing */
636 argv = split_string ("program --test 20,0 --test 30,03", &argc);
637 argv_copy = copy_stringv (argv, argc);
639 /* set it to some locale that uses commas instead of decimal points */
641 old_locale = g_strdup (setlocale (LC_NUMERIC, locale));
642 current_locale = setlocale (LC_NUMERIC, NULL);
643 if (strcmp (current_locale, locale) != 0)
645 fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
649 retval = g_option_context_parse (context, &argc, &argv, &error);
650 g_assert_no_error (error);
653 /* Last arg specified is the one that should be stored */
654 g_assert (arg_test5_double == 30.03);
657 setlocale (LC_NUMERIC, old_locale);
660 g_strfreev (argv_copy);
662 g_option_context_free (context);
668 GOptionContext *context;
670 GError *error = NULL;
674 GOptionEntry entries [] =
675 { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL },
676 { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, NULL, NULL },
679 context = g_option_context_new (NULL);
680 g_option_context_add_main_entries (context, entries, NULL);
682 /* Now try parsing */
683 argv = split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc);
684 argv_copy = copy_stringv (argv, argc);
686 retval = g_option_context_parse (context, &argc, &argv, &error);
687 g_assert_no_error (error);
690 /* Last arg specified is the one that should be stored */
691 g_assert (arg_test6_int64 == G_GINT64_CONSTANT(4294967296));
692 g_assert (arg_test6_int64_2 == G_GINT64_CONSTANT(0xfffffffff));
694 g_strfreev (argv_copy);
696 g_option_context_free (context);
700 callback_parse1 (const gchar *option_name, const gchar *value,
701 gpointer data, GError **error)
703 callback_test1_string = g_strdup (value);
708 callback_test1 (void)
710 GOptionContext *context;
712 GError *error = NULL;
716 GOptionEntry entries [] =
717 { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL },
720 context = g_option_context_new (NULL);
721 g_option_context_add_main_entries (context, entries, NULL);
723 /* Now try parsing */
724 argv = split_string ("program --test foo.txt", &argc);
725 argv_copy = copy_stringv (argv, argc);
727 retval = g_option_context_parse (context, &argc, &argv, &error);
728 g_assert_no_error (error);
731 g_assert (strcmp (callback_test1_string, "foo.txt") == 0);
733 g_free (callback_test1_string);
735 g_strfreev (argv_copy);
737 g_option_context_free (context);
741 callback_parse2 (const gchar *option_name, const gchar *value,
742 gpointer data, GError **error)
744 callback_test2_int++;
749 callback_test2 (void)
751 GOptionContext *context;
753 GError *error = NULL;
757 GOptionEntry entries [] =
758 { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL },
761 context = g_option_context_new (NULL);
762 g_option_context_add_main_entries (context, entries, NULL);
764 /* Now try parsing */
765 argv = split_string ("program --test --test", &argc);
766 argv_copy = copy_stringv (argv, argc);
768 retval = g_option_context_parse (context, &argc, &argv, &error);
769 g_assert_no_error (error);
772 g_assert (callback_test2_int == 2);
774 g_strfreev (argv_copy);
776 g_option_context_free (context);
780 callback_parse_optional (const gchar *option_name, const gchar *value,
781 gpointer data, GError **error)
783 callback_test_optional_boolean = TRUE;
785 callback_test_optional_string = g_strdup (value);
787 callback_test_optional_string = NULL;
792 callback_test_optional_1 (void)
794 GOptionContext *context;
796 GError *error = NULL;
800 GOptionEntry entries [] =
801 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
802 callback_parse_optional, NULL, NULL },
805 context = g_option_context_new (NULL);
806 g_option_context_add_main_entries (context, entries, NULL);
808 /* Now try parsing */
809 argv = split_string ("program --test foo.txt", &argc);
810 argv_copy = copy_stringv (argv, argc);
812 retval = g_option_context_parse (context, &argc, &argv, &error);
813 g_assert_no_error (error);
816 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
818 g_assert (callback_test_optional_boolean);
820 g_free (callback_test_optional_string);
822 g_strfreev (argv_copy);
824 g_option_context_free (context);
828 callback_test_optional_2 (void)
830 GOptionContext *context;
832 GError *error = NULL;
836 GOptionEntry entries [] =
837 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
838 callback_parse_optional, NULL, NULL },
841 context = g_option_context_new (NULL);
842 g_option_context_add_main_entries (context, entries, NULL);
844 /* Now try parsing */
845 argv = split_string ("program --test", &argc);
846 argv_copy = copy_stringv (argv, argc);
848 retval = g_option_context_parse (context, &argc, &argv, &error);
849 g_assert_no_error (error);
852 g_assert (callback_test_optional_string == NULL);
854 g_assert (callback_test_optional_boolean);
856 g_free (callback_test_optional_string);
858 g_strfreev (argv_copy);
860 g_option_context_free (context);
864 callback_test_optional_3 (void)
866 GOptionContext *context;
868 GError *error = NULL;
872 GOptionEntry entries [] =
873 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
874 callback_parse_optional, NULL, NULL },
877 context = g_option_context_new (NULL);
878 g_option_context_add_main_entries (context, entries, NULL);
880 /* Now try parsing */
881 argv = split_string ("program -t foo.txt", &argc);
882 argv_copy = copy_stringv (argv, argc);
884 retval = g_option_context_parse (context, &argc, &argv, &error);
885 g_assert_no_error (error);
888 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
890 g_assert (callback_test_optional_boolean);
892 g_free (callback_test_optional_string);
894 g_strfreev (argv_copy);
896 g_option_context_free (context);
901 callback_test_optional_4 (void)
903 GOptionContext *context;
905 GError *error = NULL;
909 GOptionEntry entries [] =
910 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
911 callback_parse_optional, NULL, NULL },
914 context = g_option_context_new (NULL);
915 g_option_context_add_main_entries (context, entries, NULL);
917 /* Now try parsing */
918 argv = split_string ("program -t", &argc);
919 argv_copy = copy_stringv (argv, argc);
921 retval = g_option_context_parse (context, &argc, &argv, &error);
922 g_assert_no_error (error);
925 g_assert (callback_test_optional_string == NULL);
927 g_assert (callback_test_optional_boolean);
929 g_free (callback_test_optional_string);
931 g_strfreev (argv_copy);
933 g_option_context_free (context);
937 callback_test_optional_5 (void)
939 GOptionContext *context;
942 GError *error = NULL;
946 GOptionEntry entries [] =
947 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
948 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
949 callback_parse_optional, NULL, NULL },
952 context = g_option_context_new (NULL);
953 g_option_context_add_main_entries (context, entries, NULL);
955 /* Now try parsing */
956 argv = split_string ("program --test --dummy", &argc);
957 argv_copy = copy_stringv (argv, argc);
959 retval = g_option_context_parse (context, &argc, &argv, &error);
960 g_assert_no_error (error);
963 g_assert (callback_test_optional_string == NULL);
965 g_assert (callback_test_optional_boolean);
967 g_free (callback_test_optional_string);
969 g_strfreev (argv_copy);
971 g_option_context_free (context);
975 callback_test_optional_6 (void)
977 GOptionContext *context;
980 GError *error = NULL;
984 GOptionEntry entries [] =
985 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
986 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
987 callback_parse_optional, NULL, NULL },
990 context = g_option_context_new (NULL);
991 g_option_context_add_main_entries (context, entries, NULL);
993 /* Now try parsing */
994 argv = split_string ("program -t -d", &argc);
995 argv_copy = copy_stringv (argv, argc);
997 retval = g_option_context_parse (context, &argc, &argv, &error);
998 g_assert_no_error (error);
1001 g_assert (callback_test_optional_string == NULL);
1003 g_assert (callback_test_optional_boolean);
1005 g_free (callback_test_optional_string);
1007 g_strfreev (argv_copy);
1009 g_option_context_free (context);
1013 callback_test_optional_7 (void)
1015 GOptionContext *context;
1018 GError *error = NULL;
1022 GOptionEntry entries [] =
1023 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
1024 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
1025 callback_parse_optional, NULL, NULL },
1028 context = g_option_context_new (NULL);
1029 g_option_context_add_main_entries (context, entries, NULL);
1031 /* Now try parsing */
1032 argv = split_string ("program -td", &argc);
1033 argv_copy = copy_stringv (argv, argc);
1035 retval = g_option_context_parse (context, &argc, &argv, &error);
1036 g_assert_no_error (error);
1039 g_assert (callback_test_optional_string == NULL);
1041 g_assert (callback_test_optional_boolean);
1043 g_free (callback_test_optional_string);
1045 g_strfreev (argv_copy);
1047 g_option_context_free (context);
1051 callback_test_optional_8 (void)
1053 GOptionContext *context;
1056 GError *error = NULL;
1060 GOptionEntry entries [] =
1061 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
1062 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
1063 callback_parse_optional, NULL, NULL },
1066 context = g_option_context_new (NULL);
1067 g_option_context_add_main_entries (context, entries, NULL);
1069 /* Now try parsing */
1070 argv = split_string ("program -dt foo.txt", &argc);
1071 argv_copy = copy_stringv (argv, argc);
1073 retval = g_option_context_parse (context, &argc, &argv, &error);
1074 g_assert_no_error (error);
1077 g_assert (callback_test_optional_string);
1079 g_assert (callback_test_optional_boolean);
1081 g_free (callback_test_optional_string);
1083 g_strfreev (argv_copy);
1085 g_option_context_free (context);
1088 static GPtrArray *callback_remaining_args;
1090 callback_remaining_test1_callback (const gchar *option_name, const gchar *value,
1091 gpointer data, GError **error)
1093 g_ptr_array_add (callback_remaining_args, g_strdup (value));
1098 callback_remaining_test1 (void)
1100 GOptionContext *context;
1102 GError *error = NULL;
1106 GOptionEntry entries [] =
1107 { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL },
1110 callback_remaining_args = g_ptr_array_new ();
1111 context = g_option_context_new (NULL);
1112 g_option_context_add_main_entries (context, entries, NULL);
1114 /* Now try parsing */
1115 argv = split_string ("program foo.txt blah.txt", &argc);
1116 argv_copy = copy_stringv (argv, argc);
1118 retval = g_option_context_parse (context, &argc, &argv, &error);
1119 g_assert_no_error (error);
1122 g_assert (callback_remaining_args->len == 2);
1123 g_assert (strcmp (callback_remaining_args->pdata[0], "foo.txt") == 0);
1124 g_assert (strcmp (callback_remaining_args->pdata[1], "blah.txt") == 0);
1126 g_ptr_array_foreach (callback_remaining_args, (GFunc) g_free, NULL);
1127 g_ptr_array_free (callback_remaining_args, TRUE);
1129 g_strfreev (argv_copy);
1131 g_option_context_free (context);
1135 callback_error (const gchar *option_name, const gchar *value,
1136 gpointer data, GError **error)
1138 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "42");
1143 callback_returns_false (void)
1145 GOptionContext *context;
1147 GError *error = NULL;
1151 GOptionEntry entries [] =
1152 { { "error", 0, 0, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1153 { "error-no-arg", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1154 { "error-optional-arg", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1157 context = g_option_context_new (NULL);
1158 g_option_context_add_main_entries (context, entries, NULL);
1160 /* Now try parsing */
1161 argv = split_string ("program --error value", &argc);
1162 argv_copy = copy_stringv (argv, argc);
1164 retval = g_option_context_parse (context, &argc, &argv, &error);
1165 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1166 g_assert (retval == FALSE);
1167 check_identical_stringv (argv_copy, argv);
1169 g_option_context_free (context);
1170 g_clear_error (&error);
1171 g_strfreev (argv_copy);
1174 /* And again, this time with a no-arg variant */
1175 context = g_option_context_new (NULL);
1176 g_option_context_add_main_entries (context, entries, NULL);
1178 argv = split_string ("program --error-no-arg", &argc);
1179 argv_copy = copy_stringv (argv, argc);
1181 retval = g_option_context_parse (context, &argc, &argv, &error);
1182 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1183 g_assert (retval == FALSE);
1184 check_identical_stringv (argv_copy, argv);
1186 g_option_context_free (context);
1187 g_clear_error (&error);
1188 g_strfreev (argv_copy);
1191 /* And again, this time with a optional arg variant, with argument */
1192 context = g_option_context_new (NULL);
1193 g_option_context_add_main_entries (context, entries, NULL);
1195 argv = split_string ("program --error-optional-arg value", &argc);
1196 argv_copy = copy_stringv (argv, argc);
1198 retval = g_option_context_parse (context, &argc, &argv, &error);
1199 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1200 g_assert (retval == FALSE);
1201 check_identical_stringv (argv_copy, argv);
1203 g_option_context_free (context);
1204 g_clear_error (&error);
1205 g_strfreev (argv_copy);
1208 /* And again, this time with a optional arg variant, without argument */
1209 context = g_option_context_new (NULL);
1210 g_option_context_add_main_entries (context, entries, NULL);
1212 argv = split_string ("program --error-optional-arg", &argc);
1213 argv_copy = copy_stringv (argv, argc);
1215 retval = g_option_context_parse (context, &argc, &argv, &error);
1216 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1217 g_assert (retval == FALSE);
1218 check_identical_stringv (argv_copy, argv);
1220 g_option_context_free (context);
1221 g_clear_error (&error);
1222 g_strfreev (argv_copy);
1230 GOptionContext *context;
1232 GError *error = NULL;
1233 gchar **argv, **argv_copy;
1236 GOptionEntry entries [] =
1237 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1240 context = g_option_context_new (NULL);
1241 g_option_context_set_ignore_unknown_options (context, TRUE);
1242 g_option_context_add_main_entries (context, entries, NULL);
1244 /* Now try parsing */
1245 argv = split_string ("program --test --hello", &argc);
1246 argv_copy = copy_stringv (argv, argc);
1248 retval = g_option_context_parse (context, &argc, &argv, &error);
1249 g_assert_no_error (error);
1253 arg = join_stringv (argc, argv);
1254 g_assert (strcmp (arg, "program --hello") == 0);
1257 g_strfreev (argv_copy);
1259 g_option_context_free (context);
1265 GOptionContext *context;
1267 GError *error = NULL;
1272 GOptionEntry entries [] =
1273 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
1276 context = g_option_context_new (NULL);
1277 g_option_context_set_ignore_unknown_options (context, TRUE);
1278 g_option_context_add_main_entries (context, entries, NULL);
1280 /* Now try parsing */
1281 argv = split_string ("program -test", &argc);
1282 argv_copy = copy_stringv (argv, argc);
1284 retval = g_option_context_parse (context, &argc, &argv, &error);
1285 g_assert_no_error (error);
1289 arg = join_stringv (argc, argv);
1290 g_assert (strcmp (arg, "program -es") == 0);
1293 g_strfreev (argv_copy);
1295 g_option_context_free (context);
1301 GOptionContext *context;
1303 GError *error = NULL;
1304 gchar **argv, **argv_copy;
1307 GOptionEntry entries [] =
1308 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
1311 context = g_option_context_new (NULL);
1312 g_option_context_set_ignore_unknown_options (context, TRUE);
1313 g_option_context_add_main_entries (context, entries, NULL);
1315 /* Now try parsing */
1316 argv = split_string ("program --test foo --hello", &argc);
1317 argv_copy = copy_stringv (argv, argc);
1319 retval = g_option_context_parse (context, &argc, &argv, &error);
1320 g_assert_no_error (error);
1324 arg = join_stringv (argc, argv);
1325 g_assert (strcmp (arg, "program --hello") == 0);
1327 g_assert (strcmp (ignore_test3_string, "foo") == 0);
1328 g_free (ignore_test3_string);
1331 g_strfreev (argv_copy);
1333 g_option_context_free (context);
1337 static array_test1 (void)
1339 GOptionContext *context;
1341 GError *error = NULL;
1345 GOptionEntry entries [] =
1346 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1349 context = g_option_context_new (NULL);
1350 g_option_context_add_main_entries (context, entries, NULL);
1352 /* Now try parsing */
1353 argv = split_string ("program --test foo --test bar", &argc);
1354 argv_copy = copy_stringv (argv, argc);
1356 retval = g_option_context_parse (context, &argc, &argv, &error);
1357 g_assert_no_error (error);
1361 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1362 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1363 g_assert (array_test1_array[2] == NULL);
1365 g_strfreev (array_test1_array);
1367 g_strfreev (argv_copy);
1369 g_option_context_free (context);
1375 GOptionContext *context;
1377 GOptionEntry entries1 [] =
1378 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1380 GOptionEntry entries2 [] =
1381 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1384 context = g_option_context_new (NULL);
1385 g_option_context_add_main_entries (context, entries1, NULL);
1386 g_option_context_add_main_entries (context, entries2, NULL);
1388 g_option_context_free (context);
1394 GOptionContext *context;
1396 context = g_option_context_new (NULL);
1397 g_option_context_parse (context, NULL, NULL, NULL);
1399 g_option_context_free (context);
1405 GOptionContext *context;
1412 context = g_option_context_new (NULL);
1413 g_option_context_parse (context, &argc, &argv, NULL);
1415 g_option_context_free (context);
1418 /* check that non-option arguments are left in argv by default */
1422 GOptionContext *context;
1424 GError *error = NULL;
1428 GOptionEntry entries [] = {
1429 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1433 context = g_option_context_new (NULL);
1434 g_option_context_add_main_entries (context, entries, NULL);
1436 /* Now try parsing */
1437 argv = split_string ("program foo --test bar", &argc);
1438 argv_copy = copy_stringv (argv, argc);
1440 retval = g_option_context_parse (context, &argc, &argv, &error);
1441 g_assert_no_error (error);
1445 g_assert (ignore_test1_boolean);
1446 g_assert (strcmp (argv[0], "program") == 0);
1447 g_assert (strcmp (argv[1], "foo") == 0);
1448 g_assert (strcmp (argv[2], "bar") == 0);
1449 g_assert (argv[3] == NULL);
1451 g_strfreev (argv_copy);
1453 g_option_context_free (context);
1456 /* check that -- works */
1460 GOptionContext *context;
1462 GError *error = NULL;
1466 GOptionEntry entries [] = {
1467 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1471 context = g_option_context_new (NULL);
1472 g_option_context_add_main_entries (context, entries, NULL);
1474 /* Now try parsing */
1475 argv = split_string ("program foo --test -- -bar", &argc);
1476 argv_copy = copy_stringv (argv, argc);
1478 retval = g_option_context_parse (context, &argc, &argv, &error);
1479 g_assert_no_error (error);
1483 g_assert (ignore_test1_boolean);
1484 g_assert (strcmp (argv[0], "program") == 0);
1485 g_assert (strcmp (argv[1], "foo") == 0);
1486 g_assert (strcmp (argv[2], "--") == 0);
1487 g_assert (strcmp (argv[3], "-bar") == 0);
1488 g_assert (argv[4] == NULL);
1490 g_strfreev (argv_copy);
1492 g_option_context_free (context);
1495 /* check that -- stripping works */
1499 GOptionContext *context;
1501 GError *error = NULL;
1505 GOptionEntry entries [] = {
1506 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1510 context = g_option_context_new (NULL);
1511 g_option_context_add_main_entries (context, entries, NULL);
1513 /* Now try parsing */
1514 argv = split_string ("program foo --test -- bar", &argc);
1515 argv_copy = copy_stringv (argv, argc);
1517 retval = g_option_context_parse (context, &argc, &argv, &error);
1518 g_assert_no_error (error);
1522 g_assert (ignore_test1_boolean);
1523 g_assert (strcmp (argv[0], "program") == 0);
1524 g_assert (strcmp (argv[1], "foo") == 0);
1525 g_assert (strcmp (argv[2], "bar") == 0);
1526 g_assert (argv[3] == NULL);
1528 g_strfreev (argv_copy);
1530 g_option_context_free (context);
1536 GOptionContext *context;
1538 GError *error = NULL;
1542 GOptionEntry entries [] = {
1543 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1547 context = g_option_context_new (NULL);
1548 g_option_context_set_ignore_unknown_options (context, TRUE);
1549 g_option_context_add_main_entries (context, entries, NULL);
1551 /* Now try parsing */
1552 argv = split_string ("program foo --test -bar --", &argc);
1553 argv_copy = copy_stringv (argv, argc);
1555 retval = g_option_context_parse (context, &argc, &argv, &error);
1556 g_assert_no_error (error);
1560 g_assert (ignore_test1_boolean);
1561 g_assert (strcmp (argv[0], "program") == 0);
1562 g_assert (strcmp (argv[1], "foo") == 0);
1563 g_assert (strcmp (argv[2], "-bar") == 0);
1564 g_assert (argv[3] == NULL);
1566 g_strfreev (argv_copy);
1568 g_option_context_free (context);
1574 GOptionContext *context;
1576 GError *error = NULL;
1580 GOptionEntry entries [] = {
1581 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1585 context = g_option_context_new (NULL);
1586 g_option_context_add_main_entries (context, entries, NULL);
1588 /* Now try parsing */
1589 argv = split_string ("program --test foo -- bar", &argc);
1590 argv_copy = copy_stringv (argv, argc);
1592 retval = g_option_context_parse (context, &argc, &argv, &error);
1593 g_assert_no_error (error);
1597 g_assert (ignore_test1_boolean);
1598 g_assert (strcmp (argv[0], "program") == 0);
1599 g_assert (strcmp (argv[1], "foo") == 0);
1600 g_assert (strcmp (argv[2], "bar") == 0);
1601 g_assert (argv[3] == NULL);
1603 g_strfreev (argv_copy);
1605 g_option_context_free (context);
1611 GOptionContext *context;
1613 GError *error = NULL;
1617 GOptionEntry entries [] = {
1618 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
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 -- -bar", &argc);
1627 argv_copy = copy_stringv (argv, argc);
1629 retval = g_option_context_parse (context, &argc, &argv, &error);
1630 g_assert_no_error (error);
1634 g_assert (ignore_test1_boolean);
1635 g_assert (strcmp (argv[0], "program") == 0);
1636 g_assert (strcmp (argv[1], "--") == 0);
1637 g_assert (strcmp (argv[2], "-bar") == 0);
1638 g_assert (argv[3] == NULL);
1640 g_strfreev (argv_copy);
1642 g_option_context_free (context);
1646 /* check that G_OPTION_REMAINING collects non-option arguments */
1650 GOptionContext *context;
1652 GError *error = NULL;
1656 GOptionEntry entries [] = {
1657 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1658 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1662 context = g_option_context_new (NULL);
1663 g_option_context_add_main_entries (context, entries, NULL);
1665 /* Now try parsing */
1666 argv = split_string ("program foo --test bar", &argc);
1667 argv_copy = copy_stringv (argv, argc);
1669 retval = g_option_context_parse (context, &argc, &argv, &error);
1670 g_assert_no_error (error);
1674 g_assert (ignore_test1_boolean);
1675 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1676 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1677 g_assert (array_test1_array[2] == NULL);
1679 g_strfreev (array_test1_array);
1681 g_strfreev (argv_copy);
1683 g_option_context_free (context);
1687 /* check that G_OPTION_REMAINING and -- work together */
1691 GOptionContext *context;
1693 GError *error = NULL;
1697 GOptionEntry entries [] = {
1698 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1699 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1703 context = g_option_context_new (NULL);
1704 g_option_context_add_main_entries (context, entries, NULL);
1706 /* Now try parsing */
1707 argv = split_string ("program foo --test -- -bar", &argc);
1708 argv_copy = copy_stringv (argv, argc);
1710 retval = g_option_context_parse (context, &argc, &argv, &error);
1711 g_assert_no_error (error);
1715 g_assert (ignore_test1_boolean);
1716 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1717 g_assert (strcmp (array_test1_array[1], "-bar") == 0);
1718 g_assert (array_test1_array[2] == NULL);
1720 g_strfreev (array_test1_array);
1722 g_strfreev (argv_copy);
1724 g_option_context_free (context);
1727 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1731 GOptionContext *context;
1733 GError *error = NULL;
1737 GOptionEntry entries [] = {
1738 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1739 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
1743 context = g_option_context_new (NULL);
1744 g_option_context_add_main_entries (context, entries, NULL);
1746 /* Now try parsing */
1747 argv = split_string ("program foo --test bar", &argc);
1748 argv_copy = copy_stringv (argv, argc);
1750 retval = g_option_context_parse (context, &argc, &argv, &error);
1751 g_assert_no_error (error);
1755 g_assert (ignore_test1_boolean);
1756 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1757 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1758 g_assert (array_test1_array[2] == NULL);
1760 g_strfreev (array_test1_array);
1762 g_strfreev (argv_copy);
1764 g_option_context_free (context);
1768 unknown_short_test (void)
1770 GOptionContext *context;
1772 GError *error = NULL;
1776 GOptionEntry entries [] = { { NULL } };
1778 g_test_bug ("166609");
1780 context = g_option_context_new (NULL);
1781 g_option_context_add_main_entries (context, entries, NULL);
1783 /* Now try parsing */
1784 argv = split_string ("program -0", &argc);
1785 argv_copy = copy_stringv (argv, argc);
1787 retval = g_option_context_parse (context, &argc, &argv, &error);
1789 g_assert (error != NULL);
1790 g_clear_error (&error);
1792 g_strfreev (argv_copy);
1794 g_option_context_free (context);
1797 /* test that lone dashes are treated as non-options */
1799 lonely_dash_test (void)
1801 GOptionContext *context;
1803 GError *error = NULL;
1808 g_test_bug ("168008");
1810 context = g_option_context_new (NULL);
1812 /* Now try parsing */
1813 argv = split_string ("program -", &argc);
1814 argv_copy = copy_stringv (argv, argc);
1816 retval = g_option_context_parse (context, &argc, &argv, &error);
1817 g_assert_no_error (error);
1820 g_assert (argv[1] && strcmp (argv[1], "-") == 0);
1822 g_strfreev (argv_copy);
1824 g_option_context_free (context);
1828 missing_arg_test (void)
1830 GOptionContext *context;
1832 GError *error = NULL;
1837 GOptionEntry entries [] =
1838 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1841 g_test_bug ("305576");
1843 context = g_option_context_new (NULL);
1844 g_option_context_add_main_entries (context, entries, NULL);
1846 /* Now try parsing */
1847 argv = split_string ("program --test", &argc);
1848 argv_copy = copy_stringv (argv, argc);
1850 retval = g_option_context_parse (context, &argc, &argv, &error);
1851 g_assert (retval == FALSE);
1852 g_assert (error != NULL);
1853 /* An error occurred, so argv has not been changed */
1854 check_identical_stringv (argv_copy, argv);
1855 g_clear_error (&error);
1857 g_strfreev (argv_copy);
1860 /* Try parsing again */
1861 argv = split_string ("program -t", &argc);
1862 argv_copy = copy_stringv (argv, argc);
1864 retval = g_option_context_parse (context, &argc, &argv, &error);
1865 g_assert (retval == FALSE);
1866 g_assert (error != NULL);
1867 /* An error occurred, so argv has not been changed */
1868 check_identical_stringv (argv_copy, argv);
1869 g_clear_error (&error);
1871 g_strfreev (argv_copy);
1873 g_option_context_free (context);
1876 static gchar *test_arg;
1878 static gboolean cb (const gchar *option_name,
1883 test_arg = g_strdup (value);
1888 dash_arg_test (void)
1890 GOptionContext *context;
1892 GError *error = NULL;
1896 gboolean argb = FALSE;
1897 GOptionEntry entries [] =
1898 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, cb, NULL, NULL },
1899 { "three", '3', 0, G_OPTION_ARG_NONE, &argb, NULL, NULL },
1902 g_test_bug ("577638");
1904 context = g_option_context_new (NULL);
1905 g_option_context_add_main_entries (context, entries, NULL);
1907 /* Now try parsing */
1908 argv = split_string ("program --test=-3", &argc);
1909 argv_copy = copy_stringv (argv, argc);
1913 retval = g_option_context_parse (context, &argc, &argv, &error);
1915 g_assert_no_error (error);
1916 g_assert_cmpstr (test_arg, ==, "-3");
1918 g_strfreev (argv_copy);
1923 /* Try parsing again */
1924 argv = split_string ("program --test -3", &argc);
1925 argv_copy = copy_stringv (argv, argc);
1928 retval = g_option_context_parse (context, &argc, &argv, &error);
1929 g_assert_no_error (error);
1931 g_assert_cmpstr (test_arg, ==, NULL);
1933 g_option_context_free (context);
1934 g_strfreev (argv_copy);
1941 GOptionContext *context;
1943 GOptionEntry entries [] =
1944 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1947 context = g_option_context_new (NULL);
1948 g_option_context_add_main_entries (context, entries, NULL);
1950 g_assert (g_option_context_get_help_enabled (context));
1951 g_assert (!g_option_context_get_ignore_unknown_options (context));
1952 g_assert_cmpstr (g_option_context_get_summary (context), ==, NULL);
1953 g_assert_cmpstr (g_option_context_get_description (context), ==, NULL);
1955 g_option_context_set_help_enabled (context, FALSE);
1956 g_option_context_set_ignore_unknown_options (context, TRUE);
1957 g_option_context_set_summary (context, "summary");
1958 g_option_context_set_description(context, "description");
1960 g_assert (!g_option_context_get_help_enabled (context));
1961 g_assert (g_option_context_get_ignore_unknown_options (context));
1962 g_assert_cmpstr (g_option_context_get_summary (context), ==, "summary");
1963 g_assert_cmpstr (g_option_context_get_description (context), ==, "description");
1965 g_option_context_free (context);
1969 gboolean parameter_seen;
1970 gboolean summary_seen;
1971 gboolean description_seen;
1975 static const gchar *
1976 translate_func (const gchar *str,
1979 TranslateData *d = data;
1981 if (strcmp (str, "parameter") == 0)
1982 d->parameter_seen = TRUE;
1983 else if (strcmp (str, "summary") == 0)
1984 d->summary_seen = TRUE;
1985 else if (strcmp (str, "description") == 0)
1986 d->description_seen = TRUE;
1992 destroy_notify (gpointer data)
1994 TranslateData *d = data;
1996 d->destroyed = TRUE;
2000 test_translate (void)
2002 GOptionContext *context;
2004 GOptionEntry entries [] =
2005 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2007 TranslateData data = { 0, };
2010 context = g_option_context_new ("parameter");
2011 g_option_context_add_main_entries (context, entries, NULL);
2012 g_option_context_set_summary (context, "summary");
2013 g_option_context_set_description (context, "description");
2015 g_option_context_set_translate_func (context, translate_func, &data, destroy_notify);
2017 str = g_option_context_get_help (context, FALSE, NULL);
2019 g_option_context_free (context);
2021 g_assert (data.parameter_seen);
2022 g_assert (data.summary_seen);
2023 g_assert (data.description_seen);
2024 g_assert (data.destroyed);
2030 GOptionContext *context;
2031 GOptionGroup *group;
2034 gchar **sarr = NULL;
2035 GOptionEntry entries[] = {
2036 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Test tests", "Argument to use in test" },
2037 { "test2", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, NULL, "Tests also", NULL },
2038 { "frob", 0, 0, G_OPTION_ARG_NONE, NULL, "Main frob", NULL },
2039 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
2042 GOptionEntry group_entries[] = {
2043 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Group test", "Group test arg" },
2044 { "frob", 0, G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_NONE, NULL, "Group frob", NULL },
2048 context = g_option_context_new ("blabla");
2049 g_option_context_add_main_entries (context, entries, NULL);
2050 g_option_context_set_summary (context, "Summary");
2051 g_option_context_set_description (context, "Description");
2053 group = g_option_group_new ("group1", "Group1-description", "Group1-help", NULL, NULL);
2054 g_option_group_add_entries (group, group_entries);
2056 g_option_context_add_group (context, group);
2058 str = g_option_context_get_help (context, FALSE, NULL);
2059 g_assert (strstr (str, "blabla") != NULL);
2060 g_assert (strstr (str, "Test tests") != NULL);
2061 g_assert (strstr (str, "Argument to use in test") != NULL);
2062 g_assert (strstr (str, "Tests also") == NULL);
2063 g_assert (strstr (str, "REST") != NULL);
2064 g_assert (strstr (str, "Summary") != NULL);
2065 g_assert (strstr (str, "Description") != NULL);
2066 g_assert (strstr (str, "--help") != NULL);
2067 g_assert (strstr (str, "--help-all") != NULL);
2068 g_assert (strstr (str, "--help-group1") != NULL);
2069 g_assert (strstr (str, "Group1-description") != NULL);
2070 g_assert (strstr (str, "Group1-help") != NULL);
2071 g_assert (strstr (str, "Group test arg") != NULL);
2072 g_assert (strstr (str, "Group frob") != NULL);
2073 g_assert (strstr (str, "Main frob") != NULL);
2074 g_assert (strstr (str, "--frob") != NULL);
2075 g_assert (strstr (str, "--group1-test") != NULL);
2076 g_assert (strstr (str, "--group1-frob") == NULL);
2079 g_option_context_free (context);
2083 test_help_no_options (void)
2085 GOptionContext *context;
2086 gchar **sarr = NULL;
2087 GOptionEntry entries[] = {
2088 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
2093 context = g_option_context_new ("blabla");
2094 g_option_context_add_main_entries (context, entries, NULL);
2096 str = g_option_context_get_help (context, FALSE, NULL);
2097 g_assert (strstr (str, "blabla") != NULL);
2098 g_assert (strstr (str, "REST") != NULL);
2099 g_assert (strstr (str, "Help Options") != NULL);
2100 g_assert (strstr (str, "Application Options") == NULL);
2103 g_option_context_free (context);
2107 test_help_no_help_options (void)
2109 GOptionContext *context;
2110 GOptionGroup *group;
2113 gchar **sarr = NULL;
2114 GOptionEntry entries[] = {
2115 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Test tests", "Argument to use in test" },
2116 { "test2", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, NULL, "Tests also", NULL },
2117 { "frob", 0, 0, G_OPTION_ARG_NONE, NULL, "Main frob", NULL },
2118 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
2121 GOptionEntry group_entries[] = {
2122 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Group test", "Group test arg" },
2123 { "frob", 0, G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_NONE, NULL, "Group frob", NULL },
2127 g_test_bug ("697652");
2129 context = g_option_context_new ("blabla");
2130 g_option_context_add_main_entries (context, entries, NULL);
2131 g_option_context_set_summary (context, "Summary");
2132 g_option_context_set_description (context, "Description");
2133 g_option_context_set_help_enabled (context, FALSE);
2135 group = g_option_group_new ("group1", "Group1-description", "Group1-help", NULL, NULL);
2136 g_option_group_add_entries (group, group_entries);
2138 g_option_context_add_group (context, group);
2140 str = g_option_context_get_help (context, FALSE, NULL);
2141 g_assert (strstr (str, "blabla") != NULL);
2142 g_assert (strstr (str, "Test tests") != NULL);
2143 g_assert (strstr (str, "Argument to use in test") != NULL);
2144 g_assert (strstr (str, "Tests also") == NULL);
2145 g_assert (strstr (str, "REST") != NULL);
2146 g_assert (strstr (str, "Summary") != NULL);
2147 g_assert (strstr (str, "Description") != NULL);
2148 g_assert (strstr (str, "Help Options") == NULL);
2149 g_assert (strstr (str, "--help") == NULL);
2150 g_assert (strstr (str, "--help-all") == NULL);
2151 g_assert (strstr (str, "--help-group1") == NULL);
2152 g_assert (strstr (str, "Group1-description") != NULL);
2153 g_assert (strstr (str, "Group1-help") == NULL);
2154 g_assert (strstr (str, "Group test arg") != NULL);
2155 g_assert (strstr (str, "Group frob") != NULL);
2156 g_assert (strstr (str, "Main frob") != NULL);
2157 g_assert (strstr (str, "--frob") != NULL);
2158 g_assert (strstr (str, "--group1-test") != NULL);
2159 g_assert (strstr (str, "--group1-frob") == NULL);
2162 g_option_context_free (context);
2166 set_bool (gpointer data)
2174 test_main_group (void)
2176 GOptionContext *context;
2177 GOptionGroup *group;
2180 context = g_option_context_new (NULL);
2181 g_assert (g_option_context_get_main_group (context) == NULL);
2182 group = g_option_group_new ("name", "description", "hlep", &b, set_bool);
2183 g_option_context_add_group (context, group);
2184 group = g_option_group_new ("name2", "description", "hlep", NULL, NULL);
2185 g_option_context_add_group (context, group);
2186 g_assert (g_option_context_get_main_group (context) == NULL);
2187 group = g_option_group_new ("name", "description", "hlep", NULL, NULL);
2188 g_option_context_set_main_group (context, group);
2189 g_assert (g_option_context_get_main_group (context) == group);
2191 g_option_context_free (context);
2196 static gboolean error_func_called = FALSE;
2199 error_func (GOptionContext *context,
2200 GOptionGroup *group,
2204 g_assert_cmpint (GPOINTER_TO_INT(data), ==, 1234);
2205 error_func_called = TRUE;
2209 test_error_hook (void)
2211 GOptionContext *context;
2213 GOptionEntry entries [] =
2214 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2216 GOptionGroup *group;
2221 GError *error = NULL;
2223 context = g_option_context_new (NULL);
2224 group = g_option_group_new ("name", "description", "hlep", GINT_TO_POINTER(1234), NULL);
2225 g_option_group_add_entries (group, entries);
2226 g_option_context_set_main_group (context, group);
2227 g_option_group_set_error_hook (g_option_context_get_main_group (context),
2230 argv = split_string ("program --test", &argc);
2231 argv_copy = copy_stringv (argv, argc);
2233 retval = g_option_context_parse (context, &argc, &argv, &error);
2234 g_assert (retval == FALSE);
2235 g_assert (error != NULL);
2236 /* An error occurred, so argv has not been changed */
2237 check_identical_stringv (argv_copy, argv);
2238 g_clear_error (&error);
2240 g_assert (error_func_called);
2242 g_strfreev (argv_copy);
2244 g_option_context_free (context);
2248 test_group_parse (void)
2250 GOptionContext *context;
2251 GOptionGroup *group;
2257 GOptionEntry entries[] = {
2258 { "test", 't', 0, G_OPTION_ARG_STRING, &arg1, NULL, NULL },
2259 { "faz", 'f', 0, G_OPTION_ARG_STRING, &arg2, NULL, NULL },
2262 GOptionEntry group_entries[] = {
2263 { "test", 0, 0, G_OPTION_ARG_STRING, &arg3, NULL, NULL },
2264 { "frob", 'f', 0, G_OPTION_ARG_STRING, &arg4, NULL, NULL },
2265 { "faz", 'z', 0, G_OPTION_ARG_STRING, &arg5, NULL, NULL },
2268 gchar **argv, **orig_argv;
2270 GError *error = NULL;
2273 context = g_option_context_new (NULL);
2274 g_option_context_add_main_entries (context, entries, NULL);
2275 group = g_option_group_new ("group", "A group", "help for group", NULL, NULL);
2276 g_option_group_add_entries (group, group_entries);
2277 g_option_context_add_group (context, group);
2279 argv = split_string ("program --test arg1 -f arg2 --group-test arg3 --frob arg4 -z arg5", &argc);
2280 orig_argv = g_memdup (argv, (argc + 1) * sizeof (char *));
2282 retval = g_option_context_parse (context, &argc, &argv, &error);
2284 g_assert_no_error (error);
2286 g_assert_cmpstr (arg1, ==, "arg1");
2287 g_assert_cmpstr (arg2, ==, "arg2");
2288 g_assert_cmpstr (arg3, ==, "arg3");
2289 g_assert_cmpstr (arg4, ==, "arg4");
2290 g_assert_cmpstr (arg5, ==, "arg5");
2299 g_strfreev (orig_argv);
2300 g_option_context_free (context);
2304 flag_reverse_string (void)
2306 GOptionContext *context;
2308 GOptionEntry entries [] =
2309 { { "test", 't', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2314 GError *error = NULL;
2316 if (!g_test_undefined ())
2319 context = g_option_context_new (NULL);
2321 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
2322 "*ignoring reverse flag*");
2323 g_option_context_add_main_entries (context, entries, NULL);
2324 g_test_assert_expected_messages ();
2326 argv = split_string ("program --test bla", &argc);
2328 retval = g_option_context_parse_strv (context, &argv, &error);
2329 g_assert (retval == TRUE);
2330 g_assert_no_error (error);
2332 g_option_context_free (context);
2337 flag_optional_int (void)
2339 GOptionContext *context;
2341 GOptionEntry entries [] =
2342 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &arg, NULL, NULL },
2347 GError *error = NULL;
2349 if (!g_test_undefined ())
2352 context = g_option_context_new (NULL);
2354 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
2355 "*ignoring no-arg, optional-arg or filename flags*");
2356 g_option_context_add_main_entries (context, entries, NULL);
2357 g_test_assert_expected_messages ();
2359 argv = split_string ("program --test 5", &argc);
2361 retval = g_option_context_parse_strv (context, &argv, &error);
2362 g_assert (retval == TRUE);
2363 g_assert_no_error (error);
2365 g_option_context_free (context);
2369 short_remaining (void)
2371 gboolean ignore = FALSE;
2372 gboolean remaining = FALSE;
2375 gchar** files = NULL;
2376 GError* error = NULL;
2377 GOptionEntry entries[] =
2379 { "ignore", 'i', 0, G_OPTION_ARG_NONE, &ignore, NULL, NULL },
2380 { "remaining", 'r', 0, G_OPTION_ARG_NONE, &remaining, NULL, NULL },
2381 { "number", 'n', 0, G_OPTION_ARG_INT, &number, NULL, NULL },
2382 { "text", 't', 0, G_OPTION_ARG_STRING, &text, NULL, NULL },
2383 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, NULL, NULL },
2386 GOptionContext* context;
2387 gchar **argv, **argv_copy;
2390 g_test_bug ("729563");
2392 argv = split_string ("program -ri -n 4 -t hello file1 file2", &argc);
2393 argv_copy = copy_stringv (argv, argc);
2395 context = g_option_context_new (NULL);
2397 g_option_context_add_main_entries (context, entries, NULL);
2398 g_option_context_set_ignore_unknown_options (context, TRUE);
2400 g_option_context_parse (context, &argc, &argv, &error);
2401 g_assert_no_error (error);
2404 g_assert (remaining);
2405 g_assert_cmpint (number, ==, 4);
2406 g_assert_cmpstr (text, ==, "hello");
2407 g_assert_cmpstr (files[0], ==, "file1");
2408 g_assert_cmpstr (files[1], ==, "file2");
2409 g_assert (files[2] == NULL);
2413 g_strfreev (argv_copy);
2415 g_option_context_free (context);
2425 g_test_init (&argc, &argv, NULL);
2427 g_test_bug_base ("http://bugzilla.gnome.org/");
2429 g_test_add_func ("/option/help/options", test_help);
2430 g_test_add_func ("/option/help/no-options", test_help_no_options);
2431 g_test_add_func ("/option/help/no-help-options", test_help_no_help_options);
2433 g_test_add_func ("/option/basic", test_basic);
2434 g_test_add_func ("/option/translate", test_translate);
2436 g_test_add_func ("/option/group/captions", test_group_captions);
2437 for (i = 0; i < 4; i++)
2439 test_name = g_strdup_printf ("/option/group/captions/subprocess/help-%d", i);
2440 g_test_add_data_func (test_name, GINT_TO_POINTER (i),
2441 test_group_captions_help);
2443 test_name = g_strdup_printf ("/option/group/captions/subprocess/help-all-%d", i);
2444 g_test_add_data_func (test_name, GINT_TO_POINTER (i),
2445 test_group_captions_help_all);
2447 test_name = g_strdup_printf ("/option/group/captions/subprocess/help-test-%d", i);
2448 g_test_add_data_func (test_name, GINT_TO_POINTER (i),
2449 test_group_captions_help_test);
2454 g_test_add_func ("/option/group/main", test_main_group);
2455 g_test_add_func ("/option/group/error-hook", test_error_hook);
2456 g_test_add_func ("/option/group/parse", test_group_parse);
2458 /* Test that restoration on failure works */
2459 g_test_add_func ("/option/restoration/int", error_test1);
2460 g_test_add_func ("/option/restoration/string", error_test2);
2461 g_test_add_func ("/option/restoration/boolean", error_test3);
2463 /* Test that special argument parsing works */
2464 g_test_add_func ("/option/arg/repetition/int", arg_test1);
2465 g_test_add_func ("/option/arg/repetition/string", arg_test2);
2466 g_test_add_func ("/option/arg/repetition/filename", arg_test3);
2467 g_test_add_func ("/option/arg/repetition/double", arg_test4);
2468 g_test_add_func ("/option/arg/repetition/locale", arg_test5);
2469 g_test_add_func ("/option/arg/repetition/int64", arg_test6);
2471 /* Test string arrays */
2472 g_test_add_func ("/option/arg/array/string", array_test1);
2474 /* Test callback args */
2475 g_test_add_func ("/option/arg/callback/string", callback_test1);
2476 g_test_add_func ("/option/arg/callback/count", callback_test2);
2478 /* Test optional arg flag for callback */
2479 g_test_add_func ("/option/arg/callback/optional1", callback_test_optional_1);
2480 g_test_add_func ("/option/arg/callback/optional2", callback_test_optional_2);
2481 g_test_add_func ("/option/arg/callback/optional3", callback_test_optional_3);
2482 g_test_add_func ("/option/arg/callback/optional4", callback_test_optional_4);
2483 g_test_add_func ("/option/arg/callback/optional5", callback_test_optional_5);
2484 g_test_add_func ("/option/arg/callback/optional6", callback_test_optional_6);
2485 g_test_add_func ("/option/arg/callback/optional7", callback_test_optional_7);
2486 g_test_add_func ("/option/arg/callback/optional8", callback_test_optional_8);
2488 /* Test callback with G_OPTION_REMAINING */
2489 g_test_add_func ("/option/arg/remaining/callback", callback_remaining_test1);
2491 /* Test callbacks which return FALSE */
2492 g_test_add_func ("/option/arg/remaining/callback-false", callback_returns_false);
2494 /* Test ignoring options */
2495 g_test_add_func ("/option/arg/ignore/long", ignore_test1);
2496 g_test_add_func ("/option/arg/ignore/short", ignore_test2);
2497 g_test_add_func ("/option/arg/ignore/arg", ignore_test3);
2498 g_test_add_func ("/option/context/add", add_test1);
2500 /* Test parsing empty args */
2501 /* Note there used to be an empty1 here, but it effectively moved
2502 * to option-argv0.c.
2504 g_test_add_func ("/option/context/empty2", empty_test2);
2505 g_test_add_func ("/option/context/empty3", empty_test3);
2507 /* Test handling of rest args */
2508 g_test_add_func ("/option/arg/rest/non-option", rest_test1);
2509 g_test_add_func ("/option/arg/rest/separator1", rest_test2);
2510 g_test_add_func ("/option/arg/rest/separator2", rest_test2a);
2511 g_test_add_func ("/option/arg/rest/separator3", rest_test2b);
2512 g_test_add_func ("/option/arg/rest/separator4", rest_test2c);
2513 g_test_add_func ("/option/arg/rest/separator5", rest_test2d);
2514 g_test_add_func ("/option/arg/remaining/non-option", rest_test3);
2515 g_test_add_func ("/option/arg/remaining/separator", rest_test4);
2516 g_test_add_func ("/option/arg/remaining/array", rest_test5);
2518 /* Test some invalid flag combinations */
2519 g_test_add_func ("/option/arg/reverse-string", flag_reverse_string);
2520 g_test_add_func ("/option/arg/optional-int", flag_optional_int);
2522 /* regression tests for individual bugs */
2523 g_test_add_func ("/option/bug/unknown-short", unknown_short_test);
2524 g_test_add_func ("/option/bug/lonely-dash", lonely_dash_test);
2525 g_test_add_func ("/option/bug/missing-arg", missing_arg_test);
2526 g_test_add_func ("/option/bug/dash-arg", dash_arg_test);
2527 g_test_add_func ("/option/bug/short-remaining", short_remaining);
2529 return g_test_run();