1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00

style: pre-commit.ci fixes

This commit is contained in:
pre-commit-ci[bot] 2025-04-23 01:28:38 +00:00
parent ae0735c88c
commit 31a62ac3f2

View File

@ -13,24 +13,17 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
std::size_t prefixMatch(const std::string& s1, const std::string& s2) std::size_t prefixMatch(const std::string &s1, const std::string &s2) {
{ if(s1.size() < s2.size()) {
if (s1.size() < s2.size()) if(s2.compare(0, s1.size(), s1.c_str()) == 0) {
{
if (s2.compare(0, s1.size(), s1.c_str()) == 0)
{
return s2.size() - s1.size(); return s2.size() - s1.size();
} } else {
else {
return std::string::npos; return std::string::npos;
} }
} } else {
else { if(s1.compare(0, s2.size(), s2.c_str()) == 0) {
if (s1.compare(0, s2.size(), s2.c_str()) == 0)
{
return s1.size() - s2.size(); return s1.size() - s2.size();
} } else {
else {
return std::string::npos; return std::string::npos;
} }
} }
@ -63,17 +56,15 @@ std::size_t levenshteinDistance(const std::string &s1, const std::string &s2) {
enum class MatchType : std::uint8_t { proximity, prefix }; enum class MatchType : std::uint8_t { proximity, prefix };
// Finds the closest string from a list (modified from chat gpt code) // Finds the closest string from a list (modified from chat gpt code)
std::pair<std::string, std::size_t> findClosestMatch(const std::string &input, const std::vector<std::string> &candidates,MatchType match) { std::pair<std::string, std::size_t>
findClosestMatch(const std::string &input, const std::vector<std::string> &candidates, MatchType match) {
std::string closest; std::string closest;
int minDistance = (std::numeric_limits<std::size_t>::max)(); int minDistance = (std::numeric_limits<std::size_t>::max)();
std::size_t distance = minDistance; std::size_t distance = minDistance;
for(const auto &candidate : candidates) { for(const auto &candidate : candidates) {
if (match == MatchType::proximity) if(match == MatchType::proximity) {
{
distance = levenshteinDistance(input, candidate); distance = levenshteinDistance(input, candidate);
} } else {
else
{
distance = prefixMatch(input, candidate); distance = prefixMatch(input, candidate);
} }
if(distance < minDistance) { if(distance < minDistance) {
@ -85,8 +76,7 @@ std::pair<std::string, std::size_t> findClosestMatch(const std::string &input, c
return {closest, minDistance}; return {closest, minDistance};
} }
void addCloseMatchDetection(CLI::App* app, MatchType match) void addCloseMatchDetection(CLI::App *app, MatchType match) {
{
app->allow_extras(true); app->allow_extras(true);
app->parse_complete_callback([&app, match]() { app->parse_complete_callback([&app, match]() {
@ -116,7 +106,8 @@ void addCloseMatchDetection(CLI::App* app, MatchType match)
}); });
} }
/** This example demonstrates the use of close match detection to detect invalid commands that are close matches to existing ones /** This example demonstrates the use of close match detection to detect invalid commands that are close matches to
* existing ones
*/ */
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {