From af5ee877732c20c03ebf08b7cf9ef9b0be711a2d Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 2 Apr 2016 17:31:22 -0400 Subject: [PATCH] CLI: Added ::findCommand --- src/CLI.cpp | 33 +++++++++++++++++++++++++++++++++ src/CLI.h | 1 + 2 files changed, 34 insertions(+) diff --git a/src/CLI.cpp b/src/CLI.cpp index e975f2b7..0dff0dde 100644 --- a/src/CLI.cpp +++ b/src/CLI.cpp @@ -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 ( diff --git a/src/CLI.h b/src/CLI.h index 8f6d580e..0e2e4e16 100644 --- a/src/CLI.h +++ b/src/CLI.h @@ -65,6 +65,7 @@ public: private: void handleArg0 (); void lexArguments (); + bool findCommand (); bool exactMatch (const std::string&, const std::string&) const; public: