Skip to content

Commit 452fd8a

Browse files
committed
[main] optparse: fix wrong "unknown flag" error message
1 parent 70e1dab commit 452fd8a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

main/src/optparse.hxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,13 @@ public:
265265
{
266266
bool forcePositional = false;
267267

268+
// Contains one or more flags coming from one of the arguments (e.g. "-abc" may be split
269+
// into flags "a", "b", and "c", which will be stored in `argStr`).
268270
std::vector<std::string_view> argStr;
269271

270272
for (std::size_t i = 0; i < nArgs && fErrors.empty(); ++i) {
271273
const char *arg = args[i];
274+
const char *const argOrig = arg;
272275

273276
if (strcmp(arg, "--") == 0) {
274277
forcePositional = true;
@@ -322,7 +325,7 @@ public:
322325
std::string_view argS = argStr[j];
323326
const auto *exp = GetExpectedFlag(argS);
324327
if (!exp) {
325-
fErrors.push_back(std::string("Unknown flag: ") + args[j]);
328+
fErrors.push_back(std::string("Unknown flag: ") + argOrig);
326329
break;
327330
}
328331

main/test/optparse_test.cxx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,20 @@ TEST(OptParse, PositionalMixedWithFlags)
476476
EXPECT_EQ(opts.GetSwitch("noarg"), true);
477477
EXPECT_EQ(opts.GetArgs(), std::vector<std::string>({"somename", "bar"}));
478478
}
479+
480+
TEST(OptParse, UnexpectedFlagComplex)
481+
{
482+
ROOT::RCmdLineOpts opts;
483+
opts.AddFlag({"-c", "--compress"}, ROOT::RCmdLineOpts::EFlagType::kWithArg);
484+
opts.AddFlag({"--recreate"});
485+
opts.AddFlag({"--replace"});
486+
opts.AddFlag({"-r", "--recursive"});
487+
opts.AddFlag({"-h", "--help"});
488+
opts.AddFlag({"-v"});
489+
opts.AddFlag({"-vv"});
490+
491+
const char *args[] = {"rootcp", "--recreate", "-r", "test.root", "copy1.root", "-c", "505", "-vvv"};
492+
opts.Parse(args, std::size(args));
493+
494+
EXPECT_EQ(opts.GetErrors(), std::vector<std::string>({"Unknown flag: -vvv"}));
495+
}

0 commit comments

Comments
 (0)