Browse Source

Clean up did-you-mean handling for argument_parser

default_compile_flags
vector-of-bool 3 years ago
parent
commit
e7136b6b0b
1 changed files with 11 additions and 17 deletions
  1. +11
    -17
      src/debate/argument_parser.cpp

+ 11
- 17
src/debate/argument_parser.cpp View File

std::optional<std::string> find_nearest_arg_spelling(std::string_view given) const noexcept { std::optional<std::string> find_nearest_arg_spelling(std::string_view given) const noexcept {
std::vector<std::string> candidates; std::vector<std::string> candidates;
// Only match arguments of the corrent type // Only match arguments of the corrent type
auto given_long = given.starts_with("--");
auto has_dash = given.starts_with("-");
auto parser = bottom_parser;
auto parser = bottom_parser;
while (parser) { while (parser) {
for (auto& arg : parser->arguments()) { for (auto& arg : parser->arguments()) {
if (given_long) {
for (auto& l : arg.long_spellings) {
candidates.push_back("--" + l);
}
for (auto& l : arg.long_spellings) {
candidates.push_back("--" + l);
} }
if (has_dash) {
for (auto& s : arg.short_spellings) {
candidates.push_back("-" + s);
}
}
if (!has_dash && parser->subparsers() && parser == bottom_parser) {
auto&& grp = *parser->subparsers();
for (auto& p : grp._p_subparsers) {
candidates.push_back(p.name);
}
for (auto& s : arg.short_spellings) {
candidates.push_back("-" + s);
} }
} }
parser = parser->parent().pointer(); parser = parser->parent().pointer();
} }
if (bottom_parser->subparsers()) {
auto&& grp = *bottom_parser->subparsers();
for (auto& p : grp._p_subparsers) {
candidates.push_back(p.name);
}
}
return dds::did_you_mean(given, candidates); return dds::did_you_mean(given, candidates);
} }



Loading…
Cancel
Save