5 char *error_test2_string;
6 gboolean error_test3_boolean;
9 gchar *arg_test2_string;
10 gchar *arg_test3_filename;
12 gchar **array_test1_array;
14 gboolean ignore_test1_boolean;
15 gboolean ignore_test2_boolean;
16 gchar *ignore_test3_string;
19 split_string (const char *str, int *argc)
24 argv = g_strsplit (str, " ", 0);
26 for (len = 0; argv[len] != NULL; len++);
35 join_stringv (int argc, char **argv)
40 str = g_string_new (NULL);
42 for (i = 0; i < argc; i++)
44 g_string_append (str, argv[i]);
47 g_string_append_c (str, ' ');
50 return g_string_free (str, FALSE);
53 /* Performs a shallow copy */
55 copy_stringv (char **argv, int argc)
57 return g_memdup (argv, sizeof (char *) * (argc + 1));
62 error_test1_pre_parse (GOptionContext *context,
67 g_assert (error_test1_int == 0x12345678);
73 error_test1_post_parse (GOptionContext *context,
78 g_assert (error_test1_int == 20);
80 /* Set an error in the post hook */
81 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
89 GOptionContext *context;
94 GOptionGroup *main_group;
95 GOptionEntry entries [] =
96 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
99 context = g_option_context_new (NULL);
100 g_option_context_add_main_entries (context, entries, NULL);
102 /* Set pre and post parse hooks */
103 main_group = g_option_context_get_main_group (context);
104 g_option_group_set_parse_hooks (main_group,
105 error_test1_pre_parse, error_test1_post_parse);
107 /* Now try parsing */
108 argv = split_string ("program --test 20", &argc);
110 retval = g_option_context_parse (context, &argc, &argv, &error);
111 g_assert (retval == FALSE);
113 /* On failure, values should be reset */
114 g_assert (error_test1_int == 0x12345678);
117 g_option_context_free (context);
122 error_test2_pre_parse (GOptionContext *context,
127 g_assert (strcmp (error_test2_string, "foo") == 0);
133 error_test2_post_parse (GOptionContext *context,
138 g_assert (strcmp (error_test2_string, "bar") == 0);
140 /* Set an error in the post hook */
141 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
149 GOptionContext *context;
151 GError *error = NULL;
154 GOptionGroup *main_group;
155 GOptionEntry entries [] =
156 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
159 context = g_option_context_new (NULL);
160 g_option_context_add_main_entries (context, entries, NULL);
162 /* Set pre and post parse hooks */
163 main_group = g_option_context_get_main_group (context);
164 g_option_group_set_parse_hooks (main_group,
165 error_test2_pre_parse, error_test2_post_parse);
167 /* Now try parsing */
168 argv = split_string ("program --test bar", &argc);
169 retval = g_option_context_parse (context, &argc, &argv, &error);
171 g_error_free (error);
172 g_assert (retval == FALSE);
174 g_assert (strcmp (error_test2_string, "foo") == 0);
177 g_option_context_free (context);
181 error_test3_pre_parse (GOptionContext *context,
186 g_assert (!error_test3_boolean);
192 error_test3_post_parse (GOptionContext *context,
197 g_assert (error_test3_boolean);
199 /* Set an error in the post hook */
200 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
208 GOptionContext *context;
210 GError *error = NULL;
213 GOptionGroup *main_group;
214 GOptionEntry entries [] =
215 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
218 context = g_option_context_new (NULL);
219 g_option_context_add_main_entries (context, entries, NULL);
221 /* Set pre and post parse hooks */
222 main_group = g_option_context_get_main_group (context);
223 g_option_group_set_parse_hooks (main_group,
224 error_test3_pre_parse, error_test3_post_parse);
226 /* Now try parsing */
227 argv = split_string ("program --test", &argc);
228 retval = g_option_context_parse (context, &argc, &argv, &error);
230 g_error_free (error);
231 g_assert (retval == FALSE);
233 g_assert (!error_test3_boolean);
236 g_option_context_free (context);
242 GOptionContext *context;
244 GError *error = NULL;
247 GOptionEntry entries [] =
248 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
251 context = g_option_context_new (NULL);
252 g_option_context_add_main_entries (context, entries, NULL);
254 /* Now try parsing */
255 argv = split_string ("program --test 20 --test 30", &argc);
257 retval = g_option_context_parse (context, &argc, &argv, &error);
260 /* Last arg specified is the one that should be stored */
261 g_assert (arg_test1_int == 30);
264 g_option_context_free (context);
270 GOptionContext *context;
272 GError *error = NULL;
275 GOptionEntry entries [] =
276 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
279 context = g_option_context_new (NULL);
280 g_option_context_add_main_entries (context, entries, NULL);
282 /* Now try parsing */
283 argv = split_string ("program --test foo --test bar", &argc);
285 retval = g_option_context_parse (context, &argc, &argv, &error);
288 /* Last arg specified is the one that should be stored */
289 g_assert (strcmp (arg_test2_string, "bar") == 0);
291 g_free (arg_test2_string);
294 g_option_context_free (context);
300 GOptionContext *context;
302 GError *error = NULL;
305 GOptionEntry entries [] =
306 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
309 context = g_option_context_new (NULL);
310 g_option_context_add_main_entries (context, entries, NULL);
312 /* Now try parsing */
313 argv = split_string ("program --test foo.txt", &argc);
315 retval = g_option_context_parse (context, &argc, &argv, &error);
318 /* Last arg specified is the one that should be stored */
319 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
321 g_free (arg_test3_filename);
324 g_option_context_free (context);
330 GOptionContext *context;
332 GError *error = NULL;
333 gchar **argv, **argv_copy;
336 GOptionEntry entries [] =
337 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
340 context = g_option_context_new (NULL);
341 g_option_context_set_ignore_unknown_options (context, TRUE);
342 g_option_context_add_main_entries (context, entries, NULL);
344 /* Now try parsing */
345 argv = split_string ("program --test --hello", &argc);
346 argv_copy = copy_stringv (argv, argc);
348 retval = g_option_context_parse (context, &argc, &argv, &error);
352 arg = join_stringv (argc, argv);
353 g_assert (strcmp (arg, "program --hello") == 0);
356 g_strfreev (argv_copy);
358 g_option_context_free (context);
364 GOptionContext *context;
366 GError *error = NULL;
370 GOptionEntry entries [] =
371 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
374 context = g_option_context_new (NULL);
375 g_option_context_set_ignore_unknown_options (context, TRUE);
376 g_option_context_add_main_entries (context, entries, NULL);
378 /* Now try parsing */
379 argv = split_string ("program -test", &argc);
381 retval = g_option_context_parse (context, &argc, &argv, &error);
385 arg = join_stringv (argc, argv);
386 g_assert (strcmp (arg, "program -es") == 0);
390 g_option_context_free (context);
396 GOptionContext *context;
398 GError *error = NULL;
399 gchar **argv, **argv_copy;
402 GOptionEntry entries [] =
403 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
406 context = g_option_context_new (NULL);
407 g_option_context_set_ignore_unknown_options (context, TRUE);
408 g_option_context_add_main_entries (context, entries, NULL);
410 /* Now try parsing */
411 argv = split_string ("program --test foo --hello", &argc);
412 argv_copy = copy_stringv (argv, argc);
414 retval = g_option_context_parse (context, &argc, &argv, &error);
418 arg = join_stringv (argc, argv);
419 g_assert (strcmp (arg, "program --hello") == 0);
421 g_assert (strcmp (ignore_test3_string, "foo") == 0);
422 g_free (ignore_test3_string);
425 g_strfreev (argv_copy);
427 g_option_context_free (context);
433 GOptionContext *context;
435 GError *error = NULL;
438 GOptionEntry entries [] =
439 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
442 context = g_option_context_new (NULL);
443 g_option_context_add_main_entries (context, entries, NULL);
445 /* Now try parsing */
446 argv = split_string ("program --test foo --test bar", &argc);
448 retval = g_option_context_parse (context, &argc, &argv, &error);
452 g_assert (strcmp (array_test1_array[0], "foo") == 0);
453 g_assert (strcmp (array_test1_array[1], "bar") == 0);
454 g_assert (array_test1_array[2] == NULL);
456 g_strfreev (array_test1_array);
459 g_option_context_free (context);
465 GOptionContext *context;
467 GOptionEntry entries1 [] =
468 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
470 GOptionEntry entries2 [] =
471 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
474 context = g_option_context_new (NULL);
475 g_option_context_add_main_entries (context, entries1, NULL);
476 g_option_context_add_main_entries (context, entries2, NULL);
478 g_option_context_free (context);
484 GOptionContext *context;
485 GOptionEntry entries [] =
488 context = g_option_context_new (NULL);
490 g_option_context_add_main_entries (context, entries, NULL);
492 g_option_context_parse (context, NULL, NULL, NULL);
494 g_assert (strcmp (g_get_prgname (), "<unknown>") == 0);
496 g_option_context_free (context);
502 GOptionContext *context;
504 context = g_option_context_new (NULL);
505 g_option_context_parse (context, NULL, NULL, NULL);
507 g_option_context_free (context);
513 GOptionContext *context;
520 context = g_option_context_new (NULL);
521 g_option_context_parse (context, &argc, &argv, NULL);
523 g_option_context_free (context);
526 /* check that non-option arguments are left in argv by default */
530 GOptionContext *context;
532 GError *error = NULL;
535 GOptionEntry entries [] = {
536 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
540 context = g_option_context_new (NULL);
541 g_option_context_add_main_entries (context, entries, NULL);
543 /* Now try parsing */
544 argv = split_string ("program foo --test bar", &argc);
546 retval = g_option_context_parse (context, &argc, &argv, &error);
550 g_assert (ignore_test1_boolean);
551 g_assert (strcmp (argv[0], "program") == 0);
552 g_assert (strcmp (argv[1], "foo") == 0);
553 g_assert (strcmp (argv[2], "bar") == 0);
554 g_assert (argv[3] == NULL);
557 g_option_context_free (context);
560 /* check that -- works */
564 GOptionContext *context;
566 GError *error = NULL;
569 GOptionEntry entries [] = {
570 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
574 context = g_option_context_new (NULL);
575 g_option_context_add_main_entries (context, entries, NULL);
577 /* Now try parsing */
578 argv = split_string ("program foo --test -- -bar", &argc);
580 retval = g_option_context_parse (context, &argc, &argv, &error);
584 g_assert (ignore_test1_boolean);
585 g_assert (strcmp (argv[0], "program") == 0);
586 g_assert (strcmp (argv[1], "foo") == 0);
587 g_assert (strcmp (argv[2], "--") == 0);
588 g_assert (strcmp (argv[3], "-bar") == 0);
589 g_assert (argv[4] == NULL);
592 g_option_context_free (context);
595 /* check that -- stripping works */
599 GOptionContext *context;
601 GError *error = NULL;
604 GOptionEntry entries [] = {
605 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
609 context = g_option_context_new (NULL);
610 g_option_context_add_main_entries (context, entries, NULL);
612 /* Now try parsing */
613 argv = split_string ("program foo --test -- bar", &argc);
615 retval = g_option_context_parse (context, &argc, &argv, &error);
619 g_assert (ignore_test1_boolean);
620 g_assert (strcmp (argv[0], "program") == 0);
621 g_assert (strcmp (argv[1], "foo") == 0);
622 g_assert (strcmp (argv[2], "bar") == 0);
623 g_assert (argv[3] == NULL);
626 g_option_context_free (context);
632 GOptionContext *context;
634 GError *error = NULL;
637 GOptionEntry entries [] = {
638 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
642 context = g_option_context_new (NULL);
643 g_option_context_set_ignore_unknown_options (context, TRUE);
644 g_option_context_add_main_entries (context, entries, NULL);
646 /* Now try parsing */
647 argv = split_string ("program foo --test -bar --", &argc);
649 retval = g_option_context_parse (context, &argc, &argv, &error);
653 g_assert (ignore_test1_boolean);
654 g_assert (strcmp (argv[0], "program") == 0);
655 g_assert (strcmp (argv[1], "foo") == 0);
656 g_assert (strcmp (argv[2], "-bar") == 0);
657 g_assert (argv[3] == NULL);
660 g_option_context_free (context);
666 GOptionContext *context;
668 GError *error = NULL;
671 GOptionEntry entries [] = {
672 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
676 context = g_option_context_new (NULL);
677 g_option_context_add_main_entries (context, entries, NULL);
679 /* Now try parsing */
680 argv = split_string ("program --test foo -- bar", &argc);
682 retval = g_option_context_parse (context, &argc, &argv, &error);
686 g_assert (ignore_test1_boolean);
687 g_assert (strcmp (argv[0], "program") == 0);
688 g_assert (strcmp (argv[1], "foo") == 0);
689 g_assert (strcmp (argv[2], "bar") == 0);
690 g_assert (argv[3] == NULL);
693 g_option_context_free (context);
699 GOptionContext *context;
701 GError *error = NULL;
704 GOptionEntry entries [] = {
705 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
709 context = g_option_context_new (NULL);
710 g_option_context_add_main_entries (context, entries, NULL);
712 /* Now try parsing */
713 argv = split_string ("program --test -- -bar", &argc);
715 retval = g_option_context_parse (context, &argc, &argv, &error);
719 g_assert (ignore_test1_boolean);
720 g_assert (strcmp (argv[0], "program") == 0);
721 g_assert (strcmp (argv[1], "--") == 0);
722 g_assert (strcmp (argv[2], "-bar") == 0);
723 g_assert (argv[3] == NULL);
726 g_option_context_free (context);
730 /* check that G_OPTION_REMAINING collects non-option arguments */
734 GOptionContext *context;
736 GError *error = NULL;
739 GOptionEntry entries [] = {
740 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
741 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
745 context = g_option_context_new (NULL);
746 g_option_context_add_main_entries (context, entries, NULL);
748 /* Now try parsing */
749 argv = split_string ("program foo --test bar", &argc);
751 retval = g_option_context_parse (context, &argc, &argv, &error);
755 g_assert (ignore_test1_boolean);
756 g_assert (strcmp (array_test1_array[0], "foo") == 0);
757 g_assert (strcmp (array_test1_array[1], "bar") == 0);
758 g_assert (array_test1_array[2] == NULL);
760 g_strfreev (array_test1_array);
763 g_option_context_free (context);
767 /* check that G_OPTION_REMAINING and -- work together */
771 GOptionContext *context;
773 GError *error = NULL;
776 GOptionEntry entries [] = {
777 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
778 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
782 context = g_option_context_new (NULL);
783 g_option_context_add_main_entries (context, entries, NULL);
785 /* Now try parsing */
786 argv = split_string ("program foo --test -- -bar", &argc);
788 retval = g_option_context_parse (context, &argc, &argv, &error);
792 g_assert (ignore_test1_boolean);
793 g_assert (strcmp (array_test1_array[0], "foo") == 0);
794 g_assert (strcmp (array_test1_array[1], "-bar") == 0);
795 g_assert (array_test1_array[2] == NULL);
797 g_strfreev (array_test1_array);
800 g_option_context_free (context);
803 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
807 GOptionContext *context;
809 GError *error = NULL;
812 GOptionEntry entries [] = {
813 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
814 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
818 context = g_option_context_new (NULL);
819 g_option_context_add_main_entries (context, entries, NULL);
821 /* Now try parsing */
822 argv = split_string ("program foo --test bar", &argc);
824 retval = g_option_context_parse (context, &argc, &argv, &error);
828 g_assert (ignore_test1_boolean);
829 g_assert (strcmp (array_test1_array[0], "foo") == 0);
830 g_assert (strcmp (array_test1_array[1], "bar") == 0);
831 g_assert (array_test1_array[2] == NULL);
833 g_strfreev (array_test1_array);
836 g_option_context_free (context);
840 unknown_short_test (void)
842 GOptionContext *context;
844 GError *error = NULL;
847 GOptionEntry entries [] = { { NULL } };
849 context = g_option_context_new (NULL);
850 g_option_context_add_main_entries (context, entries, NULL);
852 /* Now try parsing */
853 argv = split_string ("program -0", &argc);
855 retval = g_option_context_parse (context, &argc, &argv, &error);
859 g_option_context_free (context);
862 /* test that lone dashes are treated as non-options */
863 void lonely_dash_test (void)
865 GOptionContext *context;
867 GError *error = NULL;
871 context = g_option_context_new (NULL);
873 /* Now try parsing */
874 argv = split_string ("program -", &argc);
876 retval = g_option_context_parse (context, &argc, &argv, &error);
880 g_assert (argv[1] && strcmp (argv[1], "-") == 0);
883 g_option_context_free (context);
887 missing_arg_test (void)
889 GOptionContext *context;
891 GError *error = NULL;
895 GOptionEntry entries [] =
896 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
899 context = g_option_context_new (NULL);
900 g_option_context_add_main_entries (context, entries, NULL);
902 /* Now try parsing */
903 argv = split_string ("program --test", &argc);
905 retval = g_option_context_parse (context, &argc, &argv, &error);
906 g_assert (retval == FALSE);
907 g_clear_error (&error);
911 /* Try parsing again */
912 argv = split_string ("program --t", &argc);
914 retval = g_option_context_parse (context, &argc, &argv, &error);
915 g_assert (retval == FALSE);
918 g_option_context_free (context);
922 main (int argc, char **argv)
924 /* Test that restoration on failure works */
925 error_test1_int = 0x12345678;
927 error_test2_string = "foo";
929 error_test3_boolean = FALSE;
932 /* Test that special argument parsing works */
937 /* Test string arrays */
940 /* Test ignoring options */
947 /* Test parsing empty args */
952 /* Test handling of rest args */
963 /* test for bug 166609 */
964 unknown_short_test ();
966 /* test for bug 168008 */
969 /* test for bug 305576 */