From 8f88ba6ffa6de84c835aee701a454b5c0488532d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 11 May 2007 18:53:57 +0000 Subject: [PATCH] Allow G_OPTION_ARG_CALLBACK for G_OPTION_REMAINING. (#437297, Dave Benson) 2007-05-11 Matthias Clasen * glib/goption.c: Allow G_OPTION_ARG_CALLBACK for G_OPTION_REMAINING. (#437297, Dave Benson) * tests/option-test.c: Add a test for this. svn path=/trunk/; revision=5487 --- ChangeLog | 7 ++++++ docs/reference/ChangeLog | 5 ++++ docs/reference/glib/tmpl/option.sgml | 3 ++- glib/goption.c | 3 ++- tests/option-test.c | 45 ++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index c24cdb6..38a2b2d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2007-05-11 Matthias Clasen + + * glib/goption.c: Allow G_OPTION_ARG_CALLBACK for + G_OPTION_REMAINING. (#437297, Dave Benson) + + * tests/option-test.c: Add a test for this. + 2007-05-04 Dan Winship * glib/gkeyfile.c (g_key_file_get_boolean) diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index aa65d41..0799374 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,8 @@ +2007-05-11 Matthias Clasen + + * glib/tmpl/option.sgml: Document new G_OPTION_ARG_REMAINING + functionality. + 2007-05-03 Behdad Esfahbod * glib/glib-sections.txt: diff --git a/docs/reference/glib/tmpl/option.sgml b/docs/reference/glib/tmpl/option.sgml index f85f982..9dc78ea 100644 --- a/docs/reference/glib/tmpl/option.sgml +++ b/docs/reference/glib/tmpl/option.sgml @@ -378,7 +378,8 @@ Flags which modify individual options. If a long option in the main group has this name, it is not treated as a regular option. Instead it collects all non-option arguments which would otherwise be left in argv. The option must be of type -%G_OPTION_ARG_STRING_ARRAY or %G_OPTION_ARG_FILENAME_ARRAY. +%G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY +or %G_OPTION_ARG_FILENAME_ARRAY. diff --git a/glib/goption.c b/glib/goption.c index 3a89191..f17c9a8 100644 --- a/glib/goption.c +++ b/glib/goption.c @@ -1355,7 +1355,8 @@ parse_remaining_arg (GOptionContext *context, if (group->entries[j].long_name[0]) continue; - g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY || + g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK || + group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY || group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE); add_pending_null (context, &((*argv)[*index]), NULL); diff --git a/tests/option-test.c b/tests/option-test.c index 6a84972..fc8699c 100644 --- a/tests/option-test.c +++ b/tests/option-test.c @@ -788,6 +788,48 @@ callback_test_optional_8 (void) g_option_context_free (context); } +static GPtrArray *callback_remaining_args; +static gboolean +callback_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; +} + +void +callback_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); + 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); +} + void ignore_test1 (void) { @@ -1420,6 +1462,9 @@ main (int argc, char **argv) callback_test_optional_6 (); callback_test_optional_7 (); callback_test_optional_8 (); + + /* Test callback with G_OPTION_REMAINING */ + callback_remaining_test1 (); /* Test ignoring options */ ignore_test1 (); -- 2.7.4