mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-03 16:03:40 +02:00
Add G_OPTION_FLAG_NO_ARG and G_OPTION_FLAG_FILENAME to allow greater
2005-06-18 Matthias Clasen <mclasen@redhat.com> * glib/goption.h: * glib/goption.c: Add G_OPTION_FLAG_NO_ARG and G_OPTION_FLAG_FILENAME to allow greater control of G_OPTION_ARG_CALLBACK options. (#302632, Dan Winship) * tests/option-test.c: test callback args
This commit is contained in:
committed by
Matthias Clasen
parent
2e5ed68b65
commit
3a7a096682
@@ -9,6 +9,9 @@ int arg_test1_int;
|
||||
gchar *arg_test2_string;
|
||||
gchar *arg_test3_filename;
|
||||
|
||||
gchar *callback_test1_string;
|
||||
gboolean callback_test2_int;
|
||||
|
||||
gchar **array_test1_array;
|
||||
|
||||
gboolean ignore_test1_boolean;
|
||||
@@ -324,6 +327,78 @@ arg_test3 (void)
|
||||
g_option_context_free (context);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
callback_parse1 (const gchar *option_name, const gchar *value,
|
||||
gpointer data, GError **error)
|
||||
{
|
||||
callback_test1_string = g_strdup (value);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
callback_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);
|
||||
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 gboolean
|
||||
callback_parse2 (const gchar *option_name, const gchar *value,
|
||||
gpointer data, GError **error)
|
||||
{
|
||||
callback_test2_int++;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
callback_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);
|
||||
g_assert (retval);
|
||||
|
||||
g_assert (callback_test2_int == 2);
|
||||
|
||||
g_strfreev (argv);
|
||||
g_option_context_free (context);
|
||||
}
|
||||
|
||||
void
|
||||
ignore_test1 (void)
|
||||
{
|
||||
@@ -937,6 +1012,10 @@ main (int argc, char **argv)
|
||||
/* Test string arrays */
|
||||
array_test1 ();
|
||||
|
||||
/* Test callback args */
|
||||
callback_test1 ();
|
||||
callback_test2 ();
|
||||
|
||||
/* Test ignoring options */
|
||||
ignore_test1 ();
|
||||
ignore_test2 ();
|
||||
|
Reference in New Issue
Block a user