code to display only once the message about the help parameter + some indent fix + some more comments

This commit is contained in:
Johann Dreo 2010-08-02 17:07:32 +02:00
commit 9f4c073678
3 changed files with 61 additions and 44 deletions

View file

@ -66,6 +66,7 @@ eoParser::eoParser ( unsigned _argc, char **_argv , string _programDescription,
string _lFileParamName, char _shortHand) : string _lFileParamName, char _shortHand) :
programName(_argv[0]), programName(_argv[0]),
programDescription( _programDescription), programDescription( _programDescription),
needHelpMessage( false ),
needHelp(false, "help", "Prints this message", 'h'), needHelp(false, "help", "Prints this message", 'h'),
stopOnUnknownParam(true, "stopOnUnknownParam", "Stop if unkown param entered", '\0') stopOnUnknownParam(true, "stopOnUnknownParam", "Stop if unkown param entered", '\0')
{ {
@ -127,11 +128,12 @@ void eoParser::processParam(eoParam& param, std::string section)
params.insert(make_pair(section, &param)); params.insert(make_pair(section, &param));
} }
void eoParser::doRegisterParam(eoParam& param) const void eoParser::doRegisterParam(eoParam& param)
{ {
if (param.required() && !isItThere(param)) if (param.required() && !isItThere(param))
{ {
string msg = "Required parameter: " + param.longName() + " missing"; string msg = "Required parameter: " + param.longName() + " missing";
needHelpMessage = true;
messages.push_back(msg); messages.push_back(msg);
} }
pair<bool, string> value = getValue(param); pair<bool, string> value = getValue(param);
@ -166,7 +168,7 @@ pair<bool, string> eoParser::getValue(eoParam& _param) const
return result; return result;
} }
void eoParser::updateParameters() const void eoParser::updateParameters()
{ {
typedef MultiMapType::const_iterator It; typedef MultiMapType::const_iterator It;
@ -351,6 +353,7 @@ bool eoParser::userNeedsHelp(void)
// first, check if we want to check that ! // first, check if we want to check that !
if (stopOnUnknownParam.value()) if (stopOnUnknownParam.value())
{ {
// search for unknown long names
for (LongNameMapType::const_iterator lIt = longNameMap.begin(); lIt != longNameMap.end(); ++lIt) for (LongNameMapType::const_iterator lIt = longNameMap.begin(); lIt != longNameMap.end(); ++lIt)
{ {
string entry = lIt->first; string entry = lIt->first;
@ -367,11 +370,13 @@ bool eoParser::userNeedsHelp(void)
if (it == params.end()) if (it == params.end())
{ {
string msg = "Unknown parameter: --" + entry + " entered, type -h or --help to see available parameters"; string msg = "Unknown parameter: --" + entry + " entered";
needHelpMessage = true;
messages.push_back(msg); messages.push_back(msg);
} }
} } // for lIt
// search for unknown short names
for (ShortNameMapType::const_iterator sIt = shortNameMap.begin(); sIt != shortNameMap.end(); ++sIt) for (ShortNameMapType::const_iterator sIt = shortNameMap.begin(); sIt != shortNameMap.end(); ++sIt)
{ {
char entry = sIt->first; char entry = sIt->first;
@ -389,11 +394,19 @@ bool eoParser::userNeedsHelp(void)
if (it == params.end()) if (it == params.end())
{ {
string entryString(1, entry); string entryString(1, entry);
string msg = "Unknown parameter: -" + entryString + " entered, type -h or --help to see available parameters"; string msg = "Unknown parameter: -" + entryString + " entered";
needHelpMessage = true;
messages.push_back(msg); 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
return needHelp.value() || !messages.empty(); return needHelp.value() || !messages.empty();
} }

View file

@ -244,11 +244,11 @@ public:
private: private:
void doRegisterParam(eoParam& param) const; void doRegisterParam(eoParam& param);
std::pair<bool, std::string> getValue(eoParam& _param) const; std::pair<bool, std::string> getValue(eoParam& _param) const;
void updateParameters() const; void updateParameters();
typedef std::multimap<std::string, eoParam*> MultiMapType; typedef std::multimap<std::string, eoParam*> MultiMapType;
@ -264,6 +264,10 @@ private:
typedef std::map<std::string, std::string> LongNameMapType; typedef std::map<std::string, std::string> LongNameMapType;
LongNameMapType longNameMap; 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<bool> needHelp; eoValueParam<bool> needHelp;
eoValueParam<bool> stopOnUnknownParam; eoValueParam<bool> stopOnUnknownParam;