Commit cc23debb authored by Benoit Perrot's avatar Benoit Perrot
Browse files

Enhance option engine.

parent f69d88bf
2003-07-27 Benot Perrot <benoit@lrde.epita.fr>
* src/task/task_register.hh, src/task/task_register.cc:
Disambiguate uncomplete options. Clean usage display.
* src/mipsy.cc: Launch "--execute" if no option is specified.
2003-07-26 Benot Perrot <benoit@lrde.epita.fr>
* dev/mipsy-mk-inst-gen.py: Distribute section.hh.
......
......@@ -35,9 +35,8 @@ int main(int argc, char* argv[])
program_name = argv[0];
filename = task::TaskRegister::instance().parse_args(argc, argv);
// if (task::TaskRegister::instance().nb_of_task_to_execute_get () == 0)
// task::TaskRegister::instance().enable_task("execute");
if (task::TaskRegister::instance().enabled_tasks ().size () == 0)
task::TaskRegister::instance().enable_task("execute");
exit_status = exit_success;
task::TaskRegister::instance().execute();
......
......@@ -30,6 +30,10 @@
namespace task
{
// --------------------------------------------------------------------------
// Task Register Engine
// --------------------------------------------------------------------------
// Register a task.
void
TaskRegister::register_task (const Task& task)
......@@ -54,8 +58,16 @@ namespace task
_enabled_tasks.push_back(&task);
}
void
TaskRegister::enable_task (const std::string& task_name)
TaskRegister::enable_task (std::string task_name)
{
assertion(task_name.size() > 0);
if (task_name[0] != '-')
{
if (task_name.size() > 1)
task_name = "--" + task_name;
else
task_name = "-" + task_name;
}
assertion(has_key(_tasks, task_name));
_enabled_tasks.push_back(_tasks[task_name]);
}
......@@ -93,6 +105,70 @@ namespace task
}
// Parse arguments
char*
TaskRegister::parse_args (int argc, char *argv[])
{
char* res = 0;
const Task* task = 0;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
// if (task != 0)
// {
// enable_task(*task, arg);
// task = 0;
// continue;
// }
if ((arg[0] == '-') && (arg.size () > 1))
{
// FIXME: split on '=' for =ARG forms
std::map<std::string, Task const*>::const_iterator
it = _tasks.lower_bound(arg), nit = it;
if (it == _tasks.end() || it->first.find(arg) != 0)
{
std::cerr << program_name << ": unrecognized option `"
<< arg << "'" << std::endl;
continue;
}
if ((++nit)->first.find(arg) == 0)
{
std::cerr << program_name << ": ambiguous option `"
<< arg << "'" << std::endl;
continue;
}
task = (*it).second;
// if (task->has_arg())
// continue;
enable_task(*task);
task = 0;
}
else
res = argv[i];
}
// if (task != 0)
// std::cerr << program_name
// << ": option `" << task->long_opt() << "' takes an argument"
// << std::endl;
return res;
}
// Execute tasks, checking dependencies.
void
TaskRegister::execute (void)
{
resolve_dependencies();
for (std::list<Task const *>::const_iterator
it = _enabled_tasks.begin (); it != _enabled_tasks.end (); ++it)
(*it)->execute ();
}
// --------------------------------------------------------------------------
// Task Register Options
// --------------------------------------------------------------------------
// Display version
void
TaskRegister::version(std::ostream& ostr)
......@@ -108,6 +184,7 @@ namespace task
<< "under certain conditions; see source for details."
<< std::endl << std::endl;
}
// Display usage
void
TaskRegister::usage(std::ostream& ostr)
......@@ -118,8 +195,23 @@ namespace task
if ((*it).first.size() == 2)
ostr << (*it).second->short_opt();
ostr << "] ";
ostr << std::endl << std::endl;
unsigned len = 80;
for (std::map<std::string, Task const*>::const_iterator
it = _tasks.begin(); it != _tasks.end(); ++it)
{
if (len + (*it).first.size () + 3 >= 80)
{
ostr << std::endl << " ";
len = 0;
}
if ((*it).first.size () > 2)
ostr << "[" << (*it).first << "] ";
len += (*it).first.size () + 3;
}
ostr << std::endl;
}
// Display help
void
TaskRegister::help(std::ostream& ostr)
......@@ -148,6 +240,7 @@ namespace task
}
ostr << "Reports bugs to "PACKAGE_BUGREPORT << std::endl;
}
// Display tasks selection
void
TaskRegister::selection(std::ostream& ostr)
......@@ -157,59 +250,5 @@ namespace task
it = _enabled_tasks.begin (); it != _enabled_tasks.end (); ++it)
ostr << "\t* " << (*it)->long_opt() << std::endl;
}
// Parse arguments
char*
TaskRegister::parse_args (int argc, char *argv[])
{
char* res = 0;
const Task* task = 0;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (task != 0)
{
// task->execute(arg);
task = 0;
continue;
}
if (arg[0] == '-')
{
// FIXME: split on '=' for =ARG forms
std::map<std::string, Task const*>::const_iterator it;
if ((it = _tasks.find(arg)) == _tasks.end())
{
std::cerr << program_name << ": unrecognized option `"
<< arg << "'"
<< std::endl;
continue;
}
task = (*it).second;
// if (task->has_arg())
// continue;
enable_task(*task);
task = 0;
}
else
res = argv[i];
}
if (task != 0)
std::cerr << program_name
<< ": option `" << task->long_opt() << "' takes an argument"
<< std::endl;
resolve_dependencies();
return res;
}
// Execute tasks, checking dependencies.
void
TaskRegister::execute (void)
{
for (std::list<Task const *>::const_iterator
it = _enabled_tasks.begin (); it != _enabled_tasks.end (); ++it)
(*it)->execute ();
}
} // namespace task
......@@ -40,16 +40,17 @@ namespace task
return unique;
}
public:
const std::list<Task const*>& enabled_tasks () const
{
return _enabled_tasks;
}
public:
void register_task (const Task& task);
void enable_task (const Task& task);
void enable_task (const std::string& task_name);
void enable_task (std::string task_name);
public:
void version(std::ostream& ostr);
void usage(std::ostream& ostr);
void help(std::ostream& ostr);
void selection(std::ostream& ostr);
public:
char* parse_args (int argc, char *argv[]);
......@@ -60,6 +61,13 @@ namespace task
std::set<Task const*>& visited_tasks,
std::list<Task const*>& sorted_tasks);
void resolve_dependencies();
public:
void version(std::ostream& ostr);
void usage(std::ostream& ostr);
void help(std::ostream& ostr);
void selection(std::ostream& ostr);
private:
std::map<std::string, std::list<Task const*> > _modules;
std::map<std::string, Task const*> _tasks;
......
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