CLI: Added ::findCommand

This commit is contained in:
Paul Beckingham 2016-04-02 17:31:22 -04:00
parent 8180104d45
commit af5ee87773
2 changed files with 34 additions and 0 deletions

View file

@ -221,6 +221,7 @@ void CLI::analyze ()
_args.clear ();
handleArg0 ();
lexArguments ();
findCommand ();
}
////////////////////////////////////////////////////////////////////////////////
@ -285,6 +286,38 @@ const std::string CLI::dump (const std::string& title) const
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
// Scan all arguments and if any are an exact match for a command name, then
// tag as CMD. If an argument is an exact match for an attribute, despite being
// an inexact match for a command, then it is not a command.
bool CLI::findCommand ()
{
for (auto& a : _args)
{
auto raw = a.attribute ("raw");
std::string canonical;
// If the arg canonicalized to a 'cmd', but is also not an exact match
// for an 'attribute', proceed. Example:
// task project=foo list
// ^cmd ^cmd
// ^attribute
if (exactMatch ("command", raw))
canonical = raw;
else if (! canonicalize (canonical, "command", raw))
continue;
a.attribute ("canonical", canonical);
a.tag ("CMD");
// Stop and indicate command found.
return true;
}
// Indicate command not found.
return false;
}
////////////////////////////////////////////////////////////////////////////////
// Search for exact 'value' in _entities category.
bool CLI::exactMatch (

View file

@ -65,6 +65,7 @@ public:
private:
void handleArg0 ();
void lexArguments ();
bool findCommand ();
bool exactMatch (const std::string&, const std::string&) const;
public: