Fix arguments after -- without declared positional
Fixes #36. This fixes handling arguments passed after `--` when no positional arguments have been declared, or when the positional arguments have all been used up. The bug was that the extra arguments were lost. Now they are retained in the modified argv.
This commit is contained in:
parent
21591dc8e8
commit
848880d931
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<std::string>())
|
||||
;
|
||||
|
||||
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"));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user