diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index 633a419..3036504 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -1251,7 +1251,16 @@ Options::parse(int& argc, char**& argv) { while (current < argc) { - consume_positional(argv[current]); + if (!consume_positional(argv[current])) { + break; + } + ++current; + } + + //adjust argv for any that couldn't be swallowed + while (current != argc) { + argv[nextKeep] = argv[current]; + ++nextKeep; ++current; } } diff --git a/test/options.cpp b/test/options.cpp index 76fad97..6aea0da 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -153,3 +153,25 @@ TEST_CASE("Some positional explicit", "[positional]") CHECK(positional[0] == "c"); CHECK(positional[1] == "d"); } + +TEST_CASE("No positional with extras", "[positional]") +{ + cxxopts::Options options("posargmaster", "shows incorrect handling"); + options.add_options() + ("dummy", "oh no", cxxopts::value()) + ; + + Argv av({"extras", "--", "a", "b", "c", "d"}); + + char** argv = av.argv(); + auto argc = av.argc(); + + auto old_argv = argv; + auto old_argc = argc; + + options.parse(argc, argv); + + REQUIRE(argc == old_argc - 1); + CHECK(argv[0] == std::string("extras")); + CHECK(argv[1] == std::string("a")); +}