From 9f4c073678ab98462055d8b9e8b2324c9b41822d Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Mon, 2 Aug 2010 17:07:32 +0200 Subject: [PATCH] code to display only once the message about the help parameter + some indent fix + some more comments --- eo/src/utils/eoParser.cpp | 91 ++++++++++++++++++++++---------------- eo/src/utils/eoParser.h | 8 +++- eo/src/utils/make_help.cpp | 8 ++-- 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index 8ef2346a..baefc48f 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -66,6 +66,7 @@ eoParser::eoParser ( unsigned _argc, char **_argv , string _programDescription, string _lFileParamName, char _shortHand) : programName(_argv[0]), programDescription( _programDescription), + needHelpMessage( false ), needHelp(false, "help", "Prints this message", 'h'), stopOnUnknownParam(true, "stopOnUnknownParam", "Stop if unkown param entered", '\0') { @@ -120,18 +121,19 @@ void eoParser::processParam(eoParam& param, std::string section) // this param enters the parser: add the prefix to the long name if (prefix != "") { - param.setLongName(prefix+param.longName()); - section = prefix + section; // and to section + param.setLongName(prefix+param.longName()); + section = prefix + section; // and to section } doRegisterParam(param); // plainly register it params.insert(make_pair(section, ¶m)); } -void eoParser::doRegisterParam(eoParam& param) const +void eoParser::doRegisterParam(eoParam& param) { if (param.required() && !isItThere(param)) { string msg = "Required parameter: " + param.longName() + " missing"; + needHelpMessage = true; messages.push_back(msg); } pair value = getValue(param); @@ -166,7 +168,7 @@ pair eoParser::getValue(eoParam& _param) const return result; } -void eoParser::updateParameters() const +void eoParser::updateParameters() { typedef MultiMapType::const_iterator It; @@ -351,49 +353,60 @@ bool eoParser::userNeedsHelp(void) // first, check if we want to check that ! if (stopOnUnknownParam.value()) { + // search for unknown long names for (LongNameMapType::const_iterator lIt = longNameMap.begin(); lIt != longNameMap.end(); ++lIt) - { - string entry = lIt->first; + { + string entry = lIt->first; - MultiMapType::const_iterator it; + MultiMapType::const_iterator it; - for (it = params.begin(); it != params.end(); ++it) - { - if (entry == it->second->longName()) - { - break; - } - } + for (it = params.begin(); it != params.end(); ++it) + { + if (entry == it->second->longName()) + { + break; + } + } - if (it == params.end()) - { - string msg = "Unknown parameter: --" + entry + " entered, type -h or --help to see available parameters"; - messages.push_back(msg); - } - } + if (it == params.end()) + { + string msg = "Unknown parameter: --" + entry + " entered"; + needHelpMessage = true; + messages.push_back(msg); + } + } // for lIt + // search for unknown short names for (ShortNameMapType::const_iterator sIt = shortNameMap.begin(); sIt != shortNameMap.end(); ++sIt) - { - char entry = sIt->first; - - MultiMapType::const_iterator it; - - for (it = params.begin(); it != params.end(); ++it) { - if (entry == it->second->shortName()) - { - break; - } - } + char entry = sIt->first; + + MultiMapType::const_iterator it; + + for (it = params.begin(); it != params.end(); ++it) + { + if (entry == it->second->shortName()) + { + break; + } + } + + if (it == params.end()) + { + string entryString(1, entry); + string msg = "Unknown parameter: -" + entryString + " entered"; + needHelpMessage = true; + messages.push_back(msg); + } + } // for sIt + + if( needHelpMessage ) { + string msg = "Use -h or --help to get help about available parameters"; + messages.push_back( msg ); + } + + } // if stopOnUnknownParam - if (it == params.end()) - { - string entryString(1, entry); - string msg = "Unknown parameter: -" + entryString + " entered, type -h or --help to see available parameters"; - messages.push_back(msg); - } - } - } return needHelp.value() || !messages.empty(); } diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index 2df6c1ab..eabd3261 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -244,11 +244,11 @@ public: private: - void doRegisterParam(eoParam& param) const; + void doRegisterParam(eoParam& param); std::pair getValue(eoParam& _param) const; - void updateParameters() const; + void updateParameters(); typedef std::multimap MultiMapType; @@ -264,6 +264,10 @@ private: typedef std::map LongNameMapType; LongNameMapType longNameMap; + // flag that marks if the user need to know that there was a problem + // used to display the message about "-h" only once + bool needHelpMessage; + eoValueParam needHelp; eoValueParam stopOnUnknownParam; diff --git a/eo/src/utils/make_help.cpp b/eo/src/utils/make_help.cpp index 6647cf01..e83b4f1a 100644 --- a/eo/src/utils/make_help.cpp +++ b/eo/src/utils/make_help.cpp @@ -55,16 +55,16 @@ void make_help(eoParser & _parser) // Only help parameter will not be in status file - but who cares??? if (statusParam.value() != "") { - ofstream os(statusParam.value().c_str()); - os << _parser; // and you can use that file as parameter file + ofstream os(statusParam.value().c_str()); + os << _parser; // and you can use that file as parameter file } // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED // i.e. in case you need parameters somewhere else, postpone these if (_parser.userNeedsHelp()) { _parser.printHelp(cout); - cout << "You can use an edited copy of file " << statusParam.value() - << " as parameter file" << endl; + cout << "You can use an edited copy of file " << statusParam.value() + << " as parameter file" << endl; exit(1); } }