1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-02 05:33:53 +00:00
CLI11/examples/close_match.cpp
2025-04-30 12:34:38 +00:00

105 lines
3.4 KiB
C++

// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
// Code inspired by discussion from https://github.com/CLIUtils/CLI11/issues/1149
#include <CLI/CLI.hpp>
#include <iostream>
#include <limits>
#include <string>
#include <utility>
#include <vector>
// Levenshtein distance function code generated by chatgpt
std::size_t levenshteinDistance(const std::string &s1, const std::string &s2) {
size_t len1 = s1.size(), len2 = s2.size();
std::vector<std::vector<std::size_t>> dp(len1 + 1, std::vector<std::size_t>(len2 + 1));
for(size_t ii = 0; ii <= len1; ++ii)
dp[ii][0] = ii;
for(size_t jj = 0; jj <= len2; ++jj)
dp[0][jj] = jj;
for(size_t ii = 1; ii <= len1; ++ii) {
for(size_t jj = 1; jj <= len2; ++jj) {
std::size_t cost = (s1[ii - 1] == s2[jj - 1]) ? 0 : 1;
dp[ii][jj] = (std::min)({
dp[ii - 1][jj] + 1, // deletion
dp[ii][jj - 1] + 1, // insertion
dp[ii - 1][jj - 1] + cost // substitution
});
}
}
return dp[len1][len2];
}
// 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) {
std::string closest;
std::size_t minDistance = (std::numeric_limits<std::size_t>::max)();
for(const auto &candidate : candidates) {
std::size_t distance = levenshteinDistance(input, candidate);
if(distance < minDistance) {
minDistance = distance;
closest = candidate;
}
}
return {closest, minDistance};
}
void addCloseMatchDetection(CLI::App *app, std::size_t minDistance = 3) {
app->allow_extras(true);
app->parse_complete_callback([&app, minDistance]() {
auto extras = app->remaining();
if(extras.empty()) {
return;
}
auto subs = app->get_subcommands(nullptr);
std::vector<std::string> list;
for(const auto *sub : subs) {
if(!sub->get_name().empty()) {
list.push_back(sub->get_name());
}
auto aliases = sub->get_aliases();
if(!aliases.empty()) {
list.insert(list.end(), aliases.begin(), aliases.end());
}
}
for(auto &extra : extras) {
if(extra.front() != '-') {
auto closest = findClosestMatch(extra, list);
if(closest.second <= minDistance) {
std::cout << "unmatched commands " << extra << ", closest match is " << closest.first << "\n";
}
}
}
});
}
/** 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 value{0};
CLI::App app{"cose string App"};
// turn on prefix matching
app.allow_subcommand_prefix_matching();
app.add_option("-v", value, "value");
app.add_subcommand("install", "");
app.add_subcommand("upgrade", "");
app.add_subcommand("remove", "");
app.add_subcommand("test", "");
addCloseMatchDetection(&app, 5);
CLI11_PARSE(app, argc, argv);
return 0;
}