Argument Parsing

- Added proper handling for sequences, in that they must be contiguous.
- Added placeholder code for default command, and automatic info report.
- Eliminated Context::parse.
- Eliminated Context::run Timer, because when it goes out of scope, it
  adds timing messages to the deubg output, which at the end of Context::run
  has already been displayed.  In addition, the Context::dispatch timer
  is about 0.2 milliseconds shorter, so the two are redundant.
This commit is contained in:
Paul Beckingham 2011-06-04 18:42:33 -04:00
parent 19aa78a922
commit f9c1820740
5 changed files with 198 additions and 321 deletions

View file

@ -141,10 +141,9 @@ void Context::initialize (int argc, const char** argv)
////////////////////////////////////////////////////////////////////////////////
int Context::run ()
{
Timer t ("Context::run");
int rc;
std::string output;
try
{
rc = dispatch (output);
@ -407,254 +406,6 @@ void Context::loadAliases ()
aliases[var->substr (6)] = config.get (*var);
}
////////////////////////////////////////////////////////////////////////////////
/*
void Context::parse (
std::vector <std::string>& parseArgs,
Cmd& parseCmd,
Task& parseTask,
Sequence& parseSequence,
Subst& parseSubst,
Filter& parseFilter)
{
Timer t ("Context::parse");
Att attribute;
tagAdditions.clear ();
tagRemovals.clear ();
std::string descCandidate = "";
bool terminated = false;
bool foundSequence = false;
bool foundSomethingAfterSequence = false;
bool foundNonSequence = false;
foreach (arg, parseArgs)
{
if (!terminated)
{
// The '--' argument shuts off all parsing - everything is an argument.
if (*arg == "--")
{
debug ("parse terminator '" + *arg + "'");
terminated = true;
}
// Sequence
// Note: "add" doesn't require an ID
else if (parseCmd.command != "add" &&
! foundSomethingAfterSequence &&
parseSequence.valid (*arg))
{
debug ("parse sequence '" + *arg + "'");
parseSequence.parse (*arg);
foundSequence = true;
}
// Tags to include begin with '+'.
else if (arg->length () > 1 &&
(*arg)[0] == '+' &&
noSpaces (*arg))
{
debug ("parse tag addition '" + *arg + "'");
if (foundSequence)
foundSomethingAfterSequence = true;
foundNonSequence = true;
if (arg->find (',') != std::string::npos)
throw std::string (STRING_TAGS_NO_COMMAS);
tagAdditions.push_back (arg->substr (1));
parseTask.addTag (arg->substr (1));
}
// Tags to remove begin with '-'.
else if (arg->length () > 1 &&
(*arg)[0] == '-' &&
noSpaces (*arg))
{
debug ("parse tag removal '" + *arg + "'");
if (foundSequence)
foundSomethingAfterSequence = true;
foundNonSequence = true;
if (arg->find (',') != std::string::npos)
throw std::string (STRING_TAGS_NO_COMMAS);
tagRemovals.push_back (arg->substr (1));
}
// Substitution of description and/or annotation text.
else if (parseSubst.valid (*arg))
{
if (foundSequence)
foundSomethingAfterSequence = true;
foundNonSequence = true;
debug ("parse subst '" + *arg + "'");
parseSubst.parse (*arg);
}
// Atributes - name[.mod]:[value]
else if (attribute.valid (*arg))
{
debug ("parse attribute '" + *arg + "'");
if (foundSequence)
foundSomethingAfterSequence = true;
foundNonSequence = true;
attribute.parse (*arg);
// There has to be a better way. And it starts with a fresh coffee.
std::string name = attribute.name ();
std::string mod = attribute.mod ();
std::string value = attribute.value ();
if (attribute.validNameValue (name, mod, value))
{
attribute.name (name);
attribute.mod (mod);
attribute.value (value);
// Preserve modifier in the key, to allow multiple modifiers on the
// same attribute. Bug #252.
if (name != "" && mod != "")
parseTask[name + "." + mod] = attribute;
else
parseTask[name] = attribute;
autoFilter (attribute, parseFilter);
}
// *arg has the appearance of an attribute (foo:bar), but isn't
// recognized, so downgrade it to part of the description.
else
{
if (foundSequence)
foundSomethingAfterSequence = true;
foundNonSequence = true;
if (descCandidate.length ())
descCandidate += " ";
descCandidate += *arg;
}
}
// It might be a command if one has not already been found.
else if (parseCmd.command == "" &&
parseCmd.valid (*arg))
{
debug ("parse cmd '" + *arg + "'");
parseCmd.parse (*arg);
if (foundSequence)
foundSomethingAfterSequence = true;
foundNonSequence = true;
}
// Anything else is just considered description.
else
{
if (foundSequence)
foundSomethingAfterSequence = true;
foundNonSequence = true;
if (descCandidate.length ())
descCandidate += " ";
descCandidate += *arg;
}
}
// Command is terminated, therefore everything subsequently is a description.
else
{
debug ("parse post-termination description '" + *arg + "'");
if (foundSequence)
foundSomethingAfterSequence = true;
if (descCandidate.length ())
descCandidate += " ";
descCandidate += *arg;
}
}
if (descCandidate != "" && noVerticalSpace (descCandidate))
{
debug ("parse description '" + descCandidate + "'");
parseTask.set ("description", descCandidate);
foundNonSequence = true;
// Now convert the description to a filter on each word, if necessary.
if (parseCmd.isReadOnlyCommand ())
{
std::vector <std::string> words;
split (words, descCandidate, ' ');
std::vector <std::string>::iterator it;
for (it = words.begin (); it != words.end (); ++it)
{
Att a ("description", "contains", *it);
autoFilter (a, parseFilter);
}
}
}
// At this point, either a sequence or a command should have been found.
if (parseSequence.size () == 0 && parseCmd.command == "")
parseCmd.parse (descCandidate);
// Read-only command (reports, status, info ...) use filters. Write commands
// (add, done ...) do not. The filter was constructed iteratively above, but
// tags were omitted, so they are added now.
if (parseCmd.isReadOnlyCommand ())
autoFilter (parseFilter);
// If no command was specified, and there were no command line arguments
// then invoke the default command.
if (parseCmd.command == "")
{
if (parseArgs.size () == 0)
{
// Apply overrides, if any.
std::string defaultCommand = config.get ("default.command");
if (defaultCommand != "")
{
// Add on the overrides.
defaultCommand += " " + file_override + " " + var_overrides;
// Stuff the command line.
args.clear ();
split (args, defaultCommand, ' ');
header ("[task " + trim (defaultCommand) + "]");
// Reinitialize the context and recurse.
file_override = "";
var_overrides = "";
footnotes.clear ();
//initialize ();
parse (args, cmd, task, sequence, subst, filter);
}
else
throw std::string (STRING_TRIVIAL_INPUT);
}
// If the command "task 123" is entered, but with no modifier arguments,
// then the actual command is assumed to be "info".
else if (!foundNonSequence &&
(parseTask.id != 0 || parseSequence.size () != 0))
{
std::cout << STRING_ASSUME_INFO << "\n";
parseCmd.command = "info";
}
}
}
*/
////////////////////////////////////////////////////////////////////////////////
void Context::decomposeSortField (
const std::string& field,