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 == G_GINT64_CONSTANT(4294967296));
436
g_assert (arg_test6_int64_2 == G_GINT64_CONSTANT(0xfffffffff));
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);
791
static GPtrArray *callback_remaining_args;
793
callback_remaining_test1_callback (const gchar *option_name, const gchar *value,
794
gpointer data, GError **error)
796
g_ptr_array_add (callback_remaining_args, g_strdup (value));
801
callback_remaining_test1 (void)
803
GOptionContext *context;
805
GError *error = NULL;
808
GOptionEntry entries [] =
809
{ { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL },
812
callback_remaining_args = g_ptr_array_new ();
813
context = g_option_context_new (NULL);
814
g_option_context_add_main_entries (context, entries, NULL);
816
/* Now try parsing */
817
argv = split_string ("program foo.txt blah.txt", &argc);
819
retval = g_option_context_parse (context, &argc, &argv, &error);
822
g_assert (callback_remaining_args->len == 2);
823
g_assert (strcmp (callback_remaining_args->pdata[0], "foo.txt") == 0);
824
g_assert (strcmp (callback_remaining_args->pdata[1], "blah.txt") == 0);
826
g_ptr_array_foreach (callback_remaining_args, (GFunc) g_free, NULL);
827
g_ptr_array_free (callback_remaining_args, TRUE);
830
g_option_context_free (context);
836
GOptionContext *context;
838
GError *error = NULL;
839
gchar **argv, **argv_copy;
842
GOptionEntry entries [] =
843
{ { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
846
context = g_option_context_new (NULL);
847
g_option_context_set_ignore_unknown_options (context, TRUE);
848
g_option_context_add_main_entries (context, entries, NULL);
850
/* Now try parsing */
851
argv = split_string ("program --test --hello", &argc);
852
argv_copy = copy_stringv (argv, argc);
854
retval = g_option_context_parse (context, &argc, &argv, &error);
858
arg = join_stringv (argc, argv);
859
g_assert (strcmp (arg, "program --hello") == 0);
862
g_strfreev (argv_copy);
864
g_option_context_free (context);
870
GOptionContext *context;
872
GError *error = NULL;
876
GOptionEntry entries [] =
877
{ { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
880
context = g_option_context_new (NULL);
881
g_option_context_set_ignore_unknown_options (context, TRUE);
882
g_option_context_add_main_entries (context, entries, NULL);
884
/* Now try parsing */
885
argv = split_string ("program -test", &argc);
887
retval = g_option_context_parse (context, &argc, &argv, &error);
891
arg = join_stringv (argc, argv);
892
g_assert (strcmp (arg, "program -es") == 0);
896
g_option_context_free (context);
902
GOptionContext *context;
904
GError *error = NULL;
905
gchar **argv, **argv_copy;
908
GOptionEntry entries [] =
909
{ { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
912
context = g_option_context_new (NULL);
913
g_option_context_set_ignore_unknown_options (context, TRUE);
914
g_option_context_add_main_entries (context, entries, NULL);
916
/* Now try parsing */
917
argv = split_string ("program --test foo --hello", &argc);
918
argv_copy = copy_stringv (argv, argc);
920
retval = g_option_context_parse (context, &argc, &argv, &error);
924
arg = join_stringv (argc, argv);
925
g_assert (strcmp (arg, "program --hello") == 0);
927
g_assert (strcmp (ignore_test3_string, "foo") == 0);
928
g_free (ignore_test3_string);
931
g_strfreev (argv_copy);
933
g_option_context_free (context);
939
GOptionContext *context;
941
GError *error = NULL;
944
GOptionEntry entries [] =
945
{ { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
948
context = g_option_context_new (NULL);
949
g_option_context_add_main_entries (context, entries, NULL);
951
/* Now try parsing */
952
argv = split_string ("program --test foo --test bar", &argc);
954
retval = g_option_context_parse (context, &argc, &argv, &error);
958
g_assert (strcmp (array_test1_array[0], "foo") == 0);
959
g_assert (strcmp (array_test1_array[1], "bar") == 0);
960
g_assert (array_test1_array[2] == NULL);
962
g_strfreev (array_test1_array);
965
g_option_context_free (context);
971
GOptionContext *context;
973
GOptionEntry entries1 [] =
974
{ { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
976
GOptionEntry entries2 [] =
977
{ { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
980
context = g_option_context_new (NULL);
981
g_option_context_add_main_entries (context, entries1, NULL);
982
g_option_context_add_main_entries (context, entries2, NULL);
984
g_option_context_free (context);
990
GOptionContext *context;
991
GOptionEntry entries [] =
995
g_set_prgname (NULL);
996
context = g_option_context_new (NULL);
998
g_option_context_add_main_entries (context, entries, NULL);
1000
g_option_context_parse (context, NULL, NULL, NULL);
1002
prgname = g_get_prgname ();
1003
g_assert (prgname && strcmp (prgname, "<unknown>") == 0);
1005
g_option_context_free (context);
1011
GOptionContext *context;
1013
context = g_option_context_new (NULL);
1014
g_option_context_parse (context, NULL, NULL, NULL);
1016
g_option_context_free (context);
1022
GOptionContext *context;
1029
context = g_option_context_new (NULL);
1030
g_option_context_parse (context, &argc, &argv, NULL);
1032
g_option_context_free (context);
1035
/* check that non-option arguments are left in argv by default */
1039
GOptionContext *context;
1041
GError *error = NULL;
1044
GOptionEntry entries [] = {
1045
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1049
context = g_option_context_new (NULL);
1050
g_option_context_add_main_entries (context, entries, NULL);
1052
/* Now try parsing */
1053
argv = split_string ("program foo --test bar", &argc);
1055
retval = g_option_context_parse (context, &argc, &argv, &error);
1059
g_assert (ignore_test1_boolean);
1060
g_assert (strcmp (argv[0], "program") == 0);
1061
g_assert (strcmp (argv[1], "foo") == 0);
1062
g_assert (strcmp (argv[2], "bar") == 0);
1063
g_assert (argv[3] == NULL);
1066
g_option_context_free (context);
1069
/* check that -- works */
1073
GOptionContext *context;
1075
GError *error = NULL;
1078
GOptionEntry entries [] = {
1079
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1083
context = g_option_context_new (NULL);
1084
g_option_context_add_main_entries (context, entries, NULL);
1086
/* Now try parsing */
1087
argv = split_string ("program foo --test -- -bar", &argc);
1089
retval = g_option_context_parse (context, &argc, &argv, &error);
1093
g_assert (ignore_test1_boolean);
1094
g_assert (strcmp (argv[0], "program") == 0);
1095
g_assert (strcmp (argv[1], "foo") == 0);
1096
g_assert (strcmp (argv[2], "--") == 0);
1097
g_assert (strcmp (argv[3], "-bar") == 0);
1098
g_assert (argv[4] == NULL);
1101
g_option_context_free (context);
1104
/* check that -- stripping works */
1108
GOptionContext *context;
1110
GError *error = NULL;
1113
GOptionEntry entries [] = {
1114
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1118
context = g_option_context_new (NULL);
1119
g_option_context_add_main_entries (context, entries, NULL);
1121
/* Now try parsing */
1122
argv = split_string ("program foo --test -- bar", &argc);
1124
retval = g_option_context_parse (context, &argc, &argv, &error);
1128
g_assert (ignore_test1_boolean);
1129
g_assert (strcmp (argv[0], "program") == 0);
1130
g_assert (strcmp (argv[1], "foo") == 0);
1131
g_assert (strcmp (argv[2], "bar") == 0);
1132
g_assert (argv[3] == NULL);
1135
g_option_context_free (context);
1141
GOptionContext *context;
1143
GError *error = NULL;
1146
GOptionEntry entries [] = {
1147
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1151
context = g_option_context_new (NULL);
1152
g_option_context_set_ignore_unknown_options (context, TRUE);
1153
g_option_context_add_main_entries (context, entries, NULL);
1155
/* Now try parsing */
1156
argv = split_string ("program foo --test -bar --", &argc);
1158
retval = g_option_context_parse (context, &argc, &argv, &error);
1162
g_assert (ignore_test1_boolean);
1163
g_assert (strcmp (argv[0], "program") == 0);
1164
g_assert (strcmp (argv[1], "foo") == 0);
1165
g_assert (strcmp (argv[2], "-bar") == 0);
1166
g_assert (argv[3] == NULL);
1169
g_option_context_free (context);
1175
GOptionContext *context;
1177
GError *error = NULL;
1180
GOptionEntry entries [] = {
1181
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1185
context = g_option_context_new (NULL);
1186
g_option_context_add_main_entries (context, entries, NULL);
1188
/* Now try parsing */
1189
argv = split_string ("program --test foo -- bar", &argc);
1191
retval = g_option_context_parse (context, &argc, &argv, &error);
1195
g_assert (ignore_test1_boolean);
1196
g_assert (strcmp (argv[0], "program") == 0);
1197
g_assert (strcmp (argv[1], "foo") == 0);
1198
g_assert (strcmp (argv[2], "bar") == 0);
1199
g_assert (argv[3] == NULL);
1202
g_option_context_free (context);
1208
GOptionContext *context;
1210
GError *error = NULL;
1213
GOptionEntry entries [] = {
1214
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1218
context = g_option_context_new (NULL);
1219
g_option_context_add_main_entries (context, entries, NULL);
1221
/* Now try parsing */
1222
argv = split_string ("program --test -- -bar", &argc);
1224
retval = g_option_context_parse (context, &argc, &argv, &error);
1228
g_assert (ignore_test1_boolean);
1229
g_assert (strcmp (argv[0], "program") == 0);
1230
g_assert (strcmp (argv[1], "--") == 0);
1231
g_assert (strcmp (argv[2], "-bar") == 0);
1232
g_assert (argv[3] == NULL);
1235
g_option_context_free (context);
1239
/* check that G_OPTION_REMAINING collects non-option arguments */
1243
GOptionContext *context;
1245
GError *error = NULL;
1248
GOptionEntry entries [] = {
1249
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1250
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1254
context = g_option_context_new (NULL);
1255
g_option_context_add_main_entries (context, entries, NULL);
1257
/* Now try parsing */
1258
argv = split_string ("program foo --test bar", &argc);
1260
retval = g_option_context_parse (context, &argc, &argv, &error);
1264
g_assert (ignore_test1_boolean);
1265
g_assert (strcmp (array_test1_array[0], "foo") == 0);
1266
g_assert (strcmp (array_test1_array[1], "bar") == 0);
1267
g_assert (array_test1_array[2] == NULL);
1269
g_strfreev (array_test1_array);
1272
g_option_context_free (context);
1276
/* check that G_OPTION_REMAINING and -- work together */
1280
GOptionContext *context;
1282
GError *error = NULL;
1285
GOptionEntry entries [] = {
1286
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1287
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1291
context = g_option_context_new (NULL);
1292
g_option_context_add_main_entries (context, entries, NULL);
1294
/* Now try parsing */
1295
argv = split_string ("program foo --test -- -bar", &argc);
1297
retval = g_option_context_parse (context, &argc, &argv, &error);
1301
g_assert (ignore_test1_boolean);
1302
g_assert (strcmp (array_test1_array[0], "foo") == 0);
1303
g_assert (strcmp (array_test1_array[1], "-bar") == 0);
1304
g_assert (array_test1_array[2] == NULL);
1306
g_strfreev (array_test1_array);
1309
g_option_context_free (context);
1312
/* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1316
GOptionContext *context;
1318
GError *error = NULL;
1321
GOptionEntry entries [] = {
1322
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1323
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
1327
context = g_option_context_new (NULL);
1328
g_option_context_add_main_entries (context, entries, NULL);
1330
/* Now try parsing */
1331
argv = split_string ("program foo --test bar", &argc);
1333
retval = g_option_context_parse (context, &argc, &argv, &error);
1337
g_assert (ignore_test1_boolean);
1338
g_assert (strcmp (array_test1_array[0], "foo") == 0);
1339
g_assert (strcmp (array_test1_array[1], "bar") == 0);
1340
g_assert (array_test1_array[2] == NULL);
1342
g_strfreev (array_test1_array);
1345
g_option_context_free (context);
1349
unknown_short_test (void)
1351
GOptionContext *context;
1353
GError *error = NULL;
1356
GOptionEntry entries [] = { { NULL } };
1358
context = g_option_context_new (NULL);
1359
g_option_context_add_main_entries (context, entries, NULL);
1361
/* Now try parsing */
1362
argv = split_string ("program -0", &argc);
1364
retval = g_option_context_parse (context, &argc, &argv, &error);
1368
g_option_context_free (context);
1371
/* test that lone dashes are treated as non-options */
1372
void lonely_dash_test (void)
1374
GOptionContext *context;
1376
GError *error = NULL;
1380
context = g_option_context_new (NULL);
1382
/* Now try parsing */
1383
argv = split_string ("program -", &argc);
1385
retval = g_option_context_parse (context, &argc, &argv, &error);
1389
g_assert (argv[1] && strcmp (argv[1], "-") == 0);
1392
g_option_context_free (context);
1396
missing_arg_test (void)
1398
GOptionContext *context;
1400
GError *error = NULL;
1404
GOptionEntry entries [] =
1405
{ { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1408
context = g_option_context_new (NULL);
1409
g_option_context_add_main_entries (context, entries, NULL);
1411
/* Now try parsing */
1412
argv = split_string ("program --test", &argc);
1414
retval = g_option_context_parse (context, &argc, &argv, &error);
1415
g_assert (retval == FALSE);
1416
g_clear_error (&error);
1420
/* Try parsing again */
1421
argv = split_string ("program --t", &argc);
1423
retval = g_option_context_parse (context, &argc, &argv, &error);
1424
g_assert (retval == FALSE);
1427
g_option_context_free (context);
1431
main (int argc, char **argv)
1433
/* Test that restoration on failure works */
1434
error_test1_int = 0x12345678;
1436
error_test2_string = "foo";
1438
error_test3_boolean = FALSE;
1441
/* Test that special argument parsing works */
1449
/* Test string arrays */
1452
/* Test callback args */
1456
/* Test optional arg flag for callback */
1457
callback_test_optional_1 ();
1458
callback_test_optional_2 ();
1459
callback_test_optional_3 ();
1460
callback_test_optional_4 ();
1461
callback_test_optional_5 ();
1462
callback_test_optional_6 ();
1463
callback_test_optional_7 ();
1464
callback_test_optional_8 ();
1466
/* Test callback with G_OPTION_REMAINING */
1467
callback_remaining_test1 ();
1469
/* Test ignoring options */
1476
/* Test parsing empty args */
1481
/* Test handling of rest args */
1492
/* test for bug 166609 */
1493
unknown_short_test ();
1495
/* test for bug 168008 */
1496
lonely_dash_test ();
1498
/* test for bug 305576 */
1499
missing_arg_test ();