diff --git a/ChangeLog b/ChangeLog index d9b25041e..3260aebb0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2004-12-20 Matthias Clasen + * 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) diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index d9b25041e..3260aebb0 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,5 +1,12 @@ 2004-12-20 Matthias Clasen + * 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) diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index d9b25041e..3260aebb0 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,5 +1,12 @@ 2004-12-20 Matthias Clasen + * 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) diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index d9b25041e..3260aebb0 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,5 +1,12 @@ 2004-12-20 Matthias Clasen + * 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) diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index d9b25041e..3260aebb0 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,5 +1,12 @@ 2004-12-20 Matthias Clasen + * 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) diff --git a/glib/goption.c b/glib/goption.c index 373852c98..781d8d58c 100644 --- a/glib/goption.c +++ b/glib/goption.c @@ -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 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 */ diff --git a/tests/option-test.c b/tests/option-test.c index 38f8266ec..a40faecf0 100644 --- a/tests/option-test.c +++ b/tests/option-test.c @@ -581,6 +581,75 @@ rest_test2 (void) 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], "--") == 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); @@ -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 ();