Skip to content

Commit

Permalink
improve suggestions on unknown options (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyer authored Aug 21, 2024
1 parent 42c47ac commit ccfbbf0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
20 changes: 8 additions & 12 deletions application/F3DOptionsParser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -467,21 +467,17 @@ void ConfigurationOptions::GetOptions(F3DAppOptions& appOptions, f3d::options& o
// check if it's a long option
if (unknownOption.substr(0, 2) == "--")
{
// remove trailing '--'
unknownOption = unknownOption.substr(2);
const size_t i = unknownOption.rfind('=');

auto it = std::find_if(unknownOption.rbegin(), unknownOption.rend(),
[](unsigned char ch) { return ch == '='; });
// remove "--" and everything after the last "=" (if any)
const std::string unknownName =
unknownOption.substr(2, i != std::string::npos ? i - 2 : i);

// remove everything after the character '='
if (it != unknownOption.rend())
{
unknownOption.erase(it.base() - 1, unknownOption.end());
}
auto [closestName, dist] = this->GetClosestOption(unknownOption);
const std::string closestOption =
i == std::string::npos ? closestName : closestName + unknownOption.substr(i);

auto [name, dist] = this->GetClosestOption(unknownOption);

f3d::log::error("Did you mean '--", name, "'?");
f3d::log::error("Did you mean '--", closestOption, "'?");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion application/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ f3d_test(NAME TestInvalidTexture DATA cow.vtp ARGS --texture-material=${F3D_SOUR
f3d_test(NAME TestNonExistentInteraction DATA cow.vtp INTERACTION REGEXP "Interaction record file to play does not exist" NO_BASELINE)

# Test unknown options, do not add a --colour option
f3d_test(NAME TestUnknownOptionVerbose ARGS --colour=1,0,0 REGEXP "Did you mean '--color'?")
f3d_test(NAME TestUnknownOptionVerbose ARGS --colour=1,0,0 REGEXP "Did you mean '--color=1,0,0'?")
f3d_test(NAME TestUnknownOptionExitCode ARGS --colour=1,0,0 WILL_FAIL)

# Test non-existent config filepath, do not add a dummy.json
Expand Down

0 comments on commit ccfbbf0

Please sign in to comment.