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> 2006-03-06 Roland Levillain <roland@lrde.epita.fr>
* vcs/nolimips.rb: Use the right e-mail address. * vcs/nolimips.rb: Use the right e-mail address.
......
...@@ -143,7 +143,7 @@ namespace task ...@@ -143,7 +143,7 @@ namespace task
char* char*
TaskRegister::parse_args (int argc, char *argv[]) TaskRegister::parse_args (int argc, char *argv[])
{ {
char* res = 0; char *res = 0;
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i)
{ {
...@@ -151,50 +151,91 @@ namespace task ...@@ -151,50 +151,91 @@ namespace task
if ((1 < arg.size ()) && (arg[0] == '-')) if ((1 < arg.size ()) && (arg[0] == '-'))
{ {
std::string value; std::string *value = 0;
unsigned eq = arg.find('=');
if (eq < arg.size())
{
value = std::string(arg, eq +1);
arg.resize(eq);
}
// Identify option // Identify option
TaskRegister::const_task_iterator it(tasks_.end()); TaskRegister::const_task_iterator it(tasks_.end());
if (arg[1] == '-') // Long option if (arg[1] == '-') // Long option
it = find_task(arg); {
else // Short option // Split on `=' for value
for (unsigned l = 1; l < arg.size(); ++l) unsigned eq = arg.find('=');
{ if (eq < arg.size())
std::string short_option("-"); {
short_option += arg[l]; // +1: do not include `=' in value
value = new std::string(arg, eq + 1);
it = find_task(short_option); arg.resize(eq);
if (it != tasks_.end() && (l < arg.size() -1)) }
enable_task(*(it->second));
} // Search for long option
it = find_task(arg);
// Consider value }
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);
// 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));
}
}
}
// Enable (long or last short) task and set value if needed
if (it != tasks_.end()) if (it != tasks_.end())
{ {
const Task *task = it->second; const Task *task = it->second;
if (!task->needs_value() && !value.empty())
std::cerr if (task->needs_value())
<< program_name {
<< ": option `" << task->long_opt() if (!value)
<< "' does not take a value" << std::endl; {
else if (task->needs_value() && value.empty()) ++i;
std::cerr if (i < argc)
<< program_name value = new std::string(argv[i]);
<< ": option `" << task->long_opt() }
<< "' takes a value" << std::endl;
else if (value.empty() || task->set_value(value)) if (!value)
enable_task(*task); std::cerr
<< program_name
<< ": option `" << task->long_opt()
<< "' takes a value" << std::endl;
else
{
task->set_value(*value);
enable_task(*task);
}
}
else
{
if (value)
std::cerr
<< program_name
<< ": option `" << task->long_opt()
<< "' does not take a value" << std::endl;
else
enable_task(*task);
}
} }
else if (!value.empty()) else if (value)
std::cerr std::cerr
<< program_name << program_name
<< ": `"<< value << "': unexpected value" << std::endl; << ": `"<< *value << "': unexpected value" << std::endl;
delete value;
} }
else else
res = argv[i]; 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