From 08e65445760a453a95f9039f9ea6ab626789a12d Mon Sep 17 00:00:00 2001 From: schodet Date: Wed, 19 Apr 2006 18:49:31 +0000 Subject: Ajout de la gestion de l'aide dans l'interpreteur. --- i/marvin/src/interpreter/interpreter.cc | 36 +++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'i/marvin/src/interpreter/interpreter.cc') diff --git a/i/marvin/src/interpreter/interpreter.cc b/i/marvin/src/interpreter/interpreter.cc index bdc918a..0c067cf 100644 --- a/i/marvin/src/interpreter/interpreter.cc +++ b/i/marvin/src/interpreter/interpreter.cc @@ -30,15 +30,15 @@ Interpreter::~Interpreter (void) { for (Funcs::iterator i = funcs_.begin (); i != funcs_.end (); ++i) { - delete i->second; + delete i->second.func; } } /// Add a function, Interpreter owns f. void -Interpreter::add (const std::string &s, Func *f) +Interpreter::add (const std::string &s, Func *f, const std::string &desc) { - if (!funcs_.insert (Funcs::value_type (s, f)).second) + if (!funcs_.insert (Funcs::value_type (s, FuncDesc (f, desc))).second) { // Interpreter owns f, therefore, f must be deleted. delete f; @@ -47,6 +47,13 @@ Interpreter::add (const std::string &s, Func *f) } } +/// Add a function without description, Interpreter owns f. +void +Interpreter::add (const std::string &s, Func *f) +{ + add (s, f, ""); +} + /// Call a function by name. void Interpreter::call (const std::string &s, const Args &a, @@ -58,7 +65,7 @@ Interpreter::call (const std::string &s, const Args &a, throw std::runtime_error ("function \'" + s + "\' does not exist"); try { - (*i->second) (a, dryrun); + (*i->second.func) (a, dryrun); } catch (const std::exception &e) { @@ -83,3 +90,24 @@ Interpreter::interpretFile (const std::string &file, bool dryrun/*false*/) p.parseFile (file); } +/// Return an help string. +std::string +Interpreter::help (void) const +{ + std::string ret; + for (Funcs::const_iterator i = funcs_.begin (); i != funcs_.end (); ++i) + { + // Well, if you really want to implement this better, please do... + for (std::string::const_iterator j = i->second.desc.begin (); + j != i->second.desc.end (); j++) + { + if (*j == '\n') + ret += "\n "; + else + ret += *j; + } + ret += '\n'; + } + return ret; +} + -- cgit v1.2.3