1 /* Unit tests for GOptionContext
2 * Copyright (C) 2007 Openismus GmbH
3 * Authors: Mathias Hasselmann
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
30 static GOptionEntry main_entries
[] = {
31 { "main-switch", 0, 0,
32 G_OPTION_ARG_NONE
, NULL
,
33 "A switch that is in the main group", NULL
},
37 static GOptionEntry group_entries
[] = {
38 { "test-switch", 0, 0,
39 G_OPTION_ARG_NONE
, NULL
,
40 "A switch that is in the test group", NULL
},
44 static GOptionContext
*
45 make_options (int test_number
)
47 GOptionContext
*options
;
48 GOptionGroup
*group
= NULL
;
49 gboolean have_main_entries
= (0 != (test_number
& 1));
50 gboolean have_test_entries
= (0 != (test_number
& 2));
52 options
= g_option_context_new (NULL
);
54 if (have_main_entries
)
55 g_option_context_add_main_entries (options
, main_entries
, NULL
);
56 if (have_test_entries
)
58 group
= g_option_group_new ("test", "Test Options",
59 "Show all test options",
61 g_option_context_add_group (options
, group
);
62 g_option_group_add_entries (group
, group_entries
);
69 print_help (GOptionContext
*options
, gchar
**argv
)
74 g_option_context_parse (options
, &argc
, &argv
, &error
);
75 g_option_context_free (options
);
80 test_group_captions_help (gconstpointer test_number
)
82 GOptionContext
*options
;
83 gchar
*argv
[] = { __FILE__
, "--help", NULL
};
85 options
= make_options (GPOINTER_TO_INT (test_number
));
86 print_help (options
, argv
);
90 test_group_captions_help_all (gconstpointer test_number
)
92 GOptionContext
*options
;
93 gchar
*argv
[] = { __FILE__
, "--help-all", NULL
};
95 options
= make_options (GPOINTER_TO_INT (test_number
));
96 print_help (options
, argv
);
100 test_group_captions_help_test (gconstpointer test_number
)
102 GOptionContext
*options
;
103 gchar
*argv
[] = { __FILE__
, "--help-test", NULL
};
105 options
= make_options (GPOINTER_TO_INT (test_number
));
106 print_help (options
, argv
);
110 test_group_captions (void)
112 const gchar
*test_name_base
[] = { "help", "help-all", "help-test" };
116 g_test_bug ("504142");
118 for (i
= 0; i
< 4; ++i
)
120 gboolean have_main_entries
= (0 != (i
& 1));
121 gboolean have_test_entries
= (0 != (i
& 2));
123 for (j
= 0; j
< G_N_ELEMENTS (test_name_base
); ++j
)
125 GTestSubprocessFlags trap_flags
= 0;
126 gboolean expect_main_description
= FALSE
;
127 gboolean expect_main_switch
= FALSE
;
128 gboolean expect_test_description
= FALSE
;
129 gboolean expect_test_switch
= FALSE
;
130 gboolean expect_test_group
= FALSE
;
132 if (g_test_verbose ())
133 trap_flags
|= G_TEST_SUBPROCESS_INHERIT_STDOUT
| G_TEST_SUBPROCESS_INHERIT_STDERR
;
135 test_name
= g_strdup_printf ("/option/group/captions/subprocess/%s-%d",
136 test_name_base
[j
], i
);
137 g_test_trap_subprocess (test_name
, 0, trap_flags
);
139 g_test_trap_assert_passed ();
140 g_test_trap_assert_stderr ("");
145 g_assert_cmpstr ("help", ==, test_name_base
[j
]);
146 expect_main_switch
= have_main_entries
;
147 expect_test_group
= have_test_entries
;
151 g_assert_cmpstr ("help-all", ==, test_name_base
[j
]);
152 expect_main_switch
= have_main_entries
;
153 expect_test_switch
= have_test_entries
;
154 expect_test_group
= have_test_entries
;
158 g_assert_cmpstr ("help-test", ==, test_name_base
[j
]);
159 expect_test_switch
= have_test_entries
;
163 g_assert_not_reached ();
167 expect_main_description
|= expect_main_switch
;
168 expect_test_description
|= expect_test_switch
;
170 if (expect_main_description
)
171 g_test_trap_assert_stdout ("*Application Options*");
173 g_test_trap_assert_stdout_unmatched ("*Application Options*");
174 if (expect_main_switch
)
175 g_test_trap_assert_stdout ("*--main-switch*");
177 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
179 if (expect_test_description
)
180 g_test_trap_assert_stdout ("*Test Options*");
182 g_test_trap_assert_stdout_unmatched ("*Test Options*");
183 if (expect_test_switch
)
184 g_test_trap_assert_stdout ("*--test-switch*");
186 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
188 if (expect_test_group
)
189 g_test_trap_assert_stdout ("*--help-test*");
191 g_test_trap_assert_stdout_unmatched ("*--help-test*");
197 char *error_test2_string
;
198 gboolean error_test3_boolean
;
201 gchar
*arg_test2_string
;
202 gchar
*arg_test3_filename
;
203 gdouble arg_test4_double
;
204 gdouble arg_test5_double
;
205 gint64 arg_test6_int64
;
206 gint64 arg_test6_int64_2
;
208 gchar
*callback_test1_string
;
209 int callback_test2_int
;
211 gchar
*callback_test_optional_string
;
212 gboolean callback_test_optional_boolean
;
214 gchar
**array_test1_array
;
216 gboolean ignore_test1_boolean
;
217 gboolean ignore_test2_boolean
;
218 gchar
*ignore_test3_string
;
221 split_string (const char *str
, int *argc
)
226 argv
= g_strsplit (str
, " ", 0);
228 for (len
= 0; argv
[len
] != NULL
; len
++);
237 join_stringv (int argc
, char **argv
)
242 str
= g_string_new (NULL
);
244 for (i
= 0; i
< argc
; i
++)
246 g_string_append (str
, argv
[i
]);
249 g_string_append_c (str
, ' ');
252 return g_string_free (str
, FALSE
);
255 /* Performs a shallow copy */
257 copy_stringv (char **argv
, int argc
)
259 return g_memdup (argv
, sizeof (char *) * (argc
+ 1));
263 check_identical_stringv (gchar
**before
, gchar
**after
)
267 /* Not only is it the same string... */
268 for (i
= 0; before
[i
] != NULL
; i
++)
269 g_assert_cmpstr (before
[i
], ==, after
[i
]);
271 /* ... it is actually the same pointer */
272 for (i
= 0; before
[i
] != NULL
; i
++)
273 g_assert (before
[i
] == after
[i
]);
275 g_assert (after
[i
] == NULL
);
280 error_test1_pre_parse (GOptionContext
*context
,
285 g_assert (error_test1_int
== 0x12345678);
291 error_test1_post_parse (GOptionContext
*context
,
296 g_assert (error_test1_int
== 20);
298 /* Set an error in the post hook */
299 g_set_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
, " ");
307 GOptionContext
*context
;
309 GError
*error
= NULL
;
313 GOptionGroup
*main_group
;
314 GOptionEntry entries
[] =
315 { { "test", 0, 0, G_OPTION_ARG_INT
, &error_test1_int
, NULL
, NULL
},
318 error_test1_int
= 0x12345678;
320 context
= g_option_context_new (NULL
);
321 g_option_context_add_main_entries (context
, entries
, NULL
);
323 /* Set pre and post parse hooks */
324 main_group
= g_option_context_get_main_group (context
);
325 g_option_group_set_parse_hooks (main_group
,
326 error_test1_pre_parse
, error_test1_post_parse
);
328 /* Now try parsing */
329 argv
= split_string ("program --test 20", &argc
);
330 argv_copy
= copy_stringv (argv
, argc
);
332 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
333 g_assert (retval
== FALSE
);
334 g_assert (error
!= NULL
);
335 /* An error occurred, so argv has not been changed */
336 check_identical_stringv (argv_copy
, argv
);
337 g_clear_error (&error
);
339 /* On failure, values should be reset */
340 g_assert (error_test1_int
== 0x12345678);
342 g_strfreev (argv_copy
);
344 g_option_context_free (context
);
348 error_test2_pre_parse (GOptionContext
*context
,
353 g_assert (strcmp (error_test2_string
, "foo") == 0);
359 error_test2_post_parse (GOptionContext
*context
,
364 g_assert (strcmp (error_test2_string
, "bar") == 0);
366 /* Set an error in the post hook */
367 g_set_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
, " ");
375 GOptionContext
*context
;
377 GError
*error
= NULL
;
381 GOptionGroup
*main_group
;
382 GOptionEntry entries
[] =
383 { { "test", 0, 0, G_OPTION_ARG_STRING
, &error_test2_string
, NULL
, NULL
},
386 error_test2_string
= "foo";
388 context
= g_option_context_new (NULL
);
389 g_option_context_add_main_entries (context
, entries
, NULL
);
391 /* Set pre and post parse hooks */
392 main_group
= g_option_context_get_main_group (context
);
393 g_option_group_set_parse_hooks (main_group
,
394 error_test2_pre_parse
, error_test2_post_parse
);
396 /* Now try parsing */
397 argv
= split_string ("program --test bar", &argc
);
398 argv_copy
= copy_stringv (argv
, argc
);
399 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
401 g_assert (retval
== FALSE
);
402 g_assert (error
!= NULL
);
403 check_identical_stringv (argv_copy
, argv
);
404 g_clear_error (&error
);
406 g_assert (strcmp (error_test2_string
, "foo") == 0);
408 g_strfreev (argv_copy
);
410 g_option_context_free (context
);
414 error_test3_pre_parse (GOptionContext
*context
,
419 g_assert (!error_test3_boolean
);
425 error_test3_post_parse (GOptionContext
*context
,
430 g_assert (error_test3_boolean
);
432 /* Set an error in the post hook */
433 g_set_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
, " ");
441 GOptionContext
*context
;
443 GError
*error
= NULL
;
447 GOptionGroup
*main_group
;
448 GOptionEntry entries
[] =
449 { { "test", 0, 0, G_OPTION_ARG_NONE
, &error_test3_boolean
, NULL
, NULL
},
452 error_test3_boolean
= FALSE
;
454 context
= g_option_context_new (NULL
);
455 g_option_context_add_main_entries (context
, entries
, NULL
);
457 /* Set pre and post parse hooks */
458 main_group
= g_option_context_get_main_group (context
);
459 g_option_group_set_parse_hooks (main_group
,
460 error_test3_pre_parse
, error_test3_post_parse
);
462 /* Now try parsing */
463 argv
= split_string ("program --test", &argc
);
464 argv_copy
= copy_stringv (argv
, argc
);
465 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
467 g_assert (retval
== FALSE
);
468 g_assert (error
!= NULL
);
469 check_identical_stringv (argv_copy
, argv
);
470 g_clear_error (&error
);
472 g_assert (!error_test3_boolean
);
474 g_strfreev (argv_copy
);
476 g_option_context_free (context
);
482 GOptionContext
*context
;
484 GError
*error
= NULL
;
488 GOptionEntry entries
[] =
489 { { "test", 0, 0, G_OPTION_ARG_INT
, &arg_test1_int
, NULL
, NULL
},
492 context
= g_option_context_new (NULL
);
493 g_option_context_add_main_entries (context
, entries
, NULL
);
495 /* Now try parsing */
496 argv
= split_string ("program --test 20 --test 30", &argc
);
497 argv_copy
= copy_stringv (argv
, argc
);
499 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
500 g_assert_no_error (error
);
503 /* Last arg specified is the one that should be stored */
504 g_assert (arg_test1_int
== 30);
506 /* We free all of the strings in a copy of argv, because now argv is a
507 * subset - some have been removed in-place
509 g_strfreev (argv_copy
);
511 g_option_context_free (context
);
517 GOptionContext
*context
;
519 GError
*error
= NULL
;
523 GOptionEntry entries
[] =
524 { { "test", 0, 0, G_OPTION_ARG_STRING
, &arg_test2_string
, NULL
, NULL
},
527 context
= g_option_context_new (NULL
);
528 g_option_context_add_main_entries (context
, entries
, NULL
);
530 /* Now try parsing */
531 argv
= split_string ("program --test foo --test bar", &argc
);
532 argv_copy
= copy_stringv (argv
, argc
);
534 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
535 g_assert_no_error (error
);
538 /* Last arg specified is the one that should be stored */
539 g_assert (strcmp (arg_test2_string
, "bar") == 0);
541 g_free (arg_test2_string
);
543 g_strfreev (argv_copy
);
545 g_option_context_free (context
);
551 GOptionContext
*context
;
553 GError
*error
= NULL
;
557 GOptionEntry entries
[] =
558 { { "test", 0, 0, G_OPTION_ARG_FILENAME
, &arg_test3_filename
, NULL
, NULL
},
561 context
= g_option_context_new (NULL
);
562 g_option_context_add_main_entries (context
, entries
, NULL
);
564 /* Now try parsing */
565 argv
= split_string ("program --test foo.txt", &argc
);
566 argv_copy
= copy_stringv (argv
, argc
);
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 (strcmp (arg_test3_filename
, "foo.txt") == 0);
575 g_free (arg_test3_filename
);
577 g_strfreev (argv_copy
);
579 g_option_context_free (context
);
586 GOptionContext
*context
;
588 GError
*error
= NULL
;
592 GOptionEntry entries
[] =
593 { { "test", 0, 0, G_OPTION_ARG_DOUBLE
, &arg_test4_double
, 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 20.0 --test 30.03", &argc
);
601 argv_copy
= copy_stringv (argv
, argc
);
603 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
604 g_assert_no_error (error
);
607 /* Last arg specified is the one that should be stored */
608 g_assert (arg_test4_double
== 30.03);
610 g_strfreev (argv_copy
);
612 g_option_context_free (context
);
618 GOptionContext
*context
;
620 GError
*error
= NULL
;
624 char *old_locale
, *current_locale
;
625 const char *locale
= "de_DE.UTF-8";
626 GOptionEntry entries
[] =
627 { { "test", 0, 0, G_OPTION_ARG_DOUBLE
, &arg_test5_double
, NULL
, NULL
},
630 context
= g_option_context_new (NULL
);
631 g_option_context_add_main_entries (context
, entries
, NULL
);
633 /* Now try parsing */
634 argv
= split_string ("program --test 20,0 --test 30,03", &argc
);
635 argv_copy
= copy_stringv (argv
, argc
);
637 /* set it to some locale that uses commas instead of decimal points */
639 old_locale
= g_strdup (setlocale (LC_NUMERIC
, locale
));
640 current_locale
= setlocale (LC_NUMERIC
, NULL
);
641 if (strcmp (current_locale
, locale
) != 0)
643 fprintf (stderr
, "Cannot set locale to %s, skipping\n", locale
);
647 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
648 g_assert_no_error (error
);
651 /* Last arg specified is the one that should be stored */
652 g_assert (arg_test5_double
== 30.03);
655 setlocale (LC_NUMERIC
, old_locale
);
658 g_strfreev (argv_copy
);
660 g_option_context_free (context
);
666 GOptionContext
*context
;
668 GError
*error
= NULL
;
672 GOptionEntry entries
[] =
673 { { "test", 0, 0, G_OPTION_ARG_INT64
, &arg_test6_int64
, NULL
, NULL
},
674 { "test2", 0, 0, G_OPTION_ARG_INT64
, &arg_test6_int64_2
, NULL
, NULL
},
677 context
= g_option_context_new (NULL
);
678 g_option_context_add_main_entries (context
, entries
, NULL
);
680 /* Now try parsing */
681 argv
= split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc
);
682 argv_copy
= copy_stringv (argv
, argc
);
684 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
685 g_assert_no_error (error
);
688 /* Last arg specified is the one that should be stored */
689 g_assert (arg_test6_int64
== G_GINT64_CONSTANT(4294967296));
690 g_assert (arg_test6_int64_2
== G_GINT64_CONSTANT(0xfffffffff));
692 g_strfreev (argv_copy
);
694 g_option_context_free (context
);
698 callback_parse1 (const gchar
*option_name
, const gchar
*value
,
699 gpointer data
, GError
**error
)
701 callback_test1_string
= g_strdup (value
);
706 callback_test1 (void)
708 GOptionContext
*context
;
710 GError
*error
= NULL
;
714 GOptionEntry entries
[] =
715 { { "test", 0, 0, G_OPTION_ARG_CALLBACK
, callback_parse1
, NULL
, NULL
},
718 context
= g_option_context_new (NULL
);
719 g_option_context_add_main_entries (context
, entries
, NULL
);
721 /* Now try parsing */
722 argv
= split_string ("program --test foo.txt", &argc
);
723 argv_copy
= copy_stringv (argv
, argc
);
725 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
726 g_assert_no_error (error
);
729 g_assert (strcmp (callback_test1_string
, "foo.txt") == 0);
731 g_free (callback_test1_string
);
733 g_strfreev (argv_copy
);
735 g_option_context_free (context
);
739 callback_parse2 (const gchar
*option_name
, const gchar
*value
,
740 gpointer data
, GError
**error
)
742 callback_test2_int
++;
747 callback_test2 (void)
749 GOptionContext
*context
;
751 GError
*error
= NULL
;
755 GOptionEntry entries
[] =
756 { { "test", 0, G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
, callback_parse2
, NULL
, NULL
},
759 context
= g_option_context_new (NULL
);
760 g_option_context_add_main_entries (context
, entries
, NULL
);
762 /* Now try parsing */
763 argv
= split_string ("program --test --test", &argc
);
764 argv_copy
= copy_stringv (argv
, argc
);
766 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
767 g_assert_no_error (error
);
770 g_assert (callback_test2_int
== 2);
772 g_strfreev (argv_copy
);
774 g_option_context_free (context
);
778 callback_parse_optional (const gchar
*option_name
, const gchar
*value
,
779 gpointer data
, GError
**error
)
781 callback_test_optional_boolean
= TRUE
;
783 callback_test_optional_string
= g_strdup (value
);
785 callback_test_optional_string
= NULL
;
790 callback_test_optional_1 (void)
792 GOptionContext
*context
;
794 GError
*error
= NULL
;
798 GOptionEntry entries
[] =
799 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
800 callback_parse_optional
, NULL
, NULL
},
803 context
= g_option_context_new (NULL
);
804 g_option_context_add_main_entries (context
, entries
, NULL
);
806 /* Now try parsing */
807 argv
= split_string ("program --test foo.txt", &argc
);
808 argv_copy
= copy_stringv (argv
, argc
);
810 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
811 g_assert_no_error (error
);
814 g_assert (strcmp (callback_test_optional_string
, "foo.txt") == 0);
816 g_assert (callback_test_optional_boolean
);
818 g_free (callback_test_optional_string
);
820 g_strfreev (argv_copy
);
822 g_option_context_free (context
);
826 callback_test_optional_2 (void)
828 GOptionContext
*context
;
830 GError
*error
= NULL
;
834 GOptionEntry entries
[] =
835 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
836 callback_parse_optional
, NULL
, NULL
},
839 context
= g_option_context_new (NULL
);
840 g_option_context_add_main_entries (context
, entries
, NULL
);
842 /* Now try parsing */
843 argv
= split_string ("program --test", &argc
);
844 argv_copy
= copy_stringv (argv
, argc
);
846 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
847 g_assert_no_error (error
);
850 g_assert (callback_test_optional_string
== NULL
);
852 g_assert (callback_test_optional_boolean
);
854 g_free (callback_test_optional_string
);
856 g_strfreev (argv_copy
);
858 g_option_context_free (context
);
862 callback_test_optional_3 (void)
864 GOptionContext
*context
;
866 GError
*error
= NULL
;
870 GOptionEntry entries
[] =
871 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
872 callback_parse_optional
, NULL
, NULL
},
875 context
= g_option_context_new (NULL
);
876 g_option_context_add_main_entries (context
, entries
, NULL
);
878 /* Now try parsing */
879 argv
= split_string ("program -t foo.txt", &argc
);
880 argv_copy
= copy_stringv (argv
, argc
);
882 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
883 g_assert_no_error (error
);
886 g_assert (strcmp (callback_test_optional_string
, "foo.txt") == 0);
888 g_assert (callback_test_optional_boolean
);
890 g_free (callback_test_optional_string
);
892 g_strfreev (argv_copy
);
894 g_option_context_free (context
);
899 callback_test_optional_4 (void)
901 GOptionContext
*context
;
903 GError
*error
= NULL
;
907 GOptionEntry entries
[] =
908 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
909 callback_parse_optional
, NULL
, NULL
},
912 context
= g_option_context_new (NULL
);
913 g_option_context_add_main_entries (context
, entries
, NULL
);
915 /* Now try parsing */
916 argv
= split_string ("program -t", &argc
);
917 argv_copy
= copy_stringv (argv
, argc
);
919 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
920 g_assert_no_error (error
);
923 g_assert (callback_test_optional_string
== NULL
);
925 g_assert (callback_test_optional_boolean
);
927 g_free (callback_test_optional_string
);
929 g_strfreev (argv_copy
);
931 g_option_context_free (context
);
935 callback_test_optional_5 (void)
937 GOptionContext
*context
;
940 GError
*error
= NULL
;
944 GOptionEntry entries
[] =
945 { { "dummy", 'd', 0, G_OPTION_ARG_NONE
, &dummy
, NULL
},
946 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
947 callback_parse_optional
, NULL
, NULL
},
950 context
= g_option_context_new (NULL
);
951 g_option_context_add_main_entries (context
, entries
, NULL
);
953 /* Now try parsing */
954 argv
= split_string ("program --test --dummy", &argc
);
955 argv_copy
= copy_stringv (argv
, argc
);
957 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
958 g_assert_no_error (error
);
961 g_assert (callback_test_optional_string
== NULL
);
963 g_assert (callback_test_optional_boolean
);
965 g_free (callback_test_optional_string
);
967 g_strfreev (argv_copy
);
969 g_option_context_free (context
);
973 callback_test_optional_6 (void)
975 GOptionContext
*context
;
978 GError
*error
= NULL
;
982 GOptionEntry entries
[] =
983 { { "dummy", 'd', 0, G_OPTION_ARG_NONE
, &dummy
, NULL
},
984 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
985 callback_parse_optional
, NULL
, NULL
},
988 context
= g_option_context_new (NULL
);
989 g_option_context_add_main_entries (context
, entries
, NULL
);
991 /* Now try parsing */
992 argv
= split_string ("program -t -d", &argc
);
993 argv_copy
= copy_stringv (argv
, argc
);
995 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
996 g_assert_no_error (error
);
999 g_assert (callback_test_optional_string
== NULL
);
1001 g_assert (callback_test_optional_boolean
);
1003 g_free (callback_test_optional_string
);
1005 g_strfreev (argv_copy
);
1007 g_option_context_free (context
);
1011 callback_test_optional_7 (void)
1013 GOptionContext
*context
;
1016 GError
*error
= NULL
;
1020 GOptionEntry entries
[] =
1021 { { "dummy", 'd', 0, G_OPTION_ARG_NONE
, &dummy
, NULL
},
1022 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
1023 callback_parse_optional
, NULL
, NULL
},
1026 context
= g_option_context_new (NULL
);
1027 g_option_context_add_main_entries (context
, entries
, NULL
);
1029 /* Now try parsing */
1030 argv
= split_string ("program -td", &argc
);
1031 argv_copy
= copy_stringv (argv
, argc
);
1033 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1034 g_assert_no_error (error
);
1037 g_assert (callback_test_optional_string
== NULL
);
1039 g_assert (callback_test_optional_boolean
);
1041 g_free (callback_test_optional_string
);
1043 g_strfreev (argv_copy
);
1045 g_option_context_free (context
);
1049 callback_test_optional_8 (void)
1051 GOptionContext
*context
;
1054 GError
*error
= NULL
;
1058 GOptionEntry entries
[] =
1059 { { "dummy", 'd', 0, G_OPTION_ARG_NONE
, &dummy
, NULL
},
1060 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
,
1061 callback_parse_optional
, NULL
, NULL
},
1064 context
= g_option_context_new (NULL
);
1065 g_option_context_add_main_entries (context
, entries
, NULL
);
1067 /* Now try parsing */
1068 argv
= split_string ("program -dt foo.txt", &argc
);
1069 argv_copy
= copy_stringv (argv
, argc
);
1071 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1072 g_assert_no_error (error
);
1075 g_assert (callback_test_optional_string
);
1077 g_assert (callback_test_optional_boolean
);
1079 g_free (callback_test_optional_string
);
1081 g_strfreev (argv_copy
);
1083 g_option_context_free (context
);
1086 static GPtrArray
*callback_remaining_args
;
1088 callback_remaining_test1_callback (const gchar
*option_name
, const gchar
*value
,
1089 gpointer data
, GError
**error
)
1091 g_ptr_array_add (callback_remaining_args
, g_strdup (value
));
1096 callback_remaining_test1 (void)
1098 GOptionContext
*context
;
1100 GError
*error
= NULL
;
1104 GOptionEntry entries
[] =
1105 { { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_CALLBACK
, callback_remaining_test1_callback
, NULL
, NULL
},
1108 callback_remaining_args
= g_ptr_array_new ();
1109 context
= g_option_context_new (NULL
);
1110 g_option_context_add_main_entries (context
, entries
, NULL
);
1112 /* Now try parsing */
1113 argv
= split_string ("program foo.txt blah.txt", &argc
);
1114 argv_copy
= copy_stringv (argv
, argc
);
1116 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1117 g_assert_no_error (error
);
1120 g_assert (callback_remaining_args
->len
== 2);
1121 g_assert (strcmp (callback_remaining_args
->pdata
[0], "foo.txt") == 0);
1122 g_assert (strcmp (callback_remaining_args
->pdata
[1], "blah.txt") == 0);
1124 g_ptr_array_foreach (callback_remaining_args
, (GFunc
) g_free
, NULL
);
1125 g_ptr_array_free (callback_remaining_args
, TRUE
);
1127 g_strfreev (argv_copy
);
1129 g_option_context_free (context
);
1133 callback_error (const gchar
*option_name
, const gchar
*value
,
1134 gpointer data
, GError
**error
)
1136 g_set_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
, "42");
1141 callback_returns_false (void)
1143 GOptionContext
*context
;
1145 GError
*error
= NULL
;
1149 GOptionEntry entries
[] =
1150 { { "error", 0, 0, G_OPTION_ARG_CALLBACK
, callback_error
, NULL
, NULL
},
1151 { "error-no-arg", 0, G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
, callback_error
, NULL
, NULL
},
1152 { "error-optional-arg", 0, G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
, callback_error
, NULL
, NULL
},
1155 context
= g_option_context_new (NULL
);
1156 g_option_context_add_main_entries (context
, entries
, NULL
);
1158 /* Now try parsing */
1159 argv
= split_string ("program --error value", &argc
);
1160 argv_copy
= copy_stringv (argv
, argc
);
1162 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1163 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
);
1164 g_assert (retval
== FALSE
);
1165 check_identical_stringv (argv_copy
, argv
);
1167 g_option_context_free (context
);
1168 g_clear_error (&error
);
1169 g_strfreev (argv_copy
);
1172 /* And again, this time with a no-arg variant */
1173 context
= g_option_context_new (NULL
);
1174 g_option_context_add_main_entries (context
, entries
, NULL
);
1176 argv
= split_string ("program --error-no-arg", &argc
);
1177 argv_copy
= copy_stringv (argv
, argc
);
1179 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1180 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
);
1181 g_assert (retval
== FALSE
);
1182 check_identical_stringv (argv_copy
, argv
);
1184 g_option_context_free (context
);
1185 g_clear_error (&error
);
1186 g_strfreev (argv_copy
);
1189 /* And again, this time with a optional arg variant, with argument */
1190 context
= g_option_context_new (NULL
);
1191 g_option_context_add_main_entries (context
, entries
, NULL
);
1193 argv
= split_string ("program --error-optional-arg value", &argc
);
1194 argv_copy
= copy_stringv (argv
, argc
);
1196 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1197 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
);
1198 g_assert (retval
== FALSE
);
1199 check_identical_stringv (argv_copy
, argv
);
1201 g_option_context_free (context
);
1202 g_clear_error (&error
);
1203 g_strfreev (argv_copy
);
1206 /* And again, this time with a optional arg variant, without argument */
1207 context
= g_option_context_new (NULL
);
1208 g_option_context_add_main_entries (context
, entries
, NULL
);
1210 argv
= split_string ("program --error-optional-arg", &argc
);
1211 argv_copy
= copy_stringv (argv
, argc
);
1213 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1214 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_BAD_VALUE
);
1215 g_assert (retval
== FALSE
);
1216 check_identical_stringv (argv_copy
, argv
);
1218 g_option_context_free (context
);
1219 g_clear_error (&error
);
1220 g_strfreev (argv_copy
);
1228 GOptionContext
*context
;
1230 GError
*error
= NULL
;
1231 gchar
**argv
, **argv_copy
;
1234 GOptionEntry entries
[] =
1235 { { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1238 context
= g_option_context_new (NULL
);
1239 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1240 g_option_context_add_main_entries (context
, entries
, NULL
);
1242 /* Now try parsing */
1243 argv
= split_string ("program --test --hello", &argc
);
1244 argv_copy
= copy_stringv (argv
, argc
);
1246 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1247 g_assert_no_error (error
);
1251 arg
= join_stringv (argc
, argv
);
1252 g_assert (strcmp (arg
, "program --hello") == 0);
1255 g_strfreev (argv_copy
);
1257 g_option_context_free (context
);
1263 GOptionContext
*context
;
1265 GError
*error
= NULL
;
1270 GOptionEntry entries
[] =
1271 { { "test", 't', 0, G_OPTION_ARG_NONE
, &ignore_test2_boolean
, NULL
, NULL
},
1274 context
= g_option_context_new (NULL
);
1275 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1276 g_option_context_add_main_entries (context
, entries
, NULL
);
1278 /* Now try parsing */
1279 argv
= split_string ("program -test", &argc
);
1280 argv_copy
= copy_stringv (argv
, argc
);
1282 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1283 g_assert_no_error (error
);
1287 arg
= join_stringv (argc
, argv
);
1288 g_assert (strcmp (arg
, "program -es") == 0);
1291 g_strfreev (argv_copy
);
1293 g_option_context_free (context
);
1299 GOptionContext
*context
;
1301 GError
*error
= NULL
;
1302 gchar
**argv
, **argv_copy
;
1305 GOptionEntry entries
[] =
1306 { { "test", 0, 0, G_OPTION_ARG_STRING
, &ignore_test3_string
, NULL
, NULL
},
1309 context
= g_option_context_new (NULL
);
1310 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1311 g_option_context_add_main_entries (context
, entries
, NULL
);
1313 /* Now try parsing */
1314 argv
= split_string ("program --test foo --hello", &argc
);
1315 argv_copy
= copy_stringv (argv
, argc
);
1317 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1318 g_assert_no_error (error
);
1322 arg
= join_stringv (argc
, argv
);
1323 g_assert (strcmp (arg
, "program --hello") == 0);
1325 g_assert (strcmp (ignore_test3_string
, "foo") == 0);
1326 g_free (ignore_test3_string
);
1329 g_strfreev (argv_copy
);
1331 g_option_context_free (context
);
1335 static array_test1 (void)
1337 GOptionContext
*context
;
1339 GError
*error
= NULL
;
1343 GOptionEntry entries
[] =
1344 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY
, &array_test1_array
, NULL
, NULL
},
1347 context
= g_option_context_new (NULL
);
1348 g_option_context_add_main_entries (context
, entries
, NULL
);
1350 /* Now try parsing */
1351 argv
= split_string ("program --test foo --test bar", &argc
);
1352 argv_copy
= copy_stringv (argv
, argc
);
1354 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1355 g_assert_no_error (error
);
1359 g_assert (strcmp (array_test1_array
[0], "foo") == 0);
1360 g_assert (strcmp (array_test1_array
[1], "bar") == 0);
1361 g_assert (array_test1_array
[2] == NULL
);
1363 g_strfreev (array_test1_array
);
1365 g_strfreev (argv_copy
);
1367 g_option_context_free (context
);
1373 GOptionContext
*context
;
1375 GOptionEntry entries1
[] =
1376 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY
, NULL
, NULL
, NULL
},
1378 GOptionEntry entries2
[] =
1379 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY
, NULL
, NULL
, NULL
},
1382 context
= g_option_context_new (NULL
);
1383 g_option_context_add_main_entries (context
, entries1
, NULL
);
1384 g_option_context_add_main_entries (context
, entries2
, NULL
);
1386 g_option_context_free (context
);
1392 GOptionContext
*context
;
1394 context
= g_option_context_new (NULL
);
1395 g_option_context_parse (context
, NULL
, NULL
, NULL
);
1397 g_option_context_free (context
);
1403 GOptionContext
*context
;
1410 context
= g_option_context_new (NULL
);
1411 g_option_context_parse (context
, &argc
, &argv
, NULL
);
1413 g_option_context_free (context
);
1416 /* check that non-option arguments are left in argv by default */
1420 GOptionContext
*context
;
1422 GError
*error
= NULL
;
1426 GOptionEntry entries
[] = {
1427 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1431 context
= g_option_context_new (NULL
);
1432 g_option_context_add_main_entries (context
, entries
, NULL
);
1434 /* Now try parsing */
1435 argv
= split_string ("program foo --test bar", &argc
);
1436 argv_copy
= copy_stringv (argv
, argc
);
1438 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1439 g_assert_no_error (error
);
1443 g_assert (ignore_test1_boolean
);
1444 g_assert (strcmp (argv
[0], "program") == 0);
1445 g_assert (strcmp (argv
[1], "foo") == 0);
1446 g_assert (strcmp (argv
[2], "bar") == 0);
1447 g_assert (argv
[3] == NULL
);
1449 g_strfreev (argv_copy
);
1451 g_option_context_free (context
);
1454 /* check that -- works */
1458 GOptionContext
*context
;
1460 GError
*error
= NULL
;
1464 GOptionEntry entries
[] = {
1465 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1469 context
= g_option_context_new (NULL
);
1470 g_option_context_add_main_entries (context
, entries
, NULL
);
1472 /* Now try parsing */
1473 argv
= split_string ("program foo --test -- -bar", &argc
);
1474 argv_copy
= copy_stringv (argv
, argc
);
1476 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1477 g_assert_no_error (error
);
1481 g_assert (ignore_test1_boolean
);
1482 g_assert (strcmp (argv
[0], "program") == 0);
1483 g_assert (strcmp (argv
[1], "foo") == 0);
1484 g_assert (strcmp (argv
[2], "--") == 0);
1485 g_assert (strcmp (argv
[3], "-bar") == 0);
1486 g_assert (argv
[4] == NULL
);
1488 g_strfreev (argv_copy
);
1490 g_option_context_free (context
);
1493 /* check that -- stripping works */
1497 GOptionContext
*context
;
1499 GError
*error
= NULL
;
1503 GOptionEntry entries
[] = {
1504 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1508 context
= g_option_context_new (NULL
);
1509 g_option_context_add_main_entries (context
, entries
, NULL
);
1511 /* Now try parsing */
1512 argv
= split_string ("program foo --test -- bar", &argc
);
1513 argv_copy
= copy_stringv (argv
, argc
);
1515 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1516 g_assert_no_error (error
);
1520 g_assert (ignore_test1_boolean
);
1521 g_assert (strcmp (argv
[0], "program") == 0);
1522 g_assert (strcmp (argv
[1], "foo") == 0);
1523 g_assert (strcmp (argv
[2], "bar") == 0);
1524 g_assert (argv
[3] == NULL
);
1526 g_strfreev (argv_copy
);
1528 g_option_context_free (context
);
1534 GOptionContext
*context
;
1536 GError
*error
= NULL
;
1540 GOptionEntry entries
[] = {
1541 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1545 context
= g_option_context_new (NULL
);
1546 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1547 g_option_context_add_main_entries (context
, entries
, NULL
);
1549 /* Now try parsing */
1550 argv
= split_string ("program foo --test -bar --", &argc
);
1551 argv_copy
= copy_stringv (argv
, argc
);
1553 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1554 g_assert_no_error (error
);
1558 g_assert (ignore_test1_boolean
);
1559 g_assert (strcmp (argv
[0], "program") == 0);
1560 g_assert (strcmp (argv
[1], "foo") == 0);
1561 g_assert (strcmp (argv
[2], "-bar") == 0);
1562 g_assert (argv
[3] == NULL
);
1564 g_strfreev (argv_copy
);
1566 g_option_context_free (context
);
1572 GOptionContext
*context
;
1574 GError
*error
= NULL
;
1578 GOptionEntry entries
[] = {
1579 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1583 context
= g_option_context_new (NULL
);
1584 g_option_context_add_main_entries (context
, entries
, NULL
);
1586 /* Now try parsing */
1587 argv
= split_string ("program --test foo -- bar", &argc
);
1588 argv_copy
= copy_stringv (argv
, argc
);
1590 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1591 g_assert_no_error (error
);
1595 g_assert (ignore_test1_boolean
);
1596 g_assert (strcmp (argv
[0], "program") == 0);
1597 g_assert (strcmp (argv
[1], "foo") == 0);
1598 g_assert (strcmp (argv
[2], "bar") == 0);
1599 g_assert (argv
[3] == NULL
);
1601 g_strfreev (argv_copy
);
1603 g_option_context_free (context
);
1609 GOptionContext
*context
;
1611 GError
*error
= NULL
;
1615 GOptionEntry entries
[] = {
1616 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1620 context
= g_option_context_new (NULL
);
1621 g_option_context_add_main_entries (context
, entries
, NULL
);
1623 /* Now try parsing */
1624 argv
= split_string ("program --test -- -bar", &argc
);
1625 argv_copy
= copy_stringv (argv
, argc
);
1627 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1628 g_assert_no_error (error
);
1632 g_assert (ignore_test1_boolean
);
1633 g_assert (strcmp (argv
[0], "program") == 0);
1634 g_assert (strcmp (argv
[1], "--") == 0);
1635 g_assert (strcmp (argv
[2], "-bar") == 0);
1636 g_assert (argv
[3] == NULL
);
1638 g_strfreev (argv_copy
);
1640 g_option_context_free (context
);
1644 /* check that G_OPTION_REMAINING collects non-option arguments */
1648 GOptionContext
*context
;
1650 GError
*error
= NULL
;
1654 GOptionEntry entries
[] = {
1655 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1656 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &array_test1_array
, NULL
, NULL
},
1660 context
= g_option_context_new (NULL
);
1661 g_option_context_add_main_entries (context
, entries
, NULL
);
1663 /* Now try parsing */
1664 argv
= split_string ("program foo --test bar", &argc
);
1665 argv_copy
= copy_stringv (argv
, argc
);
1667 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1668 g_assert_no_error (error
);
1672 g_assert (ignore_test1_boolean
);
1673 g_assert (strcmp (array_test1_array
[0], "foo") == 0);
1674 g_assert (strcmp (array_test1_array
[1], "bar") == 0);
1675 g_assert (array_test1_array
[2] == NULL
);
1677 g_strfreev (array_test1_array
);
1679 g_strfreev (argv_copy
);
1681 g_option_context_free (context
);
1685 /* check that G_OPTION_REMAINING and -- work together */
1689 GOptionContext
*context
;
1691 GError
*error
= NULL
;
1695 GOptionEntry entries
[] = {
1696 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1697 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &array_test1_array
, NULL
, NULL
},
1701 context
= g_option_context_new (NULL
);
1702 g_option_context_add_main_entries (context
, entries
, NULL
);
1704 /* Now try parsing */
1705 argv
= split_string ("program foo --test -- -bar", &argc
);
1706 argv_copy
= copy_stringv (argv
, argc
);
1708 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1709 g_assert_no_error (error
);
1713 g_assert (ignore_test1_boolean
);
1714 g_assert (strcmp (array_test1_array
[0], "foo") == 0);
1715 g_assert (strcmp (array_test1_array
[1], "-bar") == 0);
1716 g_assert (array_test1_array
[2] == NULL
);
1718 g_strfreev (array_test1_array
);
1720 g_strfreev (argv_copy
);
1722 g_option_context_free (context
);
1725 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1729 GOptionContext
*context
;
1731 GError
*error
= NULL
;
1735 GOptionEntry entries
[] = {
1736 { "test", 0, 0, G_OPTION_ARG_NONE
, &ignore_test1_boolean
, NULL
, NULL
},
1737 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_FILENAME_ARRAY
, &array_test1_array
, NULL
, NULL
},
1741 context
= g_option_context_new (NULL
);
1742 g_option_context_add_main_entries (context
, entries
, NULL
);
1744 /* Now try parsing */
1745 argv
= split_string ("program foo --test bar", &argc
);
1746 argv_copy
= copy_stringv (argv
, argc
);
1748 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1749 g_assert_no_error (error
);
1753 g_assert (ignore_test1_boolean
);
1754 g_assert (strcmp (array_test1_array
[0], "foo") == 0);
1755 g_assert (strcmp (array_test1_array
[1], "bar") == 0);
1756 g_assert (array_test1_array
[2] == NULL
);
1758 g_strfreev (array_test1_array
);
1760 g_strfreev (argv_copy
);
1762 g_option_context_free (context
);
1766 unknown_short_test (void)
1768 GOptionContext
*context
;
1770 GError
*error
= NULL
;
1774 GOptionEntry entries
[] = { { NULL
} };
1776 g_test_bug ("166609");
1778 context
= g_option_context_new (NULL
);
1779 g_option_context_add_main_entries (context
, entries
, NULL
);
1781 /* Now try parsing */
1782 argv
= split_string ("program -0", &argc
);
1783 argv_copy
= copy_stringv (argv
, argc
);
1785 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1787 g_assert (error
!= NULL
);
1788 g_clear_error (&error
);
1790 g_strfreev (argv_copy
);
1792 g_option_context_free (context
);
1795 /* test that lone dashes are treated as non-options */
1797 lonely_dash_test (void)
1799 GOptionContext
*context
;
1801 GError
*error
= NULL
;
1806 g_test_bug ("168008");
1808 context
= g_option_context_new (NULL
);
1810 /* Now try parsing */
1811 argv
= split_string ("program -", &argc
);
1812 argv_copy
= copy_stringv (argv
, argc
);
1814 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1815 g_assert_no_error (error
);
1818 g_assert (argv
[1] && strcmp (argv
[1], "-") == 0);
1820 g_strfreev (argv_copy
);
1822 g_option_context_free (context
);
1826 missing_arg_test (void)
1828 GOptionContext
*context
;
1830 GError
*error
= NULL
;
1835 GOptionEntry entries
[] =
1836 { { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
1839 g_test_bug ("305576");
1841 context
= g_option_context_new (NULL
);
1842 g_option_context_add_main_entries (context
, entries
, NULL
);
1844 /* Now try parsing */
1845 argv
= split_string ("program --test", &argc
);
1846 argv_copy
= copy_stringv (argv
, argc
);
1848 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1849 g_assert (retval
== FALSE
);
1850 g_assert (error
!= NULL
);
1851 /* An error occurred, so argv has not been changed */
1852 check_identical_stringv (argv_copy
, argv
);
1853 g_clear_error (&error
);
1855 g_strfreev (argv_copy
);
1858 /* Try parsing again */
1859 argv
= split_string ("program -t", &argc
);
1860 argv_copy
= copy_stringv (argv
, argc
);
1862 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1863 g_assert (retval
== FALSE
);
1864 g_assert (error
!= NULL
);
1865 /* An error occurred, so argv has not been changed */
1866 check_identical_stringv (argv_copy
, argv
);
1867 g_clear_error (&error
);
1869 g_strfreev (argv_copy
);
1871 g_option_context_free (context
);
1874 static gchar
*test_arg
;
1876 static gboolean
cb (const gchar
*option_name
,
1881 test_arg
= g_strdup (value
);
1886 dash_arg_test (void)
1888 GOptionContext
*context
;
1890 GError
*error
= NULL
;
1894 gboolean argb
= FALSE
;
1895 GOptionEntry entries
[] =
1896 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
, cb
, NULL
, NULL
},
1897 { "three", '3', 0, G_OPTION_ARG_NONE
, &argb
, NULL
, NULL
},
1900 g_test_bug ("577638");
1902 context
= g_option_context_new (NULL
);
1903 g_option_context_add_main_entries (context
, entries
, NULL
);
1905 /* Now try parsing */
1906 argv
= split_string ("program --test=-3", &argc
);
1907 argv_copy
= copy_stringv (argv
, argc
);
1911 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1913 g_assert_no_error (error
);
1914 g_assert_cmpstr (test_arg
, ==, "-3");
1916 g_strfreev (argv_copy
);
1921 /* Try parsing again */
1922 argv
= split_string ("program --test -3", &argc
);
1923 argv_copy
= copy_stringv (argv
, argc
);
1926 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1927 g_assert_no_error (error
);
1929 g_assert_cmpstr (test_arg
, ==, NULL
);
1931 g_option_context_free (context
);
1932 g_strfreev (argv_copy
);
1939 GOptionContext
*context
;
1941 GOptionEntry entries
[] =
1942 { { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
1945 context
= g_option_context_new (NULL
);
1946 g_option_context_add_main_entries (context
, entries
, NULL
);
1948 g_assert (g_option_context_get_help_enabled (context
));
1949 g_assert (!g_option_context_get_ignore_unknown_options (context
));
1950 g_assert_cmpstr (g_option_context_get_summary (context
), ==, NULL
);
1951 g_assert_cmpstr (g_option_context_get_description (context
), ==, NULL
);
1953 g_option_context_set_help_enabled (context
, FALSE
);
1954 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1955 g_option_context_set_summary (context
, "summary");
1956 g_option_context_set_description(context
, "description");
1958 g_assert (!g_option_context_get_help_enabled (context
));
1959 g_assert (g_option_context_get_ignore_unknown_options (context
));
1960 g_assert_cmpstr (g_option_context_get_summary (context
), ==, "summary");
1961 g_assert_cmpstr (g_option_context_get_description (context
), ==, "description");
1963 g_option_context_free (context
);
1967 gboolean parameter_seen
;
1968 gboolean summary_seen
;
1969 gboolean description_seen
;
1973 static const gchar
*
1974 translate_func (const gchar
*str
,
1977 TranslateData
*d
= data
;
1979 if (strcmp (str
, "parameter") == 0)
1980 d
->parameter_seen
= TRUE
;
1981 else if (strcmp (str
, "summary") == 0)
1982 d
->summary_seen
= TRUE
;
1983 else if (strcmp (str
, "description") == 0)
1984 d
->description_seen
= TRUE
;
1990 destroy_notify (gpointer data
)
1992 TranslateData
*d
= data
;
1994 d
->destroyed
= TRUE
;
1998 test_translate (void)
2000 GOptionContext
*context
;
2002 GOptionEntry entries
[] =
2003 { { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
2005 TranslateData data
= { 0, };
2008 context
= g_option_context_new ("parameter");
2009 g_option_context_add_main_entries (context
, entries
, NULL
);
2010 g_option_context_set_summary (context
, "summary");
2011 g_option_context_set_description (context
, "description");
2013 g_option_context_set_translate_func (context
, translate_func
, &data
, destroy_notify
);
2015 str
= g_option_context_get_help (context
, FALSE
, NULL
);
2017 g_option_context_free (context
);
2019 g_assert (data
.parameter_seen
);
2020 g_assert (data
.summary_seen
);
2021 g_assert (data
.description_seen
);
2022 g_assert (data
.destroyed
);
2028 GOptionContext
*context
;
2029 GOptionGroup
*group
;
2032 gchar
**sarr
= NULL
;
2033 GOptionEntry entries
[] = {
2034 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, "Test tests", "Argument to use in test" },
2035 { "test2", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_NONE
, NULL
, "Tests also", NULL
},
2036 { "frob", 0, 0, G_OPTION_ARG_NONE
, NULL
, "Main frob", NULL
},
2037 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &sarr
, "Rest goes here", "REST" },
2040 GOptionEntry group_entries
[] = {
2041 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, "Group test", "Group test arg" },
2042 { "frob", 0, G_OPTION_FLAG_NOALIAS
, G_OPTION_ARG_NONE
, NULL
, "Group frob", NULL
},
2046 context
= g_option_context_new ("blabla");
2047 g_option_context_add_main_entries (context
, entries
, NULL
);
2048 g_option_context_set_summary (context
, "Summary");
2049 g_option_context_set_description (context
, "Description");
2051 group
= g_option_group_new ("group1", "Group1-description", "Group1-help", NULL
, NULL
);
2052 g_option_group_add_entries (group
, group_entries
);
2054 g_option_context_add_group (context
, group
);
2056 str
= g_option_context_get_help (context
, FALSE
, NULL
);
2057 g_assert (strstr (str
, "blabla") != NULL
);
2058 g_assert (strstr (str
, "Test tests") != NULL
);
2059 g_assert (strstr (str
, "Argument to use in test") != NULL
);
2060 g_assert (strstr (str
, "Tests also") == NULL
);
2061 g_assert (strstr (str
, "REST") != NULL
);
2062 g_assert (strstr (str
, "Summary") != NULL
);
2063 g_assert (strstr (str
, "Description") != NULL
);
2064 g_assert (strstr (str
, "--help") != NULL
);
2065 g_assert (strstr (str
, "--help-all") != NULL
);
2066 g_assert (strstr (str
, "--help-group1") != NULL
);
2067 g_assert (strstr (str
, "Group1-description") != NULL
);
2068 g_assert (strstr (str
, "Group1-help") != NULL
);
2069 g_assert (strstr (str
, "Group test arg") != NULL
);
2070 g_assert (strstr (str
, "Group frob") != NULL
);
2071 g_assert (strstr (str
, "Main frob") != NULL
);
2072 g_assert (strstr (str
, "--frob") != NULL
);
2073 g_assert (strstr (str
, "--group1-test") != NULL
);
2074 g_assert (strstr (str
, "--group1-frob") == NULL
);
2077 g_option_context_free (context
);
2081 test_help_no_options (void)
2083 GOptionContext
*context
;
2084 gchar
**sarr
= NULL
;
2085 GOptionEntry entries
[] = {
2086 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &sarr
, "Rest goes here", "REST" },
2091 context
= g_option_context_new ("blabla");
2092 g_option_context_add_main_entries (context
, entries
, NULL
);
2094 str
= g_option_context_get_help (context
, FALSE
, NULL
);
2095 g_assert (strstr (str
, "blabla") != NULL
);
2096 g_assert (strstr (str
, "REST") != NULL
);
2097 g_assert (strstr (str
, "Help Options") != NULL
);
2098 g_assert (strstr (str
, "Application Options") == NULL
);
2101 g_option_context_free (context
);
2105 test_help_no_help_options (void)
2107 GOptionContext
*context
;
2108 GOptionGroup
*group
;
2111 gchar
**sarr
= NULL
;
2112 GOptionEntry entries
[] = {
2113 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, "Test tests", "Argument to use in test" },
2114 { "test2", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_NONE
, NULL
, "Tests also", NULL
},
2115 { "frob", 0, 0, G_OPTION_ARG_NONE
, NULL
, "Main frob", NULL
},
2116 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &sarr
, "Rest goes here", "REST" },
2119 GOptionEntry group_entries
[] = {
2120 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, "Group test", "Group test arg" },
2121 { "frob", 0, G_OPTION_FLAG_NOALIAS
, G_OPTION_ARG_NONE
, NULL
, "Group frob", NULL
},
2125 g_test_bug ("697652");
2127 context
= g_option_context_new ("blabla");
2128 g_option_context_add_main_entries (context
, entries
, NULL
);
2129 g_option_context_set_summary (context
, "Summary");
2130 g_option_context_set_description (context
, "Description");
2131 g_option_context_set_help_enabled (context
, FALSE
);
2133 group
= g_option_group_new ("group1", "Group1-description", "Group1-help", NULL
, NULL
);
2134 g_option_group_add_entries (group
, group_entries
);
2136 g_option_context_add_group (context
, group
);
2138 str
= g_option_context_get_help (context
, FALSE
, NULL
);
2139 g_assert (strstr (str
, "blabla") != NULL
);
2140 g_assert (strstr (str
, "Test tests") != NULL
);
2141 g_assert (strstr (str
, "Argument to use in test") != NULL
);
2142 g_assert (strstr (str
, "Tests also") == NULL
);
2143 g_assert (strstr (str
, "REST") != NULL
);
2144 g_assert (strstr (str
, "Summary") != NULL
);
2145 g_assert (strstr (str
, "Description") != NULL
);
2146 g_assert (strstr (str
, "Help Options") == NULL
);
2147 g_assert (strstr (str
, "--help") == NULL
);
2148 g_assert (strstr (str
, "--help-all") == NULL
);
2149 g_assert (strstr (str
, "--help-group1") == NULL
);
2150 g_assert (strstr (str
, "Group1-description") != NULL
);
2151 g_assert (strstr (str
, "Group1-help") == NULL
);
2152 g_assert (strstr (str
, "Group test arg") != NULL
);
2153 g_assert (strstr (str
, "Group frob") != NULL
);
2154 g_assert (strstr (str
, "Main frob") != NULL
);
2155 g_assert (strstr (str
, "--frob") != NULL
);
2156 g_assert (strstr (str
, "--group1-test") != NULL
);
2157 g_assert (strstr (str
, "--group1-frob") == NULL
);
2160 g_option_context_free (context
);
2164 set_bool (gpointer data
)
2172 test_main_group (void)
2174 GOptionContext
*context
;
2175 GOptionGroup
*group
;
2178 context
= g_option_context_new (NULL
);
2179 g_assert (g_option_context_get_main_group (context
) == NULL
);
2180 group
= g_option_group_new ("name", "description", "hlep", &b
, set_bool
);
2181 g_option_context_add_group (context
, group
);
2182 group
= g_option_group_new ("name2", "description", "hlep", NULL
, NULL
);
2183 g_option_context_add_group (context
, group
);
2184 g_assert (g_option_context_get_main_group (context
) == NULL
);
2185 group
= g_option_group_new ("name", "description", "hlep", NULL
, NULL
);
2186 g_option_context_set_main_group (context
, group
);
2187 g_assert (g_option_context_get_main_group (context
) == group
);
2189 g_option_context_free (context
);
2194 static gboolean error_func_called
= FALSE
;
2197 error_func (GOptionContext
*context
,
2198 GOptionGroup
*group
,
2202 g_assert_cmpint (GPOINTER_TO_INT(data
), ==, 1234);
2203 error_func_called
= TRUE
;
2207 test_error_hook (void)
2209 GOptionContext
*context
;
2211 GOptionEntry entries
[] =
2212 { { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
2214 GOptionGroup
*group
;
2219 GError
*error
= NULL
;
2221 context
= g_option_context_new (NULL
);
2222 group
= g_option_group_new ("name", "description", "hlep", GINT_TO_POINTER(1234), NULL
);
2223 g_option_group_add_entries (group
, entries
);
2224 g_option_context_set_main_group (context
, group
);
2225 g_option_group_set_error_hook (g_option_context_get_main_group (context
),
2228 argv
= split_string ("program --test", &argc
);
2229 argv_copy
= copy_stringv (argv
, argc
);
2231 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
2232 g_assert (retval
== FALSE
);
2233 g_assert (error
!= NULL
);
2234 /* An error occurred, so argv has not been changed */
2235 check_identical_stringv (argv_copy
, argv
);
2236 g_clear_error (&error
);
2238 g_assert (error_func_called
);
2240 g_strfreev (argv_copy
);
2242 g_option_context_free (context
);
2246 test_group_parse (void)
2248 GOptionContext
*context
;
2249 GOptionGroup
*group
;
2255 GOptionEntry entries
[] = {
2256 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg1
, NULL
, NULL
},
2257 { "faz", 'f', 0, G_OPTION_ARG_STRING
, &arg2
, NULL
, NULL
},
2260 GOptionEntry group_entries
[] = {
2261 { "test", 0, 0, G_OPTION_ARG_STRING
, &arg3
, NULL
, NULL
},
2262 { "frob", 'f', 0, G_OPTION_ARG_STRING
, &arg4
, NULL
, NULL
},
2263 { "faz", 'z', 0, G_OPTION_ARG_STRING
, &arg5
, NULL
, NULL
},
2266 gchar
**argv
, **orig_argv
;
2268 GError
*error
= NULL
;
2271 context
= g_option_context_new (NULL
);
2272 g_option_context_add_main_entries (context
, entries
, NULL
);
2273 group
= g_option_group_new ("group", "A group", "help for group", NULL
, NULL
);
2274 g_option_group_add_entries (group
, group_entries
);
2275 g_option_context_add_group (context
, group
);
2277 argv
= split_string ("program --test arg1 -f arg2 --group-test arg3 --frob arg4 -z arg5", &argc
);
2278 orig_argv
= g_memdup (argv
, (argc
+ 1) * sizeof (char *));
2280 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
2282 g_assert_no_error (error
);
2284 g_assert_cmpstr (arg1
, ==, "arg1");
2285 g_assert_cmpstr (arg2
, ==, "arg2");
2286 g_assert_cmpstr (arg3
, ==, "arg3");
2287 g_assert_cmpstr (arg4
, ==, "arg4");
2288 g_assert_cmpstr (arg5
, ==, "arg5");
2297 g_strfreev (orig_argv
);
2298 g_option_context_free (context
);
2302 option_context_parse_command_line (GOptionContext
*context
,
2303 const gchar
*command_line
)
2306 guint argv_len
, argv_new_len
;
2309 argv
= split_string (command_line
, NULL
);
2310 argv_len
= g_strv_length (argv
);
2312 success
= g_option_context_parse_strv (context
, &argv
, NULL
);
2313 argv_new_len
= g_strv_length (argv
);
2316 return success
? argv_len
- argv_new_len
: -1;
2320 test_strict_posix (void)
2322 GOptionContext
*context
;
2325 GOptionEntry entries
[] = {
2326 { "foo", 'f', 0, G_OPTION_ARG_NONE
, &foo
, NULL
, NULL
},
2327 { "bar", 'b', 0, G_OPTION_ARG_NONE
, &bar
, NULL
, NULL
},
2332 context
= g_option_context_new (NULL
);
2333 g_option_context_add_main_entries (context
, entries
, NULL
);
2336 g_option_context_set_strict_posix (context
, FALSE
);
2337 n_parsed
= option_context_parse_command_line (context
, "program --foo command --bar");
2338 g_assert_cmpint (n_parsed
, ==, 2);
2339 g_assert (foo
== TRUE
);
2340 g_assert (bar
== TRUE
);
2343 g_option_context_set_strict_posix (context
, TRUE
);
2344 n_parsed
= option_context_parse_command_line (context
, "program --foo command --bar");
2345 g_assert_cmpint (n_parsed
, ==, 1);
2346 g_assert (foo
== TRUE
);
2347 g_assert (bar
== FALSE
);
2350 g_option_context_set_strict_posix (context
, TRUE
);
2351 n_parsed
= option_context_parse_command_line (context
, "program --foo --bar command");
2352 g_assert_cmpint (n_parsed
, ==, 2);
2353 g_assert (foo
== TRUE
);
2354 g_assert (bar
== TRUE
);
2357 g_option_context_set_strict_posix (context
, TRUE
);
2358 n_parsed
= option_context_parse_command_line (context
, "program command --foo --bar");
2359 g_assert_cmpint (n_parsed
, ==, 0);
2360 g_assert (foo
== FALSE
);
2361 g_assert (bar
== FALSE
);
2363 g_option_context_free (context
);
2367 flag_reverse_string (void)
2369 GOptionContext
*context
;
2371 GOptionEntry entries
[] =
2372 { { "test", 't', G_OPTION_FLAG_REVERSE
, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
2377 GError
*error
= NULL
;
2379 if (!g_test_undefined ())
2382 context
= g_option_context_new (NULL
);
2384 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
,
2385 "*ignoring reverse flag*");
2386 g_option_context_add_main_entries (context
, entries
, NULL
);
2387 g_test_assert_expected_messages ();
2389 argv
= split_string ("program --test bla", &argc
);
2391 retval
= g_option_context_parse_strv (context
, &argv
, &error
);
2392 g_assert (retval
== TRUE
);
2393 g_assert_no_error (error
);
2395 g_option_context_free (context
);
2400 flag_optional_int (void)
2402 GOptionContext
*context
;
2404 GOptionEntry entries
[] =
2405 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_INT
, &arg
, NULL
, NULL
},
2410 GError
*error
= NULL
;
2412 if (!g_test_undefined ())
2415 context
= g_option_context_new (NULL
);
2417 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
,
2418 "*ignoring no-arg, optional-arg or filename flags*");
2419 g_option_context_add_main_entries (context
, entries
, NULL
);
2420 g_test_assert_expected_messages ();
2422 argv
= split_string ("program --test 5", &argc
);
2424 retval
= g_option_context_parse_strv (context
, &argv
, &error
);
2425 g_assert (retval
== TRUE
);
2426 g_assert_no_error (error
);
2428 g_option_context_free (context
);
2432 short_remaining (void)
2434 gboolean ignore
= FALSE
;
2435 gboolean remaining
= FALSE
;
2438 gchar
** files
= NULL
;
2439 GError
* error
= NULL
;
2440 GOptionEntry entries
[] =
2442 { "ignore", 'i', 0, G_OPTION_ARG_NONE
, &ignore
, NULL
, NULL
},
2443 { "remaining", 'r', 0, G_OPTION_ARG_NONE
, &remaining
, NULL
, NULL
},
2444 { "number", 'n', 0, G_OPTION_ARG_INT
, &number
, NULL
, NULL
},
2445 { "text", 't', 0, G_OPTION_ARG_STRING
, &text
, NULL
, NULL
},
2446 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_FILENAME_ARRAY
, &files
, NULL
, NULL
},
2449 GOptionContext
* context
;
2450 gchar
**argv
, **argv_copy
;
2453 g_test_bug ("729563");
2455 argv
= split_string ("program -ri -n 4 -t hello file1 file2", &argc
);
2456 argv_copy
= copy_stringv (argv
, argc
);
2458 context
= g_option_context_new (NULL
);
2460 g_option_context_add_main_entries (context
, entries
, NULL
);
2461 g_option_context_set_ignore_unknown_options (context
, TRUE
);
2463 g_option_context_parse (context
, &argc
, &argv
, &error
);
2464 g_assert_no_error (error
);
2467 g_assert (remaining
);
2468 g_assert_cmpint (number
, ==, 4);
2469 g_assert_cmpstr (text
, ==, "hello");
2470 g_assert_cmpstr (files
[0], ==, "file1");
2471 g_assert_cmpstr (files
[1], ==, "file2");
2472 g_assert (files
[2] == NULL
);
2476 g_strfreev (argv_copy
);
2478 g_option_context_free (context
);
2485 GOptionEntry entries
[] =
2487 { "known", 0, 0, G_OPTION_ARG_STRING
, &text
, NULL
, NULL
},
2490 GOptionContext
* context
;
2493 GError
*error
= NULL
;
2495 g_test_bug ("646926");
2497 argv
= split_string ("program --known=foo --known=bar --unknown=baz", &argc
);
2499 context
= g_option_context_new (NULL
);
2501 g_option_context_add_main_entries (context
, entries
, NULL
);
2502 g_option_context_set_ignore_unknown_options (context
, FALSE
);
2503 g_option_context_parse (context
, &argc
, &argv
, &error
);
2505 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_UNKNOWN_OPTION
);
2506 g_assert_null (text
);
2508 g_option_context_free (context
);
2509 g_clear_error (&error
);
2520 g_setenv ("LC_ALL", "C", TRUE
);
2521 g_test_init (&argc
, &argv
, NULL
);
2523 g_test_bug_base ("http://bugzilla.gnome.org/");
2525 g_test_add_func ("/option/help/options", test_help
);
2526 g_test_add_func ("/option/help/no-options", test_help_no_options
);
2527 g_test_add_func ("/option/help/no-help-options", test_help_no_help_options
);
2529 g_test_add_func ("/option/basic", test_basic
);
2530 g_test_add_func ("/option/translate", test_translate
);
2532 g_test_add_func ("/option/group/captions", test_group_captions
);
2533 for (i
= 0; i
< 4; i
++)
2535 test_name
= g_strdup_printf ("/option/group/captions/subprocess/help-%d", i
);
2536 g_test_add_data_func (test_name
, GINT_TO_POINTER (i
),
2537 test_group_captions_help
);
2539 test_name
= g_strdup_printf ("/option/group/captions/subprocess/help-all-%d", i
);
2540 g_test_add_data_func (test_name
, GINT_TO_POINTER (i
),
2541 test_group_captions_help_all
);
2543 test_name
= g_strdup_printf ("/option/group/captions/subprocess/help-test-%d", i
);
2544 g_test_add_data_func (test_name
, GINT_TO_POINTER (i
),
2545 test_group_captions_help_test
);
2550 g_test_add_func ("/option/group/main", test_main_group
);
2551 g_test_add_func ("/option/group/error-hook", test_error_hook
);
2552 g_test_add_func ("/option/group/parse", test_group_parse
);
2553 g_test_add_func ("/option/strict-posix", test_strict_posix
);
2555 /* Test that restoration on failure works */
2556 g_test_add_func ("/option/restoration/int", error_test1
);
2557 g_test_add_func ("/option/restoration/string", error_test2
);
2558 g_test_add_func ("/option/restoration/boolean", error_test3
);
2560 /* Test that special argument parsing works */
2561 g_test_add_func ("/option/arg/repetition/int", arg_test1
);
2562 g_test_add_func ("/option/arg/repetition/string", arg_test2
);
2563 g_test_add_func ("/option/arg/repetition/filename", arg_test3
);
2564 g_test_add_func ("/option/arg/repetition/double", arg_test4
);
2565 g_test_add_func ("/option/arg/repetition/locale", arg_test5
);
2566 g_test_add_func ("/option/arg/repetition/int64", arg_test6
);
2568 /* Test string arrays */
2569 g_test_add_func ("/option/arg/array/string", array_test1
);
2571 /* Test callback args */
2572 g_test_add_func ("/option/arg/callback/string", callback_test1
);
2573 g_test_add_func ("/option/arg/callback/count", callback_test2
);
2575 /* Test optional arg flag for callback */
2576 g_test_add_func ("/option/arg/callback/optional1", callback_test_optional_1
);
2577 g_test_add_func ("/option/arg/callback/optional2", callback_test_optional_2
);
2578 g_test_add_func ("/option/arg/callback/optional3", callback_test_optional_3
);
2579 g_test_add_func ("/option/arg/callback/optional4", callback_test_optional_4
);
2580 g_test_add_func ("/option/arg/callback/optional5", callback_test_optional_5
);
2581 g_test_add_func ("/option/arg/callback/optional6", callback_test_optional_6
);
2582 g_test_add_func ("/option/arg/callback/optional7", callback_test_optional_7
);
2583 g_test_add_func ("/option/arg/callback/optional8", callback_test_optional_8
);
2585 /* Test callback with G_OPTION_REMAINING */
2586 g_test_add_func ("/option/arg/remaining/callback", callback_remaining_test1
);
2588 /* Test callbacks which return FALSE */
2589 g_test_add_func ("/option/arg/remaining/callback-false", callback_returns_false
);
2591 /* Test ignoring options */
2592 g_test_add_func ("/option/arg/ignore/long", ignore_test1
);
2593 g_test_add_func ("/option/arg/ignore/short", ignore_test2
);
2594 g_test_add_func ("/option/arg/ignore/arg", ignore_test3
);
2595 g_test_add_func ("/option/context/add", add_test1
);
2597 /* Test parsing empty args */
2598 /* Note there used to be an empty1 here, but it effectively moved
2599 * to option-argv0.c.
2601 g_test_add_func ("/option/context/empty2", empty_test2
);
2602 g_test_add_func ("/option/context/empty3", empty_test3
);
2604 /* Test handling of rest args */
2605 g_test_add_func ("/option/arg/rest/non-option", rest_test1
);
2606 g_test_add_func ("/option/arg/rest/separator1", rest_test2
);
2607 g_test_add_func ("/option/arg/rest/separator2", rest_test2a
);
2608 g_test_add_func ("/option/arg/rest/separator3", rest_test2b
);
2609 g_test_add_func ("/option/arg/rest/separator4", rest_test2c
);
2610 g_test_add_func ("/option/arg/rest/separator5", rest_test2d
);
2611 g_test_add_func ("/option/arg/remaining/non-option", rest_test3
);
2612 g_test_add_func ("/option/arg/remaining/separator", rest_test4
);
2613 g_test_add_func ("/option/arg/remaining/array", rest_test5
);
2615 /* Test some invalid flag combinations */
2616 g_test_add_func ("/option/arg/reverse-string", flag_reverse_string
);
2617 g_test_add_func ("/option/arg/optional-int", flag_optional_int
);
2619 /* regression tests for individual bugs */
2620 g_test_add_func ("/option/bug/unknown-short", unknown_short_test
);
2621 g_test_add_func ("/option/bug/lonely-dash", lonely_dash_test
);
2622 g_test_add_func ("/option/bug/missing-arg", missing_arg_test
);
2623 g_test_add_func ("/option/bug/dash-arg", dash_arg_test
);
2624 g_test_add_func ("/option/bug/short-remaining", short_remaining
);
2625 g_test_add_func ("/option/bug/double-free", double_free
);
2627 return g_test_run();