diff options
author | Matthias Clasen <mclasen@redhat.com> | 2004-12-20 21:09:16 +0000 |
---|---|---|
committer | Matthias Clasen <matthiasc@src.gnome.org> | 2004-12-20 21:09:16 +0000 |
commit | 9d8c5bc757b1bdec5564a8cce159da6ac63539b6 (patch) | |
tree | 74247df1643f84a25ee6fb57fa226c22b7701bce | |
parent | ad90d2de03993b6c6b7cb0a280dc1ac8fff2d7da (diff) |
Add some tests for '--' stripping.
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)
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | ChangeLog.pre-2-10 | 7 | ||||
-rw-r--r-- | ChangeLog.pre-2-12 | 7 | ||||
-rw-r--r-- | ChangeLog.pre-2-6 | 7 | ||||
-rw-r--r-- | ChangeLog.pre-2-8 | 7 | ||||
-rw-r--r-- | glib/goption.c | 21 | ||||
-rw-r--r-- | tests/option-test.c | 140 |
7 files changed, 192 insertions, 4 deletions
@@ -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) 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 <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) 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 <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) 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 <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) 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 <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) 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 <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 */ 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 @@ -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 (); |