Commit 9b49936e authored by Benoit Perrot's avatar Benoit Perrot
Browse files

2006-04-17 Benoit Perrot <benoit@lrde.epita.fr>

	Make extraction of option's value more conventional.

	* src/task/task_register.cc: Split argv[] on `=' for long options'
	values only. Split argv[] in place for short options' value. Use
	the next argv[] when no value is provided.
parent 6ae5fd93
2006-04-17 Benoit Perrot <benoit@lrde.epita.fr>
Make extraction of option's value more conventional.
* src/task/task_register.cc: Split argv[] on `=' for long options'
values only. Split argv[] in place for short options' value. Use
the next argv[] when no value is provided.
2006-03-06 Roland Levillain <roland@lrde.epita.fr>
* vcs/nolimips.rb: Use the right e-mail address.
......
......@@ -143,7 +143,7 @@ namespace task
char*
TaskRegister::parse_args (int argc, char *argv[])
{
char* res = 0;
char *res = 0;
for (int i = 1; i < argc; ++i)
{
......@@ -151,50 +151,91 @@ namespace task
if ((1 < arg.size ()) && (arg[0] == '-'))
{
std::string value;
std::string *value = 0;
// Identify option
TaskRegister::const_task_iterator it(tasks_.end());
if (arg[1] == '-') // Long option
{
// Split on `=' for value
unsigned eq = arg.find('=');
if (eq < arg.size())
{
value = std::string(arg, eq +1);
// +1: do not include `=' in value
value = new std::string(arg, eq + 1);
arg.resize(eq);
}
// Identify option
TaskRegister::const_task_iterator it(tasks_.end());
if (arg[1] == '-') // Long option
// Search for long option
it = find_task(arg);
else // Short option
for (unsigned l = 1; l < arg.size(); ++l)
}
else // Short option(s)
{
for (unsigned l = 1; l < arg.size() && !value; ++l)
{
std::string short_option("-");
short_option += arg[l];
// Search for short option
it = find_task(short_option);
if (it != tasks_.end() && (l < arg.size() -1))
// Let last task fall through the remaining of the
// routine
if (l + 1 < arg.size())
if (it != tasks_.end())
{
if (it->second->needs_value())
// Split here for value
value = new std::string(arg, l + 1);
else
// Enable task
enable_task(*(it->second));
}
}
}
// Consider value
// Enable (long or last short) task and set value if needed
if (it != tasks_.end())
{
const Task *task = it->second;
if (!task->needs_value() && !value.empty())
if (task->needs_value())
{
if (!value)
{
++i;
if (i < argc)
value = new std::string(argv[i]);
}
if (!value)
std::cerr
<< program_name
<< ": option `" << task->long_opt()
<< "' does not take a value" << std::endl;
else if (task->needs_value() && value.empty())
<< "' takes a value" << std::endl;
else
{
task->set_value(*value);
enable_task(*task);
}
}
else
{
if (value)
std::cerr
<< program_name
<< ": option `" << task->long_opt()
<< "' takes a value" << std::endl;
else if (value.empty() || task->set_value(value))
<< "' does not take a value" << std::endl;
else
enable_task(*task);
}
else if (!value.empty())
}
else if (value)
std::cerr
<< program_name
<< ": `"<< value << "': unexpected value" << std::endl;
<< ": `"<< *value << "': unexpected value" << std::endl;
delete value;
}
else
res = argv[i];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment