7 char *error_test2_string;
8 gboolean error_test3_boolean;
11 gchar *arg_test2_string;
12 gchar *arg_test3_filename;
13 gdouble arg_test4_double;
14 gdouble arg_test5_double;
15 gint64 arg_test6_int64;
16 gint64 arg_test6_int64_2;
18 gchar *callback_test1_string;
19 int callback_test2_int;
21 gchar *callback_test_optional_string;
22 gboolean callback_test_optional_boolean;
24 gchar **array_test1_array;
26 gboolean ignore_test1_boolean;
27 gboolean ignore_test2_boolean;
28 gchar *ignore_test3_string;
31 split_string (const char *str, int *argc)
36 argv = g_strsplit (str, " ", 0);
38 for (len = 0; argv[len] != NULL; len++);
47 join_stringv (int argc, char **argv)
52 str = g_string_new (NULL);
54 for (i = 0; i < argc; i++)
56 g_string_append (str, argv[i]);
59 g_string_append_c (str, ' ');
62 return g_string_free (str, FALSE);
65 /* Performs a shallow copy */
67 copy_stringv (char **argv, int argc)
69 return g_memdup (argv, sizeof (char *) * (argc + 1));
74 error_test1_pre_parse (GOptionContext *context,
79 g_assert (error_test1_int == 0x12345678);
85 error_test1_post_parse (GOptionContext *context,
90 g_assert (error_test1_int == 20);
92 /* Set an error in the post hook */
93 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
101 GOptionContext *context;
103 GError *error = NULL;
106 GOptionGroup *main_group;
107 GOptionEntry entries [] =
108 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
111 context = g_option_context_new (NULL);
112 g_option_context_add_main_entries (context, entries, NULL);
114 /* Set pre and post parse hooks */
115 main_group = g_option_context_get_main_group (context);
116 g_option_group_set_parse_hooks (main_group,
117 error_test1_pre_parse, error_test1_post_parse);
119 /* Now try parsing */
120 argv = split_string ("program --test 20", &argc);
122 retval = g_option_context_parse (context, &argc, &argv, &error);
123 g_assert (retval == FALSE);
125 /* On failure, values should be reset */
126 g_assert (error_test1_int == 0x12345678);
129 g_option_context_free (context);
134 error_test2_pre_parse (GOptionContext *context,
139 g_assert (strcmp (error_test2_string, "foo") == 0);
145 error_test2_post_parse (GOptionContext *context,
150 g_assert (strcmp (error_test2_string, "bar") == 0);
152 /* Set an error in the post hook */
153 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
161 GOptionContext *context;
163 GError *error = NULL;
166 GOptionGroup *main_group;
167 GOptionEntry entries [] =
168 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
171 context = g_option_context_new (NULL);
172 g_option_context_add_main_entries (context, entries, NULL);
174 /* Set pre and post parse hooks */
175 main_group = g_option_context_get_main_group (context);
176 g_option_group_set_parse_hooks (main_group,
177 error_test2_pre_parse, error_test2_post_parse);
179 /* Now try parsing */
180 argv = split_string ("program --test bar", &argc);
181 retval = g_option_context_parse (context, &argc, &argv, &error);
183 g_error_free (error);
184 g_assert (retval == FALSE);
186 g_assert (strcmp (error_test2_string, "foo") == 0);
189 g_option_context_free (context);
193 error_test3_pre_parse (GOptionContext *context,
198 g_assert (!error_test3_boolean);
204 error_test3_post_parse (GOptionContext *context,
209 g_assert (error_test3_boolean);
211 /* Set an error in the post hook */
212 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
220 GOptionContext *context;
222 GError *error = NULL;
225 GOptionGroup *main_group;
226 GOptionEntry entries [] =
227 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
230 context = g_option_context_new (NULL);
231 g_option_context_add_main_entries (context, entries, NULL);
233 /* Set pre and post parse hooks */
234 main_group = g_option_context_get_main_group (context);
235 g_option_group_set_parse_hooks (main_group,
236 error_test3_pre_parse, error_test3_post_parse);
238 /* Now try parsing */
239 argv = split_string ("program --test", &argc);
240 retval = g_option_context_parse (context, &argc, &argv, &error);
242 g_error_free (error);
243 g_assert (retval == FALSE);
245 g_assert (!error_test3_boolean);
248 g_option_context_free (context);
254 GOptionContext *context;
256 GError *error = NULL;
259 GOptionEntry entries [] =
260 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
263 context = g_option_context_new (NULL);
264 g_option_context_add_main_entries (context, entries, NULL);
266 /* Now try parsing */
267 argv = split_string ("program --test 20 --test 30", &argc);
269 retval = g_option_context_parse (context, &argc, &argv, &error);
272 /* Last arg specified is the one that should be stored */
273 g_assert (arg_test1_int == 30);
276 g_option_context_free (context);
282 GOptionContext *context;
284 GError *error = NULL;
287 GOptionEntry entries [] =
288 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
291 context = g_option_context_new (NULL);
292 g_option_context_add_main_entries (context, entries, NULL);
294 /* Now try parsing */
295 argv = split_string ("program --test foo --test bar", &argc);
297 retval = g_option_context_parse (context, &argc, &argv, &error);
300 /* Last arg specified is the one that should be stored */
301 g_assert (strcmp (arg_test2_string, "bar") == 0);
303 g_free (arg_test2_string);
306 g_option_context_free (context);
312 GOptionContext *context;
314 GError *error = NULL;
317 GOptionEntry entries [] =
318 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
321 context = g_option_context_new (NULL);
322 g_option_context_add_main_entries (context, entries, NULL);
324 /* Now try parsing */
325 argv = split_string ("program --test foo.txt", &argc);
327 retval = g_option_context_parse (context, &argc, &argv, &error);
330 /* Last arg specified is the one that should be stored */
331 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
333 g_free (arg_test3_filename);
336 g_option_context_free (context);
343 GOptionContext *context;
345 GError *error = NULL;
348 GOptionEntry entries [] =
349 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, NULL, NULL },
352 context = g_option_context_new (NULL);
353 g_option_context_add_main_entries (context, entries, NULL);
355 /* Now try parsing */
356 argv = split_string ("program --test 20.0 --test 30.03", &argc);
358 retval = g_option_context_parse (context, &argc, &argv, &error);
361 /* Last arg specified is the one that should be stored */
362 g_assert (arg_test4_double == 30.03);
365 g_option_context_free (context);
371 GOptionContext *context;
373 GError *error = NULL;
376 char *old_locale, *current_locale;
377 const char *locale = "de_DE";
378 GOptionEntry entries [] =
379 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test5_double, NULL, NULL },
382 context = g_option_context_new (NULL);
383 g_option_context_add_main_entries (context, entries, NULL);
385 /* Now try parsing */
386 argv = split_string ("program --test 20,0 --test 30,03", &argc);
388 /* set it to some locale that uses commas instead of decimal points */
390 old_locale = g_strdup (setlocale (LC_NUMERIC, locale));
391 current_locale = setlocale (LC_NUMERIC, NULL);
392 if (strcmp (current_locale, locale) != 0)
394 fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
398 retval = g_option_context_parse (context, &argc, &argv, &error);
401 /* Last arg specified is the one that should be stored */
402 g_assert (arg_test5_double == 30.03);
405 setlocale (LC_NUMERIC, old_locale);
409 g_option_context_free (context);
415 GOptionContext *context;
417 GError *error = NULL;
420 GOptionEntry entries [] =
421 { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL },
422 { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, NULL, NULL },
425 context = g_option_context_new (NULL);
426 g_option_context_add_main_entries (context, entries, NULL);
428 /* Now try parsing */
429 argv = split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc);
431 retval = g_option_context_parse (context, &argc, &argv, &error);
434 /* Last arg specified is the one that should be stored */
435 g_assert (arg_test6_int64 == 4294967296LL);
436 g_assert (arg_test6_int64_2 == 0xfffffffffLL);
439 g_option_context_free (context);
443 callback_parse1 (const gchar *option_name, const gchar *value,
444 gpointer data, GError **error)
446 callback_test1_string = g_strdup (value);
451 callback_test1 (void)
453 GOptionContext *context;
455 GError *error = NULL;
458 GOptionEntry entries [] =
459 { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL },
462 context = g_option_context_new (NULL);
463 g_option_context_add_main_entries (context, entries, NULL);
465 /* Now try parsing */
466 argv = split_string ("program --test foo.txt", &argc);
468 retval = g_option_context_parse (context, &argc, &argv, &error);
471 g_assert (strcmp (callback_test1_string, "foo.txt") == 0);
473 g_free (callback_test1_string);
476 g_option_context_free (context);
480 callback_parse2 (const gchar *option_name, const gchar *value,
481 gpointer data, GError **error)
483 callback_test2_int++;
488 callback_test2 (void)
490 GOptionContext *context;
492 GError *error = NULL;
495 GOptionEntry entries [] =
496 { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL },
499 context = g_option_context_new (NULL);
500 g_option_context_add_main_entries (context, entries, NULL);
502 /* Now try parsing */
503 argv = split_string ("program --test --test", &argc);
505 retval = g_option_context_parse (context, &argc, &argv, &error);
508 g_assert (callback_test2_int == 2);
511 g_option_context_free (context);
515 callback_parse_optional (const gchar *option_name, const gchar *value,
516 gpointer data, GError **error)
518 callback_test_optional_boolean = TRUE;
520 callback_test_optional_string = g_strdup (value);
522 callback_test_optional_string = NULL;
527 callback_test_optional_1 (void)
529 GOptionContext *context;
531 GError *error = NULL;
534 GOptionEntry entries [] =
535 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
536 callback_parse_optional, NULL, NULL },
539 context = g_option_context_new (NULL);
540 g_option_context_add_main_entries (context, entries, NULL);
542 /* Now try parsing */
543 argv = split_string ("program --test foo.txt", &argc);
545 retval = g_option_context_parse (context, &argc, &argv, &error);
548 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
550 g_assert (callback_test_optional_boolean);
552 g_free (callback_test_optional_string);
555 g_option_context_free (context);
559 callback_test_optional_2 (void)
561 GOptionContext *context;
563 GError *error = NULL;
566 GOptionEntry entries [] =
567 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
568 callback_parse_optional, NULL, NULL },
571 context = g_option_context_new (NULL);
572 g_option_context_add_main_entries (context, entries, NULL);
574 /* Now try parsing */
575 argv = split_string ("program --test", &argc);
577 retval = g_option_context_parse (context, &argc, &argv, &error);
580 g_assert (callback_test_optional_string == NULL);
582 g_assert (callback_test_optional_boolean);
584 g_free (callback_test_optional_string);
587 g_option_context_free (context);
591 callback_test_optional_3 (void)
593 GOptionContext *context;
595 GError *error = NULL;
598 GOptionEntry entries [] =
599 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
600 callback_parse_optional, NULL, NULL },
603 context = g_option_context_new (NULL);
604 g_option_context_add_main_entries (context, entries, NULL);
606 /* Now try parsing */
607 argv = split_string ("program -t foo.txt", &argc);
609 retval = g_option_context_parse (context, &argc, &argv, &error);
612 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
614 g_assert (callback_test_optional_boolean);
616 g_free (callback_test_optional_string);
619 g_option_context_free (context);
624 callback_test_optional_4 (void)
626 GOptionContext *context;
628 GError *error = NULL;
631 GOptionEntry entries [] =
632 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
633 callback_parse_optional, NULL, NULL },
636 context = g_option_context_new (NULL);
637 g_option_context_add_main_entries (context, entries, NULL);
639 /* Now try parsing */
640 argv = split_string ("program -t", &argc);
642 retval = g_option_context_parse (context, &argc, &argv, &error);
645 g_assert (callback_test_optional_string == NULL);
647 g_assert (callback_test_optional_boolean);
649 g_free (callback_test_optional_string);
652 g_option_context_free (context);
656 callback_test_optional_5 (void)
658 GOptionContext *context;
661 GError *error = NULL;
664 GOptionEntry entries [] =
665 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
666 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
667 callback_parse_optional, NULL, NULL },
670 context = g_option_context_new (NULL);
671 g_option_context_add_main_entries (context, entries, NULL);
673 /* Now try parsing */
674 argv = split_string ("program --test --dummy", &argc);
676 retval = g_option_context_parse (context, &argc, &argv, &error);
679 g_assert (callback_test_optional_string == NULL);
681 g_assert (callback_test_optional_boolean);
683 g_free (callback_test_optional_string);
686 g_option_context_free (context);
690 callback_test_optional_6 (void)
692 GOptionContext *context;
695 GError *error = NULL;
698 GOptionEntry entries [] =
699 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
700 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
701 callback_parse_optional, NULL, NULL },
704 context = g_option_context_new (NULL);
705 g_option_context_add_main_entries (context, entries, NULL);
707 /* Now try parsing */
708 argv = split_string ("program -t -d", &argc);
710 retval = g_option_context_parse (context, &argc, &argv, &error);
713 g_assert (callback_test_optional_string == NULL);
715 g_assert (callback_test_optional_boolean);
717 g_free (callback_test_optional_string);
720 g_option_context_free (context);
724 callback_test_optional_7 (void)
726 GOptionContext *context;
729 GError *error = NULL;
732 GOptionEntry entries [] =
733 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
734 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
735 callback_parse_optional, NULL, NULL },
738 context = g_option_context_new (NULL);
739 g_option_context_add_main_entries (context, entries, NULL);
741 /* Now try parsing */
742 argv = split_string ("program -td", &argc);
744 retval = g_option_context_parse (context, &argc, &argv, &error);
747 g_assert (callback_test_optional_string == NULL);
749 g_assert (callback_test_optional_boolean);
751 g_free (callback_test_optional_string);
754 g_option_context_free (context);
758 callback_test_optional_8 (void)
760 GOptionContext *context;
763 GError *error = NULL;
766 GOptionEntry entries [] =
767 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
768 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
769 callback_parse_optional, NULL, NULL },
772 context = g_option_context_new (NULL);
773 g_option_context_add_main_entries (context, entries, NULL);
775 /* Now try parsing */
776 argv = split_string ("program -dt foo.txt", &argc);
778 retval = g_option_context_parse (context, &argc, &argv, &error);
781 g_assert (callback_test_optional_string);
783 g_assert (callback_test_optional_boolean);
785 g_free (callback_test_optional_string);
788 g_option_context_free (context);
794 GOptionContext *context;
796 GError *error = NULL;
797 gchar **argv, **argv_copy;
800 GOptionEntry entries [] =
801 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
804 context = g_option_context_new (NULL);
805 g_option_context_set_ignore_unknown_options (context, TRUE);
806 g_option_context_add_main_entries (context, entries, NULL);
808 /* Now try parsing */
809 argv = split_string ("program --test --hello", &argc);
810 argv_copy = copy_stringv (argv, argc);
812 retval = g_option_context_parse (context, &argc, &argv, &error);
816 arg = join_stringv (argc, argv);
817 g_assert (strcmp (arg, "program --hello") == 0);
820 g_strfreev (argv_copy);
822 g_option_context_free (context);
828 GOptionContext *context;
830 GError *error = NULL;
834 GOptionEntry entries [] =
835 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
838 context = g_option_context_new (NULL);
839 g_option_context_set_ignore_unknown_options (context, TRUE);
840 g_option_context_add_main_entries (context, entries, NULL);
842 /* Now try parsing */
843 argv = split_string ("program -test", &argc);
845 retval = g_option_context_parse (context, &argc, &argv, &error);
849 arg = join_stringv (argc, argv);
850 g_assert (strcmp (arg, "program -es") == 0);
854 g_option_context_free (context);
860 GOptionContext *context;
862 GError *error = NULL;
863 gchar **argv, **argv_copy;
866 GOptionEntry entries [] =
867 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
870 context = g_option_context_new (NULL);
871 g_option_context_set_ignore_unknown_options (context, TRUE);
872 g_option_context_add_main_entries (context, entries, NULL);
874 /* Now try parsing */
875 argv = split_string ("program --test foo --hello", &argc);
876 argv_copy = copy_stringv (argv, argc);
878 retval = g_option_context_parse (context, &argc, &argv, &error);
882 arg = join_stringv (argc, argv);
883 g_assert (strcmp (arg, "program --hello") == 0);
885 g_assert (strcmp (ignore_test3_string, "foo") == 0);
886 g_free (ignore_test3_string);
889 g_strfreev (argv_copy);
891 g_option_context_free (context);
897 GOptionContext *context;
899 GError *error = NULL;
902 GOptionEntry entries [] =
903 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
906 context = g_option_context_new (NULL);
907 g_option_context_add_main_entries (context, entries, NULL);
909 /* Now try parsing */
910 argv = split_string ("program --test foo --test bar", &argc);
912 retval = g_option_context_parse (context, &argc, &argv, &error);
916 g_assert (strcmp (array_test1_array[0], "foo") == 0);
917 g_assert (strcmp (array_test1_array[1], "bar") == 0);
918 g_assert (array_test1_array[2] == NULL);
920 g_strfreev (array_test1_array);
923 g_option_context_free (context);
929 GOptionContext *context;
931 GOptionEntry entries1 [] =
932 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
934 GOptionEntry entries2 [] =
935 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
938 context = g_option_context_new (NULL);
939 g_option_context_add_main_entries (context, entries1, NULL);
940 g_option_context_add_main_entries (context, entries2, NULL);
942 g_option_context_free (context);
948 GOptionContext *context;
949 GOptionEntry entries [] =
953 g_set_prgname (NULL);
954 context = g_option_context_new (NULL);
956 g_option_context_add_main_entries (context, entries, NULL);
958 g_option_context_parse (context, NULL, NULL, NULL);
960 prgname = g_get_prgname ();
961 g_assert (prgname && strcmp (prgname, "<unknown>") == 0);
963 g_option_context_free (context);
969 GOptionContext *context;
971 context = g_option_context_new (NULL);
972 g_option_context_parse (context, NULL, NULL, NULL);
974 g_option_context_free (context);
980 GOptionContext *context;
987 context = g_option_context_new (NULL);
988 g_option_context_parse (context, &argc, &argv, NULL);
990 g_option_context_free (context);
993 /* check that non-option arguments are left in argv by default */
997 GOptionContext *context;
999 GError *error = NULL;
1002 GOptionEntry entries [] = {
1003 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1007 context = g_option_context_new (NULL);
1008 g_option_context_add_main_entries (context, entries, NULL);
1010 /* Now try parsing */
1011 argv = split_string ("program foo --test bar", &argc);
1013 retval = g_option_context_parse (context, &argc, &argv, &error);
1017 g_assert (ignore_test1_boolean);
1018 g_assert (strcmp (argv[0], "program") == 0);
1019 g_assert (strcmp (argv[1], "foo") == 0);
1020 g_assert (strcmp (argv[2], "bar") == 0);
1021 g_assert (argv[3] == NULL);
1024 g_option_context_free (context);
1027 /* check that -- works */
1031 GOptionContext *context;
1033 GError *error = NULL;
1036 GOptionEntry entries [] = {
1037 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1041 context = g_option_context_new (NULL);
1042 g_option_context_add_main_entries (context, entries, NULL);
1044 /* Now try parsing */
1045 argv = split_string ("program foo --test -- -bar", &argc);
1047 retval = g_option_context_parse (context, &argc, &argv, &error);
1051 g_assert (ignore_test1_boolean);
1052 g_assert (strcmp (argv[0], "program") == 0);
1053 g_assert (strcmp (argv[1], "foo") == 0);
1054 g_assert (strcmp (argv[2], "--") == 0);
1055 g_assert (strcmp (argv[3], "-bar") == 0);
1056 g_assert (argv[4] == NULL);
1059 g_option_context_free (context);
1062 /* check that -- stripping works */
1066 GOptionContext *context;
1068 GError *error = NULL;
1071 GOptionEntry entries [] = {
1072 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1076 context = g_option_context_new (NULL);
1077 g_option_context_add_main_entries (context, entries, NULL);
1079 /* Now try parsing */
1080 argv = split_string ("program foo --test -- bar", &argc);
1082 retval = g_option_context_parse (context, &argc, &argv, &error);
1086 g_assert (ignore_test1_boolean);
1087 g_assert (strcmp (argv[0], "program") == 0);
1088 g_assert (strcmp (argv[1], "foo") == 0);
1089 g_assert (strcmp (argv[2], "bar") == 0);
1090 g_assert (argv[3] == NULL);
1093 g_option_context_free (context);
1099 GOptionContext *context;
1101 GError *error = NULL;
1104 GOptionEntry entries [] = {
1105 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1109 context = g_option_context_new (NULL);
1110 g_option_context_set_ignore_unknown_options (context, TRUE);
1111 g_option_context_add_main_entries (context, entries, NULL);
1113 /* Now try parsing */
1114 argv = split_string ("program foo --test -bar --", &argc);
1116 retval = g_option_context_parse (context, &argc, &argv, &error);
1120 g_assert (ignore_test1_boolean);
1121 g_assert (strcmp (argv[0], "program") == 0);
1122 g_assert (strcmp (argv[1], "foo") == 0);
1123 g_assert (strcmp (argv[2], "-bar") == 0);
1124 g_assert (argv[3] == NULL);
1127 g_option_context_free (context);
1133 GOptionContext *context;
1135 GError *error = NULL;
1138 GOptionEntry entries [] = {
1139 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1143 context = g_option_context_new (NULL);
1144 g_option_context_add_main_entries (context, entries, NULL);
1146 /* Now try parsing */
1147 argv = split_string ("program --test foo -- bar", &argc);
1149 retval = g_option_context_parse (context, &argc, &argv, &error);
1153 g_assert (ignore_test1_boolean);
1154 g_assert (strcmp (argv[0], "program") == 0);
1155 g_assert (strcmp (argv[1], "foo") == 0);
1156 g_assert (strcmp (argv[2], "bar") == 0);
1157 g_assert (argv[3] == NULL);
1160 g_option_context_free (context);
1166 GOptionContext *context;
1168 GError *error = NULL;
1171 GOptionEntry entries [] = {
1172 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1176 context = g_option_context_new (NULL);
1177 g_option_context_add_main_entries (context, entries, NULL);
1179 /* Now try parsing */
1180 argv = split_string ("program --test -- -bar", &argc);
1182 retval = g_option_context_parse (context, &argc, &argv, &error);
1186 g_assert (ignore_test1_boolean);
1187 g_assert (strcmp (argv[0], "program") == 0);
1188 g_assert (strcmp (argv[1], "--") == 0);
1189 g_assert (strcmp (argv[2], "-bar") == 0);
1190 g_assert (argv[3] == NULL);
1193 g_option_context_free (context);
1197 /* check that G_OPTION_REMAINING collects non-option arguments */
1201 GOptionContext *context;
1203 GError *error = NULL;
1206 GOptionEntry entries [] = {
1207 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1208 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1212 context = g_option_context_new (NULL);
1213 g_option_context_add_main_entries (context, entries, NULL);
1215 /* Now try parsing */
1216 argv = split_string ("program foo --test bar", &argc);
1218 retval = g_option_context_parse (context, &argc, &argv, &error);
1222 g_assert (ignore_test1_boolean);
1223 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1224 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1225 g_assert (array_test1_array[2] == NULL);
1227 g_strfreev (array_test1_array);
1230 g_option_context_free (context);
1234 /* check that G_OPTION_REMAINING and -- work together */
1238 GOptionContext *context;
1240 GError *error = NULL;
1243 GOptionEntry entries [] = {
1244 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1245 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1249 context = g_option_context_new (NULL);
1250 g_option_context_add_main_entries (context, entries, NULL);
1252 /* Now try parsing */
1253 argv = split_string ("program foo --test -- -bar", &argc);
1255 retval = g_option_context_parse (context, &argc, &argv, &error);
1259 g_assert (ignore_test1_boolean);
1260 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1261 g_assert (strcmp (array_test1_array[1], "-bar") == 0);
1262 g_assert (array_test1_array[2] == NULL);
1264 g_strfreev (array_test1_array);
1267 g_option_context_free (context);
1270 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1274 GOptionContext *context;
1276 GError *error = NULL;
1279 GOptionEntry entries [] = {
1280 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1281 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
1285 context = g_option_context_new (NULL);
1286 g_option_context_add_main_entries (context, entries, NULL);
1288 /* Now try parsing */
1289 argv = split_string ("program foo --test bar", &argc);
1291 retval = g_option_context_parse (context, &argc, &argv, &error);
1295 g_assert (ignore_test1_boolean);
1296 g_assert (strcmp (array_test1_array[0], "foo") == 0);
1297 g_assert (strcmp (array_test1_array[1], "bar") == 0);
1298 g_assert (array_test1_array[2] == NULL);
1300 g_strfreev (array_test1_array);
1303 g_option_context_free (context);
1307 unknown_short_test (void)
1309 GOptionContext *context;
1311 GError *error = NULL;
1314 GOptionEntry entries [] = { { NULL } };
1316 context = g_option_context_new (NULL);
1317 g_option_context_add_main_entries (context, entries, NULL);
1319 /* Now try parsing */
1320 argv = split_string ("program -0", &argc);
1322 retval = g_option_context_parse (context, &argc, &argv, &error);
1326 g_option_context_free (context);
1329 /* test that lone dashes are treated as non-options */
1330 void lonely_dash_test (void)
1332 GOptionContext *context;
1334 GError *error = NULL;
1338 context = g_option_context_new (NULL);
1340 /* Now try parsing */
1341 argv = split_string ("program -", &argc);
1343 retval = g_option_context_parse (context, &argc, &argv, &error);
1347 g_assert (argv[1] && strcmp (argv[1], "-") == 0);
1350 g_option_context_free (context);
1354 missing_arg_test (void)
1356 GOptionContext *context;
1358 GError *error = NULL;
1362 GOptionEntry entries [] =
1363 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1366 context = g_option_context_new (NULL);
1367 g_option_context_add_main_entries (context, entries, NULL);
1369 /* Now try parsing */
1370 argv = split_string ("program --test", &argc);
1372 retval = g_option_context_parse (context, &argc, &argv, &error);
1373 g_assert (retval == FALSE);
1374 g_clear_error (&error);
1378 /* Try parsing again */
1379 argv = split_string ("program --t", &argc);
1381 retval = g_option_context_parse (context, &argc, &argv, &error);
1382 g_assert (retval == FALSE);
1385 g_option_context_free (context);
1389 main (int argc, char **argv)
1391 /* Test that restoration on failure works */
1392 error_test1_int = 0x12345678;
1394 error_test2_string = "foo";
1396 error_test3_boolean = FALSE;
1399 /* Test that special argument parsing works */
1407 /* Test string arrays */
1410 /* Test callback args */
1414 /* Test optional arg flag for callback */
1415 callback_test_optional_1 ();
1416 callback_test_optional_2 ();
1417 callback_test_optional_3 ();
1418 callback_test_optional_4 ();
1419 callback_test_optional_5 ();
1420 callback_test_optional_6 ();
1421 callback_test_optional_7 ();
1422 callback_test_optional_8 ();
1424 /* Test ignoring options */
1431 /* Test parsing empty args */
1436 /* Test handling of rest args */
1447 /* test for bug 166609 */
1448 unknown_short_test ();
1450 /* test for bug 168008 */
1451 lonely_dash_test ();
1453 /* test for bug 305576 */
1454 missing_arg_test ();