Allow G_OPTION_ARG_CALLBACK for G_OPTION_REMAINING. (#437297, Dave Benson)
authorMatthias Clasen <mclasen@redhat.com>
Fri, 11 May 2007 18:53:57 +0000 (18:53 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Fri, 11 May 2007 18:53:57 +0000 (18:53 +0000)
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

ChangeLog
docs/reference/ChangeLog
docs/reference/glib/tmpl/option.sgml
glib/goption.c
tests/option-test.c

index c24cdb6..38a2b2d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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)
index aa65d41..0799374 100644 (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:
index f85f982..9dc78ea 100644 (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>
index 3a89191..f17c9a8 100644 (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);
index 6a84972..fc8699c 100644 (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 ();