mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
CLI: Added ::canonicalize
This commit is contained in:
parent
b624aee186
commit
644439e587
2 changed files with 36 additions and 0 deletions
34
src/CLI.cpp
34
src/CLI.cpp
|
@ -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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -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 {};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue