Commit f13a79c1 authored by Julien Michel's avatar Julien Michel

Merge branch '1621-warn-unused-parameters' into 'develop'

Resolve "Warn otbcli user about unused parameters"

See merge request !144
parents 5062cd2a e5baad54
......@@ -168,7 +168,16 @@ protected:
unsigned int GetMaxKeySize() const;
private:
/** \return false if paramKey is a missing mandatory parameter */
bool CheckMissingMandatoryParameter(const std::string & paramKey) const;
/** Prints a warning to std::cerr if paramKey is an unused parameter */
void CheckUnusedParameter(const std::string & paramKey) const;
/** \return false if paramKey is an OutputFilename parameter
pointing to a path that does not exist */
bool CheckOutputPathsValidity(const std::string & paramKey) const;
CommandLineLauncher(const CommandLineLauncher &) = delete;
void operator =(const CommandLineLauncher&) = delete;
......
......@@ -511,32 +511,89 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters()
}
}
// SECOND PASS : check mandatory parameters
for (unsigned int i = 0; i < appKeyList.size(); i++)
// SECOND PASS : checks
for (const auto & paramKey : appKeyList)
{
const std::string paramKey(appKeyList[i]);
ParameterType type = m_Application->GetParameterType(paramKey);
// Check for missing mandatory parameters
if(!CheckMissingMandatoryParameter(paramKey))
return MISSINGMANDATORYPARAMETER;
// Check and warn unused parameters
CheckUnusedParameter(paramKey);
// Check output paths are valid
if(!CheckOutputPathsValidity(paramKey))
return WRONGPARAMETERVALUE;
}
return OKPARAM;
}
bool CommandLineLauncher::CheckOutputPathsValidity(const std::string & paramKey) const
{
ParameterType type = m_Application->GetParameterType(paramKey);
if (m_Application->HasValue(paramKey) &&
type == ParameterType_OutputFilename)
{
std::string filename = m_Application->GetParameterString(paramKey);
itksys::String path = itksys::SystemTools::GetFilenamePath(filename);
if (path!="" && !itksys::SystemTools::FileIsDirectory(path.c_str()))
{
std::cerr <<"ERROR: Directory doesn't exist : "<< path.c_str() << std::endl;
return false;
}
}
return true;
}
bool CommandLineLauncher::CheckMissingMandatoryParameter(const std::string & paramKey) const
{
if (m_Application->IsParameterMissing(paramKey))
{
std::cerr << "ERROR: Missing mandatory parameter -" << paramKey << "." << std::endl;
return MISSINGMANDATORYPARAMETER;
return false;
}
return true;
}
// Check output paths validity
if (m_Application->HasValue(paramKey) &&
type == ParameterType_OutputFilename)
void CommandLineLauncher::CheckUnusedParameter(const std::string & paramKey) const
{
// Check for ignored parameters
if(m_Application->HasUserValue(paramKey))
{
std::string filename = m_Application->GetParameterString(paramKey);
itksys::String path = itksys::SystemTools::GetFilenamePath(filename);
if (path!="" && !itksys::SystemTools::FileIsDirectory(path.c_str()))
// Find the position of the next dot
unsigned int start = 0;
auto end = paramKey.find_first_of('.',start);
// Until we reach en of key
while(end != std::string::npos)
{
std::cerr <<"ERROR: Directory doesn't exist : "<< path.c_str() << std::endl;
return WRONGPARAMETERVALUE;
// Extract key until the current dot
const std::string & key = paramKey.substr(0,end);
// Find the corresponding parameter type
ParameterType type = m_Application->GetParameterType(key);
// In any case update the position of current and next dot
start = end+1;
end = paramKey.find_first_of('.',start);
// If the current parameter (paramKey) has a choice in it
if(type == ParameterType_Choice)
{
const std::string & value = m_Application->GetParameterString(key);
// Check that this choice is active
if(paramKey.find(value) == std::string::npos)
{
std::cerr<<"WARNING: Parameter -"<<paramKey<<" will be ignored because -"<<key<<" is "<<value<<"."<<std::endl;
break;
}
}
}
}
}
return OKPARAM;
}
void CommandLineLauncher::LinkWatchers(itk::Object * itkNotUsed(caller), const itk::EventObject & event)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment