diff options
author | regnat <rg@regnat.ovh> | 2022-03-03 10:50:35 +0100 |
---|---|---|
committer | regnat <rg@regnat.ovh> | 2022-03-07 10:09:09 +0100 |
commit | c0792b1546ceed1c02a02ca1286ead55f79d5798 (patch) | |
tree | b03b57763994b13ea800eb1c88f96d2a2dcfae14 /src/libutil/args.cc | |
parent | b09baf690bb00125805a02e0feae9636b2114599 (diff) |
Implement a suggestions mechanism
Each `Error` class now includes a set of suggestions, and these are printed by
the top-level handler.
Diffstat (limited to 'src/libutil/args.cc')
-rw-r--r-- | src/libutil/args.cc | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/libutil/args.cc b/src/libutil/args.cc index f970c0e9e..69aa0d094 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -328,8 +328,13 @@ MultiCommand::MultiCommand(const Commands & commands_) completions->add(name); } auto i = commands.find(s); - if (i == commands.end()) - throw UsageError("'%s' is not a recognised command", s); + if (i == commands.end()) { + std::set<std::string> commandNames; + for (auto & [name, _] : commands) + commandNames.insert(name); + auto suggestions = Suggestions::bestMatches(commandNames, s); + throw UsageError(suggestions, "'%s' is not a recognised command", s); + } command = {s, i->second()}; command->second->parent = this; }} |