- Now allows rc:<file> override of ~/.taskrc file.

This commit is contained in:
Paul Beckingham 2008-06-25 01:29:42 -04:00
parent 5d158d752d
commit 1ef7b0c43a
5 changed files with 195 additions and 119 deletions

View file

@ -346,54 +346,60 @@ void parse (
for (size_t i = 0; i < args.size (); ++i)
{
std::string arg (args[i]);
size_t colon; // Pointer to colon in argument.
std::string from;
std::string to;
// An id is the first argument found that contains all digits.
if (command != "add" && // "add" doesn't require an ID
task.getId () == 0 &&
validId (arg))
task.setId (::atoi (arg.c_str ()));
// Tags begin with + or - and contain arbitrary text.
else if (validTag (arg))
// Ignore any argument that is "rc:...", because that is the command line
// specified rc file.
if (arg.substr (0, 3) != "rc:")
{
if (arg[0] == '+')
task.addTag (arg.substr (1, std::string::npos));
else if (arg[0] == '-')
task.addRemoveTag (arg.substr (1, std::string::npos));
}
size_t colon; // Pointer to colon in argument.
std::string from;
std::string to;
// Attributes contain a constant string followed by a colon, followed by a
// value.
else if ((colon = arg.find (":")) != std::string::npos)
{
std::string name = arg.substr (0, colon);
std::string value = arg.substr (colon + 1, std::string::npos);
// An id is the first argument found that contains all digits.
if (command != "add" && // "add" doesn't require an ID
task.getId () == 0 &&
validId (arg))
task.setId (::atoi (arg.c_str ()));
if (validAttribute (name, value, conf))
task.setAttribute (name, value);
}
// Tags begin with + or - and contain arbitrary text.
else if (validTag (arg))
{
if (arg[0] == '+')
task.addTag (arg.substr (1, std::string::npos));
else if (arg[0] == '-')
task.addRemoveTag (arg.substr (1, std::string::npos));
}
// Substitution of description text.
else if (validSubstitution (arg, from, to))
{
task.setSubstitution (from, to);
}
// Attributes contain a constant string followed by a colon, followed by a
// value.
else if ((colon = arg.find (":")) != std::string::npos)
{
std::string name = arg.substr (0, colon);
std::string value = arg.substr (colon + 1, std::string::npos);
// Command.
else if (command == "")
{
if (!isCommand (arg))
if (validAttribute (name, value, conf))
task.setAttribute (name, value);
}
// Substitution of description text.
else if (validSubstitution (arg, from, to))
{
task.setSubstitution (from, to);
}
// Command.
else if (command == "")
{
if (!isCommand (arg))
descCandidate += std::string (arg) + " ";
else if (validCommand (arg))
command = arg;
}
// Anything else is just considered description.
else
descCandidate += std::string (arg) + " ";
else if (validCommand (arg))
command = arg;
}
// Anything else is just considered description.
else
descCandidate += std::string (arg) + " ";
}
if (validDescription (descCandidate))

View file

@ -45,7 +45,7 @@
#endif
////////////////////////////////////////////////////////////////////////////////
void shortUsage (Config& conf)
static void shortUsage (Config& conf)
{
Table table;
int width = conf.get ("defaultwidth", 80);
@ -193,7 +193,7 @@ void shortUsage (Config& conf)
}
////////////////////////////////////////////////////////////////////////////////
void longUsage (Config& conf)
static void longUsage (Config& conf)
{
shortUsage (conf);
@ -210,6 +210,7 @@ void longUsage (Config& conf)
<< " due: Due date" << "\n"
<< " fg: Foreground color" << "\n"
<< " bg: Background color" << "\n"
<< " rc: Alternate .taskrc file" << "\n"
<< "\n"
<< "Any command or attribute name may be abbreviated if still unique:" << "\n"
<< " task list project:Home" << "\n"
@ -224,6 +225,32 @@ void longUsage (Config& conf)
<< std::endl;
}
////////////////////////////////////////////////////////////////////////////////
void loadConfFile (int argc, char** argv, Config& conf)
{
for (int i = 1; i < argc; ++i)
{
if (! strncmp (argv[i], "rc:", 3))
{
if (! access (&(argv[i][3]), F_OK))
{
std::string file = &(argv[i][3]);
conf.load (file);
return;
}
else
throw std::string ("Could not read configuration file '") + &(argv[i][3]) + "'";
}
}
struct passwd* pw = getpwuid (getuid ());
if (!pw)
throw std::string ("Could not read home directory from passwd file.");
std::string file = pw->pw_dir;
conf.createDefault (file);
}
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
@ -242,13 +269,7 @@ int main (int argc, char** argv)
// Load the config file from the home directory. If the file cannot be
// found, offer to create a sample one.
Config conf;
struct passwd* pw = getpwuid (getuid ());
if (!pw)
throw std::string ("Could not read home directory from passwd file.");
// Create a default config file and data directory if necessary.
std::string home = pw->pw_dir;
conf.createDefault (home);
loadConfFile (argc, argv, conf);
TDB tdb;
tdb.dataDirectory (conf.get ("data.location"));