- ::findCommand now checks for any abbreviated command.  If it finds either an
  exact match, or no ambiguity, it checks again to determine READCMD, WRITECMD
  or HELPER.
This commit is contained in:
Paul Beckingham 2014-05-26 15:05:56 -04:00
parent a4d908d8b2
commit 1b19414178
3 changed files with 34 additions and 19 deletions

View file

@ -156,6 +156,8 @@ int Context::initialize (int argc, const char** argv)
std::map <std::string, Command*>::iterator cmd;
for (cmd = commands.begin (); cmd != commands.end (); ++cmd)
{
parser.entity ("cmd", cmd->first);
if (cmd->first[0] == '_')
parser.entity ("helper", cmd->first);
else if (cmd->second->read_only ())

View file

@ -165,6 +165,29 @@ void Parser::entity (const std::string& name, const std::string& value)
_entities.insert (std::pair <std::string, std::string> (name, value));
}
////////////////////////////////////////////////////////////////////////////////
// Search for exact 'value' in _entities category.
bool Parser::exactMatch (
const std::string& category,
const std::string& value) const
{
// Find the category.
std::pair <std::multimap <std::string, std::string>::const_iterator, std::multimap <std::string, std::string>::const_iterator> c;
c = _entities.equal_range (category);
// Extract a list of entities for category.
std::vector <std::string> options;
std::multimap <std::string, std::string>::const_iterator e;
for (e = c.first; e != c.second; ++e)
{
// Shortcut: if an exact match is found, success.
if (value == e->second)
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
// Search for 'value' in _entities category, return canonicalized value.
bool Parser::canonicalize (
@ -278,31 +301,20 @@ void Parser::findCommand ()
if (! (*i)->hasTag ("?"))
continue;
if (canonicalize (command, "writecmd", (*i)->attribute ("raw")))
if (canonicalize (command, "cmd", (*i)->attribute ("raw")))
{
(*i)->unTag ("?");
(*i)->tag ("CMD");
(*i)->attribute ("canonical", command);
if (exactMatch ("writecmd", (*i)->attribute ("raw")))
(*i)->tag ("WRITECMD");
(*i)->attribute ("canonical", command);
break;
}
else if (canonicalize (command, "readcmd", (*i)->attribute ("raw")))
{
(*i)->unTag ("?");
(*i)->tag ("CMD");
else if (exactMatch ("writecmd", (*i)->attribute ("raw")))
(*i)->tag ("READCMD");
(*i)->attribute ("canonical", command);
break;
}
else if (canonicalize (command, "helper", (*i)->attribute ("raw")))
{
(*i)->unTag ("?");
(*i)->tag ("CMD");
else if (exactMatch ("writecmd", (*i)->attribute ("raw")))
(*i)->tag ("HELPER");
(*i)->attribute ("canonical", command);
break;
return;
}
}
}

View file

@ -43,6 +43,7 @@ public:
Tree* tree ();
Tree* parse ();
void entity (const std::string&, const std::string&);
bool exactMatch (const std::string&, const std::string&) const;
bool canonicalize (std::string&, const std::string&, const std::string&) const;
void findBinary ();