- Now parses the command line and can distinguish regular commands, as well as

custom reports.
This commit is contained in:
Paul Beckingham 2008-11-09 22:46:12 -05:00
parent 6d5309527c
commit 748300631a
5 changed files with 133 additions and 41 deletions

View file

@ -571,6 +571,7 @@ void handleExport (TDB& tdb, T& task, Config& conf)
if (out.good ())
{
out << "'id',"
<< "'uuid',"
<< "'status',"
<< "'tags',"
<< "'entry',"

View file

@ -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)
{
@ -189,8 +224,12 @@ static bool isCommand (const std::string& candidate)
std::vector <std::string> matches;
autoComplete (candidate, options, matches);
if (0 == matches.size ())
{
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;
@ -306,8 +336,13 @@ static bool validCommand (std::string& input)
{
std::string copy = input;
guess ("command", commands, copy);
if (copy == "")
{
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;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -2673,3 +2673,22 @@ void gatherNextTasks (
}
////////////////////////////////////////////////////////////////////////////////
std::string handleCustomReport (
TDB& tdb,
T& task,
Config& conf,
const std::string& report)
{
std::cout << "# woohoo!" << std::endl;
std::stringstream out;
// TODO Load columns.
// TODO Load sort order.
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -763,6 +763,8 @@ std::string runTaskCommand (
std::cout << "[task " << defaultCommand << "]" << std::endl;
}
loadCustomReports (conf);
std::string command;
T task;
parse (args, command, task, conf);
@ -782,22 +784,23 @@ std::string runTaskCommand (
else if (command == "stop") { out = handleStop (tdb, task, conf ); }
else if (command == "undo") { out = handleUndo (tdb, task, conf ); }
else if (command == "stats") { out = handleReportStats (tdb, task, conf ); }
else if (command == "list") { if (gc) tdb.gc (); out = handleList (tdb, task, conf); }
else if (command == "long") { if (gc) tdb.gc (); out = handleLongList (tdb, task, conf); }
else if (command == "ls") { if (gc) tdb.gc (); out = handleSmallList (tdb, task, conf); }
else if (command == "completed") { if (gc) tdb.gc (); out = handleCompleted (tdb, task, conf); }
else if (command == "list") { if (gc) tdb.gc (); out = handleList (tdb, task, conf ); } // TODO replace with Custom
else if (command == "long") { if (gc) tdb.gc (); out = handleLongList (tdb, task, conf ); } // TODO replace with Custom
else if (command == "ls") { if (gc) tdb.gc (); out = handleSmallList (tdb, task, conf ); } // TODO replace with Custom
else if (command == "completed") { if (gc) tdb.gc (); out = handleCompleted (tdb, task, conf ); } // TODO replace with Custom
else if (command == "summary") { if (gc) tdb.gc (); out = handleReportSummary (tdb, task, conf ); }
else if (command == "next") { if (gc) tdb.gc (); out = handleReportNext (tdb, task, conf); }
else if (command == "next") { if (gc) tdb.gc (); out = handleReportNext (tdb, task, conf ); } // TODO replace with Custom
else if (command == "history") { if (gc) tdb.gc (); out = handleReportHistory (tdb, task, conf ); }
else if (command == "ghistory") { if (gc) tdb.gc (); out = handleReportGHistory (tdb, task, conf ); }
else if (command == "calendar") { if (gc) tdb.gc (); out = handleReportCalendar (tdb, task, conf ); }
else if (command == "active") { if (gc) tdb.gc (); out = handleReportActive (tdb, task, conf); }
else if (command == "overdue") { if (gc) tdb.gc (); out = handleReportOverdue (tdb, task, conf); }
else if (command == "oldest") { if (gc) tdb.gc (); out = handleReportOldest (tdb, task, conf); }
else if (command == "newest") { if (gc) tdb.gc (); out = handleReportNewest (tdb, task, conf); }
else if (command == "active") { if (gc) tdb.gc (); out = handleReportActive (tdb, task, conf ); } // TODO replace with Custom
else if (command == "overdue") { if (gc) tdb.gc (); out = handleReportOverdue (tdb, task, conf ); } // TODO replace with Custom
else if (command == "oldest") { if (gc) tdb.gc (); out = handleReportOldest (tdb, task, conf ); } // TODO replace with Custom
else if (command == "newest") { if (gc) tdb.gc (); out = handleReportNewest (tdb, task, conf ); } // TODO replace with Custom
else if (command == "colors") { out = handleColor ( conf ); }
else if (command == "version") { out = handleVersion ( conf ); }
else if (command == "help") { longUsage ( conf ); }
else if (isCustomReport (command)) { if (gc) tdb.gc (); out = handleCustomReport (tdb, task, conf, command); } // New Custom reports
else { shortUsage ( conf ); }
return out;

View file

@ -57,6 +57,8 @@ for (typeof (c) *foreach_p = & (c); \
void parse (std::vector <std::string>&, std::string&, T&, Config&);
bool validPriority (const std::string&);
bool validDate (std::string&, Config&);
void loadCustomReports (Config&);
bool isCustomReport (const std::string&);
// task.cpp
void gatherNextTasks (const TDB&, T&, Config&, std::vector <T>&, std::vector <int>&);
@ -103,6 +105,8 @@ std::string handleReportStats (TDB&, T&, Config&);
std::string handleReportOldest (TDB&, T&, Config&);
std::string handleReportNewest (TDB&, T&, Config&);
std::string handleCustomReport (TDB&, T&, Config&, const std::string&);
// util.cpp
bool confirm (const std::string&);
void wrapText (std::vector <std::string>&, const std::string&, const int);