From 644439e58774c03f74288cb615d6db2bbd1d50df Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 2 Apr 2016 13:30:56 -0400 Subject: [PATCH] CLI: Added ::canonicalize --- src/CLI.cpp | 34 ++++++++++++++++++++++++++++++++++ src/CLI.h | 2 ++ 2 files changed, 36 insertions(+) diff --git a/src/CLI.cpp b/src/CLI.cpp index 0a90225a..8bfeeb6b 100644 --- a/src/CLI.cpp +++ b/src/CLI.cpp @@ -26,6 +26,7 @@ #include #include +#include //////////////////////////////////////////////////////////////////////////////// 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 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 matches; + if (autoComplete (value, options, matches) == 1) + { + canonicalized = matches[0]; + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/CLI.h b/src/CLI.h index de6adace..5ea45aff 100644 --- a/src/CLI.h +++ b/src/CLI.h @@ -28,6 +28,7 @@ #define INCLUDED_CLI #include +#include #include // 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 _entities {};