Skip to content

Commit

Permalink
Implemented proper handling for unrecognised command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonmux committed May 11, 2023
1 parent 58fd560 commit 8a5aba3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
30 changes: 23 additions & 7 deletions impl/command_line/arguments.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,39 @@ namespace substrate::commandLine
}, option)
};

// If we got a valid match, us the result
// If we got a valid match, use the result
if (match)
{
return std::visit(match_t
{
// We got a match and parsing it succeeded?
[this]([[maybe_unused]] const auto &result) -> std::optional<bool>
{ return add(result); },
[this]([[maybe_unused]] const auto &result) -> std::optional<bool> { return add(result); },
// Match but inner parsing failed
[](std::monostate) -> std::optional<bool>
{ return std::nullopt; },
[](std::monostate) -> std::optional<bool> { return std::nullopt; },
}, *match);
}
}
// XXX: Need to handle the no-match situation.
// After trying to match, if we got nothing, pass it through the unrecognised argument machinary
lexer.next();
return true;
// If the argument is followed by an '=', grab both parts of it for display
if (token.type() == tokenType_t::equals)
{
lexer.next();
// If there's nothing after the '=', display what we've got
if (token.type() == tokenType_t::space)
console.error("Unrecognised command line argument '"sv, argument, "='"sv);
else
{
const auto value{token.value()};
console.error("Unrecognised command line argument '"sv, argument, '=', value, "'"sv);
lexer.next();
}
}
else
console.error("Unrecognised command line argument '"sv, argument, "'"sv);
// We should now be one token past the end of the unrecognised argument,
// but turn this into a failure return all the same
return false;
}

bool arguments_t::add(item_t argument) noexcept try
Expand Down
2 changes: 1 addition & 1 deletion test/command_line/arguments.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ TEST_CASE("parse command line argument choice", "[command_line::parseArguments]"
})
};
const auto resultC{parseArguments(argsChoiceC.size(), argsChoiceC.data(), programOptions)};
// REQUIRE(resultC == std::nullopt);
REQUIRE(resultC == std::nullopt);
}

0 comments on commit 8a5aba3

Please sign in to comment.