📄 option-context.c
字号:
context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program -t -d", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); g_assert (callback_test_optional_string == NULL); g_assert (callback_test_optional_boolean); g_free (callback_test_optional_string); g_strfreev (argv); g_option_context_free (context);}voidcallback_test_optional_7 (void){ GOptionContext *context; gboolean dummy; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL }, { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, callback_parse_optional, 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 -td", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); g_assert (callback_test_optional_string == NULL); g_assert (callback_test_optional_boolean); g_free (callback_test_optional_string); g_strfreev (argv); g_option_context_free (context);}voidcallback_test_optional_8 (void){ GOptionContext *context; gboolean dummy; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL }, { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, callback_parse_optional, 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 -dt foo.txt", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); g_assert (callback_test_optional_string); g_assert (callback_test_optional_boolean); g_free (callback_test_optional_string); g_strfreev (argv); g_option_context_free (context);}static GPtrArray *callback_remaining_args;static gbooleancallback_remaining_test1_callback (const gchar *option_name, const gchar *value, gpointer data, GError **error){ g_ptr_array_add (callback_remaining_args, g_strdup (value)); return TRUE;}voidcallback_remaining_test1 (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL }, { NULL } }; callback_remaining_args = g_ptr_array_new (); context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); /* Now try parsing */ argv = split_string ("program foo.txt blah.txt", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); g_assert (callback_remaining_args->len == 2); g_assert (strcmp (callback_remaining_args->pdata[0], "foo.txt") == 0); g_assert (strcmp (callback_remaining_args->pdata[1], "blah.txt") == 0); g_ptr_array_foreach (callback_remaining_args, (GFunc) g_free, NULL); g_ptr_array_free (callback_remaining_args, TRUE); g_strfreev (argv); g_option_context_free (context);}static gbooleancallback_error (const gchar *option_name, const gchar *value, gpointer data, GError **error){ g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "42"); return FALSE;}static voidcallback_returns_false (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "error", 0, 0, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL }, { "error-no-arg", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL }, { "error-optional-arg", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, callback_error, 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 --error value", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE); g_assert (retval == FALSE); g_option_context_free (context); g_clear_error (&error); /* And again, this time with a no-arg variant */ context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); argv = split_string ("program --error-no-arg", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE); g_assert (retval == FALSE); g_option_context_free (context); g_clear_error (&error); /* And again, this time with a optional arg variant, with argument */ context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); argv = split_string ("program --error-optional-arg value", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE); g_assert (retval == FALSE); g_option_context_free (context); g_clear_error (&error); /* And again, this time with a optional arg variant, without argument */ context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); argv = split_string ("program --error-optional-arg", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE); g_assert (retval == FALSE); g_option_context_free (context); g_clear_error (&error);}voidignore_test1 (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv, **argv_copy; int argc; gchar *arg; 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 --test --hello", &argc); argv_copy = copy_stringv (argv, argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ arg = join_stringv (argc, argv); g_assert (strcmp (arg, "program --hello") == 0); g_free (arg); g_strfreev (argv_copy); g_free (argv); g_option_context_free (context);}voidignore_test2 (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; gchar *arg; GOptionEntry entries [] = { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_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 -test", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ arg = join_stringv (argc, argv); g_assert (strcmp (arg, "program -es") == 0); g_free (arg); g_strfreev (argv); g_option_context_free (context);}voidignore_test3 (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv, **argv_copy; int argc; gchar *arg; GOptionEntry entries [] = { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, 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 --test foo --hello", &argc); argv_copy = copy_stringv (argv, argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ arg = join_stringv (argc, argv); g_assert (strcmp (arg, "program --hello") == 0); g_assert (strcmp (ignore_test3_string, "foo") == 0); g_free (ignore_test3_string); g_free (arg); g_strfreev (argv_copy); g_free (argv); g_option_context_free (context);}voidarray_test1 (void){ GOptionContext *context; gboolean retval; GError *error = NULL; gchar **argv; int argc; GOptionEntry entries [] = { { "test", 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 --test foo --test bar", &argc); retval = g_option_context_parse (context, &argc, &argv, &error); assert_no_error (error); g_assert (retval); /* Check array */ 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);}voidadd_test1 (void){ GOptionContext *context; GOptionEntry entries1 [] = { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL }, { NULL } }; GOptionEntry entries2 [] = { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL }, { NULL } }; context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries1, NULL); g_option_context_add_main_entries (context, entries2, NULL); g_option_context_free (context);}voidempty_test1 (void){ GOptionContext *context; GOptionEntry entries [] = { { NULL } }; char *prgname; g_set_prgname (NULL); context = g_option_context_new (NULL); g_option_context_add_main_entries (context, entries, NULL); g_option_context_parse (context, NULL, NULL, NULL); prgname = g_get_prgname (); g_assert (prgname && strcmp (prgname, "<unknown>") == 0); g_option_context_free (context);}voidempty_test2 (void){ GOptionContext *context; context = g_option_context_new (NULL); g_option_context_parse (context, NULL, NULL, NULL); g_option_context_free (context);}voidempty_test3 (void){ GOptionContext *context; gint argc; gchar **argv; argc = 0; argv = NULL; context = g_option_context_new (NULL); g_option_context_parse (context, &argc, &argv, NULL); g_option_context_free (context);}/* check that non-option arguments are left in argv by default */voidrest_test1 (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);}/* check that -- works */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -