From 0e7bc7bb9ff3be0b28a7c60b624f0b75e780f99f Mon Sep 17 00:00:00 2001 From: dufourj Date: Thu, 20 Apr 2006 21:00:28 +0000 Subject: Tester : - better manage optargs; - correct the placement of the data_circular_buffer_factory. Interpreter : - better help format. --- i/marvin/src/interpreter/interpreter.cc | 19 ++++++---- i/marvin/src/tester/test_tester.cc | 16 ++++---- i/marvin/src/tester/tester.cc | 66 ++++++++++++++++++--------------- i/marvin/src/tester/tester.hh | 32 +++++++++------- 4 files changed, 76 insertions(+), 57 deletions(-) (limited to 'i/marvin') diff --git a/i/marvin/src/interpreter/interpreter.cc b/i/marvin/src/interpreter/interpreter.cc index 0c067cf..a160002 100644 --- a/i/marvin/src/interpreter/interpreter.cc +++ b/i/marvin/src/interpreter/interpreter.cc @@ -97,14 +97,19 @@ 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++) + ret += i->first; + if (!i->second.desc.empty ()) { - if (*j == '\n') - ret += "\n "; - else - ret += *j; + ret += " : "; + // 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'; } diff --git a/i/marvin/src/tester/test_tester.cc b/i/marvin/src/tester/test_tester.cc index b89b25f..d292c7f 100644 --- a/i/marvin/src/tester/test_tester.cc +++ b/i/marvin/src/tester/test_tester.cc @@ -30,9 +30,10 @@ class TestTester : public Tester // Constructor TestTester (int argc, char ** argv) : Tester (argc, argv) { } - void funcA (const Interpreter::Args &a) + void funcA (const Interpreter::Args &a, bool dryrun) { - std::cout << " a " << a << std::endl; + if (!dryrun) + std::cout << " a " << a << std::endl; } void funcB (void) { @@ -52,12 +53,13 @@ class TestTester : public Tester } void preRun (void) { + Interpreter &interpreter = getInterpreter (); // Add functions. - add ("a", Interpreter::memFunc (*this, &TestTester::funcA)); - add ("b", Interpreter::memFunc (*this, &TestTester::funcB)); - add ("c", Interpreter::memFunc (*this, &TestTester::funcC)); - add ("d", Interpreter::memFunc (*this, &TestTester::funcD)); - add ("e", Interpreter::memFunc (*this, &TestTester::funcE)); + interpreter.add ("a", Interpreter::memFunc (*this, &TestTester::funcA), "Function a (any)"); + interpreter.add ("b", Interpreter::memFunc (*this, &TestTester::funcB), "Function b ()"); + interpreter.add ("c", Interpreter::memFunc (*this, &TestTester::funcC), "Function c (int)"); + interpreter.add ("d", Interpreter::memFunc (*this, &TestTester::funcD), "Function d (string)"); + interpreter.add ("e", Interpreter::memFunc (*this, &TestTester::funcE), "Function e (int, string, double)"); } }; diff --git a/i/marvin/src/tester/tester.cc b/i/marvin/src/tester/tester.cc index 58e62c8..2532f7b 100644 --- a/i/marvin/src/tester/tester.cc +++ b/i/marvin/src/tester/tester.cc @@ -30,8 +30,9 @@ #include // std::ostringstream /// Constructor. -Tester::Tester (int argc, char **argv) - : config_ (argc, argv), program_(argv[0]) +Tester::Tester (int argc, char **argv, const std::string &description) + : config_ (argc, argv), program_(argv[0]), description_ (description), + showCmd_ (false) { // Parse command line getOpt (argc, argv); @@ -42,10 +43,11 @@ Tester::Tester (int argc, char **argv) /// - -c 'commands list' /// - -f /// - -h : help +/// - -l : list commands suppoted void Tester::getOpt (int argc, char **argv) { - char *optstring = "hc:f:"; + char *optstring = "hlc:f:"; int option; std::string strTmp; @@ -66,17 +68,20 @@ Tester::getOpt (int argc, char **argv) { case 'c': // Check commands - strTmp = optarg; - listOpts_.push_back (Opt(false, strTmp)); + commands_ += optarg; break; case 'f': - strTmp = optarg; - listOpts_.push_back (Opt(true, strTmp)); + commands_ += ";include \""; + commands_ += optarg; + commands_ += "\";"; break; case 'h': usage (); std::exit (0); break; + case 'l': + showCmd_ = true; + break; case '?': default: usage (); @@ -84,6 +89,11 @@ Tester::getOpt (int argc, char **argv) break; } } + if (commands_.empty () && !showCmd_) + { + usage (); + std::exit (2); + } } /// Print usage to stdout. @@ -91,17 +101,23 @@ void Tester::usage (void) const { std::cout << "Usage: " << program_ - << " [-c ]" - << " [-f ]" + << " [-c ]" + << " [-f ]" << std::endl; + if (!description_.empty ()) + std::cout << description_ << std::endl; + std::cout << " -c Commands to run" << std::endl; + std::cout << " -f File with commands" << std::endl; + std::cout << " -l List all known commands by " + << program_ << std::endl; } - -/// Add a test function, Tester owns f. -void -Tester::add (const std::string &s, Interpreter::Func *f) +/// Print all knwon function and role to stdout. +void +Tester::listCmd (void) const { - interpreter_.add (s, f); + std::cout << "Commands known by " << program_ << " :" << std::endl; + std::cout << interpreter_.help (); } /// Run commands. @@ -110,24 +126,16 @@ Tester::run (void) { // Pre-run preRun (); - // Run - ListOpts::const_iterator it; - // Check all, dry run - for (it = listOpts_.begin (); it != listOpts_.end (); it++) + if (showCmd_) { - if (it->first) - interpreter_.interpretFile (it->second, true); - else - interpreter_.interpretString (it->second, true); + listCmd (); + std::exit (0); } + // Check + interpreter_.interpretString (commands_, true); // Run ! - for (it = listOpts_.begin (); it != listOpts_.end (); it++) - { - if (it->first) - interpreter_.interpretFile (it->second, false); - else - interpreter_.interpretString (it->second, false); - } + interpreter_.interpretString (commands_, false); + // Post-run postRun (); } diff --git a/i/marvin/src/tester/tester.hh b/i/marvin/src/tester/tester.hh index 5f4e68c..ef97287 100644 --- a/i/marvin/src/tester/tester.hh +++ b/i/marvin/src/tester/tester.hh @@ -30,43 +30,47 @@ #include #include +#include class Tester { private: + /// DataCircularBufferFactory for beeing destruct at the end of the program. + /// Note : it must stay at the begining of the class ! + DataCircularBufferFactory dbc; /// The config. Config config_; - /// The interperter + /// The interperter. Interpreter interpreter_; /// Program name. std::string program_; - /// Internal typedef. - typedef std::pair Opt; - typedef std::list ListOpts; - /// Options list. - ListOpts listOpts_; + /// Description of the program. + std::string description_; + /// Commands to run. + std::string commands_; /// Getopt command line. void getOpt (int argc, char **argv); /// Print usage to stdout. void usage (void) const; + /// Print all knwon function and role to stdout. + void listCmd (void) const; + /// Show commands instead of run them. + bool showCmd_; public: /// Constructor. - Tester (int argc, char ** argv); + Tester (int argc, char ** argv, const std::string &description = ""); /// Destructor. virtual ~Tester (void) { } - /// Add a test function, Tester owns f. - void add (const std::string &s, Interpreter::Func *f); + /// Get a reference to the interpreter. + Interpreter &getInterpreter (void) { return interpreter_; } /// Run commands. /// Call preRun, check command, run them, call postRun. void run (void); - /// Executed before checking/running commands. + /// Executed before checking/running commands. Good place to add command + /// to the interpreter. virtual void preRun (void) { } /// Executed after running commands. virtual void postRun (void) { } - private: - /// DataCircularBufferFactory for beeing destruct at the end of the program. - /// It must stay at the end of the program ! - DataCircularBufferFactory dbc; }; #endif // tester_hh -- cgit v1.2.3