Index: ChangeLog
from Benoit Perrot <benoit(a)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.
Index: src/task/task_register.cc
--- src/task/task_register.cc (revision 216)
+++ src/task/task_register.cc (working copy)
@@ -151,50 +151,91 @@
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];