Return an error if an option is missing its argument. (#305576, Björn
[platform/upstream/glib.git] / tests / option-test.c
1 #include <glib.h>
2 #include <string.h>
3
4 int error_test1_int;
5 char *error_test2_string;
6 gboolean error_test3_boolean;
7
8 int arg_test1_int;
9 gchar *arg_test2_string;
10 gchar *arg_test3_filename;
11
12 gchar **array_test1_array;
13
14 gboolean ignore_test1_boolean;
15 gboolean ignore_test2_boolean;
16 gchar *ignore_test3_string;
17
18 gchar **
19 split_string (const char *str, int *argc)
20 {
21   gchar **argv;
22   int len;
23   
24   argv = g_strsplit (str, " ", 0);
25
26   for (len = 0; argv[len] != NULL; len++);
27
28   if (argc)
29     *argc = len;
30     
31   return argv;
32 }
33
34 gchar *
35 join_stringv (int argc, char **argv)
36 {
37   int i;
38   GString *str;
39
40   str = g_string_new (NULL);
41
42   for (i = 0; i < argc; i++)
43     {
44       g_string_append (str, argv[i]);
45
46       if (i < argc - 1)
47         g_string_append_c (str, ' ');
48     }
49
50   return g_string_free (str, FALSE);
51 }
52
53 /* Performs a shallow copy */
54 char **
55 copy_stringv (char **argv, int argc)
56 {
57   return g_memdup (argv, sizeof (char *) * (argc + 1));
58 }
59
60
61 static gboolean
62 error_test1_pre_parse (GOptionContext *context,
63                        GOptionGroup   *group,
64                        gpointer        data,
65                        GError        **error)
66 {
67   g_assert (error_test1_int == 0x12345678);
68
69   return TRUE;
70 }
71
72 static gboolean
73 error_test1_post_parse (GOptionContext *context,
74                         GOptionGroup   *group,
75                         gpointer          data,
76                         GError        **error)
77 {
78   g_assert (error_test1_int == 20);
79
80   /* Set an error in the post hook */
81   g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
82
83   return FALSE;
84 }
85
86 void
87 error_test1 (void)
88 {
89   GOptionContext *context;
90   gboolean retval;
91   GError *error = NULL;
92   gchar **argv;
93   int argc;
94   GOptionGroup *main_group;
95   GOptionEntry entries [] =
96     { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
97       { NULL } };
98   
99   context = g_option_context_new (NULL);
100   g_option_context_add_main_entries (context, entries, NULL);
101
102   /* Set pre and post parse hooks */
103   main_group = g_option_context_get_main_group (context);
104   g_option_group_set_parse_hooks (main_group,
105                                   error_test1_pre_parse, error_test1_post_parse);
106   
107   /* Now try parsing */
108   argv = split_string ("program --test 20", &argc);
109
110   retval = g_option_context_parse (context, &argc, &argv, &error);
111   g_assert (retval == FALSE);
112
113   /* On failure, values should be reset */
114   g_assert (error_test1_int == 0x12345678);
115   
116   g_strfreev (argv);
117   g_option_context_free (context);
118   
119 }
120
121 static gboolean
122 error_test2_pre_parse (GOptionContext *context,
123                        GOptionGroup   *group,
124                        gpointer   data,
125                        GError        **error)
126 {
127   g_assert (strcmp (error_test2_string, "foo") == 0);
128
129   return TRUE;
130 }
131
132 static gboolean
133 error_test2_post_parse (GOptionContext *context,
134                         GOptionGroup   *group,
135                         gpointer          data,
136                         GError        **error)
137 {
138   g_assert (strcmp (error_test2_string, "bar") == 0);
139
140   /* Set an error in the post hook */
141   g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
142
143   return FALSE;
144 }
145
146 void
147 error_test2 (void)
148 {
149   GOptionContext *context;
150   gboolean retval;
151   GError *error = NULL;
152   gchar **argv;
153   int argc;
154   GOptionGroup *main_group;
155   GOptionEntry entries [] =
156     { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
157       { NULL } };
158
159   context = g_option_context_new (NULL);
160   g_option_context_add_main_entries (context, entries, NULL);
161
162   /* Set pre and post parse hooks */
163   main_group = g_option_context_get_main_group (context);
164   g_option_group_set_parse_hooks (main_group,
165                                   error_test2_pre_parse, error_test2_post_parse);
166   
167   /* Now try parsing */
168   argv = split_string ("program --test bar", &argc);
169   retval = g_option_context_parse (context, &argc, &argv, &error);
170
171   g_error_free (error);
172   g_assert (retval == FALSE);
173
174   g_assert (strcmp (error_test2_string, "foo") == 0);
175   
176   g_strfreev (argv);
177   g_option_context_free (context);
178 }
179
180 static gboolean
181 error_test3_pre_parse (GOptionContext *context,
182                        GOptionGroup   *group,
183                        gpointer   data,
184                        GError        **error)
185 {
186   g_assert (!error_test3_boolean);
187
188   return TRUE;
189 }
190
191 static gboolean
192 error_test3_post_parse (GOptionContext *context,
193                         GOptionGroup   *group,
194                         gpointer          data,
195                         GError        **error)
196 {
197   g_assert (error_test3_boolean);
198
199   /* Set an error in the post hook */
200   g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
201
202   return FALSE;
203 }
204
205 void
206 error_test3 (void)
207 {
208   GOptionContext *context;
209   gboolean retval;
210   GError *error = NULL;
211   gchar **argv;
212   int argc;
213   GOptionGroup *main_group;
214   GOptionEntry entries [] =
215     { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
216       { NULL } };
217
218   context = g_option_context_new (NULL);
219   g_option_context_add_main_entries (context, entries, NULL);
220
221   /* Set pre and post parse hooks */
222   main_group = g_option_context_get_main_group (context);
223   g_option_group_set_parse_hooks (main_group,
224                                   error_test3_pre_parse, error_test3_post_parse);
225   
226   /* Now try parsing */
227   argv = split_string ("program --test", &argc);
228   retval = g_option_context_parse (context, &argc, &argv, &error);
229
230   g_error_free (error);
231   g_assert (retval == FALSE);
232
233   g_assert (!error_test3_boolean);
234   
235   g_strfreev (argv);
236   g_option_context_free (context);
237 }
238
239 void
240 arg_test1 (void)
241 {
242   GOptionContext *context;
243   gboolean retval;
244   GError *error = NULL;
245   gchar **argv;
246   int argc;
247   GOptionEntry entries [] =
248     { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
249       { NULL } };
250
251   context = g_option_context_new (NULL);
252   g_option_context_add_main_entries (context, entries, NULL);
253
254   /* Now try parsing */
255   argv = split_string ("program --test 20 --test 30", &argc);
256
257   retval = g_option_context_parse (context, &argc, &argv, &error);
258   g_assert (retval);
259
260   /* Last arg specified is the one that should be stored */
261   g_assert (arg_test1_int == 30);
262
263   g_strfreev (argv);
264   g_option_context_free (context);
265 }
266
267 void
268 arg_test2 (void)
269 {
270   GOptionContext *context;
271   gboolean retval;
272   GError *error = NULL;
273   gchar **argv;
274   int argc;
275   GOptionEntry entries [] =
276     { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
277       { NULL } };
278   
279   context = g_option_context_new (NULL);
280   g_option_context_add_main_entries (context, entries, NULL);
281
282   /* Now try parsing */
283   argv = split_string ("program --test foo --test bar", &argc);
284   
285   retval = g_option_context_parse (context, &argc, &argv, &error);
286   g_assert (retval);
287
288   /* Last arg specified is the one that should be stored */
289   g_assert (strcmp (arg_test2_string, "bar") == 0);
290
291   g_free (arg_test2_string);
292   
293   g_strfreev (argv);
294   g_option_context_free (context);
295 }
296
297 void
298 arg_test3 (void)
299 {
300   GOptionContext *context;
301   gboolean retval;
302   GError *error = NULL;
303   gchar **argv;
304   int argc;
305   GOptionEntry entries [] =
306     { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
307       { NULL } };
308   
309   context = g_option_context_new (NULL);
310   g_option_context_add_main_entries (context, entries, NULL);
311
312   /* Now try parsing */
313   argv = split_string ("program --test foo.txt", &argc);
314   
315   retval = g_option_context_parse (context, &argc, &argv, &error);
316   g_assert (retval);
317
318   /* Last arg specified is the one that should be stored */
319   g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
320
321   g_free (arg_test3_filename);
322   
323   g_strfreev (argv);
324   g_option_context_free (context);
325 }
326
327 void
328 ignore_test1 (void)
329 {
330   GOptionContext *context;
331   gboolean retval;
332   GError *error = NULL;
333   gchar **argv, **argv_copy;
334   int argc;
335   gchar *arg;
336   GOptionEntry entries [] =
337     { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
338       { NULL } };
339
340   context = g_option_context_new (NULL);
341   g_option_context_set_ignore_unknown_options (context, TRUE);
342   g_option_context_add_main_entries (context, entries, NULL);
343
344   /* Now try parsing */
345   argv = split_string ("program --test --hello", &argc);
346   argv_copy = copy_stringv (argv, argc);
347   
348   retval = g_option_context_parse (context, &argc, &argv, &error);
349   g_assert (retval);
350
351   /* Check array */
352   arg = join_stringv (argc, argv);
353   g_assert (strcmp (arg, "program --hello") == 0);
354
355   g_free (arg);
356   g_strfreev (argv_copy);
357   g_free (argv);
358   g_option_context_free (context);
359 }
360
361 void
362 ignore_test2 (void)
363 {
364   GOptionContext *context;
365   gboolean retval;
366   GError *error = NULL;
367   gchar **argv;
368   int argc;
369   gchar *arg;
370   GOptionEntry entries [] =
371     { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
372       { NULL } };
373
374   context = g_option_context_new (NULL);
375   g_option_context_set_ignore_unknown_options (context, TRUE);
376   g_option_context_add_main_entries (context, entries, NULL);
377
378   /* Now try parsing */
379   argv = split_string ("program -test", &argc);
380   
381   retval = g_option_context_parse (context, &argc, &argv, &error);
382   g_assert (retval);
383
384   /* Check array */
385   arg = join_stringv (argc, argv);
386   g_assert (strcmp (arg, "program -es") == 0);
387
388   g_free (arg);
389   g_strfreev (argv);
390   g_option_context_free (context);
391 }
392
393 void
394 ignore_test3 (void)
395 {
396   GOptionContext *context;
397   gboolean retval;
398   GError *error = NULL;
399   gchar **argv, **argv_copy;
400   int argc;
401   gchar *arg;
402   GOptionEntry entries [] =
403     { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
404       { NULL } };
405
406   context = g_option_context_new (NULL);
407   g_option_context_set_ignore_unknown_options (context, TRUE);
408   g_option_context_add_main_entries (context, entries, NULL);
409
410   /* Now try parsing */
411   argv = split_string ("program --test foo --hello", &argc);
412   argv_copy = copy_stringv (argv, argc);
413   
414   retval = g_option_context_parse (context, &argc, &argv, &error);
415   g_assert (retval);
416
417   /* Check array */
418   arg = join_stringv (argc, argv);
419   g_assert (strcmp (arg, "program --hello") == 0);
420
421   g_assert (strcmp (ignore_test3_string, "foo") == 0);
422   g_free (ignore_test3_string);
423
424   g_free (arg);
425   g_strfreev (argv_copy);
426   g_free (argv);
427   g_option_context_free (context);
428 }
429
430 void
431 array_test1 (void)
432 {
433   GOptionContext *context;
434   gboolean retval;
435   GError *error = NULL;
436   gchar **argv;
437   int argc;
438   GOptionEntry entries [] =
439     { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
440       { NULL } };
441         
442   context = g_option_context_new (NULL);
443   g_option_context_add_main_entries (context, entries, NULL);
444
445   /* Now try parsing */
446   argv = split_string ("program --test foo --test bar", &argc);
447   
448   retval = g_option_context_parse (context, &argc, &argv, &error);
449   g_assert (retval);
450
451   /* Check array */
452   g_assert (strcmp (array_test1_array[0], "foo") == 0);
453   g_assert (strcmp (array_test1_array[1], "bar") == 0);
454   g_assert (array_test1_array[2] == NULL);
455
456   g_strfreev (array_test1_array);
457   
458   g_strfreev (argv);
459   g_option_context_free (context);
460 }
461
462 void
463 add_test1 (void)
464 {
465   GOptionContext *context;
466
467   GOptionEntry entries1 [] =
468     { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
469       { NULL } };
470   GOptionEntry entries2 [] =
471     { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
472       { NULL } };
473
474   context = g_option_context_new (NULL);
475   g_option_context_add_main_entries (context, entries1, NULL);
476   g_option_context_add_main_entries (context, entries2, NULL);
477
478   g_option_context_free (context);
479 }
480
481 void
482 empty_test1 (void)
483 {
484   GOptionContext *context;
485   GOptionEntry entries [] =
486     { { NULL } };
487
488   context = g_option_context_new (NULL);
489
490   g_option_context_add_main_entries (context, entries, NULL);
491   
492   g_option_context_parse (context, NULL, NULL, NULL);
493
494   g_assert (strcmp (g_get_prgname (), "<unknown>") == 0);
495   
496   g_option_context_free (context);
497 }
498
499 void
500 empty_test2 (void)
501 {
502   GOptionContext *context;
503
504   context = g_option_context_new (NULL);
505   g_option_context_parse (context, NULL, NULL, NULL);
506   
507   g_option_context_free (context);
508 }
509
510 void
511 empty_test3 (void)
512 {
513   GOptionContext *context;
514   gint argc;
515   gchar **argv;
516
517   argc = 0;
518   argv = NULL;
519
520   context = g_option_context_new (NULL);
521   g_option_context_parse (context, &argc, &argv, NULL);
522   
523   g_option_context_free (context);
524 }
525
526 /* check that non-option arguments are left in argv by default */
527 void
528 rest_test1 (void)
529 {
530   GOptionContext *context;
531   gboolean retval;
532   GError *error = NULL;
533   gchar **argv;
534   int argc;
535   GOptionEntry entries [] = { 
536       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
537       { NULL } 
538   };
539         
540   context = g_option_context_new (NULL);
541   g_option_context_add_main_entries (context, entries, NULL);
542
543   /* Now try parsing */
544   argv = split_string ("program foo --test bar", &argc);
545   
546   retval = g_option_context_parse (context, &argc, &argv, &error);
547   g_assert (retval);
548
549   /* Check array */
550   g_assert (ignore_test1_boolean);
551   g_assert (strcmp (argv[0], "program") == 0);
552   g_assert (strcmp (argv[1], "foo") == 0);
553   g_assert (strcmp (argv[2], "bar") == 0);
554   g_assert (argv[3] == NULL);
555
556   g_strfreev (argv);
557   g_option_context_free (context);
558 }
559
560 /* check that -- works */
561 void
562 rest_test2 (void)
563 {
564   GOptionContext *context;
565   gboolean retval;
566   GError *error = NULL;
567   gchar **argv;
568   int argc;
569   GOptionEntry entries [] = { 
570       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
571       { NULL } 
572   };
573         
574   context = g_option_context_new (NULL);
575   g_option_context_add_main_entries (context, entries, NULL);
576
577   /* Now try parsing */
578   argv = split_string ("program foo --test -- -bar", &argc);
579   
580   retval = g_option_context_parse (context, &argc, &argv, &error);
581   g_assert (retval);
582
583   /* Check array */
584   g_assert (ignore_test1_boolean);
585   g_assert (strcmp (argv[0], "program") == 0);
586   g_assert (strcmp (argv[1], "foo") == 0);
587   g_assert (strcmp (argv[2], "--") == 0);
588   g_assert (strcmp (argv[3], "-bar") == 0);
589   g_assert (argv[4] == NULL);
590
591   g_strfreev (argv);
592   g_option_context_free (context);
593 }
594
595 /* check that -- stripping works */
596 void
597 rest_test2a (void)
598 {
599   GOptionContext *context;
600   gboolean retval;
601   GError *error = NULL;
602   gchar **argv;
603   int argc;
604   GOptionEntry entries [] = { 
605       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
606       { NULL } 
607   };
608         
609   context = g_option_context_new (NULL);
610   g_option_context_add_main_entries (context, entries, NULL);
611
612   /* Now try parsing */
613   argv = split_string ("program foo --test -- bar", &argc);
614   
615   retval = g_option_context_parse (context, &argc, &argv, &error);
616   g_assert (retval);
617
618   /* Check array */
619   g_assert (ignore_test1_boolean);
620   g_assert (strcmp (argv[0], "program") == 0);
621   g_assert (strcmp (argv[1], "foo") == 0);
622   g_assert (strcmp (argv[2], "bar") == 0);
623   g_assert (argv[3] == NULL);
624
625   g_strfreev (argv);
626   g_option_context_free (context);
627 }
628
629 void
630 rest_test2b (void)
631 {
632   GOptionContext *context;
633   gboolean retval;
634   GError *error = NULL;
635   gchar **argv;
636   int argc;
637   GOptionEntry entries [] = { 
638       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
639       { NULL } 
640   };
641         
642   context = g_option_context_new (NULL);
643   g_option_context_set_ignore_unknown_options (context, TRUE);
644   g_option_context_add_main_entries (context, entries, NULL);
645
646   /* Now try parsing */
647   argv = split_string ("program foo --test -bar --", &argc);
648   
649   retval = g_option_context_parse (context, &argc, &argv, &error);
650   g_assert (retval);
651
652   /* Check array */
653   g_assert (ignore_test1_boolean);
654   g_assert (strcmp (argv[0], "program") == 0);
655   g_assert (strcmp (argv[1], "foo") == 0);
656   g_assert (strcmp (argv[2], "-bar") == 0);
657   g_assert (argv[3] == NULL);
658
659   g_strfreev (argv);
660   g_option_context_free (context);
661 }
662
663 void
664 rest_test2c (void)
665 {
666   GOptionContext *context;
667   gboolean retval;
668   GError *error = NULL;
669   gchar **argv;
670   int argc;
671   GOptionEntry entries [] = { 
672       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
673       { NULL } 
674   };
675         
676   context = g_option_context_new (NULL);
677   g_option_context_add_main_entries (context, entries, NULL);
678
679   /* Now try parsing */
680   argv = split_string ("program --test foo -- bar", &argc);
681   
682   retval = g_option_context_parse (context, &argc, &argv, &error);
683   g_assert (retval);
684
685   /* Check array */
686   g_assert (ignore_test1_boolean);
687   g_assert (strcmp (argv[0], "program") == 0);
688   g_assert (strcmp (argv[1], "foo") == 0);
689   g_assert (strcmp (argv[2], "bar") == 0);
690   g_assert (argv[3] == NULL);
691
692   g_strfreev (argv);
693   g_option_context_free (context);
694 }
695
696 void
697 rest_test2d (void)
698 {
699   GOptionContext *context;
700   gboolean retval;
701   GError *error = NULL;
702   gchar **argv;
703   int argc;
704   GOptionEntry entries [] = { 
705       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
706       { NULL } 
707   };
708         
709   context = g_option_context_new (NULL);
710   g_option_context_add_main_entries (context, entries, NULL);
711
712   /* Now try parsing */
713   argv = split_string ("program --test -- -bar", &argc);
714   
715   retval = g_option_context_parse (context, &argc, &argv, &error);
716   g_assert (retval);
717
718   /* Check array */
719   g_assert (ignore_test1_boolean);
720   g_assert (strcmp (argv[0], "program") == 0);
721   g_assert (strcmp (argv[1], "--") == 0);
722   g_assert (strcmp (argv[2], "-bar") == 0);
723   g_assert (argv[3] == NULL);
724
725   g_strfreev (argv);
726   g_option_context_free (context);
727 }
728
729
730 /* check that G_OPTION_REMAINING collects non-option arguments */
731 void
732 rest_test3 (void)
733 {
734   GOptionContext *context;
735   gboolean retval;
736   GError *error = NULL;
737   gchar **argv;
738   int argc;
739   GOptionEntry entries [] = { 
740       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
741       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
742       { NULL } 
743   };
744         
745   context = g_option_context_new (NULL);
746   g_option_context_add_main_entries (context, entries, NULL);
747
748   /* Now try parsing */
749   argv = split_string ("program foo --test bar", &argc);
750   
751   retval = g_option_context_parse (context, &argc, &argv, &error);
752   g_assert (retval);
753
754   /* Check array */
755   g_assert (ignore_test1_boolean);
756   g_assert (strcmp (array_test1_array[0], "foo") == 0);
757   g_assert (strcmp (array_test1_array[1], "bar") == 0);
758   g_assert (array_test1_array[2] == NULL);
759
760   g_strfreev (array_test1_array);
761   
762   g_strfreev (argv);
763   g_option_context_free (context);
764 }
765
766
767 /* check that G_OPTION_REMAINING and -- work together */
768 void
769 rest_test4 (void)
770 {
771   GOptionContext *context;
772   gboolean retval;
773   GError *error = NULL;
774   gchar **argv;
775   int argc;
776   GOptionEntry entries [] = { 
777       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
778       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
779       { NULL } 
780   };
781         
782   context = g_option_context_new (NULL);
783   g_option_context_add_main_entries (context, entries, NULL);
784
785   /* Now try parsing */
786   argv = split_string ("program foo --test -- -bar", &argc);
787   
788   retval = g_option_context_parse (context, &argc, &argv, &error);
789   g_assert (retval);
790
791   /* Check array */
792   g_assert (ignore_test1_boolean);
793   g_assert (strcmp (array_test1_array[0], "foo") == 0);
794   g_assert (strcmp (array_test1_array[1], "-bar") == 0);
795   g_assert (array_test1_array[2] == NULL);
796
797   g_strfreev (array_test1_array);
798   
799   g_strfreev (argv);
800   g_option_context_free (context);
801 }
802
803 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
804 void
805 rest_test5 (void)
806 {
807   GOptionContext *context;
808   gboolean retval;
809   GError *error = NULL;
810   gchar **argv;
811   int argc;
812   GOptionEntry entries [] = { 
813       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
814       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
815       { NULL } 
816   };
817         
818   context = g_option_context_new (NULL);
819   g_option_context_add_main_entries (context, entries, NULL);
820
821   /* Now try parsing */
822   argv = split_string ("program foo --test bar", &argc);
823   
824   retval = g_option_context_parse (context, &argc, &argv, &error);
825   g_assert (retval);
826
827   /* Check array */
828   g_assert (ignore_test1_boolean);
829   g_assert (strcmp (array_test1_array[0], "foo") == 0);
830   g_assert (strcmp (array_test1_array[1], "bar") == 0);
831   g_assert (array_test1_array[2] == NULL);
832
833   g_strfreev (array_test1_array);
834   
835   g_strfreev (argv);
836   g_option_context_free (context);
837 }
838
839 void
840 unknown_short_test (void)
841 {
842   GOptionContext *context;
843   gboolean retval;
844   GError *error = NULL;
845   gchar **argv;
846   int argc;
847   GOptionEntry entries [] = { { NULL } };
848
849   context = g_option_context_new (NULL);
850   g_option_context_add_main_entries (context, entries, NULL);
851
852   /* Now try parsing */
853   argv = split_string ("program -0", &argc);
854
855   retval = g_option_context_parse (context, &argc, &argv, &error);
856   g_assert (!retval);
857
858   g_strfreev (argv);
859   g_option_context_free (context);
860 }
861
862 /* test that lone dashes are treated as non-options */
863 void lonely_dash_test (void)
864 {
865   GOptionContext *context;
866   gboolean retval;
867   GError *error = NULL;
868   gchar **argv;
869   int argc;
870
871   context = g_option_context_new (NULL);
872
873   /* Now try parsing */
874   argv = split_string ("program -", &argc);
875
876   retval = g_option_context_parse (context, &argc, &argv, &error);
877
878   g_assert (retval);
879
880   g_assert (argv[1] && strcmp (argv[1], "-") == 0);
881
882   g_strfreev (argv);
883   g_option_context_free (context);
884 }
885
886 void
887 missing_arg_test (void)
888 {
889   GOptionContext *context;
890   gboolean retval;
891   GError *error = NULL;
892   gchar **argv;
893   int argc;
894   gchar *arg = NULL;
895   GOptionEntry entries [] =
896     { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
897       { NULL } };
898
899   context = g_option_context_new (NULL);
900   g_option_context_add_main_entries (context, entries, NULL);
901
902   /* Now try parsing */
903   argv = split_string ("program --test", &argc);
904
905   retval = g_option_context_parse (context, &argc, &argv, &error);
906   g_assert (retval == FALSE);
907   g_clear_error (&error);
908
909   g_strfreev (argv);
910
911   /* Try parsing again */
912   argv = split_string ("program --t", &argc);
913
914   retval = g_option_context_parse (context, &argc, &argv, &error);
915   g_assert (retval == FALSE);
916
917   g_strfreev (argv);
918   g_option_context_free (context);
919 }
920
921 int
922 main (int argc, char **argv)
923 {
924   /* Test that restoration on failure works */
925   error_test1_int = 0x12345678;
926   error_test1 ();
927   error_test2_string = "foo";
928   error_test2 ();
929   error_test3_boolean = FALSE;
930   error_test3 ();
931   
932   /* Test that special argument parsing works */
933   arg_test1 ();
934   arg_test2 ();
935   arg_test3 ();
936
937   /* Test string arrays */
938   array_test1 ();
939
940   /* Test ignoring options */
941   ignore_test1 ();
942   ignore_test2 ();
943   ignore_test3 ();
944
945   add_test1 ();
946
947   /* Test parsing empty args */
948   empty_test1 ();
949   empty_test2 ();
950   empty_test3 ();
951
952   /* Test handling of rest args */
953   rest_test1 ();
954   rest_test2 ();
955   rest_test2a ();
956   rest_test2b ();
957   rest_test2c ();
958   rest_test2d ();
959   rest_test3 ();
960   rest_test4 ();
961   rest_test5 ();
962
963   /* test for bug 166609 */
964   unknown_short_test ();
965
966   /* test for bug 168008 */
967   lonely_dash_test ();
968
969   /* test for bug 305576 */
970   missing_arg_test ();
971
972   return 0;
973 }