From 0468b599ad7004bd0a08f28aa727580ce63dd1f4 Mon Sep 17 00:00:00 2001 From: Frank Schoenmann Date: Wed, 11 Apr 2018 21:11:49 +0200 Subject: [PATCH] Leave unrecognised (long) options in argument list. --- include/cxxopts.hpp | 3 +++ test/options.cpp | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index e18b401..59a7178 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -1750,6 +1750,9 @@ ParseResult::parse(int& argc, char**& argv) { if (m_allow_unrecognised) { + // keep unrecognised options in argument list, skip to next argument + argv[nextKeep] = argv[current]; + ++nextKeep; ++current; continue; } diff --git a/test/options.cpp b/test/options.cpp index 5be7470..bfaa635 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -483,9 +483,10 @@ TEST_CASE("Unrecognised options", "[options]") { Argv av({ "unknown_options", + "--unknown", "--long", "-su", - "--unknown", + "--another_unknown", }); char** argv = av.argv(); @@ -498,5 +499,7 @@ TEST_CASE("Unrecognised options", "[options]") { SECTION("After allowing unrecognised options") { options.allow_unrecognised_options(); CHECK_NOTHROW(options.parse(argc, argv)); + REQUIRE(argc == 3); + CHECK_THAT(argv[1], Catch::Equals("--unknown")); } } \ No newline at end of file