Allow G_OPTION_ARG_CALLBACK for G_OPTION_REMAINING. (#437297, Dave Benson)

2007-05-11  Matthias Clasen  <mclasen@redhat.com>

        * 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
This commit is contained in:
Matthias Clasen 2007-05-11 18:53:57 +00:00 committed by Matthias Clasen
parent ec11b71411
commit 8f88ba6ffa
5 changed files with 61 additions and 2 deletions

View File

@ -1,3 +1,10 @@
2007-05-11 Matthias Clasen <mclasen@redhat.com>
* 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 <danw@novell.com>
* glib/gkeyfile.c (g_key_file_get_boolean)

View File

@ -1,3 +1,8 @@
2007-05-11 Matthias Clasen <mclasen@redhat.com>
* glib/tmpl/option.sgml: Document new G_OPTION_ARG_REMAINING
functionality.
2007-05-03 Behdad Esfahbod <behdad@gnome.org>
* glib/glib-sections.txt:

View File

@ -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 <literal>argv</literal>. 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.
</para>
<para>

View File

@ -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);

View File

@ -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 ();