1 /* Unit tests for GOptionContext
2 * Copyright (C) 2007 Openismus GmbH
3 * Authors: Mathias Hasselmann
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
33 gchar *help_variants[] = { "--help", "--help-all", "--help-test" };
35 GOptionEntry main_entries[] = {
36 { "main-switch", 0, 0,
37 G_OPTION_ARG_NONE, NULL,
38 "A switch that is in the main group", NULL },
42 GOptionEntry group_entries[] = {
43 { "test-switch", 0, 0,
44 G_OPTION_ARG_NONE, NULL,
45 "A switch that is in the test group", NULL },
51 g_test_bug ("504142");
53 for (i = 0; i < 4; ++i)
55 gboolean have_main_entries = (0 != (i & 1));
56 gboolean have_test_entries = (0 != (i & 2));
58 GOptionContext *options;
59 GOptionGroup *group = NULL;
61 options = g_option_context_new (NULL);
63 if (have_main_entries)
64 g_option_context_add_main_entries (options, main_entries, NULL);
65 if (have_test_entries)
67 group = g_option_group_new ("test", "Test Options",
68 "Show all test options",
70 g_option_context_add_group (options, group);
71 g_option_group_add_entries (group, group_entries);
74 for (j = 0; j < G_N_ELEMENTS (help_variants); ++j)
76 GTestTrapFlags trap_flags = 0;
80 args[1] = help_variants[j];
83 if (!g_test_verbose ())
84 trap_flags |= G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR;
86 g_test_message ("test setup: args='%s', main-entries=%d, test-entries=%d",
87 args[1], have_main_entries, have_test_entries);
89 if (g_test_trap_fork (0, trap_flags))
95 g_setenv ("LANG", "C", TRUE);
97 g_option_context_parse (options, &argc, &argv, &error);
102 gboolean expect_main_description = FALSE;
103 gboolean expect_main_switch = FALSE;
105 gboolean expect_test_description = FALSE;
106 gboolean expect_test_switch = FALSE;
107 gboolean expect_test_group = FALSE;
109 g_test_trap_assert_passed ();
110 g_test_trap_assert_stderr ("");
115 g_assert_cmpstr ("--help", ==, args[1]);
116 expect_main_switch = have_main_entries;
117 expect_test_group = have_test_entries;
121 g_assert_cmpstr ("--help-all", ==, args[1]);
122 expect_main_switch = have_main_entries;
123 expect_test_switch = have_test_entries;
124 expect_test_group = have_test_entries;
128 g_assert_cmpstr ("--help-test", ==, args[1]);
129 expect_test_switch = have_test_entries;
133 g_assert_not_reached ();
137 expect_main_description |= expect_main_switch;
138 expect_test_description |= expect_test_switch;
140 if (expect_main_description)
141 g_test_trap_assert_stdout ("*Application Options*");
143 g_test_trap_assert_stdout_unmatched ("*Application Options*");
144 if (expect_main_switch)
145 g_test_trap_assert_stdout ("*--main-switch*");
147 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
149 if (expect_test_description)
150 g_test_trap_assert_stdout ("*Test Options*");
152 g_test_trap_assert_stdout_unmatched ("*Test Options*");
153 if (expect_test_switch)
154 g_test_trap_assert_stdout ("*--test-switch*");
156 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
158 if (expect_test_group)
159 g_test_trap_assert_stdout ("*--help-test*");
161 g_test_trap_assert_stdout_unmatched ("*--help-test*");
168 char *error_test2_string;
169 gboolean error_test3_boolean;
172 gchar *arg_test2_string;
173 gchar *arg_test3_filename;
174 gdouble arg_test4_double;
175 gdouble arg_test5_double;
176 gint64 arg_test6_int64;
177 gint64 arg_test6_int64_2;
179 gchar *callback_test1_string;
180 int callback_test2_int;
182 gchar *callback_test_optional_string;
183 gboolean callback_test_optional_boolean;
185 gchar **array_test1_array;
187 gboolean ignore_test1_boolean;
188 gboolean ignore_test2_boolean;
189 gchar *ignore_test3_string;
192 split_string (const char *str, int *argc)
197 argv = g_strsplit (str, " ", 0);
199 for (len = 0; argv[len] != NULL; len++);
208 join_stringv (int argc, char **argv)
213 str = g_string_new (NULL);
215 for (i = 0; i < argc; i++)
217 g_string_append (str, argv[i]);
220 g_string_append_c (str, ' ');
223 return g_string_free (str, FALSE);
226 /* Performs a shallow copy */
228 copy_stringv (char **argv, int argc)
230 return g_memdup (argv, sizeof (char *) * (argc + 1));
235 error_test1_pre_parse (GOptionContext *context,
240 g_assert (error_test1_int == 0x12345678);
246 error_test1_post_parse (GOptionContext *context,
251 g_assert (error_test1_int == 20);
253 /* Set an error in the post hook */
254 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
262 GOptionContext *context;
264 GError *error = NULL;
267 GOptionGroup *main_group;
268 GOptionEntry entries [] =
269 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
272 error_test1_int = 0x12345678;
274 context = g_option_context_new (NULL);
275 g_option_context_add_main_entries (context, entries, NULL);
277 /* Set pre and post parse hooks */
278 main_group = g_option_context_get_main_group (context);
279 g_option_group_set_parse_hooks (main_group,
280 error_test1_pre_parse, error_test1_post_parse);
282 /* Now try parsing */
283 argv = split_string ("program --test 20", &argc);
285 retval = g_option_context_parse (context, &argc, &argv, &error);
286 g_assert (retval == FALSE);
288 /* On failure, values should be reset */
289 g_assert (error_test1_int == 0x12345678);
292 g_option_context_free (context);
296 error_test2_pre_parse (GOptionContext *context,
301 g_assert (strcmp (error_test2_string, "foo") == 0);
307 error_test2_post_parse (GOptionContext *context,
312 g_assert (strcmp (error_test2_string, "bar") == 0);
314 /* Set an error in the post hook */
315 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
323 GOptionContext *context;
325 GError *error = NULL;
328 GOptionGroup *main_group;
329 GOptionEntry entries [] =
330 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
333 error_test2_string = "foo";
335 context = g_option_context_new (NULL);
336 g_option_context_add_main_entries (context, entries, NULL);
338 /* Set pre and post parse hooks */
339 main_group = g_option_context_get_main_group (context);
340 g_option_group_set_parse_hooks (main_group,
341 error_test2_pre_parse, error_test2_post_parse);
343 /* Now try parsing */
344 argv = split_string ("program --test bar", &argc);
345 retval = g_option_context_parse (context, &argc, &argv, &error);
347 g_error_free (error);
348 g_assert (retval == FALSE);
350 g_assert (strcmp (error_test2_string, "foo") == 0);
353 g_option_context_free (context);
357 error_test3_pre_parse (GOptionContext *context,
362 g_assert (!error_test3_boolean);
368 error_test3_post_parse (GOptionContext *context,
373 g_assert (error_test3_boolean);
375 /* Set an error in the post hook */
376 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
384 GOptionContext *context;
386 GError *error = NULL;
389 GOptionGroup *main_group;
390 GOptionEntry entries [] =
391 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
394 error_test3_boolean = FALSE;
396 context = g_option_context_new (NULL);
397 g_option_context_add_main_entries (context, entries, NULL);
399 /* Set pre and post parse hooks */
400 main_group = g_option_context_get_main_group (context);
401 g_option_group_set_parse_hooks (main_group,
402 error_test3_pre_parse, error_test3_post_parse);
404 /* Now try parsing */
405 argv = split_string ("program --test", &argc);
406 retval = g_option_context_parse (context, &argc, &argv, &error);
408 g_error_free (error);
409 g_assert (retval == FALSE);
411 g_assert (!error_test3_boolean);
414 g_option_context_free (context);
420 GOptionContext *context;
422 GError *error = NULL;
425 GOptionEntry entries [] =
426 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
429 context = g_option_context_new (NULL);
430 g_option_context_add_main_entries (context, entries, NULL);
432 /* Now try parsing */
433 argv = split_string ("program --test 20 --test 30", &argc);
435 retval = g_option_context_parse (context, &argc, &argv, &error);
436 g_assert_no_error (error);
439 /* Last arg specified is the one that should be stored */
440 g_assert (arg_test1_int == 30);
443 g_option_context_free (context);
449 GOptionContext *context;
451 GError *error = NULL;
454 GOptionEntry entries [] =
455 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
458 context = g_option_context_new (NULL);
459 g_option_context_add_main_entries (context, entries, NULL);
461 /* Now try parsing */
462 argv = split_string ("program --test foo --test bar", &argc);
464 retval = g_option_context_parse (context, &argc, &argv, &error);
465 g_assert_no_error (error);
468 /* Last arg specified is the one that should be stored */
469 g_assert (strcmp (arg_test2_string, "bar") == 0);
471 g_free (arg_test2_string);
474 g_option_context_free (context);
480 GOptionContext *context;
482 GError *error = NULL;
485 GOptionEntry entries [] =
486 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
489 context = g_option_context_new (NULL);
490 g_option_context_add_main_entries (context, entries, NULL);
492 /* Now try parsing */
493 argv = split_string ("program --test foo.txt", &argc);
495 retval = g_option_context_parse (context, &argc, &argv, &error);
496 g_assert_no_error (error);
499 /* Last arg specified is the one that should be stored */
500 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
502 g_free (arg_test3_filename);
505 g_option_context_free (context);
512 GOptionContext *context;
514 GError *error = NULL;
517 GOptionEntry entries [] =
518 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, NULL, NULL },
521 context = g_option_context_new (NULL);
522 g_option_context_add_main_entries (context, entries, NULL);
524 /* Now try parsing */
525 argv = split_string ("program --test 20.0 --test 30.03", &argc);
527 retval = g_option_context_parse (context, &argc, &argv, &error);
528 g_assert_no_error (error);
531 /* Last arg specified is the one that should be stored */
532 g_assert (arg_test4_double == 30.03);
535 g_option_context_free (context);
541 GOptionContext *context;
543 GError *error = NULL;
546 char *old_locale, *current_locale;
547 const char *locale = "de_DE";
548 GOptionEntry entries [] =
549 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test5_double, NULL, NULL },
552 context = g_option_context_new (NULL);
553 g_option_context_add_main_entries (context, entries, NULL);
555 /* Now try parsing */
556 argv = split_string ("program --test 20,0 --test 30,03", &argc);
558 /* set it to some locale that uses commas instead of decimal points */
560 old_locale = g_strdup (setlocale (LC_NUMERIC, locale));
561 current_locale = setlocale (LC_NUMERIC, NULL);
562 if (strcmp (current_locale, locale) != 0)
564 fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
568 retval = g_option_context_parse (context, &argc, &argv, &error);
569 g_assert_no_error (error);
572 /* Last arg specified is the one that should be stored */
573 g_assert (arg_test5_double == 30.03);
576 setlocale (LC_NUMERIC, old_locale);
580 g_option_context_free (context);
586 GOptionContext *context;
588 GError *error = NULL;
591 GOptionEntry entries [] =
592 { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL },
593 { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, NULL, NULL },
596 context = g_option_context_new (NULL);
597 g_option_context_add_main_entries (context, entries, NULL);
599 /* Now try parsing */
600 argv = split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc);
602 retval = g_option_context_parse (context, &argc, &argv, &error);
603 g_assert_no_error (error);
606 /* Last arg specified is the one that should be stored */
607 g_assert (arg_test6_int64 == G_GINT64_CONSTANT(4294967296));
608 g_assert (arg_test6_int64_2 == G_GINT64_CONSTANT(0xfffffffff));
611 g_option_context_free (context);
615 callback_parse1 (const gchar *option_name, const gchar *value,
616 gpointer data, GError **error)
618 callback_test1_string = g_strdup (value);
623 callback_test1 (void)
625 GOptionContext *context;
627 GError *error = NULL;
630 GOptionEntry entries [] =
631 { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL },
634 context = g_option_context_new (NULL);
635 g_option_context_add_main_entries (context, entries, NULL);
637 /* Now try parsing */
638 argv = split_string ("program --test foo.txt", &argc);
640 retval = g_option_context_parse (context, &argc, &argv, &error);
641 g_assert_no_error (error);
644 g_assert (strcmp (callback_test1_string, "foo.txt") == 0);
646 g_free (callback_test1_string);
649 g_option_context_free (context);
653 callback_parse2 (const gchar *option_name, const gchar *value,
654 gpointer data, GError **error)
656 callback_test2_int++;
661 callback_test2 (void)
663 GOptionContext *context;
665 GError *error = NULL;
668 GOptionEntry entries [] =
669 { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL },
672 context = g_option_context_new (NULL);
673 g_option_context_add_main_entries (context, entries, NULL);
675 /* Now try parsing */
676 argv = split_string ("program --test --test", &argc);
678 retval = g_option_context_parse (context, &argc, &argv, &error);
679 g_assert_no_error (error);
682 g_assert (callback_test2_int == 2);
685 g_option_context_free (context);
689 callback_parse_optional (const gchar *option_name, const gchar *value,
690 gpointer data, GError **error)
692 callback_test_optional_boolean = TRUE;
694 callback_test_optional_string = g_strdup (value);
696 callback_test_optional_string = NULL;
701 callback_test_optional_1 (void)
703 GOptionContext *context;
705 GError *error = NULL;
708 GOptionEntry entries [] =
709 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
710 callback_parse_optional, NULL, NULL },
713 context = g_option_context_new (NULL);
714 g_option_context_add_main_entries (context, entries, NULL);
716 /* Now try parsing */
717 argv = split_string ("program --test foo.txt", &argc);
719 retval = g_option_context_parse (context, &argc, &argv, &error);
720 g_assert_no_error (error);
723 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
725 g_assert (callback_test_optional_boolean);
727 g_free (callback_test_optional_string);
730 g_option_context_free (context);
734 callback_test_optional_2 (void)
736 GOptionContext *context;
738 GError *error = NULL;
741 GOptionEntry entries [] =
742 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
743 callback_parse_optional, NULL, NULL },
746 context = g_option_context_new (NULL);
747 g_option_context_add_main_entries (context, entries, NULL);
749 /* Now try parsing */
750 argv = split_string ("program --test", &argc);
752 retval = g_option_context_parse (context, &argc, &argv, &error);
753 g_assert_no_error (error);
756 g_assert (callback_test_optional_string == NULL);
758 g_assert (callback_test_optional_boolean);
760 g_free (callback_test_optional_string);
763 g_option_context_free (context);
767 callback_test_optional_3 (void)
769 GOptionContext *context;
771 GError *error = NULL;
774 GOptionEntry entries [] =
775 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
776 callback_parse_optional, NULL, NULL },
779 context = g_option_context_new (NULL);
780 g_option_context_add_main_entries (context, entries, NULL);
782 /* Now try parsing */
783 argv = split_string ("program -t foo.txt", &argc);
785 retval = g_option_context_parse (context, &argc, &argv, &error);
786 g_assert_no_error (error);
789 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
791 g_assert (callback_test_optional_boolean);
793 g_free (callback_test_optional_string);
796 g_option_context_free (context);
801 callback_test_optional_4 (void)
803 GOptionContext *context;
805 GError *error = NULL;
808 GOptionEntry entries [] =
809 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
810 callback_parse_optional, NULL, NULL },
813 context = g_option_context_new (NULL);
814 g_option_context_add_main_entries (context, entries, NULL);
816 /* Now try parsing */
817 argv = split_string ("program -t", &argc);
819 retval = g_option_context_parse (context, &argc, &argv, &error);
820 g_assert_no_error (error);
823 g_assert (callback_test_optional_string == NULL);
825 g_assert (callback_test_optional_boolean);
827 g_free (callback_test_optional_string);
830 g_option_context_free (context);
834 callback_test_optional_5 (void)
836 GOptionContext *context;
839 GError *error = NULL;
842 GOptionEntry entries [] =
843 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
844 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
845 callback_parse_optional, NULL, NULL },
848 context = g_option_context_new (NULL);
849 g_option_context_add_main_entries (context, entries, NULL);
851 /* Now try parsing */
852 argv = split_string ("program --test --dummy", &argc);
854 retval = g_option_context_parse (context, &argc, &argv, &error);
855 g_assert_no_error (error);
858 g_assert (callback_test_optional_string == NULL);
860 g_assert (callback_test_optional_boolean);
862 g_free (callback_test_optional_string);
865 g_option_context_free (context);
869 callback_test_optional_6 (void)
871 GOptionContext *context;
874 GError *error = NULL;
877 GOptionEntry entries [] =
878 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
879 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
880 callback_parse_optional, NULL, NULL },
883 context = g_option_context_new (NULL);
884 g_option_context_add_main_entries (context, entries, NULL);
886 /* Now try parsing */
887 argv = split_string ("program -t -d", &argc);
889 retval = g_option_context_parse (context, &argc, &argv, &error);
890 g_assert_no_error (error);
893 g_assert (callback_test_optional_string == NULL);
895 g_assert (callback_test_optional_boolean);
897 g_free (callback_test_optional_string);
900 g_option_context_free (context);
904 callback_test_optional_7 (void)
906 GOptionContext *context;
909 GError *error = NULL;
912 GOptionEntry entries [] =
913 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
914 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
915 callback_parse_optional, NULL, NULL },
918 context = g_option_context_new (NULL);
919 g_option_context_add_main_entries (context, entries, NULL);
921 /* Now try parsing */
922 argv = split_string ("program -td", &argc);
924 retval = g_option_context_parse (context, &argc, &argv, &error);
925 g_assert_no_error (error);
928 g_assert (callback_test_optional_string == NULL);
930 g_assert (callback_test_optional_boolean);
932 g_free (callback_test_optional_string);
935 g_option_context_free (context);
939 callback_test_optional_8 (void)
941 GOptionContext *context;
944 GError *error = NULL;
947 GOptionEntry entries [] =
948 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
949 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
950 callback_parse_optional, NULL, NULL },
953 context = g_option_context_new (NULL);
954 g_option_context_add_main_entries (context, entries, NULL);
956 /* Now try parsing */
957 argv = split_string ("program -dt foo.txt", &argc);
959 retval = g_option_context_parse (context, &argc, &argv, &error);
960 g_assert_no_error (error);
963 g_assert (callback_test_optional_string);
965 g_assert (callback_test_optional_boolean);
967 g_free (callback_test_optional_string);
970 g_option_context_free (context);
974 callback_test_optional_9 (void)
976 GOptionContext *context;
978 GError *error = NULL;
981 gchar *string = NULL;
982 GOptionEntry entries [] =
983 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING,
984 &string, NULL, NULL },
987 context = g_option_context_new (NULL);
988 g_option_context_add_main_entries (context, entries, NULL);
990 /* Now try parsing */
991 argv = split_string ("program -t", &argc);
993 retval = g_option_context_parse (context, &argc, &argv, &error);
994 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
996 g_assert (string == NULL);
998 g_error_free (error);
1000 g_option_context_free (context);
1004 callback_test_optional_10 (void)
1006 GOptionContext *context;
1008 GError *error = NULL;
1011 gchar *string = NULL;
1012 GOptionEntry entries [] =
1013 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING,
1014 &string, NULL, NULL },
1017 context = g_option_context_new (NULL);
1018 g_option_context_add_main_entries (context, entries, NULL);
1020 /* Now try parsing */
1021 argv = split_string ("program --test", &argc);
1023 retval = g_option_context_parse (context, &argc, &argv, &error);
1024 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1026 g_assert (string == NULL);
1028 g_error_free (error);
1030 g_option_context_free (context);
1033 static GPtrArray *callback_remaining_args;
1035 callback_remaining_test1_callback (const gchar *option_name, const gchar *value,
1036 gpointer data, GError **error)
1038 g_ptr_array_add (callback_remaining_args, g_strdup (value));
1043 callback_remaining_test1 (void)
1045 GOptionContext *context;
1047 GError *error = NULL;
1050 GOptionEntry entries [] =
1051 { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL },
1054 callback_remaining_args = g_ptr_array_new ();
1055 context = g_option_context_new (NULL);
1056 g_option_context_add_main_entries (context, entries, NULL);
1058 /* Now try parsing */
1059 argv = split_string ("program foo.txt blah.txt", &argc);
1061 retval = g_option_context_parse (context, &argc, &argv, &error);
1062 g_assert_no_error (error);
1065 g_assert (callback_remaining_args->len == 2);
1066 g_assert (strcmp (callback_remaining_args->pdata[0], "foo.txt") == 0);
1067 g_assert (strcmp (callback_remaining_args->pdata[1], "blah.txt") == 0);
1069 g_ptr_array_foreach (callback_remaining_args, (GFunc) g_free, NULL);
1070 g_ptr_array_free (callback_remaining_args, TRUE);
1073 g_option_context_free (context);
1077 callback_error (const gchar *option_name, const gchar *value,
1078 gpointer data, GError **error)
1080 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "42");
1085 callback_returns_false (void)
1087 GOptionContext *context;
1089 GError *error = NULL;
1092 GOptionEntry entries [] =
1093 { { "error", 0, 0, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1094 { "error-no-arg", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1095 { "error-optional-arg", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
1098 context = g_option_context_new (NULL);
1099 g_option_context_add_main_entries (context, entries, NULL);
1101 /* Now try parsing */
1102 argv = split_string ("program --error value", &argc);
1104 retval = g_option_context_parse (context, &argc, &argv, &error);
1105 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1106 g_assert (retval == FALSE);
1108 g_option_context_free (context);
1109 g_clear_error (&error);
1111 /* And again, this time with a no-arg variant */
1112 context = g_option_context_new (NULL);
1113 g_option_context_add_main_entries (context, entries, NULL);
1115 argv = split_string ("program --error-no-arg", &argc);
1117 retval = g_option_context_parse (context, &argc, &argv, &error);
1118 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1119 g_assert (retval == FALSE);
1121 g_option_context_free (context);
1122 g_clear_error (&error);
1124 /* And again, this time with a optional arg variant, with argument */
1125 context = g_option_context_new (NULL);
1126 g_option_context_add_main_entries (context, entries, NULL);
1128 argv = split_string ("program --error-optional-arg value", &argc);
1130 retval = g_option_context_parse (context, &argc, &argv, &error);
1131 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1132 g_assert (retval == FALSE);
1134 g_option_context_free (context);
1135 g_clear_error (&error);
1137 /* And again, this time with a optional arg variant, without argument */
1138 context = g_option_context_new (NULL);
1139 g_option_context_add_main_entries (context, entries, NULL);
1141 argv = split_string ("program --error-optional-arg", &argc);
1143 retval = g_option_context_parse (context, &argc, &argv, &error);
1144 g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
1145 g_assert (retval == FALSE);
1147 g_option_context_free (context);
1148 g_clear_error (&error);
1155 GOptionContext *context;
1157 GError *error = NULL;
1158 gchar **argv, **argv_copy;
1161 GOptionEntry entries [] =
1162 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1165 context = g_option_context_new (NULL);
1166 g_option_context_set_ignore_unknown_options (context, TRUE);
1167 g_option_context_add_main_entries (context, entries, NULL);
1169 /* Now try parsing */
1170 argv = split_string ("program --test --hello", &argc);
1171 argv_copy = copy_stringv (argv, argc);
1173 retval = g_option_context_parse (context, &argc, &argv, &error);
1174 g_assert_no_error (error);
1178 arg = join_stringv (argc, argv);
1179 g_assert (strcmp (arg, "program --hello") == 0);
1182 g_strfreev (argv_copy);
1184 g_option_context_free (context);
1190 GOptionContext *context;
1192 GError *error = NULL;
1196 GOptionEntry entries [] =
1197 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
1200 context = g_option_context_new (NULL);
1201 g_option_context_set_ignore_unknown_options (context, TRUE);
1202 g_option_context_add_main_entries (context, entries, NULL);
1204 /* Now try parsing */
1205 argv = split_string ("program -test", &argc);
1207 retval = g_option_context_parse (context, &argc, &argv, &error);
1208 g_assert_no_error (error);
1212 arg = join_stringv (argc, argv);
1213 g_assert (strcmp (arg, "program -es") == 0);
1217 g_option_context_free (context);
1223 GOptionContext *context;
1225 GError *error = NULL;
1226 gchar **argv, **argv_copy;
1229 GOptionEntry entries [] =
1230 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
1233 context = g_option_context_new (NULL);
1234 g_option_context_set_ignore_unknown_options (context, TRUE);
1235 g_option_context_add_main_entries (context, entries, NULL);
1237 /* Now try parsing */
1238 argv = split_string ("program --test foo --hello", &argc);
1239 argv_copy = copy_stringv (argv, argc);
1241 retval = g_option_context_parse (context, &argc, &argv, &error);
1242 g_assert_no_error (error);
1246 arg = join_stringv (argc, argv);
1247 g_assert (strcmp (arg, "program --hello") == 0);
1249 g_assert (strcmp (ignore_test3_string, "foo") == 0);
1250 g_free (ignore_test3_string);
1253 g_strfreev (argv_copy);
1255 g_option_context_free (context);
1259 static array_test1 (void)
1261 GOptionContext *context;
1263 GError *error = NULL;
1266 GOptionEntry entries [] =
1267 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1270 context = g_option_context_new (NULL);
1271 g_option_context_add_main_entries (context, entries, NULL);
1273 /* Now try parsing */
1274 argv = split_string ("program --test foo --test bar", &argc);
1276 retval = g_option_context_parse (context, &argc, &argv, &error);
1277 g_assert_no_error (error);
1281 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1282 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1283 g_assert (array_test1_array[2] == NULL);
1285 g_strfreev (array_test1_array);
1288 g_option_context_free (context);
1294 GOptionContext *context;
1296 GOptionEntry entries1 [] =
1297 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1299 GOptionEntry entries2 [] =
1300 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1303 context = g_option_context_new (NULL);
1304 g_option_context_add_main_entries (context, entries1, NULL);
1305 g_option_context_add_main_entries (context, entries2, NULL);
1307 g_option_context_free (context);
1313 GOptionContext *context;
1315 context = g_option_context_new (NULL);
1316 g_option_context_parse (context, NULL, NULL, NULL);
1318 g_option_context_free (context);
1324 GOptionContext *context;
1331 context = g_option_context_new (NULL);
1332 g_option_context_parse (context, &argc, &argv, NULL);
1334 g_option_context_free (context);
1337 /* check that non-option arguments are left in argv by default */
1341 GOptionContext *context;
1343 GError *error = NULL;
1346 GOptionEntry entries [] = {
1347 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1351 context = g_option_context_new (NULL);
1352 g_option_context_add_main_entries (context, entries, NULL);
1354 /* Now try parsing */
1355 argv = split_string ("program foo --test bar", &argc);
1357 retval = g_option_context_parse (context, &argc, &argv, &error);
1358 g_assert_no_error (error);
1362 g_assert (ignore_test1_boolean);
1363 g_assert (strcmp (argv[0], "program") == 0);
1364 g_assert (strcmp (argv[1], "foo") == 0);
1365 g_assert (strcmp (argv[2], "bar") == 0);
1366 g_assert (argv[3] == NULL);
1369 g_option_context_free (context);
1372 /* check that -- works */
1376 GOptionContext *context;
1378 GError *error = NULL;
1381 GOptionEntry entries [] = {
1382 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1386 context = g_option_context_new (NULL);
1387 g_option_context_add_main_entries (context, entries, NULL);
1389 /* Now try parsing */
1390 argv = split_string ("program foo --test -- -bar", &argc);
1392 retval = g_option_context_parse (context, &argc, &argv, &error);
1393 g_assert_no_error (error);
1397 g_assert (ignore_test1_boolean);
1398 g_assert (strcmp (argv[0], "program") == 0);
1399 g_assert (strcmp (argv[1], "foo") == 0);
1400 g_assert (strcmp (argv[2], "--") == 0);
1401 g_assert (strcmp (argv[3], "-bar") == 0);
1402 g_assert (argv[4] == NULL);
1405 g_option_context_free (context);
1408 /* check that -- stripping works */
1412 GOptionContext *context;
1414 GError *error = NULL;
1417 GOptionEntry entries [] = {
1418 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1422 context = g_option_context_new (NULL);
1423 g_option_context_add_main_entries (context, entries, NULL);
1425 /* Now try parsing */
1426 argv = split_string ("program foo --test -- bar", &argc);
1428 retval = g_option_context_parse (context, &argc, &argv, &error);
1429 g_assert_no_error (error);
1433 g_assert (ignore_test1_boolean);
1434 g_assert (strcmp (argv[0], "program") == 0);
1435 g_assert (strcmp (argv[1], "foo") == 0);
1436 g_assert (strcmp (argv[2], "bar") == 0);
1437 g_assert (argv[3] == NULL);
1440 g_option_context_free (context);
1446 GOptionContext *context;
1448 GError *error = NULL;
1451 GOptionEntry entries [] = {
1452 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1456 context = g_option_context_new (NULL);
1457 g_option_context_set_ignore_unknown_options (context, TRUE);
1458 g_option_context_add_main_entries (context, entries, NULL);
1460 /* Now try parsing */
1461 argv = split_string ("program foo --test -bar --", &argc);
1463 retval = g_option_context_parse (context, &argc, &argv, &error);
1464 g_assert_no_error (error);
1468 g_assert (ignore_test1_boolean);
1469 g_assert (strcmp (argv[0], "program") == 0);
1470 g_assert (strcmp (argv[1], "foo") == 0);
1471 g_assert (strcmp (argv[2], "-bar") == 0);
1472 g_assert (argv[3] == NULL);
1475 g_option_context_free (context);
1481 GOptionContext *context;
1483 GError *error = NULL;
1486 GOptionEntry entries [] = {
1487 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1491 context = g_option_context_new (NULL);
1492 g_option_context_add_main_entries (context, entries, NULL);
1494 /* Now try parsing */
1495 argv = split_string ("program --test foo -- bar", &argc);
1497 retval = g_option_context_parse (context, &argc, &argv, &error);
1498 g_assert_no_error (error);
1502 g_assert (ignore_test1_boolean);
1503 g_assert (strcmp (argv[0], "program") == 0);
1504 g_assert (strcmp (argv[1], "foo") == 0);
1505 g_assert (strcmp (argv[2], "bar") == 0);
1506 g_assert (argv[3] == NULL);
1509 g_option_context_free (context);
1515 GOptionContext *context;
1517 GError *error = NULL;
1520 GOptionEntry entries [] = {
1521 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1525 context = g_option_context_new (NULL);
1526 g_option_context_add_main_entries (context, entries, NULL);
1528 /* Now try parsing */
1529 argv = split_string ("program --test -- -bar", &argc);
1531 retval = g_option_context_parse (context, &argc, &argv, &error);
1532 g_assert_no_error (error);
1536 g_assert (ignore_test1_boolean);
1537 g_assert (strcmp (argv[0], "program") == 0);
1538 g_assert (strcmp (argv[1], "--") == 0);
1539 g_assert (strcmp (argv[2], "-bar") == 0);
1540 g_assert (argv[3] == NULL);
1543 g_option_context_free (context);
1547 /* check that G_OPTION_REMAINING collects non-option arguments */
1551 GOptionContext *context;
1553 GError *error = NULL;
1556 GOptionEntry entries [] = {
1557 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1558 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1562 context = g_option_context_new (NULL);
1563 g_option_context_add_main_entries (context, entries, NULL);
1565 /* Now try parsing */
1566 argv = split_string ("program foo --test bar", &argc);
1568 retval = g_option_context_parse (context, &argc, &argv, &error);
1569 g_assert_no_error (error);
1573 g_assert (ignore_test1_boolean);
1574 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1575 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1576 g_assert (array_test1_array[2] == NULL);
1578 g_strfreev (array_test1_array);
1581 g_option_context_free (context);
1585 /* check that G_OPTION_REMAINING and -- work together */
1589 GOptionContext *context;
1591 GError *error = NULL;
1594 GOptionEntry entries [] = {
1595 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1596 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1600 context = g_option_context_new (NULL);
1601 g_option_context_add_main_entries (context, entries, NULL);
1603 /* Now try parsing */
1604 argv = split_string ("program foo --test -- -bar", &argc);
1606 retval = g_option_context_parse (context, &argc, &argv, &error);
1607 g_assert_no_error (error);
1611 g_assert (ignore_test1_boolean);
1612 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1613 g_assert (strcmp (array_test1_array[1], "-bar") == 0);
1614 g_assert (array_test1_array[2] == NULL);
1616 g_strfreev (array_test1_array);
1619 g_option_context_free (context);
1622 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1626 GOptionContext *context;
1628 GError *error = NULL;
1631 GOptionEntry entries [] = {
1632 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1633 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
1637 context = g_option_context_new (NULL);
1638 g_option_context_add_main_entries (context, entries, NULL);
1640 /* Now try parsing */
1641 argv = split_string ("program foo --test bar", &argc);
1643 retval = g_option_context_parse (context, &argc, &argv, &error);
1644 g_assert_no_error (error);
1648 g_assert (ignore_test1_boolean);
1649 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1650 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1651 g_assert (array_test1_array[2] == NULL);
1653 g_strfreev (array_test1_array);
1656 g_option_context_free (context);
1660 unknown_short_test (void)
1662 GOptionContext *context;
1664 GError *error = NULL;
1667 GOptionEntry entries [] = { { NULL } };
1669 g_test_bug ("166609");
1671 context = g_option_context_new (NULL);
1672 g_option_context_add_main_entries (context, entries, NULL);
1674 /* Now try parsing */
1675 argv = split_string ("program -0", &argc);
1677 retval = g_option_context_parse (context, &argc, &argv, &error);
1681 g_option_context_free (context);
1684 /* test that lone dashes are treated as non-options */
1686 lonely_dash_test (void)
1688 GOptionContext *context;
1690 GError *error = NULL;
1694 g_test_bug ("168008");
1696 context = g_option_context_new (NULL);
1698 /* Now try parsing */
1699 argv = split_string ("program -", &argc);
1701 retval = g_option_context_parse (context, &argc, &argv, &error);
1702 g_assert_no_error (error);
1705 g_assert (argv[1] && strcmp (argv[1], "-") == 0);
1708 g_option_context_free (context);
1712 missing_arg_test (void)
1714 GOptionContext *context;
1716 GError *error = NULL;
1720 GOptionEntry entries [] =
1721 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1724 g_test_bug ("305576");
1726 context = g_option_context_new (NULL);
1727 g_option_context_add_main_entries (context, entries, NULL);
1729 /* Now try parsing */
1730 argv = split_string ("program --test", &argc);
1732 retval = g_option_context_parse (context, &argc, &argv, &error);
1733 g_assert (retval == FALSE);
1734 g_clear_error (&error);
1738 /* Try parsing again */
1739 argv = split_string ("program -t", &argc);
1741 retval = g_option_context_parse (context, &argc, &argv, &error);
1742 g_assert (retval == FALSE);
1745 g_option_context_free (context);
1748 static gchar *test_arg;
1750 static gboolean cb (const gchar *option_name,
1755 test_arg = g_strdup (value);
1760 dash_arg_test (void)
1762 GOptionContext *context;
1764 GError *error = NULL;
1767 gboolean argb = FALSE;
1768 GOptionEntry entries [] =
1769 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, cb, NULL, NULL },
1770 { "three", '3', 0, G_OPTION_ARG_NONE, &argb, NULL, NULL },
1773 g_test_bug ("577638");
1775 context = g_option_context_new (NULL);
1776 g_option_context_add_main_entries (context, entries, NULL);
1778 /* Now try parsing */
1779 argv = split_string ("program --test=-3", &argc);
1783 retval = g_option_context_parse (context, &argc, &argv, &error);
1785 g_assert_no_error (error);
1786 g_assert_cmpstr (test_arg, ==, "-3");
1792 /* Try parsing again */
1793 argv = split_string ("program --test -3", &argc);
1796 retval = g_option_context_parse (context, &argc, &argv, &error);
1797 g_assert_no_error (error);
1799 g_assert_cmpstr (test_arg, ==, NULL);
1801 g_option_context_free (context);
1807 GOptionContext *context;
1809 GOptionEntry entries [] =
1810 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1813 context = g_option_context_new (NULL);
1814 g_option_context_add_main_entries (context, entries, NULL);
1816 g_assert (g_option_context_get_help_enabled (context));
1817 g_assert (!g_option_context_get_ignore_unknown_options (context));
1818 g_assert_cmpstr (g_option_context_get_summary (context), ==, NULL);
1819 g_assert_cmpstr (g_option_context_get_description (context), ==, NULL);
1821 g_option_context_set_help_enabled (context, FALSE);
1822 g_option_context_set_ignore_unknown_options (context, TRUE);
1823 g_option_context_set_summary (context, "summary");
1824 g_option_context_set_description(context, "description");
1826 g_assert (!g_option_context_get_help_enabled (context));
1827 g_assert (g_option_context_get_ignore_unknown_options (context));
1828 g_assert_cmpstr (g_option_context_get_summary (context), ==, "summary");
1829 g_assert_cmpstr (g_option_context_get_description (context), ==, "description");
1831 g_option_context_free (context);
1835 test_main_group (void)
1837 GOptionContext *context;
1838 GOptionGroup *group;
1840 context = g_option_context_new (NULL);
1841 g_assert (g_option_context_get_main_group (context) == NULL);
1842 group = g_option_group_new ("name", "description", "hlep", NULL, NULL);
1843 g_option_context_add_group (context, group);
1844 g_assert (g_option_context_get_main_group (context) == NULL);
1845 group = g_option_group_new ("name", "description", "hlep", NULL, NULL);
1846 g_option_context_set_main_group (context, group);
1847 g_assert (g_option_context_get_main_group (context) == group);
1849 g_option_context_free (context);
1852 static gboolean error_func_called = FALSE;
1855 error_func (GOptionContext *context,
1856 GOptionGroup *group,
1860 g_assert_cmpint (GPOINTER_TO_INT(data), ==, 1234);
1861 error_func_called = TRUE;
1865 test_error_hook (void)
1867 GOptionContext *context;
1869 GOptionEntry entries [] =
1870 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1872 GOptionGroup *group;
1876 GError *error = NULL;
1878 context = g_option_context_new (NULL);
1879 group = g_option_group_new ("name", "description", "hlep", GINT_TO_POINTER(1234), NULL);
1880 g_option_group_add_entries (group, entries);
1881 g_option_context_set_main_group (context, group);
1882 g_option_group_set_error_hook (g_option_context_get_main_group (context),
1885 argv = split_string ("program --test", &argc);
1887 retval = g_option_context_parse (context, &argc, &argv, &error);
1888 g_assert (retval == FALSE);
1889 g_clear_error (&error);
1891 g_assert (error_func_called);
1894 g_option_context_free (context);
1898 flag_reverse_string (void)
1900 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1902 GOptionContext *context;
1904 GOptionEntry entries [] =
1905 { { "test", 't', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1910 GError *error = NULL;
1912 context = g_option_context_new (NULL);
1913 g_option_context_add_main_entries (context, entries, NULL);
1915 argv = split_string ("program --test bla", &argc);
1917 retval = g_option_context_parse (context, &argc, &argv, &error);
1918 g_assert (retval == FALSE);
1919 g_clear_error (&error);
1921 g_option_context_free (context);
1924 g_test_trap_assert_failed ();
1925 g_test_trap_assert_stderr ("*ignoring reverse flag*");
1929 flag_optional_int (void)
1931 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
1933 GOptionContext *context;
1935 GOptionEntry entries [] =
1936 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &arg, NULL, NULL },
1941 GError *error = NULL;
1943 context = g_option_context_new (NULL);
1944 g_option_context_add_main_entries (context, entries, NULL);
1946 argv = split_string ("program --test 5", &argc);
1948 retval = g_option_context_parse (context, &argc, &argv, &error);
1949 g_assert (retval == FALSE);
1950 g_clear_error (&error);
1952 g_option_context_free (context);
1955 g_test_trap_assert_failed ();
1956 g_test_trap_assert_stderr ("*ignoring no-arg, optional-arg or filename flags*");
1962 g_test_init (&argc, &argv, NULL);
1964 g_test_bug_base ("http://bugzilla.gnome.org/");
1966 g_test_add_func ("/option/basic", test_basic);
1967 g_test_add_func ("/option/group/captions", group_captions);
1968 g_test_add_func ("/option/group/main", test_main_group);
1969 g_test_add_func ("/option/group/error-hook", test_error_hook);
1971 /* Test that restoration on failure works */
1972 g_test_add_func ("/option/restoration/int", error_test1);
1973 g_test_add_func ("/option/restoration/string", error_test2);
1974 g_test_add_func ("/option/restoration/boolean", error_test3);
1976 /* Test that special argument parsing works */
1977 g_test_add_func ("/option/arg/repetition/int", arg_test1);
1978 g_test_add_func ("/option/arg/repetition/string", arg_test2);
1979 g_test_add_func ("/option/arg/repetition/filename", arg_test3);
1980 g_test_add_func ("/option/arg/repetition/double", arg_test4);
1981 g_test_add_func ("/option/arg/repetition/locale", arg_test5);
1982 g_test_add_func ("/option/arg/repetition/int64", arg_test6);
1984 /* Test string arrays */
1985 g_test_add_func ("/option/arg/array/string", array_test1);
1987 /* Test callback args */
1988 g_test_add_func ("/option/arg/callback/string", callback_test1);
1989 g_test_add_func ("/option/arg/callback/count", callback_test2);
1991 /* Test optional arg flag for callback */
1992 g_test_add_func ("/option/arg/callback/optional1", callback_test_optional_1);
1993 g_test_add_func ("/option/arg/callback/optional2", callback_test_optional_2);
1994 g_test_add_func ("/option/arg/callback/optional3", callback_test_optional_3);
1995 g_test_add_func ("/option/arg/callback/optional4", callback_test_optional_4);
1996 g_test_add_func ("/option/arg/callback/optional5", callback_test_optional_5);
1997 g_test_add_func ("/option/arg/callback/optional6", callback_test_optional_6);
1998 g_test_add_func ("/option/arg/callback/optional7", callback_test_optional_7);
1999 g_test_add_func ("/option/arg/callback/optional8", callback_test_optional_8);
2001 /* Test callback with G_OPTION_REMAINING */
2002 g_test_add_func ("/option/arg/remaining/callback", callback_remaining_test1);
2004 /* Test callbacks which return FALSE */
2005 g_test_add_func ("/option/arg/remaining/callback-false", callback_returns_false);
2007 /* Test ignoring options */
2008 g_test_add_func ("/option/arg/ignore/long", ignore_test1);
2009 g_test_add_func ("/option/arg/ignore/short", ignore_test2);
2010 g_test_add_func ("/option/arg/ignore/arg", ignore_test3);
2011 g_test_add_func ("/option/context/add", add_test1);
2013 /* Test parsing empty args */
2014 /* Note there used to be an empty1 here, but it effectively moved
2015 * to option-argv0.c.
2017 g_test_add_func ("/option/context/empty2", empty_test2);
2018 g_test_add_func ("/option/context/empty3", empty_test3);
2020 /* Test handling of rest args */
2021 g_test_add_func ("/option/arg/rest/non-option", rest_test1);
2022 g_test_add_func ("/option/arg/rest/separator1", rest_test2);
2023 g_test_add_func ("/option/arg/rest/separator2", rest_test2a);
2024 g_test_add_func ("/option/arg/rest/separator3", rest_test2b);
2025 g_test_add_func ("/option/arg/rest/separator4", rest_test2c);
2026 g_test_add_func ("/option/arg/rest/separator5", rest_test2d);
2027 g_test_add_func ("/option/arg/remaining/non-option", rest_test3);
2028 g_test_add_func ("/option/arg/remaining/separator", rest_test4);
2029 g_test_add_func ("/option/arg/remaining/array", rest_test5);
2031 /* Test some invalid flag combinations */
2032 g_test_add_func ("/option/arg/reverse-string", flag_reverse_string);
2033 g_test_add_func ("/option/arg/optional-int", flag_optional_int);
2035 /* regression tests for individual bugs */
2036 g_test_add_func ("/option/bug/unknown-short", unknown_short_test);
2037 g_test_add_func ("/option/bug/lonely-dash", lonely_dash_test);
2038 g_test_add_func ("/option/bug/missing-arg", missing_arg_test);
2039 g_test_add_func ("/option/bug/dash-arg", dash_arg_test);
2041 return g_test_run();