CLI: Added ::canonicalize

This commit is contained in:
Paul Beckingham 2016-04-02 13:30:56 -04:00
parent b624aee186
commit 644439e587
2 changed files with 36 additions and 0 deletions

View file

@ -26,6 +26,7 @@
#include <cmake.h>
#include <CLI.h>
#include <shared.h>
////////////////////////////////////////////////////////////////////////////////
void CLI::entity (const std::string& category, const std::string& name)
@ -41,3 +42,36 @@ void CLI::entity (const std::string& category, const std::string& name)
}
////////////////////////////////////////////////////////////////////////////////
// Search for 'value' in _entities category, return canonicalized value.
bool CLI::canonicalize (
std::string& canonicalized,
const std::string& category,
const std::string& value) const
{
// Extract a list of entities for category.
std::vector <std::string> options;
auto c = _entities.equal_range (category);
for (auto e = c.first; e != c.second; ++e)
{
// Shortcut: if an exact match is found, success.
if (value == e->second)
{
canonicalized = value;
return true;
}
options.push_back (e->second);
}
// Match against the options, throw away results.
std::vector <std::string> matches;
if (autoComplete (value, options, matches) == 1)
{
canonicalized = matches[0];
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -28,6 +28,7 @@
#define INCLUDED_CLI
#include <string>
#include <vector>
#include <map>
// Represents the command line.
@ -36,6 +37,7 @@ class CLI
public:
CLI () = default;
void entity (const std::string&, const std::string&);
bool canonicalize (std::string&, const std::string&, const std::string&) const;
public:
std::multimap <std::string, std::string> _entities {};