📄 option-context.c
字号:
voidrest_test2 (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, { NULL } }; context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program foo --test -- -bar", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ g_assert (ignore_test1_boolean); g_assert (strcmp (argv[0], "program") == 0); g_assert (strcmp (argv[1], "foo") == 0); g_assert (strcmp (argv[2], "--") == 0); g_assert (strcmp (argv[3], "-bar") == 0); g_assert (argv[4] == NULL); g_strfreev (argv); g_option_context_free (context);}/* check that -- stripping works */voidrest_test2a (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, { NULL } }; context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program foo --test -- bar", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ g_assert (ignore_test1_boolean); g_assert (strcmp (argv[0], "program") == 0); g_assert (strcmp (argv[1], "foo") == 0); g_assert (strcmp (argv[2], "bar") == 0); g_assert (argv[3] == NULL); g_strfreev (argv); g_option_context_free (context);}voidrest_test2b (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, { NULL } }; context = g_option_context_new (NULL); g_option_context_set_ignore_unknown_options (context, TRUE); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program foo --test -bar --", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ g_assert (ignore_test1_boolean); g_assert (strcmp (argv[0], "program") == 0); g_assert (strcmp (argv[1], "foo") == 0); g_assert (strcmp (argv[2], "-bar") == 0); g_assert (argv[3] == NULL); g_strfreev (argv); g_option_context_free (context);}voidrest_test2c (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, { NULL } }; context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program --test foo -- bar", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ g_assert (ignore_test1_boolean); g_assert (strcmp (argv[0], "program") == 0); g_assert (strcmp (argv[1], "foo") == 0); g_assert (strcmp (argv[2], "bar") == 0); g_assert (argv[3] == NULL); g_strfreev (argv); g_option_context_free (context);}voidrest_test2d (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, { NULL } }; context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program --test -- -bar", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ g_assert (ignore_test1_boolean); g_assert (strcmp (argv[0], "program") == 0); g_assert (strcmp (argv[1], "--") == 0); g_assert (strcmp (argv[2], "-bar") == 0); g_assert (argv[3] == NULL); g_strfreev (argv); g_option_context_free (context);}/* check that G_OPTION_REMAINING collects non-option arguments */voidrest_test3 (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL }, { NULL } }; context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program foo --test bar", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ g_assert (ignore_test1_boolean); g_assert (strcmp (array_test1_array[0], "foo") == 0); g_assert (strcmp (array_test1_array[1], "bar") == 0); g_assert (array_test1_array[2] == NULL); g_strfreev (array_test1_array); g_strfreev (argv); g_option_context_free (context);}/* check that G_OPTION_REMAINING and -- work together */voidrest_test4 (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL }, { NULL } }; context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program foo --test -- -bar", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ g_assert (ignore_test1_boolean); g_assert (strcmp (array_test1_array[0], "foo") == 0); g_assert (strcmp (array_test1_array[1], "-bar") == 0); g_assert (array_test1_array[2] == NULL); g_strfreev (array_test1_array); g_strfreev (argv); g_option_context_free (context);}/* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */voidrest_test5 (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL }, { NULL } }; context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program foo --test bar", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ g_assert (ignore_test1_boolean); g_assert (strcmp (array_test1_array[0], "foo") == 0); g_assert (strcmp (array_test1_array[1], "bar") == 0); g_assert (array_test1_array[2] == NULL); g_strfreev (array_test1_array); g_strfreev (argv); g_option_context_free (context);}voidunknown_short_test (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { NULL } }; g_test_bug ("166609"); context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program -0", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); g_assert (!retval); g_strfreev (argv); g_option_context_free (context);}/* test that lone dashes are treated as non-options */void lonely_dash_test (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; g_test_bug ("168008"); context = g_option_context_new (NULL); /* Now try parsing */ argv = split_string ("program -", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); g_assert (argv[1] && strcmp (argv[1], "-") == 0); g_strfreev (argv); g_option_context_free (context);}voidmissing_arg_test (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; gchar *arg = NULL; GOptionEntry entries [] = { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL }, { NULL } }; g_test_bug ("305576"); context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program --test", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); g_assert (retval == FALSE); g_clear_error (&error); g_strfreev (argv); /* Try parsing again */ argv = split_string ("program --t", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); g_assert (retval == FALSE); g_strfreev (argv); g_option_context_free (context);}intmain (int argc, char *argv[]){ g_test_init (&argc, &argv, NULL); g_test_bug_base ("http://bugzilla.gnome.org/"); g_test_add_func ("/group/captions", group_captions); /* Test that restoration on failure works */ g_test_add_func ("/restoration/int", error_test1); g_test_add_func ("/restoration/string", error_test2); g_test_add_func ("/restoration/boolean", error_test3); /* Test that special argument parsing works */ g_test_add_func ("/arg/repetition/int", arg_test1); g_test_add_func ("/arg/repetition/string", arg_test2); g_test_add_func ("/arg/repetition/filename", arg_test3); g_test_add_func ("/arg/repetition/double", arg_test4); g_test_add_func ("/arg/repetition/locale", arg_test5); g_test_add_func ("/arg/repetition/int64", arg_test6); /* Test string arrays */ g_test_add_func ("/arg/array/string", array_test1); /* Test callback args */ g_test_add_func ("/arg/callback/string", callback_test1); g_test_add_func ("/arg/callback/count", callback_test2); /* Test optional arg flag for callback */ g_test_add_func ("/arg/callback/optional1", callback_test_optional_1); g_test_add_func ("/arg/callback/optional2", callback_test_optional_2); g_test_add_func ("/arg/callback/optional3", callback_test_optional_3); g_test_add_func ("/arg/callback/optional4", callback_test_optional_4); g_test_add_func ("/arg/callback/optional5", callback_test_optional_5); g_test_add_func ("/arg/callback/optional6", callback_test_optional_6); g_test_add_func ("/arg/callback/optional7", callback_test_optional_7); g_test_add_func ("/arg/callback/optional8", callback_test_optional_8); /* Test callback with G_OPTION_REMAINING */ g_test_add_func ("/arg/remaining/callback", callback_remaining_test1); /* Test callbacks which return FALSE */ g_test_add_func ("/arg/remaining/callback-false", callback_returns_false); /* Test ignoring options */ g_test_add_func ("/arg/ignore/long", ignore_test1); g_test_add_func ("/arg/ignore/short", ignore_test2); g_test_add_func ("/arg/ignore/arg", ignore_test3); g_test_add_func ("/context/add", add_test1); /* Test parsing empty args */ g_test_add_func ("/context/empty1", empty_test1); g_test_add_func ("/context/empty2", empty_test2); g_test_add_func ("/context/empty3", empty_test3); /* Test handling of rest args */ g_test_add_func ("/arg/rest/non-option", rest_test1); g_test_add_func ("/arg/rest/separator1", rest_test2); g_test_add_func ("/arg/rest/separator2", rest_test2a); g_test_add_func ("/arg/rest/separator3", rest_test2b); g_test_add_func ("/arg/rest/separator4", rest_test2c); g_test_add_func ("/arg/rest/separator5", rest_test2d); g_test_add_func ("/arg/remaining/non-option", rest_test3); g_test_add_func ("/arg/remaining/separator", rest_test4); g_test_add_func ("/arg/remaining/array", rest_test5); /* regression tests for individual bugs */ g_test_add_func ("/bug/unknown-short", unknown_short_test); g_test_add_func ("/bug/lonely-dash", lonely_dash_test); g_test_add_func ("/bug/missing-arg", missing_arg_test); return g_test_run();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -