6 char *error_test2_string;
7 gboolean error_test3_boolean;
10 gchar *arg_test2_string;
11 gchar *arg_test3_filename;
13 gchar **array_test1_array;
15 gboolean ignore_test1_boolean;
16 gboolean ignore_test2_boolean;
17 gchar *ignore_test3_string;
20 split_string (const char *str, int *argc)
25 argv = g_strsplit (str, " ", 0);
27 for (len = 0; argv[len] != NULL; len++);
36 join_stringv (int argc, char **argv)
41 str = g_string_new (NULL);
43 for (i = 0; i < argc; i++)
45 g_string_append (str, argv[i]);
48 g_string_append_c (str, ' ');
51 return g_string_free (str, FALSE);
54 /* Performs a shallow copy */
56 copy_stringv (char **argv, int argc)
58 return g_memdup (argv, sizeof (char *) * (argc + 1));
63 error_test1_pre_parse (GOptionContext *context,
68 g_assert (error_test1_int == 0x12345678);
74 error_test1_post_parse (GOptionContext *context,
79 g_assert (error_test1_int == 20);
81 /* Set an error in the post hook */
82 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
90 GOptionContext *context;
95 GOptionGroup *main_group;
96 GOptionEntry entries [] =
97 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
100 context = g_option_context_new (NULL);
101 g_option_context_add_main_entries (context, entries, NULL);
103 /* Set pre and post parse hooks */
104 main_group = g_option_context_get_main_group (context);
105 g_option_group_set_parse_hooks (main_group,
106 error_test1_pre_parse, error_test1_post_parse);
108 /* Now try parsing */
109 argv = split_string ("program --test 20", &argc);
111 retval = g_option_context_parse (context, &argc, &argv, &error);
112 g_assert (retval == FALSE);
114 /* On failure, values should be reset */
115 g_assert (error_test1_int == 0x12345678);
118 g_option_context_free (context);
123 error_test2_pre_parse (GOptionContext *context,
128 g_assert (strcmp (error_test2_string, "foo") == 0);
134 error_test2_post_parse (GOptionContext *context,
139 g_assert (strcmp (error_test2_string, "bar") == 0);
141 /* Set an error in the post hook */
142 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
150 GOptionContext *context;
152 GError *error = NULL;
155 GOptionGroup *main_group;
156 GOptionEntry entries [] =
157 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
160 context = g_option_context_new (NULL);
161 g_option_context_add_main_entries (context, entries, NULL);
163 /* Set pre and post parse hooks */
164 main_group = g_option_context_get_main_group (context);
165 g_option_group_set_parse_hooks (main_group,
166 error_test2_pre_parse, error_test2_post_parse);
168 /* Now try parsing */
169 argv = split_string ("program --test bar", &argc);
170 retval = g_option_context_parse (context, &argc, &argv, &error);
172 g_error_free (error);
173 g_assert (retval == FALSE);
175 g_assert (strcmp (error_test2_string, "foo") == 0);
178 g_option_context_free (context);
182 error_test3_pre_parse (GOptionContext *context,
187 g_assert (!error_test3_boolean);
193 error_test3_post_parse (GOptionContext *context,
198 g_assert (error_test3_boolean);
200 /* Set an error in the post hook */
201 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
209 GOptionContext *context;
211 GError *error = NULL;
214 GOptionGroup *main_group;
215 GOptionEntry entries [] =
216 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
219 context = g_option_context_new (NULL);
220 g_option_context_add_main_entries (context, entries, NULL);
222 /* Set pre and post parse hooks */
223 main_group = g_option_context_get_main_group (context);
224 g_option_group_set_parse_hooks (main_group,
225 error_test3_pre_parse, error_test3_post_parse);
227 /* Now try parsing */
228 argv = split_string ("program --test", &argc);
229 retval = g_option_context_parse (context, &argc, &argv, &error);
231 g_error_free (error);
232 g_assert (retval == FALSE);
234 g_assert (!error_test3_boolean);
237 g_option_context_free (context);
243 GOptionContext *context;
245 GError *error = NULL;
248 GOptionEntry entries [] =
249 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
252 context = g_option_context_new (NULL);
253 g_option_context_add_main_entries (context, entries, NULL);
255 /* Now try parsing */
256 argv = split_string ("program --test 20 --test 30", &argc);
258 retval = g_option_context_parse (context, &argc, &argv, &error);
261 /* Last arg specified is the one that should be stored */
262 g_assert (arg_test1_int == 30);
265 g_option_context_free (context);
271 GOptionContext *context;
273 GError *error = NULL;
276 GOptionEntry entries [] =
277 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
280 context = g_option_context_new (NULL);
281 g_option_context_add_main_entries (context, entries, NULL);
283 /* Now try parsing */
284 argv = split_string ("program --test foo --test bar", &argc);
286 retval = g_option_context_parse (context, &argc, &argv, &error);
289 /* Last arg specified is the one that should be stored */
290 g_assert (strcmp (arg_test2_string, "bar") == 0);
292 g_free (arg_test2_string);
295 g_option_context_free (context);
301 GOptionContext *context;
303 GError *error = NULL;
306 GOptionEntry entries [] =
307 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
310 context = g_option_context_new (NULL);
311 g_option_context_add_main_entries (context, entries, NULL);
313 /* Now try parsing */
314 argv = split_string ("program --test foo.txt", &argc);
316 retval = g_option_context_parse (context, &argc, &argv, &error);
319 /* Last arg specified is the one that should be stored */
320 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
322 g_free (arg_test3_filename);
325 g_option_context_free (context);
331 GOptionContext *context;
333 GError *error = NULL;
334 gchar **argv, **argv_copy;
337 GOptionEntry entries [] =
338 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
341 context = g_option_context_new (NULL);
342 g_option_context_set_ignore_unknown_options (context, TRUE);
343 g_option_context_add_main_entries (context, entries, NULL);
345 /* Now try parsing */
346 argv = split_string ("program --test --hello", &argc);
347 argv_copy = copy_stringv (argv, argc);
349 retval = g_option_context_parse (context, &argc, &argv, &error);
353 arg = join_stringv (argc, argv);
354 g_assert (strcmp (arg, "program --hello") == 0);
357 g_strfreev (argv_copy);
359 g_option_context_free (context);
365 GOptionContext *context;
367 GError *error = NULL;
371 GOptionEntry entries [] =
372 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
375 context = g_option_context_new (NULL);
376 g_option_context_set_ignore_unknown_options (context, TRUE);
377 g_option_context_add_main_entries (context, entries, NULL);
379 /* Now try parsing */
380 argv = split_string ("program -test", &argc);
382 retval = g_option_context_parse (context, &argc, &argv, &error);
386 arg = join_stringv (argc, argv);
387 g_assert (strcmp (arg, "program -es") == 0);
391 g_option_context_free (context);
397 GOptionContext *context;
399 GError *error = NULL;
400 gchar **argv, **argv_copy;
403 GOptionEntry entries [] =
404 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
407 context = g_option_context_new (NULL);
408 g_option_context_set_ignore_unknown_options (context, TRUE);
409 g_option_context_add_main_entries (context, entries, NULL);
411 /* Now try parsing */
412 argv = split_string ("program --test foo --hello", &argc);
413 argv_copy = copy_stringv (argv, argc);
415 retval = g_option_context_parse (context, &argc, &argv, &error);
419 arg = join_stringv (argc, argv);
420 g_assert (strcmp (arg, "program --hello") == 0);
422 g_assert (strcmp (ignore_test3_string, "foo") == 0);
423 g_free (ignore_test3_string);
426 g_strfreev (argv_copy);
428 g_option_context_free (context);
434 GOptionContext *context;
436 GError *error = NULL;
439 GOptionEntry entries [] =
440 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
443 context = g_option_context_new (NULL);
444 g_option_context_add_main_entries (context, entries, NULL);
446 /* Now try parsing */
447 argv = split_string ("program --test foo --test bar", &argc);
449 retval = g_option_context_parse (context, &argc, &argv, &error);
453 g_assert (strcmp (array_test1_array[0], "foo") == 0);
454 g_assert (strcmp (array_test1_array[1], "bar") == 0);
455 g_assert (array_test1_array[2] == NULL);
457 g_strfreev (array_test1_array);
460 g_option_context_free (context);
466 GOptionContext *context;
468 GOptionEntry entries1 [] =
469 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
471 GOptionEntry entries2 [] =
472 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
475 context = g_option_context_new (NULL);
476 g_option_context_add_main_entries (context, entries1, NULL);
477 g_option_context_add_main_entries (context, entries2, NULL);
479 g_option_context_free (context);
485 GOptionContext *context;
486 GOptionEntry entries [] =
489 context = g_option_context_new (NULL);
491 g_option_context_add_main_entries (context, entries, NULL);
493 g_option_context_parse (context, NULL, NULL, NULL);
495 g_assert (strcmp (g_get_prgname (), "<unknown>") == 0);
497 g_option_context_free (context);
503 GOptionContext *context;
505 context = g_option_context_new (NULL);
506 g_option_context_parse (context, NULL, NULL, NULL);
508 g_option_context_free (context);
514 GOptionContext *context;
521 context = g_option_context_new (NULL);
522 g_option_context_parse (context, &argc, &argv, NULL);
524 g_option_context_free (context);
528 main (int argc, char **argv)
530 /* Test that restoration on failure works */
531 error_test1_int = 0x12345678;
533 error_test2_string = "foo";
535 error_test3_boolean = FALSE;
538 /* Test that special argument parsing works */
543 /* Test string arrays */
546 /* Test ignoring options */
553 /* Test parsing empty args */