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
);
1825 /* test that three dashes are treated as non-options */
1827 triple_dash_test (void)
1829 GOptionContext
*context
;
1830 GOptionGroup
*group
;
1832 GError
*error
= NULL
;
1837 GOptionEntry entries
[] =
1838 { { "foo", 0, 0, G_OPTION_ARG_INT
, &arg1
, NULL
, NULL
},
1841 GOptionEntry group_entries
[] =
1842 { { "test", 0, 0, G_OPTION_ARG_INT
, &arg2
, NULL
, NULL
},
1846 context
= g_option_context_new (NULL
);
1847 g_option_context_add_main_entries (context
, entries
, NULL
);
1849 group
= g_option_group_new ("group", "Group description", "Group help", NULL
, NULL
);
1850 g_option_group_add_entries (group
, group_entries
);
1852 g_option_context_add_group (context
, group
);
1854 /* Now try parsing */
1855 argv
= split_string ("program ---test 42", &argc
);
1856 argv_copy
= copy_stringv (argv
, argc
);
1858 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1859 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_UNKNOWN_OPTION
);
1860 g_assert (retval
== FALSE
);
1862 g_option_context_free (context
);
1863 g_clear_error (&error
);
1864 g_strfreev (argv_copy
);
1869 missing_arg_test (void)
1871 GOptionContext
*context
;
1873 GError
*error
= NULL
;
1878 GOptionEntry entries
[] =
1879 { { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
1882 g_test_bug ("305576");
1884 context
= g_option_context_new (NULL
);
1885 g_option_context_add_main_entries (context
, entries
, NULL
);
1887 /* Now try parsing */
1888 argv
= split_string ("program --test", &argc
);
1889 argv_copy
= copy_stringv (argv
, argc
);
1891 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1892 g_assert (retval
== FALSE
);
1893 g_assert (error
!= NULL
);
1894 /* An error occurred, so argv has not been changed */
1895 check_identical_stringv (argv_copy
, argv
);
1896 g_clear_error (&error
);
1898 g_strfreev (argv_copy
);
1901 /* Try parsing again */
1902 argv
= split_string ("program -t", &argc
);
1903 argv_copy
= copy_stringv (argv
, argc
);
1905 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1906 g_assert (retval
== FALSE
);
1907 g_assert (error
!= NULL
);
1908 /* An error occurred, so argv has not been changed */
1909 check_identical_stringv (argv_copy
, argv
);
1910 g_clear_error (&error
);
1912 g_strfreev (argv_copy
);
1914 g_option_context_free (context
);
1917 static gchar
*test_arg
;
1919 static gboolean
cb (const gchar
*option_name
,
1924 test_arg
= g_strdup (value
);
1929 dash_arg_test (void)
1931 GOptionContext
*context
;
1933 GError
*error
= NULL
;
1937 gboolean argb
= FALSE
;
1938 GOptionEntry entries
[] =
1939 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_CALLBACK
, cb
, NULL
, NULL
},
1940 { "three", '3', 0, G_OPTION_ARG_NONE
, &argb
, NULL
, NULL
},
1943 g_test_bug ("577638");
1945 context
= g_option_context_new (NULL
);
1946 g_option_context_add_main_entries (context
, entries
, NULL
);
1948 /* Now try parsing */
1949 argv
= split_string ("program --test=-3", &argc
);
1950 argv_copy
= copy_stringv (argv
, argc
);
1954 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1956 g_assert_no_error (error
);
1957 g_assert_cmpstr (test_arg
, ==, "-3");
1959 g_strfreev (argv_copy
);
1964 /* Try parsing again */
1965 argv
= split_string ("program --test -3", &argc
);
1966 argv_copy
= copy_stringv (argv
, argc
);
1969 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
1970 g_assert_no_error (error
);
1972 g_assert_cmpstr (test_arg
, ==, NULL
);
1974 g_option_context_free (context
);
1975 g_strfreev (argv_copy
);
1982 GOptionContext
*context
;
1984 GOptionEntry entries
[] =
1985 { { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
1988 context
= g_option_context_new (NULL
);
1989 g_option_context_add_main_entries (context
, entries
, NULL
);
1991 g_assert (g_option_context_get_help_enabled (context
));
1992 g_assert (!g_option_context_get_ignore_unknown_options (context
));
1993 g_assert_cmpstr (g_option_context_get_summary (context
), ==, NULL
);
1994 g_assert_cmpstr (g_option_context_get_description (context
), ==, NULL
);
1996 g_option_context_set_help_enabled (context
, FALSE
);
1997 g_option_context_set_ignore_unknown_options (context
, TRUE
);
1998 g_option_context_set_summary (context
, "summary");
1999 g_option_context_set_description(context
, "description");
2001 g_assert (!g_option_context_get_help_enabled (context
));
2002 g_assert (g_option_context_get_ignore_unknown_options (context
));
2003 g_assert_cmpstr (g_option_context_get_summary (context
), ==, "summary");
2004 g_assert_cmpstr (g_option_context_get_description (context
), ==, "description");
2006 g_option_context_free (context
);
2010 gboolean parameter_seen
;
2011 gboolean summary_seen
;
2012 gboolean description_seen
;
2016 static const gchar
*
2017 translate_func (const gchar
*str
,
2020 TranslateData
*d
= data
;
2022 if (strcmp (str
, "parameter") == 0)
2023 d
->parameter_seen
= TRUE
;
2024 else if (strcmp (str
, "summary") == 0)
2025 d
->summary_seen
= TRUE
;
2026 else if (strcmp (str
, "description") == 0)
2027 d
->description_seen
= TRUE
;
2033 destroy_notify (gpointer data
)
2035 TranslateData
*d
= data
;
2037 d
->destroyed
= TRUE
;
2041 test_translate (void)
2043 GOptionContext
*context
;
2045 GOptionEntry entries
[] =
2046 { { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
2048 TranslateData data
= { 0, };
2051 context
= g_option_context_new ("parameter");
2052 g_option_context_add_main_entries (context
, entries
, NULL
);
2053 g_option_context_set_summary (context
, "summary");
2054 g_option_context_set_description (context
, "description");
2056 g_option_context_set_translate_func (context
, translate_func
, &data
, destroy_notify
);
2058 str
= g_option_context_get_help (context
, FALSE
, NULL
);
2060 g_option_context_free (context
);
2062 g_assert (data
.parameter_seen
);
2063 g_assert (data
.summary_seen
);
2064 g_assert (data
.description_seen
);
2065 g_assert (data
.destroyed
);
2071 GOptionContext
*context
;
2072 GOptionGroup
*group
;
2075 gchar
**sarr
= NULL
;
2076 GOptionEntry entries
[] = {
2077 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, "Test tests", "Argument to use in test" },
2078 { "test2", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_NONE
, NULL
, "Tests also", NULL
},
2079 { "frob", 0, 0, G_OPTION_ARG_NONE
, NULL
, "Main frob", NULL
},
2080 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &sarr
, "Rest goes here", "REST" },
2083 GOptionEntry group_entries
[] = {
2084 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, "Group test", "Group test arg" },
2085 { "frob", 0, G_OPTION_FLAG_NOALIAS
, G_OPTION_ARG_NONE
, NULL
, "Group frob", NULL
},
2089 context
= g_option_context_new ("blabla");
2090 g_option_context_add_main_entries (context
, entries
, NULL
);
2091 g_option_context_set_summary (context
, "Summary");
2092 g_option_context_set_description (context
, "Description");
2094 group
= g_option_group_new ("group1", "Group1-description", "Group1-help", NULL
, NULL
);
2095 g_option_group_add_entries (group
, group_entries
);
2097 g_option_context_add_group (context
, group
);
2099 str
= g_option_context_get_help (context
, FALSE
, NULL
);
2100 g_assert (strstr (str
, "blabla") != NULL
);
2101 g_assert (strstr (str
, "Test tests") != NULL
);
2102 g_assert (strstr (str
, "Argument to use in test") != NULL
);
2103 g_assert (strstr (str
, "Tests also") == NULL
);
2104 g_assert (strstr (str
, "REST") != NULL
);
2105 g_assert (strstr (str
, "Summary") != NULL
);
2106 g_assert (strstr (str
, "Description") != NULL
);
2107 g_assert (strstr (str
, "--help") != NULL
);
2108 g_assert (strstr (str
, "--help-all") != NULL
);
2109 g_assert (strstr (str
, "--help-group1") != NULL
);
2110 g_assert (strstr (str
, "Group1-description") != NULL
);
2111 g_assert (strstr (str
, "Group1-help") != NULL
);
2112 g_assert (strstr (str
, "Group test arg") != NULL
);
2113 g_assert (strstr (str
, "Group frob") != NULL
);
2114 g_assert (strstr (str
, "Main frob") != NULL
);
2115 g_assert (strstr (str
, "--frob") != NULL
);
2116 g_assert (strstr (str
, "--group1-test") != NULL
);
2117 g_assert (strstr (str
, "--group1-frob") == NULL
);
2120 g_option_context_free (context
);
2124 test_help_no_options (void)
2126 GOptionContext
*context
;
2127 gchar
**sarr
= NULL
;
2128 GOptionEntry entries
[] = {
2129 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &sarr
, "Rest goes here", "REST" },
2134 context
= g_option_context_new ("blabla");
2135 g_option_context_add_main_entries (context
, entries
, NULL
);
2137 str
= g_option_context_get_help (context
, FALSE
, NULL
);
2138 g_assert (strstr (str
, "blabla") != NULL
);
2139 g_assert (strstr (str
, "REST") != NULL
);
2140 g_assert (strstr (str
, "Help Options") != NULL
);
2141 g_assert (strstr (str
, "Application Options") == NULL
);
2144 g_option_context_free (context
);
2148 test_help_no_help_options (void)
2150 GOptionContext
*context
;
2151 GOptionGroup
*group
;
2154 gchar
**sarr
= NULL
;
2155 GOptionEntry entries
[] = {
2156 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, "Test tests", "Argument to use in test" },
2157 { "test2", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_NONE
, NULL
, "Tests also", NULL
},
2158 { "frob", 0, 0, G_OPTION_ARG_NONE
, NULL
, "Main frob", NULL
},
2159 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &sarr
, "Rest goes here", "REST" },
2162 GOptionEntry group_entries
[] = {
2163 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, "Group test", "Group test arg" },
2164 { "frob", 0, G_OPTION_FLAG_NOALIAS
, G_OPTION_ARG_NONE
, NULL
, "Group frob", NULL
},
2168 g_test_bug ("697652");
2170 context
= g_option_context_new ("blabla");
2171 g_option_context_add_main_entries (context
, entries
, NULL
);
2172 g_option_context_set_summary (context
, "Summary");
2173 g_option_context_set_description (context
, "Description");
2174 g_option_context_set_help_enabled (context
, FALSE
);
2176 group
= g_option_group_new ("group1", "Group1-description", "Group1-help", NULL
, NULL
);
2177 g_option_group_add_entries (group
, group_entries
);
2179 g_option_context_add_group (context
, group
);
2181 str
= g_option_context_get_help (context
, FALSE
, NULL
);
2182 g_assert (strstr (str
, "blabla") != NULL
);
2183 g_assert (strstr (str
, "Test tests") != NULL
);
2184 g_assert (strstr (str
, "Argument to use in test") != NULL
);
2185 g_assert (strstr (str
, "Tests also") == NULL
);
2186 g_assert (strstr (str
, "REST") != NULL
);
2187 g_assert (strstr (str
, "Summary") != NULL
);
2188 g_assert (strstr (str
, "Description") != NULL
);
2189 g_assert (strstr (str
, "Help Options") == NULL
);
2190 g_assert (strstr (str
, "--help") == NULL
);
2191 g_assert (strstr (str
, "--help-all") == NULL
);
2192 g_assert (strstr (str
, "--help-group1") == NULL
);
2193 g_assert (strstr (str
, "Group1-description") != NULL
);
2194 g_assert (strstr (str
, "Group1-help") == NULL
);
2195 g_assert (strstr (str
, "Group test arg") != NULL
);
2196 g_assert (strstr (str
, "Group frob") != NULL
);
2197 g_assert (strstr (str
, "Main frob") != NULL
);
2198 g_assert (strstr (str
, "--frob") != NULL
);
2199 g_assert (strstr (str
, "--group1-test") != NULL
);
2200 g_assert (strstr (str
, "--group1-frob") == NULL
);
2203 g_option_context_free (context
);
2207 set_bool (gpointer data
)
2215 test_main_group (void)
2217 GOptionContext
*context
;
2218 GOptionGroup
*group
;
2221 context
= g_option_context_new (NULL
);
2222 g_assert (g_option_context_get_main_group (context
) == NULL
);
2223 group
= g_option_group_new ("name", "description", "hlep", &b
, set_bool
);
2224 g_option_context_add_group (context
, group
);
2225 group
= g_option_group_new ("name2", "description", "hlep", NULL
, NULL
);
2226 g_option_context_add_group (context
, group
);
2227 g_assert (g_option_context_get_main_group (context
) == NULL
);
2228 group
= g_option_group_new ("name", "description", "hlep", NULL
, NULL
);
2229 g_option_context_set_main_group (context
, group
);
2230 g_assert (g_option_context_get_main_group (context
) == group
);
2232 g_option_context_free (context
);
2237 static gboolean error_func_called
= FALSE
;
2240 error_func (GOptionContext
*context
,
2241 GOptionGroup
*group
,
2245 g_assert_cmpint (GPOINTER_TO_INT(data
), ==, 1234);
2246 error_func_called
= TRUE
;
2250 test_error_hook (void)
2252 GOptionContext
*context
;
2254 GOptionEntry entries
[] =
2255 { { "test", 't', 0, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
2257 GOptionGroup
*group
;
2262 GError
*error
= NULL
;
2264 context
= g_option_context_new (NULL
);
2265 group
= g_option_group_new ("name", "description", "hlep", GINT_TO_POINTER(1234), NULL
);
2266 g_option_group_add_entries (group
, entries
);
2267 g_option_context_set_main_group (context
, group
);
2268 g_option_group_set_error_hook (g_option_context_get_main_group (context
),
2271 argv
= split_string ("program --test", &argc
);
2272 argv_copy
= copy_stringv (argv
, argc
);
2274 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
2275 g_assert (retval
== FALSE
);
2276 g_assert (error
!= NULL
);
2277 /* An error occurred, so argv has not been changed */
2278 check_identical_stringv (argv_copy
, argv
);
2279 g_clear_error (&error
);
2281 g_assert (error_func_called
);
2283 g_strfreev (argv_copy
);
2285 g_option_context_free (context
);
2289 test_group_parse (void)
2291 GOptionContext
*context
;
2292 GOptionGroup
*group
;
2298 GOptionEntry entries
[] = {
2299 { "test", 't', 0, G_OPTION_ARG_STRING
, &arg1
, NULL
, NULL
},
2300 { "faz", 'f', 0, G_OPTION_ARG_STRING
, &arg2
, NULL
, NULL
},
2303 GOptionEntry group_entries
[] = {
2304 { "test", 0, 0, G_OPTION_ARG_STRING
, &arg3
, NULL
, NULL
},
2305 { "frob", 'f', 0, G_OPTION_ARG_STRING
, &arg4
, NULL
, NULL
},
2306 { "faz", 'z', 0, G_OPTION_ARG_STRING
, &arg5
, NULL
, NULL
},
2309 gchar
**argv
, **orig_argv
;
2311 GError
*error
= NULL
;
2314 context
= g_option_context_new (NULL
);
2315 g_option_context_add_main_entries (context
, entries
, NULL
);
2316 group
= g_option_group_new ("group", "A group", "help for group", NULL
, NULL
);
2317 g_option_group_add_entries (group
, group_entries
);
2318 g_option_context_add_group (context
, group
);
2320 argv
= split_string ("program --test arg1 -f arg2 --group-test arg3 --frob arg4 -z arg5", &argc
);
2321 orig_argv
= g_memdup (argv
, (argc
+ 1) * sizeof (char *));
2323 retval
= g_option_context_parse (context
, &argc
, &argv
, &error
);
2325 g_assert_no_error (error
);
2327 g_assert_cmpstr (arg1
, ==, "arg1");
2328 g_assert_cmpstr (arg2
, ==, "arg2");
2329 g_assert_cmpstr (arg3
, ==, "arg3");
2330 g_assert_cmpstr (arg4
, ==, "arg4");
2331 g_assert_cmpstr (arg5
, ==, "arg5");
2340 g_strfreev (orig_argv
);
2341 g_option_context_free (context
);
2345 option_context_parse_command_line (GOptionContext
*context
,
2346 const gchar
*command_line
)
2349 guint argv_len
, argv_new_len
;
2352 argv
= split_string (command_line
, NULL
);
2353 argv_len
= g_strv_length (argv
);
2355 success
= g_option_context_parse_strv (context
, &argv
, NULL
);
2356 argv_new_len
= g_strv_length (argv
);
2359 return success
? argv_len
- argv_new_len
: -1;
2363 test_strict_posix (void)
2365 GOptionContext
*context
;
2368 GOptionEntry entries
[] = {
2369 { "foo", 'f', 0, G_OPTION_ARG_NONE
, &foo
, NULL
, NULL
},
2370 { "bar", 'b', 0, G_OPTION_ARG_NONE
, &bar
, NULL
, NULL
},
2375 context
= g_option_context_new (NULL
);
2376 g_option_context_add_main_entries (context
, entries
, NULL
);
2379 g_option_context_set_strict_posix (context
, FALSE
);
2380 n_parsed
= option_context_parse_command_line (context
, "program --foo command --bar");
2381 g_assert_cmpint (n_parsed
, ==, 2);
2382 g_assert (foo
== TRUE
);
2383 g_assert (bar
== TRUE
);
2386 g_option_context_set_strict_posix (context
, TRUE
);
2387 n_parsed
= option_context_parse_command_line (context
, "program --foo command --bar");
2388 g_assert_cmpint (n_parsed
, ==, 1);
2389 g_assert (foo
== TRUE
);
2390 g_assert (bar
== FALSE
);
2393 g_option_context_set_strict_posix (context
, TRUE
);
2394 n_parsed
= option_context_parse_command_line (context
, "program --foo --bar command");
2395 g_assert_cmpint (n_parsed
, ==, 2);
2396 g_assert (foo
== TRUE
);
2397 g_assert (bar
== TRUE
);
2400 g_option_context_set_strict_posix (context
, TRUE
);
2401 n_parsed
= option_context_parse_command_line (context
, "program command --foo --bar");
2402 g_assert_cmpint (n_parsed
, ==, 0);
2403 g_assert (foo
== FALSE
);
2404 g_assert (bar
== FALSE
);
2406 g_option_context_free (context
);
2410 flag_reverse_string (void)
2412 GOptionContext
*context
;
2414 GOptionEntry entries
[] =
2415 { { "test", 't', G_OPTION_FLAG_REVERSE
, G_OPTION_ARG_STRING
, &arg
, NULL
, NULL
},
2420 GError
*error
= NULL
;
2422 if (!g_test_undefined ())
2425 context
= g_option_context_new (NULL
);
2427 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
,
2428 "*ignoring reverse flag*");
2429 g_option_context_add_main_entries (context
, entries
, NULL
);
2430 g_test_assert_expected_messages ();
2432 argv
= split_string ("program --test bla", &argc
);
2434 retval
= g_option_context_parse_strv (context
, &argv
, &error
);
2435 g_assert (retval
== TRUE
);
2436 g_assert_no_error (error
);
2438 g_option_context_free (context
);
2443 flag_optional_int (void)
2445 GOptionContext
*context
;
2447 GOptionEntry entries
[] =
2448 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG
, G_OPTION_ARG_INT
, &arg
, NULL
, NULL
},
2453 GError
*error
= NULL
;
2455 if (!g_test_undefined ())
2458 context
= g_option_context_new (NULL
);
2460 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
,
2461 "*ignoring no-arg, optional-arg or filename flags*");
2462 g_option_context_add_main_entries (context
, entries
, NULL
);
2463 g_test_assert_expected_messages ();
2465 argv
= split_string ("program --test 5", &argc
);
2467 retval
= g_option_context_parse_strv (context
, &argv
, &error
);
2468 g_assert (retval
== TRUE
);
2469 g_assert_no_error (error
);
2471 g_option_context_free (context
);
2475 short_remaining (void)
2477 gboolean ignore
= FALSE
;
2478 gboolean remaining
= FALSE
;
2481 gchar
** files
= NULL
;
2482 GError
* error
= NULL
;
2483 GOptionEntry entries
[] =
2485 { "ignore", 'i', 0, G_OPTION_ARG_NONE
, &ignore
, NULL
, NULL
},
2486 { "remaining", 'r', 0, G_OPTION_ARG_NONE
, &remaining
, NULL
, NULL
},
2487 { "number", 'n', 0, G_OPTION_ARG_INT
, &number
, NULL
, NULL
},
2488 { "text", 't', 0, G_OPTION_ARG_STRING
, &text
, NULL
, NULL
},
2489 { G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_FILENAME_ARRAY
, &files
, NULL
, NULL
},
2492 GOptionContext
* context
;
2493 gchar
**argv
, **argv_copy
;
2496 g_test_bug ("729563");
2498 argv
= split_string ("program -ri -n 4 -t hello file1 file2", &argc
);
2499 argv_copy
= copy_stringv (argv
, argc
);
2501 context
= g_option_context_new (NULL
);
2503 g_option_context_add_main_entries (context
, entries
, NULL
);
2504 g_option_context_set_ignore_unknown_options (context
, TRUE
);
2506 g_option_context_parse (context
, &argc
, &argv
, &error
);
2507 g_assert_no_error (error
);
2510 g_assert (remaining
);
2511 g_assert_cmpint (number
, ==, 4);
2512 g_assert_cmpstr (text
, ==, "hello");
2513 g_assert_cmpstr (files
[0], ==, "file1");
2514 g_assert_cmpstr (files
[1], ==, "file2");
2515 g_assert (files
[2] == NULL
);
2519 g_strfreev (argv_copy
);
2521 g_option_context_free (context
);
2528 GOptionEntry entries
[] =
2530 { "known", 0, 0, G_OPTION_ARG_STRING
, &text
, NULL
, NULL
},
2533 GOptionContext
* context
;
2536 GError
*error
= NULL
;
2538 g_test_bug ("646926");
2540 argv
= split_string ("program --known=foo --known=bar --unknown=baz", &argc
);
2542 context
= g_option_context_new (NULL
);
2544 g_option_context_add_main_entries (context
, entries
, NULL
);
2545 g_option_context_set_ignore_unknown_options (context
, FALSE
);
2546 g_option_context_parse (context
, &argc
, &argv
, &error
);
2548 g_assert_error (error
, G_OPTION_ERROR
, G_OPTION_ERROR_UNKNOWN_OPTION
);
2549 g_assert_null (text
);
2551 g_option_context_free (context
);
2552 g_clear_error (&error
);
2564 g_setenv ("LC_ALL", "C", TRUE
);
2565 g_test_init (&argc
, &argv
, NULL
);
2567 g_test_bug_base ("http://bugzilla.gnome.org/");
2569 g_test_add_func ("/option/help/options", test_help
);
2570 g_test_add_func ("/option/help/no-options", test_help_no_options
);
2571 g_test_add_func ("/option/help/no-help-options", test_help_no_help_options
);
2573 g_test_add_func ("/option/basic", test_basic
);
2574 g_test_add_func ("/option/translate", test_translate
);
2576 g_test_add_func ("/option/group/captions", test_group_captions
);
2577 for (i
= 0; i
< 4; i
++)
2579 test_name
= g_strdup_printf ("/option/group/captions/subprocess/help-%d", i
);
2580 g_test_add_data_func (test_name
, GINT_TO_POINTER (i
),
2581 test_group_captions_help
);
2583 test_name
= g_strdup_printf ("/option/group/captions/subprocess/help-all-%d", i
);
2584 g_test_add_data_func (test_name
, GINT_TO_POINTER (i
),
2585 test_group_captions_help_all
);
2587 test_name
= g_strdup_printf ("/option/group/captions/subprocess/help-test-%d", i
);
2588 g_test_add_data_func (test_name
, GINT_TO_POINTER (i
),
2589 test_group_captions_help_test
);
2594 g_test_add_func ("/option/group/main", test_main_group
);
2595 g_test_add_func ("/option/group/error-hook", test_error_hook
);
2596 g_test_add_func ("/option/group/parse", test_group_parse
);
2597 g_test_add_func ("/option/strict-posix", test_strict_posix
);
2599 /* Test that restoration on failure works */
2600 g_test_add_func ("/option/restoration/int", error_test1
);
2601 g_test_add_func ("/option/restoration/string", error_test2
);
2602 g_test_add_func ("/option/restoration/boolean", error_test3
);
2604 /* Test that special argument parsing works */
2605 g_test_add_func ("/option/arg/repetition/int", arg_test1
);
2606 g_test_add_func ("/option/arg/repetition/string", arg_test2
);
2607 g_test_add_func ("/option/arg/repetition/filename", arg_test3
);
2608 g_test_add_func ("/option/arg/repetition/double", arg_test4
);
2609 g_test_add_func ("/option/arg/repetition/locale", arg_test5
);
2610 g_test_add_func ("/option/arg/repetition/int64", arg_test6
);
2612 /* Test string arrays */
2613 g_test_add_func ("/option/arg/array/string", array_test1
);
2615 /* Test callback args */
2616 g_test_add_func ("/option/arg/callback/string", callback_test1
);
2617 g_test_add_func ("/option/arg/callback/count", callback_test2
);
2619 /* Test optional arg flag for callback */
2620 g_test_add_func ("/option/arg/callback/optional1", callback_test_optional_1
);
2621 g_test_add_func ("/option/arg/callback/optional2", callback_test_optional_2
);
2622 g_test_add_func ("/option/arg/callback/optional3", callback_test_optional_3
);
2623 g_test_add_func ("/option/arg/callback/optional4", callback_test_optional_4
);
2624 g_test_add_func ("/option/arg/callback/optional5", callback_test_optional_5
);
2625 g_test_add_func ("/option/arg/callback/optional6", callback_test_optional_6
);
2626 g_test_add_func ("/option/arg/callback/optional7", callback_test_optional_7
);
2627 g_test_add_func ("/option/arg/callback/optional8", callback_test_optional_8
);
2629 /* Test callback with G_OPTION_REMAINING */
2630 g_test_add_func ("/option/arg/remaining/callback", callback_remaining_test1
);
2632 /* Test callbacks which return FALSE */
2633 g_test_add_func ("/option/arg/remaining/callback-false", callback_returns_false
);
2635 /* Test ignoring options */
2636 g_test_add_func ("/option/arg/ignore/long", ignore_test1
);
2637 g_test_add_func ("/option/arg/ignore/short", ignore_test2
);
2638 g_test_add_func ("/option/arg/ignore/arg", ignore_test3
);
2639 g_test_add_func ("/option/context/add", add_test1
);
2641 /* Test parsing empty args */
2642 /* Note there used to be an empty1 here, but it effectively moved
2643 * to option-argv0.c.
2645 g_test_add_func ("/option/context/empty2", empty_test2
);
2646 g_test_add_func ("/option/context/empty3", empty_test3
);
2648 /* Test handling of rest args */
2649 g_test_add_func ("/option/arg/rest/non-option", rest_test1
);
2650 g_test_add_func ("/option/arg/rest/separator1", rest_test2
);
2651 g_test_add_func ("/option/arg/rest/separator2", rest_test2a
);
2652 g_test_add_func ("/option/arg/rest/separator3", rest_test2b
);
2653 g_test_add_func ("/option/arg/rest/separator4", rest_test2c
);
2654 g_test_add_func ("/option/arg/rest/separator5", rest_test2d
);
2655 g_test_add_func ("/option/arg/remaining/non-option", rest_test3
);
2656 g_test_add_func ("/option/arg/remaining/separator", rest_test4
);
2657 g_test_add_func ("/option/arg/remaining/array", rest_test5
);
2659 /* Test some invalid flag combinations */
2660 g_test_add_func ("/option/arg/reverse-string", flag_reverse_string
);
2661 g_test_add_func ("/option/arg/optional-int", flag_optional_int
);
2663 /* regression tests for individual bugs */
2664 g_test_add_func ("/option/bug/unknown-short", unknown_short_test
);
2665 g_test_add_func ("/option/bug/lonely-dash", lonely_dash_test
);
2666 g_test_add_func ("/option/bug/triple-dash", triple_dash_test
);
2667 g_test_add_func ("/option/bug/missing-arg", missing_arg_test
);
2668 g_test_add_func ("/option/bug/dash-arg", dash_arg_test
);
2669 g_test_add_func ("/option/bug/short-remaining", short_remaining
);
2670 g_test_add_func ("/option/bug/double-free", double_free
);
2672 return g_test_run();