Add some tests for '--' stripping.
authorMatthias Clasen <mclasen@redhat.com>
Mon, 20 Dec 2004 21:09:16 +0000 (21:09 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Mon, 20 Dec 2004 21:09:16 +0000 (21:09 +0000)
2004-12-20  Matthias Clasen  <mclasen@redhat.com>

* tests/option-test.c: Add some tests for '--'
stripping.

* glib/goption.c (g_option_context_parse): Don't
strip '--' if it would be needed by a second option
parser.  (#161701)

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib/goption.c
tests/option-test.c

index d9b2504..3260aeb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2004-12-20  Matthias Clasen  <mclasen@redhat.com>
 
+       * tests/option-test.c: Add some tests for '--' 
+       stripping.
+
+       * glib/goption.c (g_option_context_parse): Don't
+       strip '--' if it would be needed by a second option
+       parser.  (#161701)
+
        * glib/gunicollate.c (g_utf8_collate): Make docs 
        more accurate.  (#161683, Marcin Krzyzanowski)
 
index d9b2504..3260aeb 100644 (file)
@@ -1,5 +1,12 @@
 2004-12-20  Matthias Clasen  <mclasen@redhat.com>
 
+       * tests/option-test.c: Add some tests for '--' 
+       stripping.
+
+       * glib/goption.c (g_option_context_parse): Don't
+       strip '--' if it would be needed by a second option
+       parser.  (#161701)
+
        * glib/gunicollate.c (g_utf8_collate): Make docs 
        more accurate.  (#161683, Marcin Krzyzanowski)
 
index d9b2504..3260aeb 100644 (file)
@@ -1,5 +1,12 @@
 2004-12-20  Matthias Clasen  <mclasen@redhat.com>
 
+       * tests/option-test.c: Add some tests for '--' 
+       stripping.
+
+       * glib/goption.c (g_option_context_parse): Don't
+       strip '--' if it would be needed by a second option
+       parser.  (#161701)
+
        * glib/gunicollate.c (g_utf8_collate): Make docs 
        more accurate.  (#161683, Marcin Krzyzanowski)
 
index d9b2504..3260aeb 100644 (file)
@@ -1,5 +1,12 @@
 2004-12-20  Matthias Clasen  <mclasen@redhat.com>
 
+       * tests/option-test.c: Add some tests for '--' 
+       stripping.
+
+       * glib/goption.c (g_option_context_parse): Don't
+       strip '--' if it would be needed by a second option
+       parser.  (#161701)
+
        * glib/gunicollate.c (g_utf8_collate): Make docs 
        more accurate.  (#161683, Marcin Krzyzanowski)
 
index d9b2504..3260aeb 100644 (file)
@@ -1,5 +1,12 @@
 2004-12-20  Matthias Clasen  <mclasen@redhat.com>
 
+       * tests/option-test.c: Add some tests for '--' 
+       stripping.
+
+       * glib/goption.c (g_option_context_parse): Don't
+       strip '--' if it would be needed by a second option
+       parser.  (#161701)
+
        * glib/gunicollate.c (g_utf8_collate): Make docs 
        more accurate.  (#161683, Marcin Krzyzanowski)
 
index 373852c..781d8d5 100644 (file)
@@ -1057,8 +1057,10 @@ free_pending_nulls (GOptionContext *context,
  *
  * If the parsing is successful, any parsed arguments are
  * removed from the array and @argc and @argv are updated 
- * accordingly. In case of an error, @argc and @argv are
- * left unmodified.
+ * accordingly. A '--' option is stripped from @argv
+ * unless there are unparsed options before and after it, 
+ * or some of the options after it start with '-'. In case 
+ * of an error, @argc and @argv are left unmodified. 
  *
  * If automatic <option>--help</option> support is enabled
  * (see g_option_context_set_help_enabled()), and the 
@@ -1120,6 +1122,8 @@ g_option_context_parse (GOptionContext   *context,
   if (argc && argv)
     {
       gboolean stop_parsing = FALSE;
+      gboolean has_unknown = FALSE;
+      gint separator_pos = 0;
 
       for (i = 1; i < *argc; i++)
        {
@@ -1137,7 +1141,7 @@ g_option_context_parse (GOptionContext   *context,
                  /* '--' terminates list of arguments */
                  if (*arg == 0)
                    {
-                     add_pending_null (context, &((*argv)[i]), NULL);
+                     separator_pos = i;
                      stop_parsing = TRUE;
                      continue;
                    }
@@ -1306,11 +1310,14 @@ g_option_context_parse (GOptionContext   *context,
                    }
                }
              
+             if (!parsed)
+               has_unknown = TRUE;
+
              if (!parsed && !context->ignore_unknown)
                {
                  g_set_error (error,
                               G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
-                              _("Unknown option %s"), (*argv)[i]);
+                                  _("Unknown option %s"), (*argv)[i]);
                  goto fail;
                }
            }
@@ -1322,8 +1329,14 @@ g_option_context_parse (GOptionContext   *context,
                                        argc, argv, error, &parsed))
                goto fail;
              
+             if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
+               separator_pos = 0;
            }
        }
+
+      if (separator_pos > 0)
+       add_pending_null (context, &((*argv)[separator_pos]), NULL);
+       
     }
 
   /* Call post-parse hooks */
index 38f8266..a40faec 100644 (file)
@@ -585,6 +585,75 @@ rest_test2 (void)
   g_assert (ignore_test1_boolean);
   g_assert (strcmp (argv[0], "program") == 0);
   g_assert (strcmp (argv[1], "foo") == 0);
+  g_assert (strcmp (argv[2], "--") == 0);
+  g_assert (strcmp (argv[3], "-bar") == 0);
+  g_assert (argv[4] == NULL);
+
+  g_strfreev (argv);
+  g_option_context_free (context);
+}
+
+/* check that -- stripping works */
+void
+rest_test2a (void)
+{
+  GOptionContext *context;
+  gboolean retval;
+  GError *error = NULL;
+  gchar **argv;
+  int argc;
+  GOptionEntry entries [] = { 
+      { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, 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 foo --test -- bar", &argc);
+  
+  retval = g_option_context_parse (context, &argc, &argv, &error);
+  g_assert (retval);
+
+  /* Check array */
+  g_assert (ignore_test1_boolean);
+  g_assert (strcmp (argv[0], "program") == 0);
+  g_assert (strcmp (argv[1], "foo") == 0);
+  g_assert (strcmp (argv[2], "bar") == 0);
+  g_assert (argv[3] == NULL);
+
+  g_strfreev (argv);
+  g_option_context_free (context);
+}
+
+void
+rest_test2b (void)
+{
+  GOptionContext *context;
+  gboolean retval;
+  GError *error = NULL;
+  gchar **argv;
+  int argc;
+  GOptionEntry entries [] = { 
+      { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
+      { NULL } 
+  };
+        
+  context = g_option_context_new (NULL);
+  g_option_context_set_ignore_unknown_options (context, TRUE);
+  g_option_context_add_main_entries (context, entries, NULL);
+
+  /* Now try parsing */
+  argv = split_string ("program foo --test -bar --", &argc);
+  
+  retval = g_option_context_parse (context, &argc, &argv, &error);
+  g_assert (retval);
+
+  /* Check array */
+  g_assert (ignore_test1_boolean);
+  g_assert (strcmp (argv[0], "program") == 0);
+  g_assert (strcmp (argv[1], "foo") == 0);
   g_assert (strcmp (argv[2], "-bar") == 0);
   g_assert (argv[3] == NULL);
 
@@ -592,6 +661,73 @@ rest_test2 (void)
   g_option_context_free (context);
 }
 
+void
+rest_test2c (void)
+{
+  GOptionContext *context;
+  gboolean retval;
+  GError *error = NULL;
+  gchar **argv;
+  int argc;
+  GOptionEntry entries [] = { 
+      { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, 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 -- bar", &argc);
+  
+  retval = g_option_context_parse (context, &argc, &argv, &error);
+  g_assert (retval);
+
+  /* Check array */
+  g_assert (ignore_test1_boolean);
+  g_assert (strcmp (argv[0], "program") == 0);
+  g_assert (strcmp (argv[1], "foo") == 0);
+  g_assert (strcmp (argv[2], "bar") == 0);
+  g_assert (argv[3] == NULL);
+
+  g_strfreev (argv);
+  g_option_context_free (context);
+}
+
+void
+rest_test2d (void)
+{
+  GOptionContext *context;
+  gboolean retval;
+  GError *error = NULL;
+  gchar **argv;
+  int argc;
+  GOptionEntry entries [] = { 
+      { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, 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 -- -bar", &argc);
+  
+  retval = g_option_context_parse (context, &argc, &argv, &error);
+  g_assert (retval);
+
+  /* Check array */
+  g_assert (ignore_test1_boolean);
+  g_assert (strcmp (argv[0], "program") == 0);
+  g_assert (strcmp (argv[1], "--") == 0);
+  g_assert (strcmp (argv[2], "-bar") == 0);
+  g_assert (argv[3] == NULL);
+
+  g_strfreev (argv);
+  g_option_context_free (context);
+}
+
+
 /* check that G_OPTION_REMAINING collects non-option arguments */
 void
 rest_test3 (void)
@@ -736,6 +872,10 @@ main (int argc, char **argv)
   /* Test handling of rest args */
   rest_test1 ();
   rest_test2 ();
+  rest_test2a ();
+  rest_test2b ();
+  rest_test2c ();
+  rest_test2d ();
   rest_test3 ();
   rest_test4 ();
   rest_test5 ();