diff options
author | Matthias Clasen <mclasen@redhat.com> | 2005-06-18 04:55:26 +0000 |
---|---|---|
committer | Matthias Clasen <matthiasc@src.gnome.org> | 2005-06-18 04:55:26 +0000 |
commit | 3a7a09668253973235e7e48fcaf9b06051c8c3b2 (patch) | |
tree | 43399650a47e07f7d108cf986efc1c8025acc7bf /tests | |
parent | 2e5ed68b653c67b25e859cc77a4282d8b9d724c0 (diff) |
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
Diffstat (limited to 'tests')
-rw-r--r-- | tests/option-test.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/option-test.c b/tests/option-test.c index 3c4035fb8..ffd1761e5 100644 --- a/tests/option-test.c +++ b/tests/option-test.c @@ -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 (); |