Commit 1957ae49 authored by Paul's avatar Paul
Browse files

Format

parent 6f41d56f
...@@ -403,28 +403,29 @@ struct argument_parser ...@@ -403,28 +403,29 @@ struct argument_parser
{ {
struct result_t struct result_t
{ {
const argument* arg = nullptr; const argument* arg = nullptr;
std::string correct = ""; std::string correct = "";
std::string incorrect = ""; std::string incorrect = "";
std::ptrdiff_t distance = std::numeric_limits<std::ptrdiff_t>::max(); std::ptrdiff_t distance = std::numeric_limits<std::ptrdiff_t>::max();
}; };
result_t result; result_t result;
for(const auto& input:inputs) for(const auto& input : inputs)
{ {
if (input.empty()) if(input.empty())
continue; continue;
if (input[0] != '-') if(input[0] != '-')
continue; continue;
for(const auto& arg:arguments) for(const auto& arg : arguments)
{ {
for(const auto& flag:arg.flags) for(const auto& flag : arg.flags)
{ {
if (flag.empty()) if(flag.empty())
continue; continue;
if (flag[0] != '-') if(flag[0] != '-')
continue; continue;
auto d = levenshtein_distance(flag.begin(), flag.end(), input.begin(), input.end()); auto d =
if (d < result.distance) levenshtein_distance(flag.begin(), flag.end(), input.begin(), input.end());
if(d < result.distance)
result = result_t{&arg, flag, input, d}; result = result_t{&arg, flag, input, d};
} }
} }
...@@ -452,12 +453,13 @@ struct argument_parser ...@@ -452,12 +453,13 @@ struct argument_parser
auto sc = spellcheck(inputs); auto sc = spellcheck(inputs);
std::cout << sc.distance << std::endl; std::cout << sc.distance << std::endl;
std::cout << sc.correct << std::endl; std::cout << sc.correct << std::endl;
if (sc.distance < 5) if(sc.distance < 5)
{ {
std::cout << "Found argument '" << color::fg_yellow << sc.incorrect << "'"; std::cout << "Found argument '" << color::fg_yellow << sc.incorrect << "'";
std::cout << " which wasn't expected, or isn't valid in this context" << std::endl; std::cout << " which wasn't expected, or isn't valid in this context" << std::endl;
std::cout << " "; std::cout << " ";
std::cout << "Did you mean " << color::fg_green << sc.correct << color::reset << "?" << std::endl; std::cout << "Did you mean " << color::fg_green << sc.correct << color::reset << "?"
<< std::endl;
std::cout << std::endl; std::cout << std::endl;
print_usage_for(*sc.arg, sc.correct); print_usage_for(*sc.arg, sc.correct);
} }
......
...@@ -51,14 +51,15 @@ void group_unique(Iterator start, Iterator last, Output out, Predicate pred) ...@@ -51,14 +51,15 @@ void group_unique(Iterator start, Iterator last, Output out, Predicate pred)
} }
} }
template<class Iterator1, class Iterator2> template <class Iterator1, class Iterator2>
std::ptrdiff_t levenshtein_distance(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2) std::ptrdiff_t
levenshtein_distance(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
{ {
if (first1 == last1) if(first1 == last1)
return std::distance(first2, last2); return std::distance(first2, last2);
if (first2 == last2) if(first2 == last2)
return std::distance(first1, last1); return std::distance(first1, last1);
if (*first1 == *first2) if(*first1 == *first2)
return levenshtein_distance(std::next(first1), last1, std::next(first2), last2); return levenshtein_distance(std::next(first1), last1, std::next(first2), last2);
auto x1 = levenshtein_distance(std::next(first1), last1, std::next(first2), last2); auto x1 = levenshtein_distance(std::next(first1), last1, std::next(first2), last2);
auto x2 = levenshtein_distance(first1, last1, std::next(first2), last2); auto x2 = levenshtein_distance(first1, last1, std::next(first2), last2);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment