Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / glib / tests / option-context.c
blob34ebfaaf6b8d2f7f69b74e45d40ba19151977ed1
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.
23 #include <glib.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <locale.h>
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 },
34 { 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 },
41 { 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",
60 NULL, NULL);
61 g_option_context_add_group (options, group);
62 g_option_group_add_entries (group, group_entries);
65 return options;
68 static void
69 print_help (GOptionContext *options, gchar **argv)
71 gint argc = 3;
72 GError *error = NULL;
74 g_option_context_parse (options, &argc, &argv, &error);
75 g_option_context_free (options);
76 exit(0);
79 static void
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);
89 static void
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);
99 static void
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);
109 static void
110 test_group_captions (void)
112 const gchar *test_name_base[] = { "help", "help-all", "help-test" };
113 gchar *test_name;
114 gint i, j;
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);
138 g_free (test_name);
139 g_test_trap_assert_passed ();
140 g_test_trap_assert_stderr ("");
142 switch (j)
144 case 0:
145 g_assert_cmpstr ("help", ==, test_name_base[j]);
146 expect_main_switch = have_main_entries;
147 expect_test_group = have_test_entries;
148 break;
150 case 1:
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;
155 break;
157 case 2:
158 g_assert_cmpstr ("help-test", ==, test_name_base[j]);
159 expect_test_switch = have_test_entries;
160 break;
162 default:
163 g_assert_not_reached ();
164 break;
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*");
172 else
173 g_test_trap_assert_stdout_unmatched ("*Application Options*");
174 if (expect_main_switch)
175 g_test_trap_assert_stdout ("*--main-switch*");
176 else
177 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
179 if (expect_test_description)
180 g_test_trap_assert_stdout ("*Test Options*");
181 else
182 g_test_trap_assert_stdout_unmatched ("*Test Options*");
183 if (expect_test_switch)
184 g_test_trap_assert_stdout ("*--test-switch*");
185 else
186 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
188 if (expect_test_group)
189 g_test_trap_assert_stdout ("*--help-test*");
190 else
191 g_test_trap_assert_stdout_unmatched ("*--help-test*");
196 int error_test1_int;
197 char *error_test2_string;
198 gboolean error_test3_boolean;
200 int arg_test1_int;
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;
220 static gchar **
221 split_string (const char *str, int *argc)
223 gchar **argv;
224 int len;
226 argv = g_strsplit (str, " ", 0);
228 for (len = 0; argv[len] != NULL; len++);
230 if (argc)
231 *argc = len;
233 return argv;
236 static gchar *
237 join_stringv (int argc, char **argv)
239 int i;
240 GString *str;
242 str = g_string_new (NULL);
244 for (i = 0; i < argc; i++)
246 g_string_append (str, argv[i]);
248 if (i < argc - 1)
249 g_string_append_c (str, ' ');
252 return g_string_free (str, FALSE);
255 /* Performs a shallow copy */
256 static char **
257 copy_stringv (char **argv, int argc)
259 return g_memdup (argv, sizeof (char *) * (argc + 1));
262 static void
263 check_identical_stringv (gchar **before, gchar **after)
265 guint i;
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);
279 static gboolean
280 error_test1_pre_parse (GOptionContext *context,
281 GOptionGroup *group,
282 gpointer data,
283 GError **error)
285 g_assert (error_test1_int == 0x12345678);
287 return TRUE;
290 static gboolean
291 error_test1_post_parse (GOptionContext *context,
292 GOptionGroup *group,
293 gpointer data,
294 GError **error)
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, " ");
301 return FALSE;
304 static void
305 error_test1 (void)
307 GOptionContext *context;
308 gboolean retval;
309 GError *error = NULL;
310 gchar **argv;
311 gchar **argv_copy;
312 int argc;
313 GOptionGroup *main_group;
314 GOptionEntry entries [] =
315 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
316 { 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);
343 g_free (argv);
344 g_option_context_free (context);
347 static gboolean
348 error_test2_pre_parse (GOptionContext *context,
349 GOptionGroup *group,
350 gpointer data,
351 GError **error)
353 g_assert (strcmp (error_test2_string, "foo") == 0);
355 return TRUE;
358 static gboolean
359 error_test2_post_parse (GOptionContext *context,
360 GOptionGroup *group,
361 gpointer data,
362 GError **error)
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, " ");
369 return FALSE;
372 static void
373 error_test2 (void)
375 GOptionContext *context;
376 gboolean retval;
377 GError *error = NULL;
378 gchar **argv;
379 gchar **argv_copy;
380 int argc;
381 GOptionGroup *main_group;
382 GOptionEntry entries [] =
383 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
384 { 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);
409 g_free (argv);
410 g_option_context_free (context);
413 static gboolean
414 error_test3_pre_parse (GOptionContext *context,
415 GOptionGroup *group,
416 gpointer data,
417 GError **error)
419 g_assert (!error_test3_boolean);
421 return TRUE;
424 static gboolean
425 error_test3_post_parse (GOptionContext *context,
426 GOptionGroup *group,
427 gpointer data,
428 GError **error)
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, " ");
435 return FALSE;
438 static void
439 error_test3 (void)
441 GOptionContext *context;
442 gboolean retval;
443 GError *error = NULL;
444 gchar **argv;
445 gchar **argv_copy;
446 int argc;
447 GOptionGroup *main_group;
448 GOptionEntry entries [] =
449 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
450 { 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);
475 g_free (argv);
476 g_option_context_free (context);
479 static void
480 arg_test1 (void)
482 GOptionContext *context;
483 gboolean retval;
484 GError *error = NULL;
485 gchar **argv;
486 gchar **argv_copy;
487 int argc;
488 GOptionEntry entries [] =
489 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
490 { 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);
501 g_assert (retval);
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);
510 g_free (argv);
511 g_option_context_free (context);
514 static void
515 arg_test2 (void)
517 GOptionContext *context;
518 gboolean retval;
519 GError *error = NULL;
520 gchar **argv;
521 gchar **argv_copy;
522 int argc;
523 GOptionEntry entries [] =
524 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
525 { 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);
536 g_assert (retval);
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);
544 g_free (argv);
545 g_option_context_free (context);
548 static void
549 arg_test3 (void)
551 GOptionContext *context;
552 gboolean retval;
553 GError *error = NULL;
554 gchar **argv;
555 gchar **argv_copy;
556 int argc;
557 GOptionEntry entries [] =
558 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
559 { 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);
570 g_assert (retval);
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);
578 g_free (argv);
579 g_option_context_free (context);
583 static void
584 arg_test4 (void)
586 GOptionContext *context;
587 gboolean retval;
588 GError *error = NULL;
589 gchar **argv_copy;
590 gchar **argv;
591 int argc;
592 GOptionEntry entries [] =
593 { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, NULL, NULL },
594 { 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);
605 g_assert (retval);
607 /* Last arg specified is the one that should be stored */
608 g_assert (arg_test4_double == 30.03);
610 g_strfreev (argv_copy);
611 g_free (argv);
612 g_option_context_free (context);
615 static void
616 arg_test5 (void)
618 GOptionContext *context;
619 gboolean retval;
620 GError *error = NULL;
621 gchar **argv;
622 gchar **argv_copy;
623 int argc;
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 },
628 { 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);
644 goto cleanup;
647 retval = g_option_context_parse (context, &argc, &argv, &error);
648 g_assert_no_error (error);
649 g_assert (retval);
651 /* Last arg specified is the one that should be stored */
652 g_assert (arg_test5_double == 30.03);
654 cleanup:
655 setlocale (LC_NUMERIC, old_locale);
656 g_free (old_locale);
658 g_strfreev (argv_copy);
659 g_free (argv);
660 g_option_context_free (context);
663 static void
664 arg_test6 (void)
666 GOptionContext *context;
667 gboolean retval;
668 GError *error = NULL;
669 gchar **argv;
670 gchar **argv_copy;
671 int argc;
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 },
675 { 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);
686 g_assert (retval);
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);
693 g_free (argv);
694 g_option_context_free (context);
697 static gboolean
698 callback_parse1 (const gchar *option_name, const gchar *value,
699 gpointer data, GError **error)
701 callback_test1_string = g_strdup (value);
702 return TRUE;
705 static void
706 callback_test1 (void)
708 GOptionContext *context;
709 gboolean retval;
710 GError *error = NULL;
711 gchar **argv;
712 gchar **argv_copy;
713 int argc;
714 GOptionEntry entries [] =
715 { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL },
716 { 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);
727 g_assert (retval);
729 g_assert (strcmp (callback_test1_string, "foo.txt") == 0);
731 g_free (callback_test1_string);
733 g_strfreev (argv_copy);
734 g_free (argv);
735 g_option_context_free (context);
738 static gboolean
739 callback_parse2 (const gchar *option_name, const gchar *value,
740 gpointer data, GError **error)
742 callback_test2_int++;
743 return TRUE;
746 static void
747 callback_test2 (void)
749 GOptionContext *context;
750 gboolean retval;
751 GError *error = NULL;
752 gchar **argv;
753 gchar **argv_copy;
754 int argc;
755 GOptionEntry entries [] =
756 { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL },
757 { 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);
768 g_assert (retval);
770 g_assert (callback_test2_int == 2);
772 g_strfreev (argv_copy);
773 g_free (argv);
774 g_option_context_free (context);
777 static gboolean
778 callback_parse_optional (const gchar *option_name, const gchar *value,
779 gpointer data, GError **error)
781 callback_test_optional_boolean = TRUE;
782 if (value)
783 callback_test_optional_string = g_strdup (value);
784 else
785 callback_test_optional_string = NULL;
786 return TRUE;
789 static void
790 callback_test_optional_1 (void)
792 GOptionContext *context;
793 gboolean retval;
794 GError *error = NULL;
795 gchar **argv;
796 gchar **argv_copy;
797 int argc;
798 GOptionEntry entries [] =
799 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
800 callback_parse_optional, NULL, NULL },
801 { 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);
812 g_assert (retval);
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);
821 g_free (argv);
822 g_option_context_free (context);
825 static void
826 callback_test_optional_2 (void)
828 GOptionContext *context;
829 gboolean retval;
830 GError *error = NULL;
831 gchar **argv;
832 gchar **argv_copy;
833 int argc;
834 GOptionEntry entries [] =
835 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
836 callback_parse_optional, NULL, NULL },
837 { 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);
848 g_assert (retval);
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);
857 g_free (argv);
858 g_option_context_free (context);
861 static void
862 callback_test_optional_3 (void)
864 GOptionContext *context;
865 gboolean retval;
866 GError *error = NULL;
867 gchar **argv_copy;
868 gchar **argv;
869 int argc;
870 GOptionEntry entries [] =
871 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
872 callback_parse_optional, NULL, NULL },
873 { 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);
884 g_assert (retval);
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);
893 g_free (argv);
894 g_option_context_free (context);
898 static void
899 callback_test_optional_4 (void)
901 GOptionContext *context;
902 gboolean retval;
903 GError *error = NULL;
904 gchar **argv;
905 gchar **argv_copy;
906 int argc;
907 GOptionEntry entries [] =
908 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
909 callback_parse_optional, NULL, NULL },
910 { 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);
921 g_assert (retval);
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);
930 g_free (argv);
931 g_option_context_free (context);
934 static void
935 callback_test_optional_5 (void)
937 GOptionContext *context;
938 gboolean dummy;
939 gboolean retval;
940 GError *error = NULL;
941 gchar **argv;
942 gchar **argv_copy;
943 int argc;
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 },
948 { 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);
959 g_assert (retval);
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);
968 g_free (argv);
969 g_option_context_free (context);
972 static void
973 callback_test_optional_6 (void)
975 GOptionContext *context;
976 gboolean dummy;
977 gboolean retval;
978 GError *error = NULL;
979 gchar **argv;
980 gchar **argv_copy;
981 int argc;
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 },
986 { 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);
997 g_assert (retval);
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);
1006 g_free (argv);
1007 g_option_context_free (context);
1010 static void
1011 callback_test_optional_7 (void)
1013 GOptionContext *context;
1014 gboolean dummy;
1015 gboolean retval;
1016 GError *error = NULL;
1017 gchar **argv;
1018 gchar **argv_copy;
1019 int argc;
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 },
1024 { 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);
1035 g_assert (retval);
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);
1044 g_free (argv);
1045 g_option_context_free (context);
1048 static void
1049 callback_test_optional_8 (void)
1051 GOptionContext *context;
1052 gboolean dummy;
1053 gboolean retval;
1054 GError *error = NULL;
1055 gchar **argv;
1056 gchar **argv_copy;
1057 int argc;
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 },
1062 { 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);
1073 g_assert (retval);
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);
1082 g_free (argv);
1083 g_option_context_free (context);
1086 static GPtrArray *callback_remaining_args;
1087 static gboolean
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));
1092 return TRUE;
1095 static void
1096 callback_remaining_test1 (void)
1098 GOptionContext *context;
1099 gboolean retval;
1100 GError *error = NULL;
1101 gchar **argv;
1102 gchar **argv_copy;
1103 int argc;
1104 GOptionEntry entries [] =
1105 { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL },
1106 { 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);
1118 g_assert (retval);
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);
1128 g_free (argv);
1129 g_option_context_free (context);
1132 static gboolean
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");
1137 return FALSE;
1140 static void
1141 callback_returns_false (void)
1143 GOptionContext *context;
1144 gboolean retval;
1145 GError *error = NULL;
1146 gchar **argv;
1147 gchar **argv_copy;
1148 int argc;
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 },
1153 { 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);
1170 g_free (argv);
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);
1187 g_free (argv);
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);
1204 g_free (argv);
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);
1221 g_free (argv);
1225 static void
1226 ignore_test1 (void)
1228 GOptionContext *context;
1229 gboolean retval;
1230 GError *error = NULL;
1231 gchar **argv, **argv_copy;
1232 int argc;
1233 gchar *arg;
1234 GOptionEntry entries [] =
1235 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1236 { 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);
1248 g_assert (retval);
1250 /* Check array */
1251 arg = join_stringv (argc, argv);
1252 g_assert (strcmp (arg, "program --hello") == 0);
1254 g_free (arg);
1255 g_strfreev (argv_copy);
1256 g_free (argv);
1257 g_option_context_free (context);
1260 static void
1261 ignore_test2 (void)
1263 GOptionContext *context;
1264 gboolean retval;
1265 GError *error = NULL;
1266 gchar **argv;
1267 gchar **argv_copy;
1268 int argc;
1269 gchar *arg;
1270 GOptionEntry entries [] =
1271 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
1272 { 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);
1284 g_assert (retval);
1286 /* Check array */
1287 arg = join_stringv (argc, argv);
1288 g_assert (strcmp (arg, "program -es") == 0);
1290 g_free (arg);
1291 g_strfreev (argv_copy);
1292 g_free (argv);
1293 g_option_context_free (context);
1296 static void
1297 ignore_test3 (void)
1299 GOptionContext *context;
1300 gboolean retval;
1301 GError *error = NULL;
1302 gchar **argv, **argv_copy;
1303 int argc;
1304 gchar *arg;
1305 GOptionEntry entries [] =
1306 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
1307 { 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);
1319 g_assert (retval);
1321 /* Check array */
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);
1328 g_free (arg);
1329 g_strfreev (argv_copy);
1330 g_free (argv);
1331 g_option_context_free (context);
1334 void
1335 static array_test1 (void)
1337 GOptionContext *context;
1338 gboolean retval;
1339 GError *error = NULL;
1340 gchar **argv;
1341 gchar **argv_copy;
1342 int argc;
1343 GOptionEntry entries [] =
1344 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1345 { 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);
1356 g_assert (retval);
1358 /* Check array */
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);
1366 g_free (argv);
1367 g_option_context_free (context);
1370 static void
1371 add_test1 (void)
1373 GOptionContext *context;
1375 GOptionEntry entries1 [] =
1376 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1377 { NULL } };
1378 GOptionEntry entries2 [] =
1379 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1380 { 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);
1389 static void
1390 empty_test2 (void)
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);
1400 static void
1401 empty_test3 (void)
1403 GOptionContext *context;
1404 gint argc;
1405 gchar **argv;
1407 argc = 0;
1408 argv = NULL;
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 */
1417 static void
1418 rest_test1 (void)
1420 GOptionContext *context;
1421 gboolean retval;
1422 GError *error = NULL;
1423 gchar **argv;
1424 gchar **argv_copy;
1425 int argc;
1426 GOptionEntry entries [] = {
1427 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1428 { 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);
1440 g_assert (retval);
1442 /* Check array */
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);
1450 g_free (argv);
1451 g_option_context_free (context);
1454 /* check that -- works */
1455 static void
1456 rest_test2 (void)
1458 GOptionContext *context;
1459 gboolean retval;
1460 GError *error = NULL;
1461 gchar **argv;
1462 gchar **argv_copy;
1463 int argc;
1464 GOptionEntry entries [] = {
1465 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1466 { 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);
1478 g_assert (retval);
1480 /* Check array */
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);
1489 g_free (argv);
1490 g_option_context_free (context);
1493 /* check that -- stripping works */
1494 static void
1495 rest_test2a (void)
1497 GOptionContext *context;
1498 gboolean retval;
1499 GError *error = NULL;
1500 gchar **argv;
1501 gchar **argv_copy;
1502 int argc;
1503 GOptionEntry entries [] = {
1504 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1505 { 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);
1517 g_assert (retval);
1519 /* Check array */
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);
1527 g_free (argv);
1528 g_option_context_free (context);
1531 static void
1532 rest_test2b (void)
1534 GOptionContext *context;
1535 gboolean retval;
1536 GError *error = NULL;
1537 gchar **argv;
1538 gchar **argv_copy;
1539 int argc;
1540 GOptionEntry entries [] = {
1541 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1542 { 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);
1555 g_assert (retval);
1557 /* Check array */
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);
1565 g_free (argv);
1566 g_option_context_free (context);
1569 static void
1570 rest_test2c (void)
1572 GOptionContext *context;
1573 gboolean retval;
1574 GError *error = NULL;
1575 gchar **argv;
1576 gchar **argv_copy;
1577 int argc;
1578 GOptionEntry entries [] = {
1579 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1580 { 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);
1592 g_assert (retval);
1594 /* Check array */
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);
1602 g_free (argv);
1603 g_option_context_free (context);
1606 static void
1607 rest_test2d (void)
1609 GOptionContext *context;
1610 gboolean retval;
1611 GError *error = NULL;
1612 gchar **argv;
1613 gchar **argv_copy;
1614 int argc;
1615 GOptionEntry entries [] = {
1616 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1617 { 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);
1629 g_assert (retval);
1631 /* Check array */
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);
1639 g_free (argv);
1640 g_option_context_free (context);
1644 /* check that G_OPTION_REMAINING collects non-option arguments */
1645 static void
1646 rest_test3 (void)
1648 GOptionContext *context;
1649 gboolean retval;
1650 GError *error = NULL;
1651 gchar **argv;
1652 gchar **argv_copy;
1653 int argc;
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 },
1657 { 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);
1669 g_assert (retval);
1671 /* Check array */
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);
1680 g_free (argv);
1681 g_option_context_free (context);
1685 /* check that G_OPTION_REMAINING and -- work together */
1686 static void
1687 rest_test4 (void)
1689 GOptionContext *context;
1690 gboolean retval;
1691 GError *error = NULL;
1692 gchar **argv;
1693 gchar **argv_copy;
1694 int argc;
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 },
1698 { 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);
1710 g_assert (retval);
1712 /* Check array */
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);
1721 g_free (argv);
1722 g_option_context_free (context);
1725 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1726 static void
1727 rest_test5 (void)
1729 GOptionContext *context;
1730 gboolean retval;
1731 GError *error = NULL;
1732 gchar **argv;
1733 gchar **argv_copy;
1734 int argc;
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 },
1738 { 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);
1750 g_assert (retval);
1752 /* Check array */
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);
1761 g_free (argv);
1762 g_option_context_free (context);
1765 static void
1766 unknown_short_test (void)
1768 GOptionContext *context;
1769 gboolean retval;
1770 GError *error = NULL;
1771 gchar **argv;
1772 gchar **argv_copy;
1773 int argc;
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);
1786 g_assert (!retval);
1787 g_assert (error != NULL);
1788 g_clear_error (&error);
1790 g_strfreev (argv_copy);
1791 g_free (argv);
1792 g_option_context_free (context);
1795 /* test that lone dashes are treated as non-options */
1796 static void
1797 lonely_dash_test (void)
1799 GOptionContext *context;
1800 gboolean retval;
1801 GError *error = NULL;
1802 gchar **argv;
1803 gchar **argv_copy;
1804 int argc;
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);
1816 g_assert (retval);
1818 g_assert (argv[1] && strcmp (argv[1], "-") == 0);
1820 g_strfreev (argv_copy);
1821 g_free (argv);
1822 g_option_context_free (context);
1825 /* test that three dashes are treated as non-options */
1826 static void
1827 triple_dash_test (void)
1829 GOptionContext *context;
1830 GOptionGroup *group;
1831 gboolean retval;
1832 GError *error = NULL;
1833 gchar **argv;
1834 gchar **argv_copy;
1835 int argc;
1836 gint arg1, arg2;
1837 GOptionEntry entries [] =
1838 { { "foo", 0, 0, G_OPTION_ARG_INT, &arg1, NULL, NULL},
1839 { NULL }
1841 GOptionEntry group_entries [] =
1842 { { "test", 0, 0, G_OPTION_ARG_INT, &arg2, NULL, NULL},
1843 { 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);
1865 g_free (argv);
1868 static void
1869 missing_arg_test (void)
1871 GOptionContext *context;
1872 gboolean retval;
1873 GError *error = NULL;
1874 gchar **argv;
1875 gchar **argv_copy;
1876 int argc;
1877 gchar *arg = NULL;
1878 GOptionEntry entries [] =
1879 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1880 { 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);
1899 g_free (argv);
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);
1913 g_free (argv);
1914 g_option_context_free (context);
1917 static gchar *test_arg;
1919 static gboolean cb (const gchar *option_name,
1920 const gchar *value,
1921 gpointer data,
1922 GError **error)
1924 test_arg = g_strdup (value);
1925 return TRUE;
1928 static void
1929 dash_arg_test (void)
1931 GOptionContext *context;
1932 gboolean retval;
1933 GError *error = NULL;
1934 gchar **argv;
1935 gchar **argv_copy;
1936 int argc;
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 },
1941 { 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);
1952 test_arg = NULL;
1953 error = NULL;
1954 retval = g_option_context_parse (context, &argc, &argv, &error);
1955 g_assert (retval);
1956 g_assert_no_error (error);
1957 g_assert_cmpstr (test_arg, ==, "-3");
1959 g_strfreev (argv_copy);
1960 g_free (argv);
1961 g_free (test_arg);
1962 test_arg = NULL;
1964 /* Try parsing again */
1965 argv = split_string ("program --test -3", &argc);
1966 argv_copy = copy_stringv (argv, argc);
1968 error = NULL;
1969 retval = g_option_context_parse (context, &argc, &argv, &error);
1970 g_assert_no_error (error);
1971 g_assert (retval);
1972 g_assert_cmpstr (test_arg, ==, NULL);
1974 g_option_context_free (context);
1975 g_strfreev (argv_copy);
1976 g_free (argv);
1979 static void
1980 test_basic (void)
1982 GOptionContext *context;
1983 gchar *arg = NULL;
1984 GOptionEntry entries [] =
1985 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1986 { 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);
2009 typedef struct {
2010 gboolean parameter_seen;
2011 gboolean summary_seen;
2012 gboolean description_seen;
2013 gboolean destroyed;
2014 } TranslateData;
2016 static const gchar *
2017 translate_func (const gchar *str,
2018 gpointer data)
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;
2029 return str;
2032 static void
2033 destroy_notify (gpointer data)
2035 TranslateData *d = data;
2037 d->destroyed = TRUE;
2040 static void
2041 test_translate (void)
2043 GOptionContext *context;
2044 gchar *arg = NULL;
2045 GOptionEntry entries [] =
2046 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2047 { NULL } };
2048 TranslateData data = { 0, };
2049 gchar *str;
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);
2059 g_free (str);
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);
2068 static void
2069 test_help (void)
2071 GOptionContext *context;
2072 GOptionGroup *group;
2073 gchar *str;
2074 gchar *arg = NULL;
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" },
2081 { NULL }
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 },
2086 { 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);
2118 g_free (str);
2120 g_option_context_free (context);
2123 static void
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" },
2130 { NULL }
2132 gchar *str;
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);
2143 g_free (str);
2144 g_option_context_free (context);
2147 static void
2148 test_help_no_help_options (void)
2150 GOptionContext *context;
2151 GOptionGroup *group;
2152 gchar *str;
2153 gchar *arg = NULL;
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" },
2160 { NULL }
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 },
2165 { 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);
2201 g_free (str);
2203 g_option_context_free (context);
2206 static void
2207 set_bool (gpointer data)
2209 gboolean *b = data;
2211 *b = TRUE;
2214 static void
2215 test_main_group (void)
2217 GOptionContext *context;
2218 GOptionGroup *group;
2219 gboolean b = FALSE;
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);
2234 g_assert (b);
2237 static gboolean error_func_called = FALSE;
2239 static void
2240 error_func (GOptionContext *context,
2241 GOptionGroup *group,
2242 gpointer data,
2243 GError **error)
2245 g_assert_cmpint (GPOINTER_TO_INT(data), ==, 1234);
2246 error_func_called = TRUE;
2249 static void
2250 test_error_hook (void)
2252 GOptionContext *context;
2253 gchar *arg = NULL;
2254 GOptionEntry entries [] =
2255 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2256 { NULL } };
2257 GOptionGroup *group;
2258 gchar **argv;
2259 gchar **argv_copy;
2260 gint argc;
2261 gboolean retval;
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),
2269 error_func);
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);
2284 g_free (argv);
2285 g_option_context_free (context);
2288 static void
2289 test_group_parse (void)
2291 GOptionContext *context;
2292 GOptionGroup *group;
2293 gchar *arg1 = NULL;
2294 gchar *arg2 = NULL;
2295 gchar *arg3 = NULL;
2296 gchar *arg4 = NULL;
2297 gchar *arg5 = NULL;
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 },
2301 { 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 },
2307 { NULL }
2309 gchar **argv, **orig_argv;
2310 gint argc;
2311 GError *error = NULL;
2312 gboolean retval;
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);
2326 g_assert (retval);
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");
2333 g_free (arg1);
2334 g_free (arg2);
2335 g_free (arg3);
2336 g_free (arg4);
2337 g_free (arg5);
2339 g_free (argv);
2340 g_strfreev (orig_argv);
2341 g_option_context_free (context);
2344 static gint
2345 option_context_parse_command_line (GOptionContext *context,
2346 const gchar *command_line)
2348 gchar **argv;
2349 guint argv_len, argv_new_len;
2350 gboolean success;
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);
2358 g_strfreev (argv);
2359 return success ? argv_len - argv_new_len : -1;
2362 static void
2363 test_strict_posix (void)
2365 GOptionContext *context;
2366 gboolean foo;
2367 gboolean bar;
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 },
2371 { NULL }
2373 gint n_parsed;
2375 context = g_option_context_new (NULL);
2376 g_option_context_add_main_entries (context, entries, NULL);
2378 foo = bar = FALSE;
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);
2385 foo = bar = FALSE;
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);
2392 foo = 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);
2399 foo = bar = FALSE;
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);
2409 static void
2410 flag_reverse_string (void)
2412 GOptionContext *context;
2413 gchar *arg = NULL;
2414 GOptionEntry entries [] =
2415 { { "test", 't', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_STRING, &arg, NULL, NULL },
2416 { NULL } };
2417 gchar **argv;
2418 gint argc;
2419 gboolean retval;
2420 GError *error = NULL;
2422 if (!g_test_undefined ())
2423 return;
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);
2437 g_strfreev (argv);
2438 g_option_context_free (context);
2439 g_free (arg);
2442 static void
2443 flag_optional_int (void)
2445 GOptionContext *context;
2446 gint arg = 0;
2447 GOptionEntry entries [] =
2448 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &arg, NULL, NULL },
2449 { NULL } };
2450 gchar **argv;
2451 gint argc;
2452 gboolean retval;
2453 GError *error = NULL;
2455 if (!g_test_undefined ())
2456 return;
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);
2470 g_strfreev (argv);
2471 g_option_context_free (context);
2474 static void
2475 short_remaining (void)
2477 gboolean ignore = FALSE;
2478 gboolean remaining = FALSE;
2479 gint number = 0;
2480 gchar* text = NULL;
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 },
2490 { NULL }
2492 GOptionContext* context;
2493 gchar **argv, **argv_copy;
2494 gint argc;
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);
2509 g_assert (ignore);
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);
2517 g_free (text);
2518 g_strfreev (files);
2519 g_strfreev (argv_copy);
2520 g_free (argv);
2521 g_option_context_free (context);
2524 static void
2525 double_free (void)
2527 gchar* text = NULL;
2528 GOptionEntry entries[] =
2530 { "known", 0, 0, G_OPTION_ARG_STRING, &text, NULL, NULL },
2531 { NULL }
2533 GOptionContext* context;
2534 gchar **argv;
2535 gint argc;
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);
2553 g_strfreev (argv);
2558 main (int argc,
2559 char *argv[])
2561 int i;
2562 gchar *test_name;
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);
2582 g_free (test_name);
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);
2586 g_free (test_name);
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);
2591 g_free (test_name);
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();