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.
32 static GOptionEntry main_entries[] = {
33 { "main-switch", 0, 0,
34 G_OPTION_ARG_NONE, NULL,
35 "A switch that is in the main group", NULL },
39 static GOptionEntry group_entries[] = {
40 { "test-switch", 0, 0,
41 G_OPTION_ARG_NONE, NULL,
42 "A switch that is in the test group", NULL },
46 static GOptionContext *
47 make_options (int test_number)
49 GOptionContext *options;
50 GOptionGroup *group = NULL;
51 gboolean have_main_entries = (0 != (test_number & 1));
52 gboolean have_test_entries = (0 != (test_number & 2));
54 options = g_option_context_new (NULL);
56 if (have_main_entries)
57 g_option_context_add_main_entries (options, main_entries, NULL);
58 if (have_test_entries)
60 group = g_option_group_new ("test", "Test Options",
61 "Show all test options",
63 g_option_context_add_group (options, group);
64 g_option_group_add_entries (group, group_entries);
71 print_help (GOptionContext *options, gchar **argv)
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" };
119 g_test_bug ("504142");
121 for (i = 0; i < 4; ++i)
123 gboolean have_main_entries = (0 != (i & 1));
124 gboolean have_test_entries = (0 != (i & 2));
126 for (j = 0; j < G_N_ELEMENTS (test_name_base); ++j)
128 GTestSubprocessFlags trap_flags = 0;
129 gboolean expect_main_description = FALSE;
130 gboolean expect_main_switch = FALSE;
131 gboolean expect_test_description = FALSE;
132 gboolean expect_test_switch = FALSE;
133 gboolean expect_test_group = FALSE;
135 if (g_test_verbose ())
136 trap_flags |= G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR;
138 test_name = g_strdup_printf ("/option/group/captions/subprocess/%s-%u",
139 test_name_base[j], i);
140 g_test_trap_subprocess (test_name, 0, trap_flags);
142 g_test_trap_assert_passed ();
143 g_test_trap_assert_stderr ("");
148 g_assert_cmpstr ("help", ==, test_name_base[j]);
149 expect_main_switch = have_main_entries;
150 expect_test_group = have_test_entries;
154 g_assert_cmpstr ("help-all", ==, test_name_base[j]);
155 expect_main_switch = have_main_entries;
156 expect_test_switch = have_test_entries;
157 expect_test_group = have_test_entries;
161 g_assert_cmpstr ("help-test", ==, test_name_base[j]);
162 expect_test_switch = have_test_entries;
166 g_assert_not_reached ();
170 expect_main_description |= expect_main_switch;
171 expect_test_description |= expect_test_switch;
173 if (expect_main_description)
174 g_test_trap_assert_stdout ("*Application Options*");
176 g_test_trap_assert_stdout_unmatched ("*Application Options*");
177 if (expect_main_switch)
178 g_test_trap_assert_stdout ("*--main-switch*");
180 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
182 if (expect_test_description)
183 g_test_trap_assert_stdout ("*Test Options*");
185 g_test_trap_assert_stdout_unmatched ("*Test Options*");
186 if (expect_test_switch)
187 g_test_trap_assert_stdout ("*--test-switch*");
189 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
191 if (expect_test_group)
192 g_test_trap_assert_stdout ("*--help-test*");
194 g_test_trap_assert_stdout_unmatched ("*--help-test*");
200 char *error_test2_string;
201 gboolean error_test3_boolean;
204 gchar *arg_test2_string;
205 gchar *arg_test3_filename;
206 gdouble arg_test4_double;
207 gdouble arg_test5_double;
208 gint64 arg_test6_int64;
209 gint64 arg_test6_int64_2;
211 gchar *callback_test1_string;
212 int callback_test2_int;
214 gchar *callback_test_optional_string;
215 gboolean callback_test_optional_boolean;
217 gchar **array_test1_array;
219 gboolean ignore_test1_boolean;
220 gboolean ignore_test2_boolean;
221 gchar *ignore_test3_string;
224 split_string (const char *str, int *argc)
229 argv = g_strsplit (str, " ", 0);
231 for (len = 0; argv[len] != NULL; len++);
240 join_stringv (int argc, char **argv)
245 str = g_string_new (NULL);
247 for (i = 0; i < argc; i++)
249 g_string_append (str, argv[i]);
252 g_string_append_c (str, ' ');
255 return g_string_free (str, FALSE);
258 /* Performs a shallow copy */
260 copy_stringv (char **argv, int argc)
262 return g_memdup2 (argv, sizeof (char *) * (argc + 1));
266 check_identical_stringv (gchar **before, gchar **after)
270 /* Not only is it the same string... */
271 for (i = 0; before[i] != NULL; i++)
272 g_assert_cmpstr (before[i], ==, after[i]);
274 /* ... it is actually the same pointer */
275 for (i = 0; before[i] != NULL; i++)
276 g_assert (before[i] == after[i]);
278 g_assert (after[i] == NULL);
283 error_test1_pre_parse (GOptionContext *context,
288 g_assert (error_test1_int == 0x12345678);
294 error_test1_post_parse (GOptionContext *context,
299 g_assert (error_test1_int == 20);
301 /* Set an error in the post hook */
302 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
310 GOptionContext *context;
312 GError *error = NULL;
316 GOptionGroup *main_group;
317 GOptionEntry entries [] =
318 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
321 error_test1_int = 0x12345678;
323 context = g_option_context_new (NULL);
324 g_option_context_add_main_entries (context, entries, NULL);
326 /* Set pre and post parse hooks */
327 main_group = g_option_context_get_main_group (context);
328 g_option_group_set_parse_hooks (main_group,
329 error_test1_pre_parse, error_test1_post_parse);
331 /* Now try parsing */
332 argv = split_string ("program --test 20", &argc);
333 argv_copy = copy_stringv (argv, argc);
335 retval = g_option_context_parse (context, &argc, &argv, &error);
336 g_assert (retval == FALSE);
337 g_assert (error != NULL);
338 /* An error occurred, so argv has not been changed */
339 check_identical_stringv (argv_copy, argv);
340 g_clear_error (&error);
342 /* On failure, values should be reset */
343 g_assert (error_test1_int == 0x12345678);
345 g_strfreev (argv_copy);
347 g_option_context_free (context);
351 error_test2_pre_parse (GOptionContext *context,
356 g_assert (strcmp (error_test2_string, "foo") == 0);
362 error_test2_post_parse (GOptionContext *context,
367 g_assert (strcmp (error_test2_string, "bar") == 0);
369 /* Set an error in the post hook */
370 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
378 GOptionContext *context;
380 GError *error = NULL;
384 GOptionGroup *main_group;
385 GOptionEntry entries [] =
386 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
389 error_test2_string = "foo";
391 context = g_option_context_new (NULL);
392 g_option_context_add_main_entries (context, entries, NULL);
394 /* Set pre and post parse hooks */
395 main_group = g_option_context_get_main_group (context);
396 g_option_group_set_parse_hooks (main_group,
397 error_test2_pre_parse, error_test2_post_parse);
399 /* Now try parsing */
400 argv = split_string ("program --test bar", &argc);
401 argv_copy = copy_stringv (argv, argc);
402 retval = g_option_context_parse (context, &argc, &argv, &error);
404 g_assert (retval == FALSE);
405 g_assert (error != NULL);
406 check_identical_stringv (argv_copy, argv);
407 g_clear_error (&error);
409 g_assert (strcmp (error_test2_string, "foo") == 0);
411 g_strfreev (argv_copy);
413 g_option_context_free (context);
417 error_test3_pre_parse (GOptionContext *context,
422 g_assert (!error_test3_boolean);
428 error_test3_post_parse (GOptionContext *context,
433 g_assert (error_test3_boolean);
435 /* Set an error in the post hook */
436 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
444 GOptionContext *context;
446 GError *error = NULL;
450 GOptionGroup *main_group;
451 GOptionEntry entries [] =
452 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
455 error_test3_boolean = FALSE;
457 context = g_option_context_new (NULL);
458 g_option_context_add_main_entries (context, entries, NULL);
460 /* Set pre and post parse hooks */
461 main_group = g_option_context_get_main_group (context);
462 g_option_group_set_parse_hooks (main_group,
463 error_test3_pre_parse, error_test3_post_parse);
465 /* Now try parsing */
466 argv = split_string ("program --test", &argc);
467 argv_copy = copy_stringv (argv, argc);
468 retval = g_option_context_parse (context, &argc, &argv, &error);
470 g_assert (retval == FALSE);
471 g_assert (error != NULL);
472 check_identical_stringv (argv_copy, argv);
473 g_clear_error (&error);
475 g_assert (!error_test3_boolean);
477 g_strfreev (argv_copy);
479 g_option_context_free (context);
485 GOptionContext *context;
487 GError *error = NULL;
491 GOptionEntry entries [] =
492 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
495 context = g_option_context_new (NULL);
496 g_option_context_add_main_entries (context, entries, NULL);
498 /* Now try parsing */
499 argv = split_string ("program --test 20 --test 30", &argc);
500 argv_copy = copy_stringv (argv, argc);
502 retval = g_option_context_parse (context, &argc, &argv, &error);
503 g_assert_no_error (error);
506 /* Last arg specified is the one that should be stored */
507 g_assert (arg_test1_int == 30);
509 /* We free all of the strings in a copy of argv, because now argv is a
510 * subset - some have been removed in-place
512 g_strfreev (argv_copy);
514 g_option_context_free (context);
520 GOptionContext *context;
522 GError *error = NULL;
526 GOptionEntry entries [] =
527 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
530 context = g_option_context_new (NULL);
531 g_option_context_add_main_entries (context, entries, NULL);
533 /* Now try parsing */
534 argv = split_string ("program --test foo --test bar", &argc);
535 argv_copy = copy_stringv (argv, argc);
537 retval = g_option_context_parse (context, &argc, &argv, &error);
538 g_assert_no_error (error);
541 /* Last arg specified is the one that should be stored */
542 g_assert (strcmp (arg_test2_string, "bar") == 0);
544 g_free (arg_test2_string);
546 g_strfreev (argv_copy);
548 g_option_context_free (context);
554 GOptionContext *context;
556 GError *error = NULL;
560 GOptionEntry entries [] =
561 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
564 context = g_option_context_new (NULL);
565 g_option_context_add_main_entries (context, entries, NULL);
567 /* Now try parsing */
568 argv = split_string ("program --test foo.txt", &argc);
569 argv_copy = copy_stringv (argv, argc);
571 retval = g_option_context_parse (context, &argc, &argv, &error);
572 g_assert_no_error (error);
575 /* Last arg specified is the one that should be stored */
576 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
578 g_free (arg_test3_filename);
580 g_strfreev (argv_copy);
582 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.UTF-8";
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, 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, 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, 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, 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 an 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 an 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);
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);
1827 /* test that three dashes are treated as non-options */
1829 triple_dash_test (void)
1831 GOptionContext *context;
1832 GOptionGroup *group;
1834 GError *error = NULL;
1839 GOptionEntry entries [] =
1840 { { "foo", 0, 0, G_OPTION_ARG_INT, &arg1, NULL, NULL},
1843 GOptionEntry group_entries [] =
1844 { { "test", 0, 0, G_OPTION_ARG_INT, &arg2, NULL, NULL},
1848 context = g_option_context_new (NULL);
1849 g_option_context_add_main_entries (context, entries, NULL);
1851 group = g_option_group_new ("group", "Group description", "Group help", NULL, NULL);
1852 g_option_group_add_entries (group, group_entries);
1854 g_option_context_add_group (context, group);
1856 /* Now try parsing */
1857 argv = split_string ("program ---test 42", &argc);
1858 argv_copy = copy_stringv (argv, argc);
1860 retval = g_option_context_parse (context, &argc, &argv, &error);
1861 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION);
1862 g_assert (retval == FALSE);
1864 g_option_context_free (context);
1865 g_clear_error (&error);
1866 g_strfreev (argv_copy);
1871 missing_arg_test (void)
1873 GOptionContext *context;
1875 GError *error = NULL;
1880 GOptionEntry entries [] =
1881 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1884 g_test_bug ("305576");
1886 context = g_option_context_new (NULL);
1887 g_option_context_add_main_entries (context, entries, NULL);
1889 /* Now try parsing */
1890 argv = split_string ("program --test", &argc);
1891 argv_copy = copy_stringv (argv, argc);
1893 retval = g_option_context_parse (context, &argc, &argv, &error);
1894 g_assert (retval == FALSE);
1895 g_assert (error != NULL);
1896 /* An error occurred, so argv has not been changed */
1897 check_identical_stringv (argv_copy, argv);
1898 g_clear_error (&error);
1900 g_strfreev (argv_copy);
1903 /* Try parsing again */
1904 argv = split_string ("program -t", &argc);
1905 argv_copy = copy_stringv (argv, argc);
1907 retval = g_option_context_parse (context, &argc, &argv, &error);
1908 g_assert (retval == FALSE);
1909 g_assert (error != NULL);
1910 /* An error occurred, so argv has not been changed */
1911 check_identical_stringv (argv_copy, argv);
1912 g_clear_error (&error);
1914 g_strfreev (argv_copy);
1916 g_option_context_free (context);
1918 /* Checking g_option_context_parse_strv on NULL args */
1919 context = g_option_context_new (NULL);
1920 g_assert_true (g_option_context_parse_strv (context, NULL, NULL));
1921 g_option_context_free (context);
1924 static gchar *test_arg;
1926 static gboolean cb (const gchar *option_name,
1931 test_arg = g_strdup (value);
1936 dash_arg_test (void)
1938 GOptionContext *context;
1940 GError *error = NULL;
1944 gboolean argb = FALSE;
1945 GOptionEntry entries [] =
1946 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, cb, NULL, NULL },
1947 { "three", '3', 0, G_OPTION_ARG_NONE, &argb, NULL, NULL },
1950 g_test_bug ("577638");
1952 context = g_option_context_new (NULL);
1953 g_option_context_add_main_entries (context, entries, NULL);
1955 /* Now try parsing */
1956 argv = split_string ("program --test=-3", &argc);
1957 argv_copy = copy_stringv (argv, argc);
1961 retval = g_option_context_parse (context, &argc, &argv, &error);
1963 g_assert_no_error (error);
1964 g_assert_cmpstr (test_arg, ==, "-3");
1966 g_strfreev (argv_copy);
1971 /* Try parsing again */
1972 argv = split_string ("program --test -3", &argc);
1973 argv_copy = copy_stringv (argv, argc);
1976 retval = g_option_context_parse (context, &argc, &argv, &error);
1977 g_assert_no_error (error);
1979 g_assert_cmpstr (test_arg, ==, NULL);
1981 g_option_context_free (context);
1982 g_strfreev (argv_copy);
1989 GOptionContext *context;
1991 GOptionEntry entries [] =
1992 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1995 context = g_option_context_new (NULL);
1996 g_option_context_add_main_entries (context, entries, NULL);
1998 g_assert (g_option_context_get_help_enabled (context));
1999 g_assert (!g_option_context_get_ignore_unknown_options (context));
2000 g_assert_cmpstr (g_option_context_get_summary (context), ==, NULL);
2001 g_assert_cmpstr (g_option_context_get_description (context), ==, NULL);
2003 g_option_context_set_help_enabled (context, FALSE);
2004 g_option_context_set_ignore_unknown_options (context, TRUE);
2005 g_option_context_set_summary (context, "summary");
2006 g_option_context_set_description(context, "description");
2008 g_assert (!g_option_context_get_help_enabled (context));
2009 g_assert (g_option_context_get_ignore_unknown_options (context));
2010 g_assert_cmpstr (g_option_context_get_summary (context), ==, "summary");
2011 g_assert_cmpstr (g_option_context_get_description (context), ==, "description");
2013 g_option_context_free (context);
2017 gboolean parameter_seen;
2018 gboolean summary_seen;
2019 gboolean description_seen;
2023 static const gchar *
2024 translate_func (const gchar *str,
2027 TranslateData *d = data;
2029 if (strcmp (str, "parameter") == 0)
2030 d->parameter_seen = TRUE;
2031 else if (strcmp (str, "summary") == 0)
2032 d->summary_seen = TRUE;
2033 else if (strcmp (str, "description") == 0)
2034 d->description_seen = TRUE;
2040 destroy_notify (gpointer data)
2042 TranslateData *d = data;
2044 d->destroyed = TRUE;
2048 test_translate (void)
2050 GOptionContext *context;
2052 GOptionEntry entries [] =
2053 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2055 TranslateData data = { 0, };
2058 context = g_option_context_new ("parameter");
2059 g_option_context_add_main_entries (context, entries, NULL);
2060 g_option_context_set_summary (context, "summary");
2061 g_option_context_set_description (context, "description");
2063 g_option_context_set_translate_func (context, translate_func, &data, destroy_notify);
2065 str = g_option_context_get_help (context, FALSE, NULL);
2067 g_option_context_free (context);
2069 g_assert (data.parameter_seen);
2070 g_assert (data.summary_seen);
2071 g_assert (data.description_seen);
2072 g_assert (data.destroyed);
2078 GOptionContext *context;
2079 GOptionGroup *group;
2082 gchar **sarr = NULL;
2083 GOptionEntry entries[] = {
2084 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Test tests", "Argument to use in test" },
2085 { "test2", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, NULL, "Tests also", NULL },
2086 { "frob", 0, 0, G_OPTION_ARG_NONE, NULL, "Main frob", NULL },
2087 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
2090 GOptionEntry group_entries[] = {
2091 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Group test", "Group test arg" },
2092 { "frob", 0, G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_NONE, NULL, "Group frob", NULL },
2096 context = g_option_context_new ("blabla");
2097 g_option_context_add_main_entries (context, entries, NULL);
2098 g_option_context_set_summary (context, "Summary");
2099 g_option_context_set_description (context, "Description");
2101 group = g_option_group_new ("group1", "Group1-description", "Group1-help", NULL, NULL);
2102 g_option_group_add_entries (group, group_entries);
2104 g_option_context_add_group (context, group);
2106 str = g_option_context_get_help (context, FALSE, NULL);
2107 g_assert (strstr (str, "blabla") != NULL);
2108 g_assert (strstr (str, "Test tests") != NULL);
2109 g_assert (strstr (str, "Argument to use in test") != NULL);
2110 g_assert (strstr (str, "Tests also") == NULL);
2111 g_assert (strstr (str, "REST") != NULL);
2112 g_assert (strstr (str, "Summary") != NULL);
2113 g_assert (strstr (str, "Description") != NULL);
2114 g_assert (strstr (str, "--help") != NULL);
2115 g_assert (strstr (str, "--help-all") != NULL);
2116 g_assert (strstr (str, "--help-group1") != NULL);
2117 g_assert (strstr (str, "Group1-description") != NULL);
2118 g_assert (strstr (str, "Group1-help") != NULL);
2119 g_assert (strstr (str, "Group test arg") != NULL);
2120 g_assert (strstr (str, "Group frob") != NULL);
2121 g_assert (strstr (str, "Main frob") != NULL);
2122 g_assert (strstr (str, "--frob") != NULL);
2123 g_assert (strstr (str, "--group1-test") != NULL);
2124 g_assert (strstr (str, "--group1-frob") == NULL);
2127 g_option_context_free (context);
2131 test_help_no_options (void)
2133 GOptionContext *context;
2134 gchar **sarr = NULL;
2135 GOptionEntry entries[] = {
2136 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
2141 context = g_option_context_new ("blabla");
2142 g_option_context_add_main_entries (context, entries, NULL);
2144 str = g_option_context_get_help (context, FALSE, NULL);
2145 g_assert (strstr (str, "blabla") != NULL);
2146 g_assert (strstr (str, "REST") != NULL);
2147 g_assert (strstr (str, "Help Options") != NULL);
2148 g_assert (strstr (str, "Application Options") == NULL);
2151 g_option_context_free (context);
2155 test_help_no_help_options (void)
2157 GOptionContext *context;
2158 GOptionGroup *group;
2161 gchar **sarr = NULL;
2162 GOptionEntry entries[] = {
2163 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Test tests", "Argument to use in test" },
2164 { "test2", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, NULL, "Tests also", NULL },
2165 { "frob", 0, 0, G_OPTION_ARG_NONE, NULL, "Main frob", NULL },
2166 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &sarr, "Rest goes here", "REST" },
2169 GOptionEntry group_entries[] = {
2170 { "test", 't', 0, G_OPTION_ARG_STRING, &arg, "Group test", "Group test arg" },
2171 { "frob", 0, G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_NONE, NULL, "Group frob", NULL },
2175 g_test_bug ("697652");
2177 context = g_option_context_new ("blabla");
2178 g_option_context_add_main_entries (context, entries, NULL);
2179 g_option_context_set_summary (context, "Summary");
2180 g_option_context_set_description (context, "Description");
2181 g_option_context_set_help_enabled (context, FALSE);
2183 group = g_option_group_new ("group1", "Group1-description", "Group1-help", NULL, NULL);
2184 g_option_group_add_entries (group, group_entries);
2186 g_option_context_add_group (context, group);
2188 str = g_option_context_get_help (context, FALSE, NULL);
2189 g_assert (strstr (str, "blabla") != NULL);
2190 g_assert (strstr (str, "Test tests") != NULL);
2191 g_assert (strstr (str, "Argument to use in test") != NULL);
2192 g_assert (strstr (str, "Tests also") == NULL);
2193 g_assert (strstr (str, "REST") != NULL);
2194 g_assert (strstr (str, "Summary") != NULL);
2195 g_assert (strstr (str, "Description") != NULL);
2196 g_assert (strstr (str, "Help Options") == NULL);
2197 g_assert (strstr (str, "--help") == NULL);
2198 g_assert (strstr (str, "--help-all") == NULL);
2199 g_assert (strstr (str, "--help-group1") == NULL);
2200 g_assert (strstr (str, "Group1-description") != NULL);
2201 g_assert (strstr (str, "Group1-help") == NULL);
2202 g_assert (strstr (str, "Group test arg") != NULL);
2203 g_assert (strstr (str, "Group frob") != NULL);
2204 g_assert (strstr (str, "Main frob") != NULL);
2205 g_assert (strstr (str, "--frob") != NULL);
2206 g_assert (strstr (str, "--group1-test") != NULL);
2207 g_assert (strstr (str, "--group1-frob") == NULL);
2210 g_option_context_free (context);
2214 set_bool (gpointer data)
2222 test_main_group (void)
2224 GOptionContext *context;
2225 GOptionGroup *group;
2228 context = g_option_context_new (NULL);
2229 g_assert (g_option_context_get_main_group (context) == NULL);
2230 group = g_option_group_new ("name", "description", "hlep", &b, set_bool);
2231 g_option_context_add_group (context, group);
2232 group = g_option_group_new ("name2", "description", "hlep", NULL, NULL);
2233 g_option_context_add_group (context, group);
2234 g_assert (g_option_context_get_main_group (context) == NULL);
2235 group = g_option_group_new ("name", "description", "hlep", NULL, NULL);
2236 g_option_context_set_main_group (context, group);
2237 g_assert (g_option_context_get_main_group (context) == group);
2239 g_option_context_free (context);
2244 static gboolean error_func_called = FALSE;
2247 error_func (GOptionContext *context,
2248 GOptionGroup *group,
2252 g_assert_cmpint (GPOINTER_TO_INT(data), ==, 1234);
2253 error_func_called = TRUE;
2257 test_error_hook (void)
2259 GOptionContext *context;
2261 GOptionEntry entries [] =
2262 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2264 GOptionGroup *group;
2269 GError *error = NULL;
2271 context = g_option_context_new (NULL);
2272 group = g_option_group_new ("name", "description", "hlep", GINT_TO_POINTER(1234), NULL);
2273 g_option_group_add_entries (group, entries);
2274 g_option_context_set_main_group (context, group);
2275 g_option_group_set_error_hook (g_option_context_get_main_group (context),
2278 argv = split_string ("program --test", &argc);
2279 argv_copy = copy_stringv (argv, argc);
2281 retval = g_option_context_parse (context, &argc, &argv, &error);
2282 g_assert (retval == FALSE);
2283 g_assert (error != NULL);
2284 /* An error occurred, so argv has not been changed */
2285 check_identical_stringv (argv_copy, argv);
2286 g_clear_error (&error);
2288 g_assert (error_func_called);
2290 g_strfreev (argv_copy);
2292 g_option_context_free (context);
2296 test_group_parse (void)
2298 GOptionContext *context;
2299 GOptionGroup *group;
2305 GOptionEntry entries[] = {
2306 { "test", 't', 0, G_OPTION_ARG_STRING, &arg1, NULL, NULL },
2307 { "faz", 'f', 0, G_OPTION_ARG_STRING, &arg2, NULL, NULL },
2310 GOptionEntry group_entries[] = {
2311 { "test", 0, 0, G_OPTION_ARG_STRING, &arg3, NULL, NULL },
2312 { "frob", 'f', 0, G_OPTION_ARG_STRING, &arg4, NULL, NULL },
2313 { "faz", 'z', 0, G_OPTION_ARG_STRING, &arg5, NULL, NULL },
2316 gchar **argv, **orig_argv;
2318 GError *error = NULL;
2321 context = g_option_context_new (NULL);
2322 g_option_context_add_main_entries (context, entries, NULL);
2323 group = g_option_group_new ("group", "A group", "help for group", NULL, NULL);
2324 g_option_group_add_entries (group, group_entries);
2325 g_option_context_add_group (context, group);
2327 argv = split_string ("program --test arg1 -f arg2 --group-test arg3 --frob arg4 -z arg5", &argc);
2328 orig_argv = g_memdup2 (argv, (argc + 1) * sizeof (char *));
2330 retval = g_option_context_parse (context, &argc, &argv, &error);
2332 g_assert_no_error (error);
2334 g_assert_cmpstr (arg1, ==, "arg1");
2335 g_assert_cmpstr (arg2, ==, "arg2");
2336 g_assert_cmpstr (arg3, ==, "arg3");
2337 g_assert_cmpstr (arg4, ==, "arg4");
2338 g_assert_cmpstr (arg5, ==, "arg5");
2347 g_strfreev (orig_argv);
2348 g_option_context_free (context);
2352 option_context_parse_command_line (GOptionContext *context,
2353 const gchar *command_line)
2356 guint argv_len, argv_new_len;
2359 argv = split_string (command_line, NULL);
2360 argv_len = g_strv_length (argv);
2362 success = g_option_context_parse_strv (context, &argv, NULL);
2363 argv_new_len = g_strv_length (argv);
2366 return success ? (gint) (argv_len - argv_new_len) : -1;
2370 test_strict_posix (void)
2372 GOptionContext *context;
2375 GOptionEntry entries[] = {
2376 { "foo", 'f', 0, G_OPTION_ARG_NONE, &foo, NULL, NULL },
2377 { "bar", 'b', 0, G_OPTION_ARG_NONE, &bar, NULL, NULL },
2382 context = g_option_context_new (NULL);
2383 g_option_context_add_main_entries (context, entries, NULL);
2386 g_option_context_set_strict_posix (context, FALSE);
2387 n_parsed = option_context_parse_command_line (context, "program --foo command --bar");
2388 g_assert_cmpint (n_parsed, ==, 2);
2389 g_assert (foo == TRUE);
2390 g_assert (bar == TRUE);
2393 g_option_context_set_strict_posix (context, TRUE);
2394 n_parsed = option_context_parse_command_line (context, "program --foo command --bar");
2395 g_assert_cmpint (n_parsed, ==, 1);
2396 g_assert (foo == TRUE);
2397 g_assert (bar == FALSE);
2400 g_option_context_set_strict_posix (context, TRUE);
2401 n_parsed = option_context_parse_command_line (context, "program --foo --bar command");
2402 g_assert_cmpint (n_parsed, ==, 2);
2403 g_assert (foo == TRUE);
2404 g_assert (bar == TRUE);
2407 g_option_context_set_strict_posix (context, TRUE);
2408 n_parsed = option_context_parse_command_line (context, "program command --foo --bar");
2409 g_assert_cmpint (n_parsed, ==, 0);
2410 g_assert (foo == FALSE);
2411 g_assert (bar == FALSE);
2413 g_option_context_free (context);
2417 flag_reverse_string (void)
2419 GOptionContext *context;
2421 GOptionEntry entries [] =
2422 { { "test", 't', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2427 GError *error = NULL;
2429 if (!g_test_undefined ())
2432 context = g_option_context_new (NULL);
2434 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
2435 "*ignoring reverse flag*");
2436 g_option_context_add_main_entries (context, entries, NULL);
2437 g_test_assert_expected_messages ();
2439 argv = split_string ("program --test bla", &argc);
2441 retval = g_option_context_parse_strv (context, &argv, &error);
2442 g_assert (retval == TRUE);
2443 g_assert_no_error (error);
2445 g_option_context_free (context);
2450 flag_optional_int (void)
2452 GOptionContext *context;
2454 GOptionEntry entries [] =
2455 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &arg, NULL, NULL },
2460 GError *error = NULL;
2462 if (!g_test_undefined ())
2465 context = g_option_context_new (NULL);
2467 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
2468 "*ignoring no-arg, optional-arg or filename flags*");
2469 g_option_context_add_main_entries (context, entries, NULL);
2470 g_test_assert_expected_messages ();
2472 argv = split_string ("program --test 5", &argc);
2474 retval = g_option_context_parse_strv (context, &argv, &error);
2475 g_assert (retval == TRUE);
2476 g_assert_no_error (error);
2478 g_option_context_free (context);
2482 short_remaining (void)
2484 gboolean ignore = FALSE;
2485 gboolean remaining = FALSE;
2488 gchar** files = NULL;
2489 GError* error = NULL;
2490 GOptionEntry entries[] =
2492 { "ignore", 'i', 0, G_OPTION_ARG_NONE, &ignore, NULL, NULL },
2493 { "remaining", 'r', 0, G_OPTION_ARG_NONE, &remaining, NULL, NULL },
2494 { "number", 'n', 0, G_OPTION_ARG_INT, &number, NULL, NULL },
2495 { "text", 't', 0, G_OPTION_ARG_STRING, &text, NULL, NULL },
2496 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, NULL, NULL },
2499 GOptionContext* context;
2500 gchar **argv, **argv_copy;
2503 g_test_bug ("729563");
2505 argv = split_string ("program -ri -n 4 -t hello file1 file2", &argc);
2506 argv_copy = copy_stringv (argv, argc);
2508 context = g_option_context_new (NULL);
2510 g_option_context_add_main_entries (context, entries, NULL);
2511 g_option_context_set_ignore_unknown_options (context, TRUE);
2513 g_option_context_parse (context, &argc, &argv, &error);
2514 g_assert_no_error (error);
2517 g_assert (remaining);
2518 g_assert_cmpint (number, ==, 4);
2519 g_assert_cmpstr (text, ==, "hello");
2520 g_assert_cmpstr (files[0], ==, "file1");
2521 g_assert_cmpstr (files[1], ==, "file2");
2522 g_assert (files[2] == NULL);
2526 g_strfreev (argv_copy);
2528 g_option_context_free (context);
2535 GOptionEntry entries[] =
2537 { "known", 0, 0, G_OPTION_ARG_STRING, &text, NULL, NULL },
2540 GOptionContext* context;
2543 GError *error = NULL;
2545 g_test_bug ("646926");
2547 argv = split_string ("program --known=foo --known=bar --unknown=baz", &argc);
2549 context = g_option_context_new (NULL);
2551 g_option_context_add_main_entries (context, entries, NULL);
2552 g_option_context_set_ignore_unknown_options (context, FALSE);
2553 g_option_context_parse (context, &argc, &argv, &error);
2555 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION);
2556 g_assert_null (text);
2558 g_option_context_free (context);
2559 g_clear_error (&error);
2567 GOptionContext *context;
2569 GError *error = NULL;
2573 double test_val = NAN;
2574 GOptionEntry entries [] =
2575 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &test_val, NULL, NULL },
2578 context = g_option_context_new (NULL);
2579 g_option_context_add_main_entries (context, entries, NULL);
2581 /* Now try parsing */
2582 argv = split_string ("program --test 0", &argc);
2583 argv_copy = copy_stringv (argv, argc);
2585 retval = g_option_context_parse (context, &argc, &argv, &error);
2586 g_assert_no_error (error);
2589 /* Last arg specified is the one that should be stored */
2590 g_assert (test_val == 0);
2592 g_strfreev (argv_copy);
2594 g_option_context_free (context);
2604 g_setenv ("LC_ALL", "C", TRUE);
2605 g_test_init (&argc, &argv, NULL);
2607 g_test_bug_base ("http://bugzilla.gnome.org/");
2609 g_test_add_func ("/option/help/options", test_help);
2610 g_test_add_func ("/option/help/no-options", test_help_no_options);
2611 g_test_add_func ("/option/help/no-help-options", test_help_no_help_options);
2613 g_test_add_func ("/option/basic", test_basic);
2614 g_test_add_func ("/option/translate", test_translate);
2616 g_test_add_func ("/option/group/captions", test_group_captions);
2617 for (i = 0; i < 4; i++)
2619 test_name = g_strdup_printf ("/option/group/captions/subprocess/help-%d", i);
2620 g_test_add_data_func (test_name, GINT_TO_POINTER (i),
2621 test_group_captions_help);
2623 test_name = g_strdup_printf ("/option/group/captions/subprocess/help-all-%d", i);
2624 g_test_add_data_func (test_name, GINT_TO_POINTER (i),
2625 test_group_captions_help_all);
2627 test_name = g_strdup_printf ("/option/group/captions/subprocess/help-test-%d", i);
2628 g_test_add_data_func (test_name, GINT_TO_POINTER (i),
2629 test_group_captions_help_test);
2634 g_test_add_func ("/option/group/main", test_main_group);
2635 g_test_add_func ("/option/group/error-hook", test_error_hook);
2636 g_test_add_func ("/option/group/parse", test_group_parse);
2637 g_test_add_func ("/option/strict-posix", test_strict_posix);
2639 /* Test that restoration on failure works */
2640 g_test_add_func ("/option/restoration/int", error_test1);
2641 g_test_add_func ("/option/restoration/string", error_test2);
2642 g_test_add_func ("/option/restoration/boolean", error_test3);
2644 /* Test that special argument parsing works */
2645 g_test_add_func ("/option/arg/repetition/int", arg_test1);
2646 g_test_add_func ("/option/arg/repetition/string", arg_test2);
2647 g_test_add_func ("/option/arg/repetition/filename", arg_test3);
2648 g_test_add_func ("/option/arg/repetition/double", arg_test4);
2649 g_test_add_func ("/option/arg/repetition/locale", arg_test5);
2650 g_test_add_func ("/option/arg/repetition/int64", arg_test6);
2652 /* Test string arrays */
2653 g_test_add_func ("/option/arg/array/string", array_test1);
2655 /* Test callback args */
2656 g_test_add_func ("/option/arg/callback/string", callback_test1);
2657 g_test_add_func ("/option/arg/callback/count", callback_test2);
2659 /* Test optional arg flag for callback */
2660 g_test_add_func ("/option/arg/callback/optional1", callback_test_optional_1);
2661 g_test_add_func ("/option/arg/callback/optional2", callback_test_optional_2);
2662 g_test_add_func ("/option/arg/callback/optional3", callback_test_optional_3);
2663 g_test_add_func ("/option/arg/callback/optional4", callback_test_optional_4);
2664 g_test_add_func ("/option/arg/callback/optional5", callback_test_optional_5);
2665 g_test_add_func ("/option/arg/callback/optional6", callback_test_optional_6);
2666 g_test_add_func ("/option/arg/callback/optional7", callback_test_optional_7);
2667 g_test_add_func ("/option/arg/callback/optional8", callback_test_optional_8);
2669 /* Test callback with G_OPTION_REMAINING */
2670 g_test_add_func ("/option/arg/remaining/callback", callback_remaining_test1);
2672 /* Test callbacks which return FALSE */
2673 g_test_add_func ("/option/arg/remaining/callback-false", callback_returns_false);
2675 /* Test ignoring options */
2676 g_test_add_func ("/option/arg/ignore/long", ignore_test1);
2677 g_test_add_func ("/option/arg/ignore/short", ignore_test2);
2678 g_test_add_func ("/option/arg/ignore/arg", ignore_test3);
2679 g_test_add_func ("/option/context/add", add_test1);
2681 /* Test parsing empty args */
2682 /* Note there used to be an empty1 here, but it effectively moved
2683 * to option-argv0.c.
2685 g_test_add_func ("/option/context/empty2", empty_test2);
2686 g_test_add_func ("/option/context/empty3", empty_test3);
2688 /* Test handling of rest args */
2689 g_test_add_func ("/option/arg/rest/non-option", rest_test1);
2690 g_test_add_func ("/option/arg/rest/separator1", rest_test2);
2691 g_test_add_func ("/option/arg/rest/separator2", rest_test2a);
2692 g_test_add_func ("/option/arg/rest/separator3", rest_test2b);
2693 g_test_add_func ("/option/arg/rest/separator4", rest_test2c);
2694 g_test_add_func ("/option/arg/rest/separator5", rest_test2d);
2695 g_test_add_func ("/option/arg/remaining/non-option", rest_test3);
2696 g_test_add_func ("/option/arg/remaining/separator", rest_test4);
2697 g_test_add_func ("/option/arg/remaining/array", rest_test5);
2699 /* Test some invalid flag combinations */
2700 g_test_add_func ("/option/arg/reverse-string", flag_reverse_string);
2701 g_test_add_func ("/option/arg/optional-int", flag_optional_int);
2703 /* regression tests for individual bugs */
2704 g_test_add_func ("/option/bug/unknown-short", unknown_short_test);
2705 g_test_add_func ("/option/bug/lonely-dash", lonely_dash_test);
2706 g_test_add_func ("/option/bug/triple-dash", triple_dash_test);
2707 g_test_add_func ("/option/bug/missing-arg", missing_arg_test);
2708 g_test_add_func ("/option/bug/dash-arg", dash_arg_test);
2709 g_test_add_func ("/option/bug/short-remaining", short_remaining);
2710 g_test_add_func ("/option/bug/double-free", double_free);
2711 g_test_add_func ("/option/bug/double-zero", double_zero);
2713 return g_test_run();