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;
18 split_string (const char *str, int *argc)
23 argv = g_strsplit (str, " ", 0);
25 for (len = 0; argv[len] != NULL; len++);
34 join_stringv (int argc, char **argv)
39 str = g_string_new (NULL);
41 for (i = 0; i < argc; i++)
43 g_string_append (str, argv[i]);
46 g_string_append_c (str, ' ');
49 return g_string_free (str, FALSE);
52 /* Performs a shallow copy */
54 copy_stringv (char **argv, int argc)
56 return g_memdup (argv, sizeof (char *) * (argc + 1));
61 error_test1_pre_parse (GOptionContext *context,
66 g_assert (error_test1_int == 0x12345678);
72 error_test1_post_parse (GOptionContext *context,
77 g_assert (error_test1_int == 20);
79 /* Set an error in the post hook */
80 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, NULL);
88 GOptionContext *context;
93 GOptionGroup *main_group;
94 GOptionEntry entries [] =
95 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
98 context = g_option_context_new (NULL);
99 g_option_context_add_main_entries (context, entries, NULL);
101 /* Set pre and post parse hooks */
102 main_group = g_option_context_get_main_group (context);
103 g_option_group_set_parse_hooks (main_group,
104 error_test1_pre_parse, error_test1_post_parse);
106 /* Now try parsing */
107 argv = split_string ("program --test 20", &argc);
109 retval = g_option_context_parse (context, &argc, &argv, &error);
110 g_assert (retval == FALSE);
112 /* On failure, values should be reset */
113 g_assert (error_test1_int == 0x12345678);
116 g_option_context_free (context);
121 error_test2_pre_parse (GOptionContext *context,
126 g_assert (strcmp (error_test2_string, "foo") == 0);
132 error_test2_post_parse (GOptionContext *context,
137 g_assert (strcmp (error_test2_string, "bar") == 0);
139 /* Set an error in the post hook */
140 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, NULL);
148 GOptionContext *context;
150 GError *error = NULL;
153 GOptionGroup *main_group;
154 GOptionEntry entries [] =
155 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
158 context = g_option_context_new (NULL);
159 g_option_context_add_main_entries (context, entries, NULL);
161 /* Set pre and post parse hooks */
162 main_group = g_option_context_get_main_group (context);
163 g_option_group_set_parse_hooks (main_group,
164 error_test2_pre_parse, error_test2_post_parse);
166 /* Now try parsing */
167 argv = split_string ("program --test bar", &argc);
168 retval = g_option_context_parse (context, &argc, &argv, &error);
170 g_error_free (error);
171 g_assert (retval == FALSE);
173 g_assert (strcmp (error_test2_string, "foo") == 0);
176 g_option_context_free (context);
180 error_test3_pre_parse (GOptionContext *context,
185 g_assert (!error_test3_boolean);
191 error_test3_post_parse (GOptionContext *context,
196 g_assert (error_test3_boolean);
198 /* Set an error in the post hook */
199 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, NULL);
207 GOptionContext *context;
209 GError *error = NULL;
212 GOptionGroup *main_group;
213 GOptionEntry entries [] =
214 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
217 context = g_option_context_new (NULL);
218 g_option_context_add_main_entries (context, entries, NULL);
220 /* Set pre and post parse hooks */
221 main_group = g_option_context_get_main_group (context);
222 g_option_group_set_parse_hooks (main_group,
223 error_test3_pre_parse, error_test3_post_parse);
225 /* Now try parsing */
226 argv = split_string ("program --test", &argc);
227 retval = g_option_context_parse (context, &argc, &argv, &error);
229 g_error_free (error);
230 g_assert (retval == FALSE);
232 g_assert (!error_test3_boolean);
235 g_option_context_free (context);
241 GOptionContext *context;
243 GError *error = NULL;
246 GOptionEntry entries [] =
247 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
250 context = g_option_context_new (NULL);
251 g_option_context_add_main_entries (context, entries, NULL);
253 /* Now try parsing */
254 argv = split_string ("program --test 20 --test 30", &argc);
256 retval = g_option_context_parse (context, &argc, &argv, &error);
259 /* Last arg specified is the one that should be stored */
260 g_assert (arg_test1_int == 30);
263 g_option_context_free (context);
269 GOptionContext *context;
271 GError *error = NULL;
274 GOptionEntry entries [] =
275 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
278 context = g_option_context_new (NULL);
279 g_option_context_add_main_entries (context, entries, NULL);
281 /* Now try parsing */
282 argv = split_string ("program --test foo --test bar", &argc);
284 retval = g_option_context_parse (context, &argc, &argv, &error);
287 /* Last arg specified is the one that should be stored */
288 g_assert (strcmp (arg_test2_string, "bar") == 0);
290 g_free (arg_test2_string);
293 g_option_context_free (context);
299 GOptionContext *context;
301 GError *error = NULL;
302 gchar **argv, **argv_copy;
305 GOptionEntry entries [] =
306 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
309 context = g_option_context_new (NULL);
310 g_option_context_set_ignore_unknown_options (context, TRUE);
311 g_option_context_add_main_entries (context, entries, NULL);
313 /* Now try parsing */
314 argv = split_string ("program --test --hello", &argc);
315 argv_copy = copy_stringv (argv, argc);
317 retval = g_option_context_parse (context, &argc, &argv, &error);
321 arg = join_stringv (argc, argv);
322 g_assert (strcmp (arg, "program --hello") == 0);
325 g_strfreev (argv_copy);
327 g_option_context_free (context);
333 GOptionContext *context;
335 GError *error = NULL;
339 GOptionEntry entries [] =
340 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
343 context = g_option_context_new (NULL);
344 g_option_context_set_ignore_unknown_options (context, TRUE);
345 g_option_context_add_main_entries (context, entries, NULL);
347 /* Now try parsing */
348 argv = split_string ("program -test", &argc);
350 retval = g_option_context_parse (context, &argc, &argv, &error);
354 arg = join_stringv (argc, argv);
355 g_assert (strcmp (arg, "program -es") == 0);
359 g_option_context_free (context);
365 GOptionContext *context;
367 GError *error = NULL;
370 GOptionEntry entries [] =
371 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
374 context = g_option_context_new (NULL);
375 g_option_context_add_main_entries (context, entries, NULL);
377 /* Now try parsing */
378 argv = split_string ("program --test foo --test bar", &argc);
380 retval = g_option_context_parse (context, &argc, &argv, &error);
384 g_assert (strcmp (array_test1_array[0], "foo") == 0);
385 g_assert (strcmp (array_test1_array[1], "bar") == 0);
386 g_assert (array_test1_array[2] == NULL);
388 g_strfreev (array_test1_array);
391 g_option_context_free (context);
397 GOptionContext *context;
399 GOptionEntry entries1 [] =
400 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
402 GOptionEntry entries2 [] =
403 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
406 context = g_option_context_new (NULL);
407 g_option_context_add_main_entries (context, entries1, NULL);
408 g_option_context_add_main_entries (context, entries2, NULL);
410 g_option_context_free (context);
416 GOptionContext *context;
417 GOptionEntry entries [] =
420 context = g_option_context_new (NULL);
422 g_option_context_add_main_entries (context, entries, NULL);
424 g_option_context_parse (context, NULL, NULL, NULL);
426 g_assert (strcmp (g_get_prgname (), "<unknown>") == 0);
428 g_option_context_free (context);
434 GOptionContext *context;
436 context = g_option_context_new (NULL);
437 g_option_context_parse (context, NULL, NULL, NULL);
439 g_option_context_free (context);
443 main (int argc, char **argv)
445 /* Test that restoration on failure works */
446 error_test1_int = 0x12345678;
448 error_test2_string = "foo";
450 error_test3_boolean = FALSE;
453 /* Test that special argument parsing works */
457 /* Test string arrays */
460 /* Test ignoring options */
466 /* Test parsing empty args */