- Now parses the command line and can distinguish regular commands, as well as
custom reports.
This commit is contained in:
@@ -35,6 +35,8 @@
|
||||
#include "T.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// NOTE: These are static arrays only because there is no initializer list for
|
||||
// std::vector.
|
||||
static const char* colors[] =
|
||||
{
|
||||
"bold",
|
||||
@@ -147,6 +149,9 @@ static const char* commands[] =
|
||||
"",
|
||||
};
|
||||
|
||||
static std::vector <std::string> customReports;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void guess (const std::string& type, const char** list, std::string& candidate)
|
||||
{
|
||||
std::vector <std::string> options;
|
||||
@@ -180,6 +185,36 @@ void guess (const std::string& type, const char** list, std::string& candidate)
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void guess (const std::string& type, std::vector<std::string>& options, std::string& candidate)
|
||||
{
|
||||
std::vector <std::string> matches;
|
||||
autoComplete (candidate, options, matches);
|
||||
if (1 == matches.size ())
|
||||
candidate = matches[0];
|
||||
|
||||
else if (0 == matches.size ())
|
||||
// throw std::string ("Unrecognized ") + type + " '" + candidate + "'";
|
||||
candidate = "";
|
||||
|
||||
else
|
||||
{
|
||||
std::string error = "Ambiguous ";
|
||||
error += type;
|
||||
error += " '";
|
||||
error += candidate;
|
||||
error += "' - could be either of ";
|
||||
for (size_t i = 0; i < matches.size (); ++i)
|
||||
{
|
||||
if (i)
|
||||
error += ", ";
|
||||
error += matches[i];
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static bool isCommand (const std::string& candidate)
|
||||
{
|
||||
@@ -190,7 +225,11 @@ static bool isCommand (const std::string& candidate)
|
||||
std::vector <std::string> matches;
|
||||
autoComplete (candidate, options, matches);
|
||||
if (0 == matches.size ())
|
||||
return false;
|
||||
{
|
||||
autoComplete (candidate, customReports, matches);
|
||||
if (0 == matches.size ())
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -284,15 +323,6 @@ static bool validTag (const std::string& input)
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static bool validDescription (const std::string& input)
|
||||
{
|
||||
/*
|
||||
if (input.length () > 0 &&
|
||||
input.find ("\r") == std::string::npos &&
|
||||
input.find ("\f") == std::string::npos &&
|
||||
input.find ("\n") == std::string::npos)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
*/
|
||||
if (input.length () == 0) return false;
|
||||
if (input.find ("\r") != std::string::npos) return false;
|
||||
if (input.find ("\f") != std::string::npos) return false;
|
||||
@@ -307,7 +337,12 @@ static bool validCommand (std::string& input)
|
||||
std::string copy = input;
|
||||
guess ("command", commands, copy);
|
||||
if (copy == "")
|
||||
return false;
|
||||
{
|
||||
copy = input;
|
||||
guess ("command", customReports, copy);
|
||||
if (copy == "")
|
||||
return false;
|
||||
}
|
||||
|
||||
input = copy;
|
||||
return true;
|
||||
@@ -450,4 +485,34 @@ void parse (
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void loadCustomReports (Config& conf)
|
||||
{
|
||||
std::vector <std::string> all;
|
||||
conf.all (all);
|
||||
|
||||
foreach (i, all)
|
||||
{
|
||||
if (i->substr (0, 7) == "report.")
|
||||
{
|
||||
std::string report = i->substr (7, std::string::npos);
|
||||
unsigned int columns = report.find (".columns");
|
||||
if (columns != std::string::npos)
|
||||
{
|
||||
report = report.substr (0, columns);
|
||||
customReports.push_back (report);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool isCustomReport (const std::string& report)
|
||||
{
|
||||
foreach (i, customReports)
|
||||
if (*i == report)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
Reference in New Issue
Block a user