6 char *error_test2_string;
7 gboolean error_test3_boolean;
10 gchar *arg_test2_string;
12 gchar **array_test1_array;
14 gboolean ignore_test1_boolean;
15 gboolean ignore_test2_boolean;
16 gchar *ignore_test3_string;
19 split_string (const char *str, int *argc)
24 argv = g_strsplit (str, " ", 0);
26 for (len = 0; argv[len] != NULL; len++);
35 join_stringv (int argc, char **argv)
40 str = g_string_new (NULL);
42 for (i = 0; i < argc; i++)
44 g_string_append (str, argv[i]);
47 g_string_append_c (str, ' ');
50 return g_string_free (str, FALSE);
53 /* Performs a shallow copy */
55 copy_stringv (char **argv, int argc)
57 return g_memdup (argv, sizeof (char *) * (argc + 1));
62 error_test1_pre_parse (GOptionContext *context,
67 g_assert (error_test1_int == 0x12345678);
73 error_test1_post_parse (GOptionContext *context,
78 g_assert (error_test1_int == 20);
80 /* Set an error in the post hook */
81 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, NULL);
89 GOptionContext *context;
94 GOptionGroup *main_group;
95 GOptionEntry entries [] =
96 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
99 context = g_option_context_new (NULL);
100 g_option_context_add_main_entries (context, entries, NULL);
102 /* Set pre and post parse hooks */
103 main_group = g_option_context_get_main_group (context);
104 g_option_group_set_parse_hooks (main_group,
105 error_test1_pre_parse, error_test1_post_parse);
107 /* Now try parsing */
108 argv = split_string ("program --test 20", &argc);
110 retval = g_option_context_parse (context, &argc, &argv, &error);
111 g_assert (retval == FALSE);
113 /* On failure, values should be reset */
114 g_assert (error_test1_int == 0x12345678);
117 g_option_context_free (context);
122 error_test2_pre_parse (GOptionContext *context,
127 g_assert (strcmp (error_test2_string, "foo") == 0);
133 error_test2_post_parse (GOptionContext *context,
138 g_assert (strcmp (error_test2_string, "bar") == 0);
140 /* Set an error in the post hook */
141 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, NULL);
149 GOptionContext *context;
151 GError *error = NULL;
154 GOptionGroup *main_group;
155 GOptionEntry entries [] =
156 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
159 context = g_option_context_new (NULL);
160 g_option_context_add_main_entries (context, entries, NULL);
162 /* Set pre and post parse hooks */
163 main_group = g_option_context_get_main_group (context);
164 g_option_group_set_parse_hooks (main_group,
165 error_test2_pre_parse, error_test2_post_parse);
167 /* Now try parsing */
168 argv = split_string ("program --test bar", &argc);
169 retval = g_option_context_parse (context, &argc, &argv, &error);
171 g_error_free (error);
172 g_assert (retval == FALSE);
174 g_assert (strcmp (error_test2_string, "foo") == 0);
177 g_option_context_free (context);
181 error_test3_pre_parse (GOptionContext *context,
186 g_assert (!error_test3_boolean);
192 error_test3_post_parse (GOptionContext *context,
197 g_assert (error_test3_boolean);
199 /* Set an error in the post hook */
200 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, NULL);
208 GOptionContext *context;
210 GError *error = NULL;
213 GOptionGroup *main_group;
214 GOptionEntry entries [] =
215 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
218 context = g_option_context_new (NULL);
219 g_option_context_add_main_entries (context, entries, NULL);
221 /* Set pre and post parse hooks */
222 main_group = g_option_context_get_main_group (context);
223 g_option_group_set_parse_hooks (main_group,
224 error_test3_pre_parse, error_test3_post_parse);
226 /* Now try parsing */
227 argv = split_string ("program --test", &argc);
228 retval = g_option_context_parse (context, &argc, &argv, &error);
230 g_error_free (error);
231 g_assert (retval == FALSE);
233 g_assert (!error_test3_boolean);
236 g_option_context_free (context);
242 GOptionContext *context;
244 GError *error = NULL;
247 GOptionEntry entries [] =
248 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
251 context = g_option_context_new (NULL);
252 g_option_context_add_main_entries (context, entries, NULL);
254 /* Now try parsing */
255 argv = split_string ("program --test 20 --test 30", &argc);
257 retval = g_option_context_parse (context, &argc, &argv, &error);
260 /* Last arg specified is the one that should be stored */
261 g_assert (arg_test1_int == 30);
264 g_option_context_free (context);
270 GOptionContext *context;
272 GError *error = NULL;
275 GOptionEntry entries [] =
276 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
279 context = g_option_context_new (NULL);
280 g_option_context_add_main_entries (context, entries, NULL);
282 /* Now try parsing */
283 argv = split_string ("program --test foo --test bar", &argc);
285 retval = g_option_context_parse (context, &argc, &argv, &error);
288 /* Last arg specified is the one that should be stored */
289 g_assert (strcmp (arg_test2_string, "bar") == 0);
291 g_free (arg_test2_string);
294 g_option_context_free (context);
300 GOptionContext *context;
302 GError *error = NULL;
303 gchar **argv, **argv_copy;
306 GOptionEntry entries [] =
307 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
310 context = g_option_context_new (NULL);
311 g_option_context_set_ignore_unknown_options (context, TRUE);
312 g_option_context_add_main_entries (context, entries, NULL);
314 /* Now try parsing */
315 argv = split_string ("program --test --hello", &argc);
316 argv_copy = copy_stringv (argv, argc);
318 retval = g_option_context_parse (context, &argc, &argv, &error);
322 arg = join_stringv (argc, argv);
323 g_assert (strcmp (arg, "program --hello") == 0);
326 g_strfreev (argv_copy);
328 g_option_context_free (context);
334 GOptionContext *context;
336 GError *error = NULL;
340 GOptionEntry entries [] =
341 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
344 context = g_option_context_new (NULL);
345 g_option_context_set_ignore_unknown_options (context, TRUE);
346 g_option_context_add_main_entries (context, entries, NULL);
348 /* Now try parsing */
349 argv = split_string ("program -test", &argc);
351 retval = g_option_context_parse (context, &argc, &argv, &error);
355 arg = join_stringv (argc, argv);
356 g_assert (strcmp (arg, "program -es") == 0);
360 g_option_context_free (context);
366 GOptionContext *context;
368 GError *error = NULL;
369 gchar **argv, **argv_copy;
372 GOptionEntry entries [] =
373 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
376 context = g_option_context_new (NULL);
377 g_option_context_set_ignore_unknown_options (context, TRUE);
378 g_option_context_add_main_entries (context, entries, NULL);
380 /* Now try parsing */
381 argv = split_string ("program --test foo --hello", &argc);
382 argv_copy = copy_stringv (argv, argc);
384 retval = g_option_context_parse (context, &argc, &argv, &error);
388 arg = join_stringv (argc, argv);
389 g_assert (strcmp (arg, "program --hello") == 0);
391 g_assert (strcmp (ignore_test3_string, "foo") == 0);
392 g_free (ignore_test3_string);
395 g_strfreev (argv_copy);
399 argv = split_string ("program --test=foo --hello", &argc);
400 argv_copy = copy_stringv (argv, argc);
402 retval = g_option_context_parse (context, &argc, &argv, &error);
406 arg = join_stringv (argc, argv);
407 g_assert (strcmp (arg, "program --hello") == 0);
409 g_assert (strcmp (ignore_test3_string, "foo") == 0);
410 g_free (ignore_test3_string);
413 g_strfreev (argv_copy);
415 g_option_context_free (context);
421 GOptionContext *context;
423 GError *error = NULL;
426 GOptionEntry entries [] =
427 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
430 context = g_option_context_new (NULL);
431 g_option_context_add_main_entries (context, entries, NULL);
433 /* Now try parsing */
434 argv = split_string ("program --test foo --test bar", &argc);
436 retval = g_option_context_parse (context, &argc, &argv, &error);
440 g_assert (strcmp (array_test1_array[0], "foo") == 0);
441 g_assert (strcmp (array_test1_array[1], "bar") == 0);
442 g_assert (array_test1_array[2] == NULL);
444 g_strfreev (array_test1_array);
447 g_option_context_free (context);
453 GOptionContext *context;
455 GOptionEntry entries1 [] =
456 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
458 GOptionEntry entries2 [] =
459 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
462 context = g_option_context_new (NULL);
463 g_option_context_add_main_entries (context, entries1, NULL);
464 g_option_context_add_main_entries (context, entries2, NULL);
466 g_option_context_free (context);
472 GOptionContext *context;
473 GOptionEntry entries [] =
476 context = g_option_context_new (NULL);
478 g_option_context_add_main_entries (context, entries, NULL);
480 g_option_context_parse (context, NULL, NULL, NULL);
482 g_assert (strcmp (g_get_prgname (), "<unknown>") == 0);
484 g_option_context_free (context);
490 GOptionContext *context;
492 context = g_option_context_new (NULL);
493 g_option_context_parse (context, NULL, NULL, NULL);
495 g_option_context_free (context);
499 main (int argc, char **argv)
501 /* Test that restoration on failure works */
502 error_test1_int = 0x12345678;
504 error_test2_string = "foo";
506 error_test3_boolean = FALSE;
509 /* Test that special argument parsing works */
513 /* Test string arrays */
516 /* Test ignoring options */
523 /* Test parsing empty args */