⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 option-context.c

📁 this is a glib for c language
💻 C
📖 第 1 页 / 共 4 页
字号:
  /* Now try parsing */  argv = split_string ("program --test 20 --test 30", &argc);  retval = g_option_context_parse (context, &argc, &argv, &error);  assert_no_error (error);  g_assert (retval);  /* Last arg specified is the one that should be stored */  g_assert (arg_test1_int == 30);  g_strfreev (argv);  g_option_context_free (context);}voidarg_test2 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, 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);  /* Last arg specified is the one that should be stored */  g_assert (strcmp (arg_test2_string, "bar") == 0);  g_free (arg_test2_string);    g_strfreev (argv);  g_option_context_free (context);}voidarg_test3 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, 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.txt", &argc);    retval = g_option_context_parse (context, &argc, &argv, &error);  assert_no_error (error);  g_assert (retval);  /* Last arg specified is the one that should be stored */  g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);  g_free (arg_test3_filename);    g_strfreev (argv);  g_option_context_free (context);}voidarg_test4 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, 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 20.0 --test 30.03", &argc);  retval = g_option_context_parse (context, &argc, &argv, &error);  assert_no_error (error);  g_assert (retval);  /* Last arg specified is the one that should be stored */  g_assert (arg_test4_double == 30.03);  g_strfreev (argv);  g_option_context_free (context);}voidarg_test5 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  char *old_locale, *current_locale;  const char *locale = "de_DE";  GOptionEntry entries [] =    { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test5_double, 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 20,0 --test 30,03", &argc);  /* set it to some locale that uses commas instead of decimal points */    old_locale = g_strdup (setlocale (LC_NUMERIC, locale));  current_locale = setlocale (LC_NUMERIC, NULL);  if (strcmp (current_locale, locale) != 0)    {      fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);      goto cleanup;     }  retval = g_option_context_parse (context, &argc, &argv, &error);  assert_no_error (error);  g_assert (retval);  /* Last arg specified is the one that should be stored */  g_assert (arg_test5_double == 30.03); cleanup:  setlocale (LC_NUMERIC, old_locale);  g_free (old_locale);  g_strfreev (argv);  g_option_context_free (context);}voidarg_test6 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL },      { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, 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 4294967297 --test 4294967296 --test2 0xfffffffff", &argc);  retval = g_option_context_parse (context, &argc, &argv, &error);  assert_no_error (error);  g_assert (retval);  /* Last arg specified is the one that should be stored */  g_assert (arg_test6_int64 == G_GINT64_CONSTANT(4294967296));  g_assert (arg_test6_int64_2 == G_GINT64_CONSTANT(0xfffffffff));  g_strfreev (argv);  g_option_context_free (context);}static gbooleancallback_parse1 (const gchar *option_name, const gchar *value,		 gpointer data, GError **error){	callback_test1_string = g_strdup (value);	return TRUE;}voidcallback_test1 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, 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.txt", &argc);    retval = g_option_context_parse (context, &argc, &argv, &error);  assert_no_error (error);  g_assert (retval);  g_assert (strcmp (callback_test1_string, "foo.txt") == 0);  g_free (callback_test1_string);    g_strfreev (argv);  g_option_context_free (context);}static gbooleancallback_parse2 (const gchar *option_name, const gchar *value,		 gpointer data, GError **error){	callback_test2_int++;	return TRUE;}voidcallback_test2 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, 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 --test", &argc);    retval = g_option_context_parse (context, &argc, &argv, &error);  assert_no_error (error);  g_assert (retval);  g_assert (callback_test2_int == 2);    g_strfreev (argv);  g_option_context_free (context);}static gbooleancallback_parse_optional (const gchar *option_name, const gchar *value,		 gpointer data, GError **error){	callback_test_optional_boolean = TRUE;	if (value)		callback_test_optional_string = g_strdup (value);	else		callback_test_optional_string = NULL;	return TRUE;}voidcallback_test_optional_1 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "test", 0, 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 --test foo.txt", &argc);    retval = g_option_context_parse (context, &argc, &argv, &error);  assert_no_error (error);  g_assert (retval);  g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);    g_assert (callback_test_optional_boolean);  g_free (callback_test_optional_string);    g_strfreev (argv);  g_option_context_free (context);}voidcallback_test_optional_2 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "test", 0, 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 --test", &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_3 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "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 -t foo.txt", &argc);    retval = g_option_context_parse (context, &argc, &argv, &error);  assert_no_error (error);  g_assert (retval);  g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);    g_assert (callback_test_optional_boolean);  g_free (callback_test_optional_string);    g_strfreev (argv);  g_option_context_free (context);}voidcallback_test_optional_4 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  int argc;  GOptionEntry entries [] =    { { "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 -t", &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_5 (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 --test --dummy", &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_6 (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 } };  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -