Skip to content
Snippets Groups Projects
Commit a3cfd3ec authored by Cyrille Valladeau's avatar Cyrille Valladeau
Browse files

ENH: modulePath is now optionnal + handle the choice paramter

parent 9c484cff
No related branches found
No related tags found
No related merge requests found
...@@ -102,12 +102,7 @@ CommandLineLauncher::Load() ...@@ -102,12 +102,7 @@ CommandLineLauncher::Load()
return false; return false;
} }
if ( this->LoadPath() == false ) this->LoadPath();
{
std::cerr<<"ERROR: Invalid paths..."<<std::endl;
std::cerr<<"ERROR: Please check values : "<<m_Parser->GetAttributAsString( "--modulePath", m_Expression )<<"."<<std::endl;
return false;
}
this->LoadApplication(); this->LoadApplication();
return true; return true;
...@@ -164,17 +159,22 @@ CommandLineLauncher::BeforeExecute() ...@@ -164,17 +159,22 @@ CommandLineLauncher::BeforeExecute()
return false; return false;
} }
if ( this->LoadParameters() != OKPARAM ) try
{ {
std::cerr<<"ERROR: troubles loading parameter, please check your line argument..."<<std::endl; if ( this->LoadParameters() != OKPARAM )
std::cerr<<"ERROR:"<< this->LoadParameters()<<std::endl; {
std::cerr<<OKPARAM<<std::endl; std::cerr<<"ERROR: troubles loading parameter, please check your line argument..."<<std::endl;
std::cerr<<MISSINGMANDATORYPARAMETER<<std::endl; // Force to reload the application, the LoadParameters can change wrong values
std::cerr<<MISSINGPARAMETERVALUE<<std::endl; this->LoadApplication();
std::cerr<<WRONGPARAMETERVALUE<<std::endl; this->DisplayHelp();
std::cerr<<INVALIDNUMBEROFVALUE<<std::endl;
std::cerr<<DEFAULT<<std::endl; return false;
}
}
catch(itk::ExceptionObject& err)
{
std::cerr<<"ERROR: troubles in parameter setting, please check your line argument..."<<std::endl;
std::cerr<<err.GetDescription()<<std::endl;
// Force to reload the application, the LoadParameters can change wrong values // Force to reload the application, the LoadParameters can change wrong values
this->LoadApplication(); this->LoadApplication();
this->DisplayHelp(); this->DisplayHelp();
...@@ -219,31 +219,28 @@ CommandLineLauncher::BeforeExecute() ...@@ -219,31 +219,28 @@ CommandLineLauncher::BeforeExecute()
return true; return true;
} }
bool void
CommandLineLauncher::LoadPath() CommandLineLauncher::LoadPath()
{ {
std::vector<std::string> pathList; std::vector<std::string> pathList;
// look for the paths // If users has set path...
if( m_Parser->GetPaths( pathList, m_Expression) != CommandLineParser::OK ) if( m_Parser->GetPaths( pathList, m_Expression) == CommandLineParser::OK )
{ {
return false; // Contain paths into a string, separating each path with ":"
} m_Path = std::string("");
// Contain paths into a string, separating each path with ":" for( unsigned i=0; i<pathList.size(); i++)
m_Path = std::string(""); {
for( unsigned i=0; i<pathList.size(); i++) m_Path.append(pathList[i]);
{ m_Path.append(":");
m_Path.append(pathList[i]); }
m_Path.append(":");
std::string specificEnv("ITK_AUTOLOAD_PATH=");
specificEnv.append(m_Path);
// do NOT use putenv() directly, since the string memory must be managed carefully
itksys::SystemTools::PutEnv(specificEnv.c_str());
// Reload factories to take into account new path
itk::ObjectFactoryBase::ReHash();
} }
std::string specificEnv("ITK_AUTOLOAD_PATH=");
specificEnv.append(m_Path);
// do NOT use putenv() directly, since the string memory must be managed carefully
itksys::SystemTools::PutEnv(specificEnv.c_str());
// Reload factories to take into account new path
itk::ObjectFactoryBase::ReHash();
return true;
} }
...@@ -280,6 +277,101 @@ CommandLineLauncher::LoadParameters() ...@@ -280,6 +277,101 @@ CommandLineLauncher::LoadParameters()
itkExceptionMacro("No application loaded"); itkExceptionMacro("No application loaded");
} }
const std::vector<std::string> appKeyList = m_Application->GetParametersKeys( true );
// Loop over each parameter key declared in the application
for( unsigned int i=0; i<appKeyList.size(); i++ )
{
const std::string paramKey(appKeyList[i]);
std::vector<std::string> values;
Parameter::Pointer param = m_Application->GetParameterByKey( paramKey );
ParameterType type = m_Application->GetParameterType( paramKey );
const bool paramExists( m_Parser->IsAttributExists( std::string("--").append(paramKey), m_Expression ) );
const bool hasValue = m_Application->HasValue( paramKey );
// Check if mandatory parameter are present and have value
if( param->GetMandatory() == true )
{
if( !paramExists )
{
// If key doesn't exist and parameter hasn't default value set...
if( !hasValue )
{
std::cout<<"MISSINGMANDATORYPARAMETER: "<<paramKey<<" "<<m_Application->HasValue( paramKey )<<std::endl;
return MISSINGMANDATORYPARAMETER;
}
}
if( paramExists )
{
values = m_Parser->GetAttribut( std::string("--").append(paramKey), m_Expression);
if( values.size() == 0 && !m_Application->HasValue( paramKey ) )
{
std::cout<<"MISSINGPARAMETERVALUE: "<<paramKey<<std::endl;
return MISSINGPARAMETERVALUE;
}
}
}
// Check if non mandatory parameter have values
else
{
if( paramExists )
{
values = m_Parser->GetAttribut( std::string("--").append(paramKey), m_Expression);
if( values.size() == 0 )
{
std::cout<<"MISSINGPARAMETERVALUE: "<<paramKey<<std::endl;
return MISSINGPARAMETERVALUE;
}
}
}
// If the param is optionnal and hasn't been set : don't do anything
// If the param is mandatory but a default value exists : don't do anything
if( paramExists )
{
// List values parameter case
if( type == ParameterType_InputImageList )
{
dynamic_cast<InputImageListParameter *>(param.GetPointer())->SetListFromFileName( values );
}
else if( type == ParameterType_StringList )
{
dynamic_cast<StringListParameter *>(param.GetPointer())->SetValue( values );
}
else if( values.size() != 1)
{
std::cout<<"INVALIDNUMBEROFVALUE: "<<paramKey<<" "<<values.size()<<std::endl;
return INVALIDNUMBEROFVALUE;
}
// Single value parameter
if( type == ParameterType_Choice || type == ParameterType_Float || type == ParameterType_Int || type == ParameterType_Radius
|| type == ParameterType_Directory || type == ParameterType_String || type == ParameterType_Filename || type == ParameterType_InputComplexImage
|| type == ParameterType_InputImage || type == ParameterType_InputVectorData || type == ParameterType_OutputImage || type == ParameterType_OutputVectorData )
{
m_Application->SetParameterString( paramKey, values[0] );
}
else if( type == ParameterType_Empty )
{
if( values[0] == "1" || values[0] == "true")
{
dynamic_cast<EmptyParameter *>(param.GetPointer())->SetActive(true);
}
else if( values[0] == "0" || values[0] == "false")
{
dynamic_cast<EmptyParameter *>(param.GetPointer())->SetActive(false);
}
else
{
std::cout<<"WRONGPARAMETERVALUE: "<<paramKey<<std::endl;
return WRONGPARAMETERVALUE;
}
}
}
}
/*
// Mandatory case // Mandatory case
ParameterGroup::Pointer paramGr = m_Application->GetParameterList(); ParameterGroup::Pointer paramGr = m_Application->GetParameterList();
const unsigned int nbOfParam = paramGr->GetNumberOfParameters(); const unsigned int nbOfParam = paramGr->GetNumberOfParameters();
...@@ -289,6 +381,8 @@ CommandLineLauncher::LoadParameters() ...@@ -289,6 +381,8 @@ CommandLineLauncher::LoadParameters()
std::vector<std::string> values; std::vector<std::string> values;
Parameter::Pointer param = paramGr->GetParameterByIndex(i); Parameter::Pointer param = paramGr->GetParameterByIndex(i);
const std::string paramKey(param->GetKey()); const std::string paramKey(param->GetKey());
ParameterType type = m_Application->GetParameterType( paramKey );
const bool paramExists( m_Parser->IsAttributExists( std::string("--").append(paramKey), m_Expression ) ); const bool paramExists( m_Parser->IsAttributExists( std::string("--").append(paramKey), m_Expression ) );
// Check if mandatory parameter are present and have value // Check if mandatory parameter are present and have value
if( param->GetMandatory() == true ) if( param->GetMandatory() == true )
...@@ -319,7 +413,6 @@ CommandLineLauncher::LoadParameters() ...@@ -319,7 +413,6 @@ CommandLineLauncher::LoadParameters()
// If the param is optionnal and hasn't been set : don't do anything // If the param is optionnal and hasn't been set : don't do anything
if( paramExists ) if( paramExists )
{ {
ParameterType type = m_Application->GetParameterType( paramKey );
// List values parameter case // List values parameter case
if( type == ParameterType_InputImageList ) if( type == ParameterType_InputImageList )
{ {
...@@ -331,13 +424,11 @@ CommandLineLauncher::LoadParameters() ...@@ -331,13 +424,11 @@ CommandLineLauncher::LoadParameters()
} }
else if( values.size() != 1) else if( values.size() != 1)
{ {
std::cout<<values.size()<<std::endl;
std::cout<<paramKey <<std::endl;
return INVALIDNUMBEROFVALUE; return INVALIDNUMBEROFVALUE;
} }
// Single value parameter // Single value parameter
if( type == ParameterType_Choice || type == ParameterType_Float || type == ParameterType_Int || type == ParameterType_Radius if( type == ParameterType_Float || type == ParameterType_Int || type == ParameterType_Radius
|| type == ParameterType_Directory || type == ParameterType_String || type == ParameterType_Filename || type == ParameterType_InputComplexImage || type == ParameterType_Directory || type == ParameterType_String || type == ParameterType_Filename || type == ParameterType_InputComplexImage
|| type == ParameterType_InputImage || type == ParameterType_InputVectorData || type == ParameterType_OutputImage || type == ParameterType_OutputVectorData ) || type == ParameterType_InputImage || type == ParameterType_InputVectorData || type == ParameterType_OutputImage || type == ParameterType_OutputVectorData )
{ {
...@@ -360,6 +451,7 @@ CommandLineLauncher::LoadParameters() ...@@ -360,6 +451,7 @@ CommandLineLauncher::LoadParameters()
} }
} }
} }
*/
return OKPARAM; return OKPARAM;
} }
...@@ -402,34 +494,45 @@ CommandLineLauncher::DisplayHelp() ...@@ -402,34 +494,45 @@ CommandLineLauncher::DisplayHelp()
std::cerr<<"DESCRIPTION: "<<m_Application->GetDescription()<<std::endl; std::cerr<<"DESCRIPTION: "<<m_Application->GetDescription()<<std::endl;
std::cerr<<"PARAMETERS: "<<std::endl; std::cerr<<"PARAMETERS: "<<std::endl;
ParameterGroup::Pointer paramGr = m_Application->GetParameterList(); std::cerr<<"====== Mandatory parameters: ======"<<std::endl;
const unsigned int nbOfParam = paramGr->GetNumberOfParameters();
std::cerr<<"=== Mandatory parameters: "<<std::endl; const std::vector<std::string> appKeyList = m_Application->GetParametersKeys( true );
std::cerr<<m_Parser->GetModulePathKey()<<" (Executables paths)"<<std::endl; const unsigned int nbOfParam = appKeyList.size();
std::cerr<<"\t Description: Paths to the executable library."<<std::endl;
std::cerr<<"\t Type: Boolean"<<std::endl;
if( !m_Parser->IsAttributExists( m_Parser->GetModulePathKey(), m_Expression ) )
std::cerr<<"\t Status: ENVIRONEMENT PATH"<<std::endl;
else if( m_Path == "")
std::cerr<< "\t Status: NO VALUE ASSOCIATED "<<m_Path<<std::endl;
else
std::cerr<< "\t Status: USER VALUE: "<<m_Path<<std::endl;
// Mandatory parameters // Mandatory parameters
for( unsigned int i=0; i<nbOfParam; i++ ) for( unsigned int i=0; i<nbOfParam; i++ )
{ {
Parameter::Pointer param = paramGr->GetParameterByIndex(i); Parameter::Pointer param = m_Application->GetParameterByKey( appKeyList[i] );
// Check if mandatory parameter are present and have value // Check if mandatory parameter are present and have value
if( param->GetMandatory() == true ) if( param->GetMandatory() == true )
{ {
std::cerr<< this->DisplayParameterHelp( param ); std::cerr<< this->DisplayParameterHelp( param, appKeyList[i] );
} }
} }
// Optional parameters // Optional parameters
std::cerr<<"=== Optional parameters: "<<std::endl; std::cerr<<"====== Optional parameters: ======"<<std::endl;
//// Module path parameter
std::cerr<<m_Parser->GetModulePathKey()<<" (Executables paths)"<<std::endl;
std::cerr<<"\t Description: Paths to the executable library."<<std::endl;
std::cerr<<"\t Type: List of path (ie. String)"<<std::endl;
if( !m_Parser->IsAttributExists( m_Parser->GetModulePathKey(), m_Expression ) )
{
const std::string envVal = itksys::SystemTools::GetEnv("ITK_AUTOLOAD_PATH");
if( envVal.size() != 0)
std::cerr<< "\t Status: ENVIRONEMENT PATH : "<<envVal<<std::endl;
else
std::cerr<< "\t Status: NO VALUE "<<std::endl;
}
else if( m_Path == "")
{
std::cerr<< "\t Status: NO VALUE "<<std::endl;
}
else
std::cerr<< "\t Status: USER VALUE: "<<m_Path<<std::endl;
//// progress report parameter
std::cerr<<"--progress (Report progress)"<<std::endl; std::cerr<<"--progress (Report progress)"<<std::endl;
std::cerr<<"\t Description: Do report progress."<<std::endl; std::cerr<<"\t Description: Do report progress."<<std::endl;
std::cerr<<"\t Type: Boolean"<<std::endl; std::cerr<<"\t Type: Boolean"<<std::endl;
...@@ -442,11 +545,11 @@ CommandLineLauncher::DisplayHelp() ...@@ -442,11 +545,11 @@ CommandLineLauncher::DisplayHelp()
std::cerr<< "\t Status: USER VALUE: "<<m_Parser->GetAttribut( "--progress", m_Expression )[0]<<std::endl; std::cerr<< "\t Status: USER VALUE: "<<m_Parser->GetAttribut( "--progress", m_Expression )[0]<<std::endl;
for( unsigned int i=0; i<nbOfParam; i++ ) for( unsigned int i=0; i<nbOfParam; i++ )
{ {
Parameter::Pointer param = paramGr->GetParameterByIndex(i); Parameter::Pointer param = m_Application->GetParameterByKey( appKeyList[i] );
// Check if mandatory parameter are present and have value // Check if mandatory parameter are present and have value
if( param->GetMandatory() != true ) if( param->GetMandatory() != true )
{ {
std::cerr << this->DisplayParameterHelp( param ); std::cerr << this->DisplayParameterHelp( param, appKeyList[i] );
} }
...@@ -455,25 +558,31 @@ CommandLineLauncher::DisplayHelp() ...@@ -455,25 +558,31 @@ CommandLineLauncher::DisplayHelp()
} }
std::string std::string
CommandLineLauncher::DisplayParameterHelp( const Parameter::Pointer & param ) CommandLineLauncher::DisplayParameterHelp( const Parameter::Pointer & param, const std::string paramKey )
{ {
const std::string paramKey( param->GetKey() ); // Display the type the type
ParameterType type = m_Application->GetParameterType( paramKey );
if( type == ParameterType_Group )
{
return "";
}
itk::OStringStream oss; itk::OStringStream oss;
oss<<"--"<<paramKey<<" ("<<param->GetName()<<")"<< std::endl; oss<<"--"<<paramKey<<" ("<<param->GetName()<<")"<< std::endl;
// Display parameter description
if( std::string(param->GetDescription()).size() != 0 ) if( std::string(param->GetDescription()).size() != 0 )
{ {
oss<<"\t Description: "<<param->GetDescription()<<std::endl; oss<<"\t Description: "<<param->GetDescription()<< std::endl;;
} }
else
// Display the type the type
ParameterType type = m_Application->GetParameterType( paramKey );
if( type == ParameterType_Choice )
{ {
oss<<"\t Type: String"<<std::endl; oss<<"\t Description: none"<< std::endl;;
} }
else if( type == ParameterType_Radius )
// Display the type the parameter
if( type == ParameterType_Radius )
{ {
oss<<"\t Type: Int"<<std::endl; oss<<"\t Type: Int"<<std::endl;
} }
...@@ -517,17 +626,40 @@ CommandLineLauncher::DisplayParameterHelp( const Parameter::Pointer & param ) ...@@ -517,17 +626,40 @@ CommandLineLauncher::DisplayParameterHelp( const Parameter::Pointer & param )
{ {
oss<<"\t Type: String"<<std::endl; oss<<"\t Type: String"<<std::endl;
} }
else if( type == ParameterType_Group ) else if( type == ParameterType_Choice )
{ {
oss<<"\t Type: Group"<<std::endl; oss<<"\t Type: String: ";
std::vector<std::string> keys = m_Application->GetChoiceKeys(paramKey);
std::vector<std::string> names = m_Application->GetChoiceNames(paramKey);
for( unsigned int j=0; j<keys.size(); j++ )
{
oss << keys[j] << " for "<<names[j];
if( j<= keys.size()-1 )
{
oss << ", ";
}
}
oss<<std::endl;
} }
else else
{ {
oss<<"\t Type: Type not handle yet"<<std::endl; oss<<"\t Type: Type not handle yet"<<std::endl;
} }
// Display parameter values
if( m_Application->HasValue( paramKey ) ) if( m_Application->HasValue( paramKey ) )
oss << "\t Default value: "<<m_Application->GetParameterAsString( paramKey )<< std::endl; {
// In the case choice, don't show the enum type.
if( type == ParameterType_Choice )
{
oss << "\t Default value: "<< m_Application->GetChoiceKeys(paramKey)[m_Application->GetParameterInt( paramKey )]<< std::endl;
}
else
{
oss << "\t Default value: "<<m_Application->GetParameterAsString( paramKey )<< std::endl;
}
}
else else
oss << "\t Default value: none"<<std::endl; oss << "\t Default value: none"<<std::endl;
......
...@@ -106,7 +106,7 @@ protected: ...@@ -106,7 +106,7 @@ protected:
virtual ~CommandLineLauncher(); virtual ~CommandLineLauncher();
/** Load the executable path. It looks for the key --modulePath, extract and interpret as path the following strings. */ /** Load the executable path. It looks for the key --modulePath, extract and interpret as path the following strings. */
bool LoadPath(); void LoadPath();
/** Load the application calling the CreateApplication method of the ApplicationRegistry classes. /** Load the application calling the CreateApplication method of the ApplicationRegistry classes.
* Pay attention, the executable paths have to be loaded or set in the environment before calling the function. */ * Pay attention, the executable paths have to be loaded or set in the environment before calling the function. */
...@@ -116,7 +116,7 @@ protected: ...@@ -116,7 +116,7 @@ protected:
CommandLineLauncher::ParamResultType LoadParameters(); CommandLineLauncher::ParamResultType LoadParameters();
/** Create and display the help of the application */ /** Create and display the help of the application */
std::string DisplayParameterHelp( const Parameter::Pointer & param ); std::string DisplayParameterHelp( const Parameter::Pointer & param, const std::string paramKey );
/** Check if each key is unique in the expression. */ /** Check if each key is unique in the expression. */
bool CheckUnicity(); bool CheckUnicity();
......
...@@ -237,11 +237,18 @@ bool ...@@ -237,11 +237,18 @@ bool
CommandLineParser::IsAttributExists( const std::string key, const std::string & exp ) CommandLineParser::IsAttributExists( const std::string key, const std::string & exp )
{ {
std::string keySpaced = key; std::string keySpaced = key;
// Add space to avoid troubles with key twhich starts by another one : --out and --outmax for example
keySpaced.append(" "); keySpaced.append(" ");
std::size_t found = exp.find(keySpaced); std::size_t found = exp.find(keySpaced);
if( found == std::string::npos ) if( found == std::string::npos )
{ {
return false; // Case the attribut is at the end of the expression : no space
found = exp.find(key);
if( found == std::string::npos )
{
return false;
}
return true;
} }
return true; return true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment