diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx index ab2e89448354d8a9a2988ec07678e82553e597c0..6a4f58ed425cd99028ed8151dd9b3bad74de21d4 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx @@ -102,12 +102,7 @@ CommandLineLauncher::Load() return false; } - if ( this->LoadPath() == false ) - { - std::cerr<<"ERROR: Invalid paths..."<<std::endl; - std::cerr<<"ERROR: Please check values : "<<m_Parser->GetAttributAsString( "--modulePath", m_Expression )<<"."<<std::endl; - return false; - } + this->LoadPath(); this->LoadApplication(); return true; @@ -164,17 +159,22 @@ CommandLineLauncher::BeforeExecute() return false; } - if ( this->LoadParameters() != OKPARAM ) + try { - std::cerr<<"ERROR: troubles loading parameter, please check your line argument..."<<std::endl; - std::cerr<<"ERROR:"<< this->LoadParameters()<<std::endl; - std::cerr<<OKPARAM<<std::endl; - std::cerr<<MISSINGMANDATORYPARAMETER<<std::endl; - std::cerr<<MISSINGPARAMETERVALUE<<std::endl; - std::cerr<<WRONGPARAMETERVALUE<<std::endl; - std::cerr<<INVALIDNUMBEROFVALUE<<std::endl; - std::cerr<<DEFAULT<<std::endl; - + if ( this->LoadParameters() != OKPARAM ) + { + std::cerr<<"ERROR: troubles loading parameter, please check your line argument..."<<std::endl; + // Force to reload the application, the LoadParameters can change wrong values + this->LoadApplication(); + this->DisplayHelp(); + + 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 this->LoadApplication(); this->DisplayHelp(); @@ -219,31 +219,28 @@ CommandLineLauncher::BeforeExecute() return true; } -bool +void CommandLineLauncher::LoadPath() { std::vector<std::string> pathList; - // look for the paths - if( m_Parser->GetPaths( pathList, m_Expression) != CommandLineParser::OK ) + // If users has set path... + if( m_Parser->GetPaths( pathList, m_Expression) == CommandLineParser::OK ) { - return false; - } - // Contain paths into a string, separating each path with ":" - m_Path = std::string(""); - for( unsigned i=0; i<pathList.size(); i++) - { - m_Path.append(pathList[i]); - m_Path.append(":"); + // Contain paths into a string, separating each path with ":" + m_Path = std::string(""); + for( unsigned i=0; i<pathList.size(); i++) + { + 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() 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 ParameterGroup::Pointer paramGr = m_Application->GetParameterList(); const unsigned int nbOfParam = paramGr->GetNumberOfParameters(); @@ -289,6 +381,8 @@ CommandLineLauncher::LoadParameters() std::vector<std::string> values; Parameter::Pointer param = paramGr->GetParameterByIndex(i); const std::string paramKey(param->GetKey()); + ParameterType type = m_Application->GetParameterType( paramKey ); + const bool paramExists( m_Parser->IsAttributExists( std::string("--").append(paramKey), m_Expression ) ); // Check if mandatory parameter are present and have value if( param->GetMandatory() == true ) @@ -319,7 +413,6 @@ CommandLineLauncher::LoadParameters() // If the param is optionnal and hasn't been set : don't do anything if( paramExists ) { - ParameterType type = m_Application->GetParameterType( paramKey ); // List values parameter case if( type == ParameterType_InputImageList ) { @@ -331,13 +424,11 @@ CommandLineLauncher::LoadParameters() } else if( values.size() != 1) { - std::cout<<values.size()<<std::endl; - std::cout<<paramKey <<std::endl; return INVALIDNUMBEROFVALUE; } // 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_InputImage || type == ParameterType_InputVectorData || type == ParameterType_OutputImage || type == ParameterType_OutputVectorData ) { @@ -360,6 +451,7 @@ CommandLineLauncher::LoadParameters() } } } +*/ return OKPARAM; } @@ -402,34 +494,45 @@ CommandLineLauncher::DisplayHelp() std::cerr<<"DESCRIPTION: "<<m_Application->GetDescription()<<std::endl; std::cerr<<"PARAMETERS: "<<std::endl; - ParameterGroup::Pointer paramGr = m_Application->GetParameterList(); - const unsigned int nbOfParam = paramGr->GetNumberOfParameters(); + std::cerr<<"====== Mandatory parameters: ======"<<std::endl; - std::cerr<<"=== Mandatory parameters: "<<std::endl; - std::cerr<<m_Parser->GetModulePathKey()<<" (Executables paths)"<<std::endl; - 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; + const std::vector<std::string> appKeyList = m_Application->GetParametersKeys( true ); + const unsigned int nbOfParam = appKeyList.size(); // Mandatory parameters 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 if( param->GetMandatory() == true ) { - std::cerr<< this->DisplayParameterHelp( param ); + std::cerr<< this->DisplayParameterHelp( param, appKeyList[i] ); } } // 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<<"\t Description: Do report progress."<<std::endl; std::cerr<<"\t Type: Boolean"<<std::endl; @@ -442,11 +545,11 @@ CommandLineLauncher::DisplayHelp() std::cerr<< "\t Status: USER VALUE: "<<m_Parser->GetAttribut( "--progress", m_Expression )[0]<<std::endl; 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 if( param->GetMandatory() != true ) { - std::cerr << this->DisplayParameterHelp( param ); + std::cerr << this->DisplayParameterHelp( param, appKeyList[i] ); } @@ -455,25 +558,31 @@ CommandLineLauncher::DisplayHelp() } 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; oss<<"--"<<paramKey<<" ("<<param->GetName()<<")"<< std::endl; - + + // Display parameter description if( std::string(param->GetDescription()).size() != 0 ) { - oss<<"\t Description: "<<param->GetDescription()<<std::endl; + oss<<"\t Description: "<<param->GetDescription()<< std::endl;; } - - // Display the type the type - ParameterType type = m_Application->GetParameterType( paramKey ); - - if( type == ParameterType_Choice ) + else { - 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; } @@ -517,17 +626,40 @@ CommandLineLauncher::DisplayParameterHelp( const Parameter::Pointer & param ) { 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 { oss<<"\t Type: Type not handle yet"<<std::endl; } + // Display parameter values 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 oss << "\t Default value: none"<<std::endl; diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h index 0f8ae8b71973860020fe4667d607210a0a0ce23a..4b11c5126b4b6b7a15048e4e4e17c15223006342 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h @@ -106,7 +106,7 @@ protected: virtual ~CommandLineLauncher(); /** 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. * Pay attention, the executable paths have to be loaded or set in the environment before calling the function. */ @@ -116,7 +116,7 @@ protected: CommandLineLauncher::ParamResultType LoadParameters(); /** 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. */ bool CheckUnicity(); diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx index 5c53e4ce0a56138db5c5f7a3cd780c018ba943ad..84d71daed8dc470fbde8a9b95e1ad115a9d91522 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx @@ -237,11 +237,18 @@ bool CommandLineParser::IsAttributExists( const std::string key, const std::string & exp ) { std::string keySpaced = key; + // Add space to avoid troubles with key twhich starts by another one : --out and --outmax for example keySpaced.append(" "); std::size_t found = exp.find(keySpaced); 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;