1 /* Unit tests for GOptionContext
2 * Copyright (C) 2007 Openismus GmbH
3 * Authors: Mathias Hasselmann
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
33 gchar
*help_variants
[] = { "--help", "--help-all", "--help-test" };
35 GOptionEntry main_entries
[] = {
38 G_OPTION_ARG_NONE
, NULL
,
39 "A switch that is in the main group", NULL
},
43 GOptionEntry group_entries
[] = {
46 G_OPTION_ARG_NONE
, NULL
,
47 "A switch that is in the test group", NULL
},
53 g_test_bug ("504142");
55 for (i
= 0; i
< 4; ++i
)
57 gboolean have_main_entries
= (0 != (i
& 1));
58 gboolean have_test_entries
= (0 != (i
& 2));
60 GOptionContext
*options
;
61 GOptionGroup
*group
= NULL
;
63 options
= g_option_context_new (NULL
);
65 if (have_main_entries
)
66 g_option_context_add_main_entries (options
, main_entries
, NULL
);
67 if (have_test_entries
)
69 group
= g_option_group_new ("test", "Test Options",
70 "Show all test options",
72 g_option_context_add_group (options
, group
);
73 g_option_group_add_entries (group
, group_entries
);
76 for (j
= 0; j
< G_N_ELEMENTS (help_variants
); ++j
)
78 GTestTrapFlags trap_flags
= 0;
82 args
[1] = help_variants
[j
];
85 if (!g_test_verbose ())
86 trap_flags
|= G_TEST_TRAP_SILENCE_STDOUT
| G_TEST_TRAP_SILENCE_STDERR
;
88 g_test_message ("test setup: args='%s', main-entries=%d, test-entries=%d",
89 args
[1], have_main_entries
, have_test_entries
);
91 if (g_test_trap_fork (0, trap_flags
))
97 g_setenv ("LANG", "C", TRUE
);
99 g_option_context_parse (options
, &argc
, &argv
, &error
);
104 gboolean expect_main_description
= FALSE
;
105 gboolean expect_main_switch
= FALSE
;
107 gboolean expect_test_description
= FALSE
;
108 gboolean expect_test_switch
= FALSE
;
109 gboolean expect_test_group
= FALSE
;
111 g_test_trap_assert_passed ();
112 g_test_trap_assert_stderr ("");
117 g_assert_cmpstr ("--help", ==, args
[1]);
118 expect_main_switch
= have_main_entries
;
119 expect_test_group
= have_test_entries
;
123 g_assert_cmpstr ("--help-all", ==, args
[1]);
124 expect_main_switch
= have_main_entries
;
125 expect_test_switch
= have_test_entries
;
126 expect_test_group
= have_test_entries
;
130 g_assert_cmpstr ("--help-test", ==, args
[1]);
131 expect_test_switch
= have_test_entries
;
135 g_assert_not_reached ();
139 expect_main_description
|= expect_main_switch
;
140 expect_test_description
|= expect_test_switch
;
142 if (expect_main_description
)
143 g_test_trap_assert_stdout ("*Application Options*");
145 g_test_trap_assert_stdout_unmatched ("*Application Options*");
146 if (expect_main_switch
)
147 g_test_trap_assert_stdout ("*--main-switch*");
149 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
151 if (expect_test_description
)
152 g_test_trap_assert_stdout ("*Test Options*");
154 g_test_trap_assert_stdout_unmatched ("*Test Options*");
155 if (expect_test_switch
)
156 g_test_trap_assert_stdout ("*--test-switch*");
158 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
160 if (expect_test_group
)
161 g_test_trap_assert_stdout ("*--help-test*");
163 g_test_trap_assert_stdout_unmatched ("*--help-test*");
170 char *error_test2_string
;
171 gboolean error_test3_boolean
;
174 gchar
*arg_test2_string
;
175 gchar
*arg_test3_filename
;
176 gdouble arg_test4_double
;
177 gdouble arg_test5_double
;
178 gint64 arg_test6_int64
;
179 gint64 arg_test6_int64_2
;
181 gchar
*callback_test1_string
;
182 int callback_test2_int
;
184 gchar
*callback_test_optional_string
;
185 gboolean callback_test_optional_boolean
;
187 gchar
**array_test1_array
;
189 gboolean ignore_test1_boolean
;
190 gboolean ignore_test2_boolean
;
191 gchar
*ignore_test3_string
;
194 split_string (const char *str
, int *argc
)
199 argv
= g_strsplit (str
, " ", 0);
201 for (len
= 0; argv
[len
] != NULL
; len
++);
210 join_stringv (int argc
, char **argv
)
215 str
= g_string_new (NULL
);
217 for (i
= 0; i
< argc
; i
++)
219 g_string_append (str
, argv
[i
]);
222 g_string_append_c (str
, ' ');
225 return g_string_free (str
, FALSE
);
228 /* Performs a shallow copy */
230 copy_stringv (char **argv
, int argc
)
232 return g_memdup (argv
, sizeof (char *) * (argc
+ 1));
237 error_test1_pre_parse (GOptionContext
*context
,
242 g_assert (error_test1_int
== 0x12345678);
248 error_test1_post_parse (GOptionContext
*context
,
253 g_assert (error_test1_int
== 20);
255 /* Set an error in the post hook */
256 g_set_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
, " ");
264 GOptionContext
*context
;
266 GError
*error
= NULL
;
269 GOptionGroup
*main_group
;
270 GOptionEntry entries
[] =
271 { { "test", 0, 0, G_OPTION_ARG_INT
, &error_test1_int
, NULL
, NULL
},
274 error_test1_int
= 0x12345678;
276 context
= g_option_context_new (NULL
);
277 g_option_context_add_main_entries (context
, entries
, NULL
);
279 /* Set pre and post parse hooks */
280 main_group
= g_option_context_get_main_group (context
);
281 g_option_group_set_parse_hooks (main_group
,
282 error_test1_pre_parse
, error_test1_post_parse
);
284 /* Now try parsing */
285 argv
= split_string ("program --test 20", &argc
);
287 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
288 g_assert (retval
== FALSE
);
290 /* On failure, values should be reset */
291 g_assert (error_test1_int
== 0x12345678);
294 g_option_context_free (context
);
298 error_test2_pre_parse (GOptionContext
*context
,
303 g_assert (strcmp (error_test2_string
, "foo") == 0);
309 error_test2_post_parse (GOptionContext
*context
,
314 g_assert (strcmp (error_test2_string
, "bar") == 0);
316 /* Set an error in the post hook */
317 g_set_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
, " ");
325 GOptionContext
*context
;
327 GError
*error
= NULL
;
330 GOptionGroup
*main_group
;
331 GOptionEntry entries
[] =
332 { { "test", 0, 0, G_OPTION_ARG_STRING
, &error_test2_string
, NULL
, NULL
},
335 error_test2_string
= "foo";
337 context
= g_option_context_new (NULL
);
338 g_option_context_add_main_entries (context
, entries
, NULL
);
340 /* Set pre and post parse hooks */
341 main_group
= g_option_context_get_main_group (context
);
342 g_option_group_set_parse_hooks (main_group
,
343 error_test2_pre_parse
, error_test2_post_parse
);
345 /* Now try parsing */
346 argv
= split_string ("program --test bar", &argc
);
347 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
349 g_error_free (error
);
350 g_assert (retval
== FALSE
);
352 g_assert (strcmp (error_test2_string
, "foo") == 0);
355 g_option_context_free (context
);
359 error_test3_pre_parse (GOptionContext
*context
,
364 g_assert (!error_test3_boolean
);
370 error_test3_post_parse (GOptionContext
*context
,
375 g_assert (error_test3_boolean
);
377 /* Set an error in the post hook */
378 g_set_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
, " ");
386 GOptionContext
*context
;
388 GError
*error
= NULL
;
391 GOptionGroup
*main_group
;
392 GOptionEntry entries
[] =
393 { { "test", 0, 0, G_OPTION_ARG_NONE
, &error_test3_boolean
, NULL
, NULL
},
396 error_test3_boolean
= FALSE
;
398 context
= g_option_context_new (NULL
);
399 g_option_context_add_main_entries (context
, entries
, NULL
);
401 /* Set pre and post parse hooks */
402 main_group
= g_option_context_get_main_group (context
);
403 g_option_group_set_parse_hooks (main_group
,
404 error_test3_pre_parse
, error_test3_post_parse
);
406 /* Now try parsing */
407 argv
= split_string ("program --test", &argc
);
408 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
410 g_error_free (error
);
411 g_assert (retval
== FALSE
);
413 g_assert (!error_test3_boolean
);
416 g_option_context_free (context
);
420 assert_no_error (GError
*error
)
424 fprintf (stderr
, "unexpected error: %s, %d, %s\n", g_quark_to_string (error
->domain
), error
->code
, error
->message
);
432 GOptionContext
*context
;
434 GError
*error
= NULL
;
437 GOptionEntry entries
[] =
438 { { "test", 0, 0, G_OPTION_ARG_INT
, &arg_test1_int
, NULL
, NULL
},
441 context
= g_option_context_new (NULL
);
442 g_option_context_add_main_entries (context
, entries
, NULL
);
444 /* Now try parsing */
445 argv
= split_string ("program --test 20 --test 30", &argc
);
447 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
448 assert_no_error (error
);
451 /* Last arg specified is the one that should be stored */
452 g_assert (arg_test1_int
== 30);
455 g_option_context_free (context
);
461 GOptionContext
*context
;
463 GError
*error
= NULL
;
466 GOptionEntry entries
[] =
467 { { "test", 0, 0, G_OPTION_ARG_STRING
, &arg_test2_string
, NULL
, NULL
},
470 context
= g_option_context_new (NULL
);
471 g_option_context_add_main_entries (context
, entries
, NULL
);
473 /* Now try parsing */
474 argv
= split_string ("program --test foo --test bar", &argc
);
476 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
477 assert_no_error (error
);
480 /* Last arg specified is the one that should be stored */
481 g_assert (strcmp (arg_test2_string
, "bar") == 0);
483 g_free (arg_test2_string
);
486 g_option_context_free (context
);
492 GOptionContext
*context
;
494 GError
*error
= NULL
;
497 GOptionEntry entries
[] =
498 { { "test", 0, 0, G_OPTION_ARG_FILENAME
, &arg_test3_filename
, NULL
, NULL
},
501 context
= g_option_context_new (NULL
);
502 g_option_context_add_main_entries (context
, entries
, NULL
);
504 /* Now try parsing */
505 argv
= split_string ("program --test foo.txt", &argc
);
507 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
508 assert_no_error (error
);
511 /* Last arg specified is the one that should be stored */
512 g_assert (strcmp (arg_test3_filename
, "foo.txt") == 0);
514 g_free (arg_test3_filename
);
517 g_option_context_free (context
);
524 GOptionContext
*context
;
526 GError
*error
= NULL
;
529 GOptionEntry entries
[] =
530 { { "test", 0, 0, G_OPTION_ARG_DOUBLE
, &arg_test4_double
, NULL
, NULL
},
533 context
= g_option_context_new (NULL
);
534 g_option_context_add_main_entries (context
, entries
, NULL
);
536 /* Now try parsing */
537 argv
= split_string ("program --test 20.0 --test 30.03", &argc
);
539 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
540 assert_no_error (error
);
543 /* Last arg specified is the one that should be stored */
544 g_assert (arg_test4_double
== 30.03);
547 g_option_context_free (context
);
553 GOptionContext
*context
;
555 GError
*error
= NULL
;
558 char *old_locale
, *current_locale
;
559 const char *locale
= "de_DE";
560 GOptionEntry entries
[] =
561 { { "test", 0, 0, G_OPTION_ARG_DOUBLE
, &arg_test5_double
, NULL
, NULL
},
564 context
= g_option_context_new (NULL
);
565 g_option_context_add_main_entries (context
, entries
, NULL
);
567 /* Now try parsing */
568 argv
= split_string ("program --test 20,0 --test 30,03", &argc
);
570 /* set it to some locale that uses commas instead of decimal points */
572 old_locale
= g_strdup (setlocale (LC_NUMERIC
, locale
));
573 current_locale
= setlocale (LC_NUMERIC
, NULL
);
574 if (strcmp (current_locale
, locale
) != 0)
576 fprintf (stderr
, "Cannot set locale to %s, skipping\n", locale
);
580 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
581 assert_no_error (error
);
584 /* Last arg specified is the one that should be stored */
585 g_assert (arg_test5_double
== 30.03);
588 setlocale (LC_NUMERIC
, old_locale
);
592 g_option_context_free (context
);
598 GOptionContext
*context
;
600 GError
*error
= NULL
;
603 GOptionEntry entries
[] =
604 { { "test", 0, 0, G_OPTION_ARG_INT64
, &arg_test6_int64
, NULL
, NULL
},
605 { "test2", 0, 0, G_OPTION_ARG_INT64
, &arg_test6_int64_2
, NULL
, NULL
},
608 context
= g_option_context_new (NULL
);
609 g_option_context_add_main_entries (context
, entries
, NULL
);
611 /* Now try parsing */
612 argv
= split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc
);
614 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
615 assert_no_error (error
);
618 /* Last arg specified is the one that should be stored */
619 g_assert (arg_test6_int64
== G_GINT64_CONSTANT(4294967296));
620 g_assert (arg_test6_int64_2
== G_GINT64_CONSTANT(0xfffffffff));
623 g_option_context_free (context
);
627 callback_parse1 (const gchar
*option_name
, const gchar
*value
,
628 gpointer data
, GError
**error
)
630 callback_test1_string
= g_strdup (value
);
635 callback_test1 (void)
637 GOptionContext
*context
;
639 GError
*error
= NULL
;
642 GOptionEntry entries
[] =
643 { { "test", 0, 0, G_OPTION_ARG_CALLBACK
, callback_parse1
, NULL
, NULL
},
646 context
= g_option_context_new (NULL
);
647 g_option_context_add_main_entries (context
, entries
, NULL
);
649 /* Now try parsing */
650 argv
= split_string ("program --test foo.txt", &argc
);
652 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
653 assert_no_error (error
);
656 g_assert (strcmp (callback_test1_string
, "foo.txt") == 0);
658 g_free (callback_test1_string
);
661 g_option_context_free (context
);
665 callback_parse2 (const gchar
*option_name
, const gchar
*value
,
666 gpointer data
, GError
**error
)
668 callback_test2_int
++;
673 callback_test2 (void)
675 GOptionContext
*context
;
677 GError
*error
= NULL
;
680 GOptionEntry entries
[] =
681 { { "test", 0, G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
, callback_parse2
, NULL
, NULL
},
684 context
= g_option_context_new (NULL
);
685 g_option_context_add_main_entries (context
, entries
, NULL
);
687 /* Now try parsing */
688 argv
= split_string ("program --test --test", &argc
);
690 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
691 assert_no_error (error
);
694 g_assert (callback_test2_int
== 2);
697 g_option_context_free (context
);
701 callback_parse_optional (const gchar
*option_name
, const gchar
*value
,
702 gpointer data
, GError
**error
)
704 callback_test_optional_boolean
= TRUE
;
706 callback_test_optional_string
= g_strdup (value
);
708 callback_test_optional_string
= NULL
;
713 callback_test_optional_1 (void)
715 GOptionContext
*context
;
717 GError
*error
= NULL
;
720 GOptionEntry entries
[] =
721 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
722 callback_parse_optional
, NULL
, NULL
},
725 context
= g_option_context_new (NULL
);
726 g_option_context_add_main_entries (context
, entries
, NULL
);
728 /* Now try parsing */
729 argv
= split_string ("program --test foo.txt", &argc
);
731 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
732 assert_no_error (error
);
735 g_assert (strcmp (callback_test_optional_string
, "foo.txt") == 0);
737 g_assert (callback_test_optional_boolean
);
739 g_free (callback_test_optional_string
);
742 g_option_context_free (context
);
746 callback_test_optional_2 (void)
748 GOptionContext
*context
;
750 GError
*error
= NULL
;
753 GOptionEntry entries
[] =
754 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
755 callback_parse_optional
, NULL
, NULL
},
758 context
= g_option_context_new (NULL
);
759 g_option_context_add_main_entries (context
, entries
, NULL
);
761 /* Now try parsing */
762 argv
= split_string ("program --test", &argc
);
764 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
765 assert_no_error (error
);
768 g_assert (callback_test_optional_string
== NULL
);
770 g_assert (callback_test_optional_boolean
);
772 g_free (callback_test_optional_string
);
775 g_option_context_free (context
);
779 callback_test_optional_3 (void)
781 GOptionContext
*context
;
783 GError
*error
= NULL
;
786 GOptionEntry entries
[] =
787 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
788 callback_parse_optional
, NULL
, NULL
},
791 context
= g_option_context_new (NULL
);
792 g_option_context_add_main_entries (context
, entries
, NULL
);
794 /* Now try parsing */
795 argv
= split_string ("program -t foo.txt", &argc
);
797 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
798 assert_no_error (error
);
801 g_assert (strcmp (callback_test_optional_string
, "foo.txt") == 0);
803 g_assert (callback_test_optional_boolean
);
805 g_free (callback_test_optional_string
);
808 g_option_context_free (context
);
813 callback_test_optional_4 (void)
815 GOptionContext
*context
;
817 GError
*error
= NULL
;
820 GOptionEntry entries
[] =
821 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
822 callback_parse_optional
, NULL
, NULL
},
825 context
= g_option_context_new (NULL
);
826 g_option_context_add_main_entries (context
, entries
, NULL
);
828 /* Now try parsing */
829 argv
= split_string ("program -t", &argc
);
831 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
832 assert_no_error (error
);
835 g_assert (callback_test_optional_string
== NULL
);
837 g_assert (callback_test_optional_boolean
);
839 g_free (callback_test_optional_string
);
842 g_option_context_free (context
);
846 callback_test_optional_5 (void)
848 GOptionContext
*context
;
851 GError
*error
= NULL
;
854 GOptionEntry entries
[] =
855 { { "dummy", 'd', 0, G_OPTION_ARG_NONE
, &dummy
, NULL
},
856 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
857 callback_parse_optional
, NULL
, NULL
},
860 context
= g_option_context_new (NULL
);
861 g_option_context_add_main_entries (context
, entries
, NULL
);
863 /* Now try parsing */
864 argv
= split_string ("program --test --dummy", &argc
);
866 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
867 assert_no_error (error
);
870 g_assert (callback_test_optional_string
== NULL
);
872 g_assert (callback_test_optional_boolean
);
874 g_free (callback_test_optional_string
);
877 g_option_context_free (context
);
881 callback_test_optional_6 (void)
883 GOptionContext
*context
;
886 GError
*error
= NULL
;
889 GOptionEntry entries
[] =
890 { { "dummy", 'd', 0, G_OPTION_ARG_NONE
, &dummy
, NULL
},
891 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
892 callback_parse_optional
, NULL
, NULL
},
895 context
= g_option_context_new (NULL
);
896 g_option_context_add_main_entries (context
, entries
, NULL
);
898 /* Now try parsing */
899 argv
= split_string ("program -t -d", &argc
);
901 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
902 assert_no_error (error
);
905 g_assert (callback_test_optional_string
== NULL
);
907 g_assert (callback_test_optional_boolean
);
909 g_free (callback_test_optional_string
);
912 g_option_context_free (context
);
916 callback_test_optional_7 (void)
918 GOptionContext
*context
;
921 GError
*error
= NULL
;
924 GOptionEntry entries
[] =
925 { { "dummy", 'd', 0, G_OPTION_ARG_NONE
, &dummy
, NULL
},
926 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
927 callback_parse_optional
, NULL
, NULL
},
930 context
= g_option_context_new (NULL
);
931 g_option_context_add_main_entries (context
, entries
, NULL
);
933 /* Now try parsing */
934 argv
= split_string ("program -td", &argc
);
936 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
937 assert_no_error (error
);
940 g_assert (callback_test_optional_string
== NULL
);
942 g_assert (callback_test_optional_boolean
);
944 g_free (callback_test_optional_string
);
947 g_option_context_free (context
);
951 callback_test_optional_8 (void)
953 GOptionContext
*context
;
956 GError
*error
= NULL
;
959 GOptionEntry entries
[] =
960 { { "dummy", 'd', 0, G_OPTION_ARG_NONE
, &dummy
, NULL
},
961 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
962 callback_parse_optional
, NULL
, NULL
},
965 context
= g_option_context_new (NULL
);
966 g_option_context_add_main_entries (context
, entries
, NULL
);
968 /* Now try parsing */
969 argv
= split_string ("program -dt foo.txt", &argc
);
971 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
972 assert_no_error (error
);
975 g_assert (callback_test_optional_string
);
977 g_assert (callback_test_optional_boolean
);
979 g_free (callback_test_optional_string
);
982 g_option_context_free (context
);
985 static GPtrArray
*callback_remaining_args
;
987 callback_remaining_test1_callback (const gchar
*option_name
, const gchar
*value
,
988 gpointer data
, GError
**error
)
990 g_ptr_array_add (callback_remaining_args
, g_strdup (value
));
995 callback_remaining_test1 (void)
997 GOptionContext
*context
;
999 GError
*error
= NULL
;
1002 GOptionEntry entries
[] =
1003 { { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_CALLBACK
, callback_remaining_test1_callback
, NULL
, NULL
},
1006 callback_remaining_args
= g_ptr_array_new ();
1007 context
= g_option_context_new (NULL
);
1008 g_option_context_add_main_entries (context
, entries
, NULL
);
1010 /* Now try parsing */
1011 argv
= split_string ("program foo.txt blah.txt", &argc
);
1013 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1014 assert_no_error (error
);
1017 g_assert (callback_remaining_args
->len
== 2);
1018 g_assert (strcmp (callback_remaining_args
->pdata
[0], "foo.txt") == 0);
1019 g_assert (strcmp (callback_remaining_args
->pdata
[1], "blah.txt") == 0);
1021 g_ptr_array_foreach (callback_remaining_args
, (GFunc
) g_free
, NULL
);
1022 g_ptr_array_free (callback_remaining_args
, TRUE
);
1025 g_option_context_free (context
);
1029 callback_error (const gchar
*option_name
, const gchar
*value
,
1030 gpointer data
, GError
**error
)
1032 g_set_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
, "42");
1037 callback_returns_false (void)
1039 GOptionContext
*context
;
1041 GError
*error
= NULL
;
1044 GOptionEntry entries
[] =
1045 { { "error", 0, 0, G_OPTION_ARG_CALLBACK
, callback_error
, NULL
, NULL
},
1046 { "error-no-arg", 0, G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
, callback_error
, NULL
, NULL
},
1047 { "error-optional-arg", 0, G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
, callback_error
, NULL
, NULL
},
1050 context
= g_option_context_new (NULL
);
1051 g_option_context_add_main_entries (context
, entries
, NULL
);
1053 /* Now try parsing */
1054 argv
= split_string ("program --error value", &argc
);
1056 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1057 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
);
1058 g_assert (retval
== FALSE
);
1060 g_option_context_free (context
);
1061 g_clear_error (&error
);
1063 /* And again, this time with a no-arg variant */
1064 context
= g_option_context_new (NULL
);
1065 g_option_context_add_main_entries (context
, entries
, NULL
);
1067 argv
= split_string ("program --error-no-arg", &argc
);
1069 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1070 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
);
1071 g_assert (retval
== FALSE
);
1073 g_option_context_free (context
);
1074 g_clear_error (&error
);
1076 /* And again, this time with a optional arg variant, with argument */
1077 context
= g_option_context_new (NULL
);
1078 g_option_context_add_main_entries (context
, entries
, NULL
);
1080 argv
= split_string ("program --error-optional-arg value", &argc
);
1082 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1083 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
);
1084 g_assert (retval
== FALSE
);
1086 g_option_context_free (context
);
1087 g_clear_error (&error
);
1089 /* And again, this time with a optional arg variant, without argument */
1090 context
= g_option_context_new (NULL
);
1091 g_option_context_add_main_entries (context
, entries
, NULL
);
1093 argv
= split_string ("program --error-optional-arg", &argc
);
1095 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1096 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
);
1097 g_assert (retval
== FALSE
);
1099 g_option_context_free (context
);
1100 g_clear_error (&error
);
1107 GOptionContext
*context
;
1109 GError
*error
= NULL
;
1110 gchar
**argv
, **argv_copy
;
1113 GOptionEntry entries
[] =
1114 { { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1117 context
= g_option_context_new (NULL
);
1118 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1119 g_option_context_add_main_entries (context
, entries
, NULL
);
1121 /* Now try parsing */
1122 argv
= split_string ("program --test --hello", &argc
);
1123 argv_copy
= copy_stringv (argv
, argc
);
1125 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1126 assert_no_error (error
);
1130 arg
= join_stringv (argc
, argv
);
1131 g_assert (strcmp (arg
, "program --hello") == 0);
1134 g_strfreev (argv_copy
);
1136 g_option_context_free (context
);
1142 GOptionContext
*context
;
1144 GError
*error
= NULL
;
1148 GOptionEntry entries
[] =
1149 { { "test", 't', 0, G_OPTION_ARG_NONE
, &ignore_test2_boolean
, NULL
, NULL
},
1152 context
= g_option_context_new (NULL
);
1153 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1154 g_option_context_add_main_entries (context
, entries
, NULL
);
1156 /* Now try parsing */
1157 argv
= split_string ("program -test", &argc
);
1159 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1160 assert_no_error (error
);
1164 arg
= join_stringv (argc
, argv
);
1165 g_assert (strcmp (arg
, "program -es") == 0);
1169 g_option_context_free (context
);
1175 GOptionContext
*context
;
1177 GError
*error
= NULL
;
1178 gchar
**argv
, **argv_copy
;
1181 GOptionEntry entries
[] =
1182 { { "test", 0, 0, G_OPTION_ARG_STRING
, &ignore_test3_string
, NULL
, NULL
},
1185 context
= g_option_context_new (NULL
);
1186 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1187 g_option_context_add_main_entries (context
, entries
, NULL
);
1189 /* Now try parsing */
1190 argv
= split_string ("program --test foo --hello", &argc
);
1191 argv_copy
= copy_stringv (argv
, argc
);
1193 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1194 assert_no_error (error
);
1198 arg
= join_stringv (argc
, argv
);
1199 g_assert (strcmp (arg
, "program --hello") == 0);
1201 g_assert (strcmp (ignore_test3_string
, "foo") == 0);
1202 g_free (ignore_test3_string
);
1205 g_strfreev (argv_copy
);
1207 g_option_context_free (context
);
1213 GOptionContext
*context
;
1215 GError
*error
= NULL
;
1218 GOptionEntry entries
[] =
1219 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY
, &array_test1_array
, NULL
, NULL
},
1222 context
= g_option_context_new (NULL
);
1223 g_option_context_add_main_entries (context
, entries
, NULL
);
1225 /* Now try parsing */
1226 argv
= split_string ("program --test foo --test bar", &argc
);
1228 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1229 assert_no_error (error
);
1233 g_assert (strcmp (array_test1_array
[0], "foo") == 0);
1234 g_assert (strcmp (array_test1_array
[1], "bar") == 0);
1235 g_assert (array_test1_array
[2] == NULL
);
1237 g_strfreev (array_test1_array
);
1240 g_option_context_free (context
);
1246 GOptionContext
*context
;
1248 GOptionEntry entries1
[] =
1249 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY
, NULL
, NULL
, NULL
},
1251 GOptionEntry entries2
[] =
1252 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY
, NULL
, NULL
, NULL
},
1255 context
= g_option_context_new (NULL
);
1256 g_option_context_add_main_entries (context
, entries1
, NULL
);
1257 g_option_context_add_main_entries (context
, entries2
, NULL
);
1259 g_option_context_free (context
);
1265 GOptionContext
*context
;
1266 GOptionEntry entries
[] =
1270 g_set_prgname (NULL
);
1271 context
= g_option_context_new (NULL
);
1273 g_option_context_add_main_entries (context
, entries
, NULL
);
1275 g_option_context_parse (context
, NULL
, NULL
, NULL
);
1277 prgname
= g_get_prgname ();
1278 g_assert (prgname
&& strcmp (prgname
, "<unknown>") == 0);
1280 g_option_context_free (context
);
1286 GOptionContext
*context
;
1288 context
= g_option_context_new (NULL
);
1289 g_option_context_parse (context
, NULL
, NULL
, NULL
);
1291 g_option_context_free (context
);
1297 GOptionContext
*context
;
1304 context
= g_option_context_new (NULL
);
1305 g_option_context_parse (context
, &argc
, &argv
, NULL
);
1307 g_option_context_free (context
);
1310 /* check that non-option arguments are left in argv by default */
1314 GOptionContext
*context
;
1316 GError
*error
= NULL
;
1319 GOptionEntry entries
[] = {
1320 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1324 context
= g_option_context_new (NULL
);
1325 g_option_context_add_main_entries (context
, entries
, NULL
);
1327 /* Now try parsing */
1328 argv
= split_string ("program foo --test bar", &argc
);
1330 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1331 assert_no_error (error
);
1335 g_assert (ignore_test1_boolean
);
1336 g_assert (strcmp (argv
[0], "program") == 0);
1337 g_assert (strcmp (argv
[1], "foo") == 0);
1338 g_assert (strcmp (argv
[2], "bar") == 0);
1339 g_assert (argv
[3] == NULL
);
1342 g_option_context_free (context
);
1345 /* check that -- works */
1349 GOptionContext
*context
;
1351 GError
*error
= NULL
;
1354 GOptionEntry entries
[] = {
1355 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1359 context
= g_option_context_new (NULL
);
1360 g_option_context_add_main_entries (context
, entries
, NULL
);
1362 /* Now try parsing */
1363 argv
= split_string ("program foo --test -- -bar", &argc
);
1365 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1366 assert_no_error (error
);
1370 g_assert (ignore_test1_boolean
);
1371 g_assert (strcmp (argv
[0], "program") == 0);
1372 g_assert (strcmp (argv
[1], "foo") == 0);
1373 g_assert (strcmp (argv
[2], "--") == 0);
1374 g_assert (strcmp (argv
[3], "-bar") == 0);
1375 g_assert (argv
[4] == NULL
);
1378 g_option_context_free (context
);
1381 /* check that -- stripping works */
1385 GOptionContext
*context
;
1387 GError
*error
= NULL
;
1390 GOptionEntry entries
[] = {
1391 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1395 context
= g_option_context_new (NULL
);
1396 g_option_context_add_main_entries (context
, entries
, NULL
);
1398 /* Now try parsing */
1399 argv
= split_string ("program foo --test -- bar", &argc
);
1401 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1402 assert_no_error (error
);
1406 g_assert (ignore_test1_boolean
);
1407 g_assert (strcmp (argv
[0], "program") == 0);
1408 g_assert (strcmp (argv
[1], "foo") == 0);
1409 g_assert (strcmp (argv
[2], "bar") == 0);
1410 g_assert (argv
[3] == NULL
);
1413 g_option_context_free (context
);
1419 GOptionContext
*context
;
1421 GError
*error
= NULL
;
1424 GOptionEntry entries
[] = {
1425 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1429 context
= g_option_context_new (NULL
);
1430 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1431 g_option_context_add_main_entries (context
, entries
, NULL
);
1433 /* Now try parsing */
1434 argv
= split_string ("program foo --test -bar --", &argc
);
1436 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1437 assert_no_error (error
);
1441 g_assert (ignore_test1_boolean
);
1442 g_assert (strcmp (argv
[0], "program") == 0);
1443 g_assert (strcmp (argv
[1], "foo") == 0);
1444 g_assert (strcmp (argv
[2], "-bar") == 0);
1445 g_assert (argv
[3] == NULL
);
1448 g_option_context_free (context
);
1454 GOptionContext
*context
;
1456 GError
*error
= NULL
;
1459 GOptionEntry entries
[] = {
1460 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1464 context
= g_option_context_new (NULL
);
1465 g_option_context_add_main_entries (context
, entries
, NULL
);
1467 /* Now try parsing */
1468 argv
= split_string ("program --test foo -- bar", &argc
);
1470 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1471 assert_no_error (error
);
1475 g_assert (ignore_test1_boolean
);
1476 g_assert (strcmp (argv
[0], "program") == 0);
1477 g_assert (strcmp (argv
[1], "foo") == 0);
1478 g_assert (strcmp (argv
[2], "bar") == 0);
1479 g_assert (argv
[3] == NULL
);
1482 g_option_context_free (context
);
1488 GOptionContext
*context
;
1490 GError
*error
= NULL
;
1493 GOptionEntry entries
[] = {
1494 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1498 context
= g_option_context_new (NULL
);
1499 g_option_context_add_main_entries (context
, entries
, NULL
);
1501 /* Now try parsing */
1502 argv
= split_string ("program --test -- -bar", &argc
);
1504 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1505 assert_no_error (error
);
1509 g_assert (ignore_test1_boolean
);
1510 g_assert (strcmp (argv
[0], "program") == 0);
1511 g_assert (strcmp (argv
[1], "--") == 0);
1512 g_assert (strcmp (argv
[2], "-bar") == 0);
1513 g_assert (argv
[3] == NULL
);
1516 g_option_context_free (context
);
1520 /* check that G_OPTION_REMAINING collects non-option arguments */
1524 GOptionContext
*context
;
1526 GError
*error
= NULL
;
1529 GOptionEntry entries
[] = {
1530 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1531 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &array_test1_array
, NULL
, NULL
},
1535 context
= g_option_context_new (NULL
);
1536 g_option_context_add_main_entries (context
, entries
, NULL
);
1538 /* Now try parsing */
1539 argv
= split_string ("program foo --test bar", &argc
);
1541 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1542 assert_no_error (error
);
1546 g_assert (ignore_test1_boolean
);
1547 g_assert (strcmp (array_test1_array
[0], "foo") == 0);
1548 g_assert (strcmp (array_test1_array
[1], "bar") == 0);
1549 g_assert (array_test1_array
[2] == NULL
);
1551 g_strfreev (array_test1_array
);
1554 g_option_context_free (context
);
1558 /* check that G_OPTION_REMAINING and -- work together */
1562 GOptionContext
*context
;
1564 GError
*error
= NULL
;
1567 GOptionEntry entries
[] = {
1568 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1569 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &array_test1_array
, NULL
, NULL
},
1573 context
= g_option_context_new (NULL
);
1574 g_option_context_add_main_entries (context
, entries
, NULL
);
1576 /* Now try parsing */
1577 argv
= split_string ("program foo --test -- -bar", &argc
);
1579 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1580 assert_no_error (error
);
1584 g_assert (ignore_test1_boolean
);
1585 g_assert (strcmp (array_test1_array
[0], "foo") == 0);
1586 g_assert (strcmp (array_test1_array
[1], "-bar") == 0);
1587 g_assert (array_test1_array
[2] == NULL
);
1589 g_strfreev (array_test1_array
);
1592 g_option_context_free (context
);
1595 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1599 GOptionContext
*context
;
1601 GError
*error
= NULL
;
1604 GOptionEntry entries
[] = {
1605 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1606 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_FILENAME_ARRAY
, &array_test1_array
, NULL
, NULL
},
1610 context
= g_option_context_new (NULL
);
1611 g_option_context_add_main_entries (context
, entries
, NULL
);
1613 /* Now try parsing */
1614 argv
= split_string ("program foo --test bar", &argc
);
1616 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1617 assert_no_error (error
);
1621 g_assert (ignore_test1_boolean
);
1622 g_assert (strcmp (array_test1_array
[0], "foo") == 0);
1623 g_assert (strcmp (array_test1_array
[1], "bar") == 0);
1624 g_assert (array_test1_array
[2] == NULL
);
1626 g_strfreev (array_test1_array
);
1629 g_option_context_free (context
);
1633 unknown_short_test (void)
1635 GOptionContext
*context
;
1637 GError
*error
= NULL
;
1640 GOptionEntry entries
[] = { { NULL
} };
1642 g_test_bug ("166609");
1644 context
= g_option_context_new (NULL
);
1645 g_option_context_add_main_entries (context
, entries
, NULL
);
1647 /* Now try parsing */
1648 argv
= split_string ("program -0", &argc
);
1650 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1654 g_option_context_free (context
);
1657 /* test that lone dashes are treated as non-options */
1658 void lonely_dash_test (void)
1660 GOptionContext
*context
;
1662 GError
*error
= NULL
;
1666 g_test_bug ("168008");
1668 context
= g_option_context_new (NULL
);
1670 /* Now try parsing */
1671 argv
= split_string ("program -", &argc
);
1673 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1674 assert_no_error (error
);
1677 g_assert (argv
[1] && strcmp (argv
[1], "-") == 0);
1680 g_option_context_free (context
);
1684 missing_arg_test (void)
1686 GOptionContext
*context
;
1688 GError
*error
= NULL
;
1692 GOptionEntry entries
[] =
1693 { { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
1696 g_test_bug ("305576");
1698 context
= g_option_context_new (NULL
);
1699 g_option_context_add_main_entries (context
, entries
, NULL
);
1701 /* Now try parsing */
1702 argv
= split_string ("program --test", &argc
);
1704 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1705 g_assert (retval
== FALSE
);
1706 g_clear_error (&error
);
1710 /* Try parsing again */
1711 argv
= split_string ("program --t", &argc
);
1713 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1714 g_assert (retval
== FALSE
);
1717 g_option_context_free (context
);
1724 g_test_init (&argc
, &argv
, NULL
);
1726 g_test_bug_base ("http://bugzilla.gnome.org/");
1727 g_test_add_func ("/group/captions", group_captions
);
1729 /* Test that restoration on failure works */
1730 g_test_add_func ("/restoration/int", error_test1
);
1731 g_test_add_func ("/restoration/string", error_test2
);
1732 g_test_add_func ("/restoration/boolean", error_test3
);
1734 /* Test that special argument parsing works */
1735 g_test_add_func ("/arg/repetition/int", arg_test1
);
1736 g_test_add_func ("/arg/repetition/string", arg_test2
);
1737 g_test_add_func ("/arg/repetition/filename", arg_test3
);
1738 g_test_add_func ("/arg/repetition/double", arg_test4
);
1739 g_test_add_func ("/arg/repetition/locale", arg_test5
);
1740 g_test_add_func ("/arg/repetition/int64", arg_test6
);
1742 /* Test string arrays */
1743 g_test_add_func ("/arg/array/string", array_test1
);
1745 /* Test callback args */
1746 g_test_add_func ("/arg/callback/string", callback_test1
);
1747 g_test_add_func ("/arg/callback/count", callback_test2
);
1749 /* Test optional arg flag for callback */
1750 g_test_add_func ("/arg/callback/optional1", callback_test_optional_1
);
1751 g_test_add_func ("/arg/callback/optional2", callback_test_optional_2
);
1752 g_test_add_func ("/arg/callback/optional3", callback_test_optional_3
);
1753 g_test_add_func ("/arg/callback/optional4", callback_test_optional_4
);
1754 g_test_add_func ("/arg/callback/optional5", callback_test_optional_5
);
1755 g_test_add_func ("/arg/callback/optional6", callback_test_optional_6
);
1756 g_test_add_func ("/arg/callback/optional7", callback_test_optional_7
);
1757 g_test_add_func ("/arg/callback/optional8", callback_test_optional_8
);
1759 /* Test callback with G_OPTION_REMAINING */
1760 g_test_add_func ("/arg/remaining/callback", callback_remaining_test1
);
1762 /* Test callbacks which return FALSE */
1763 g_test_add_func ("/arg/remaining/callback-false", callback_returns_false
);
1765 /* Test ignoring options */
1766 g_test_add_func ("/arg/ignore/long", ignore_test1
);
1767 g_test_add_func ("/arg/ignore/short", ignore_test2
);
1768 g_test_add_func ("/arg/ignore/arg", ignore_test3
);
1770 g_test_add_func ("/context/add", add_test1
);
1772 /* Test parsing empty args */
1773 g_test_add_func ("/context/empty1", empty_test1
);
1774 g_test_add_func ("/context/empty2", empty_test2
);
1775 g_test_add_func ("/context/empty3", empty_test3
);
1777 /* Test handling of rest args */
1778 g_test_add_func ("/arg/rest/non-option", rest_test1
);
1779 g_test_add_func ("/arg/rest/separator1", rest_test2
);
1780 g_test_add_func ("/arg/rest/separator2", rest_test2a
);
1781 g_test_add_func ("/arg/rest/separator3", rest_test2b
);
1782 g_test_add_func ("/arg/rest/separator4", rest_test2c
);
1783 g_test_add_func ("/arg/rest/separator5", rest_test2d
);
1784 g_test_add_func ("/arg/remaining/non-option", rest_test3
);
1785 g_test_add_func ("/arg/remaining/separator", rest_test4
);
1786 g_test_add_func ("/arg/remaining/array", rest_test5
);
1788 /* regression tests for individual bugs */
1789 g_test_add_func ("/bug/unknown-short", unknown_short_test
);
1790 g_test_add_func ("/bug/lonely-dash", lonely_dash_test
);
1791 g_test_add_func ("/bug/missing-arg", missing_arg_test
);
1793 return g_test_run();