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')
{ {
@ -120,18 +121,19 @@ void eoParser::processParam(eoParam& param, std::string section)
// this param enters the parser: add the prefix to the long name // this param enters the parser: add the prefix to the long name
if (prefix != "") if (prefix != "")
{ {
param.setLongName(prefix+param.longName()); param.setLongName(prefix+param.longName());
section = prefix + section; // and to section section = prefix + section; // and to section
} }
doRegisterParam(param); // plainly register it doRegisterParam(param); // plainly register it
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,49 +353,60 @@ 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;
MultiMapType::const_iterator it; MultiMapType::const_iterator it;
for (it = params.begin(); it != params.end(); ++it) for (it = params.begin(); it != params.end(); ++it)
{ {
if (entry == it->second->longName()) if (entry == it->second->longName())
{ {
break; break;
} }
} }
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";
messages.push_back(msg); needHelpMessage = true;
} 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;
MultiMapType::const_iterator it;
for (it = params.begin(); it != params.end(); ++it)
{ {
if (entry == it->second->shortName()) char entry = sIt->first;
{
break; 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(); 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;

View file

@ -55,16 +55,16 @@ void make_help(eoParser & _parser)
// Only help parameter will not be in status file - but who cares??? // Only help parameter will not be in status file - but who cares???
if (statusParam.value() != "") if (statusParam.value() != "")
{ {
ofstream os(statusParam.value().c_str()); ofstream os(statusParam.value().c_str());
os << _parser; // and you can use that file as parameter file os << _parser; // and you can use that file as parameter file
} }
// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
// i.e. in case you need parameters somewhere else, postpone these // i.e. in case you need parameters somewhere else, postpone these
if (_parser.userNeedsHelp()) if (_parser.userNeedsHelp())
{ {
_parser.printHelp(cout); _parser.printHelp(cout);
cout << "You can use an edited copy of file " << statusParam.value() cout << "You can use an edited copy of file " << statusParam.value()
<< " as parameter file" << endl; << " as parameter file" << endl;
exit(1); exit(1);
} }
} }