diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx index e3d84fd4fd05d908d7a4a7b12959c1d1f229c3f0..b04ba0e2c51a52a2e113f86582fe189f0724cac7 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx @@ -65,16 +65,16 @@ CommandLineLauncher::~CommandLineLauncher() { } -void +bool CommandLineLauncher::Load( const std::string & exp ) { m_Expression = exp; - this->Load(); + return this->Load(); } -void +bool CommandLineLauncher::Load() { if (m_Expression == "") @@ -82,8 +82,15 @@ CommandLineLauncher::Load() itkExceptionMacro("No expression specified..."); } - this->LoadPath(); + if ( this->LoadPath() == false ) + { + std::cout<<"Invalid paths..."<<std::endl; + std::cout<<"Please check values : "<<m_Parser->GetAttributAsString( "--modulePath", m_Expression )<<"."<<std::endl; + return false; + } this->LoadApplication(); + + return true; } void @@ -101,7 +108,13 @@ CommandLineLauncher::Execute() } else { - this->LoadParameters(); + if ( this->LoadParameters() != OKPARAM ) + { + this->LoadApplication(); + this->DisplayHelp(); + + return; + } m_Application->Execute(); } } @@ -121,19 +134,26 @@ CommandLineLauncher::ExecuteAndWriteOutput() } else { - this->LoadParameters(); + if ( this->LoadParameters() != OKPARAM ) + { + this->LoadApplication(); + this->DisplayHelp(); + + return; + } + m_Application->ExecuteAndWriteOutput(); } } -void +bool CommandLineLauncher::LoadPath() { std::vector<std::string> pathList; // look for the paths if( m_Parser->GetPaths( pathList, m_Expression) != CommandLineParser::OK ) { - return; + return false; } // Contain paths into a string, separating each path with ":" m_Path = std::string(""); @@ -149,6 +169,8 @@ CommandLineLauncher::LoadPath() itksys::SystemTools::PutEnv(specificEnv.c_str()); // Reload factories to take into account new path itk::ObjectFactoryBase::ReHash(); + + return true; } @@ -194,7 +216,7 @@ CommandLineLauncher::LoadParameters() { if( !m_Parser->IsAttributExists( std::string("--").append(param->GetKey()), m_Expression ) ) { - std::cout<<param->GetKey()<<std::endl; + std::cout<<"No param for (tu fais quoi la??)... "<<param->GetKey()<<std::endl; return MISSINGMANDATORYPARAMETER; } values = m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression); @@ -268,6 +290,7 @@ CommandLineLauncher::LoadParameters() } } } + return OKPARAM; } @@ -301,43 +324,8 @@ CommandLineLauncher::DisplayHelp() // Check if mandatory parameter are present and have value if( param->GetMandatory() == true ) { - itk::OStringStream oss; - oss<<"--"<<param->GetKey()<<" ("<<param->GetName()<<")"<< std::endl; - - if( std::string(param->GetDescription()).size() != 0 ) - { - oss<<"\t Description: "<<param->GetDescription()<<std::endl; - } - if( m_Application->HasValue( param->GetKey() ) ) - oss << "\t Default value: "<<m_Application->GetParameterAsString( param->GetKey() )<< std::endl; - else - oss << "\t Default value: none"<<std::endl; - - - std::cout<<"sssiiiizzzee: "<<m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression).size()<<std::endl; - if( !m_Parser->IsAttributExists( std::string("--").append(param->GetKey()), m_Expression) ) - { - std::cout<<"exist pas "<<std::string("--").append(param->GetKey())<<" => "<<m_Application->HasValue( param->GetKey())<<std::endl; - - if ( !m_Application->HasValue( param->GetKey() ) ) - oss << "\t Status: MISSING"<< std::endl; - else - oss << "\t Status: DEFAULT VALUE"<< std::endl; - } - else if( m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression).size() == 0 ) - { - oss << "\t Status: NO VALUE ASSOCIATED"<< std::endl; - } - else - { - oss << "\t Status: USER VALUE: "; - oss << m_Parser->GetAttributAsString( std::string("--").append(param->GetKey()), m_Expression ); - oss << std::endl; - } - - std::cerr<< oss.str(); + std::cerr<< this->DisplayParameterHelp( param ); } -std::cerr<<std::endl; } bool addOptionTag = true; @@ -348,37 +336,13 @@ std::cerr<<std::endl; // Check if mandatory parameter are present and have value if( param->GetMandatory() != true ) { - itk::OStringStream oss; - oss<<"--"<<param->GetKey()<<std::endl; - oss<<"\t Name: "<<param->GetName()<< std::endl; - - if( std::string(param->GetDescription()).size() != 0 ) - { - oss<<"\t Description: "<<param->GetDescription()<<std::endl; - } - if( m_Application->HasValue( param->GetKey() ) ) - oss << "\t Default value: "<<m_Application->GetParameterAsString( param->GetKey() )<< std::endl; - else - oss << "\t Default value: none"<<std::endl; - if( !m_Parser->IsAttributExists( std::string("--").append(param->GetKey()), m_Expression) ) - { - if ( !m_Application->HasValue( param->GetKey() ) ) - oss << "\t Status: MISSING"<< std::endl; - else - oss << "\t Status: DEFAULT VALUE"<< std::endl; - } - else if( m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression).size() == 0 ) + if( addOptionTag == true ) { - oss << "\t Status: NO VALUE ASSOCIATED"<< std::endl; - } - else - { - oss << "\t Status: USER VALUE: "; - oss << m_Parser->GetAttributAsString( std::string("--").append(param->GetKey()), m_Expression ); - oss << std::endl; + std::cerr<<"=== Optional parameters: "<<std::endl; + addOptionTag = false; } - std::cerr<< oss.str(); + std::cerr << this->DisplayParameterHelp( param ); } @@ -386,6 +350,43 @@ std::cerr<<std::endl; } +std::string +CommandLineLauncher::DisplayParameterHelp( const Parameter::Pointer & param ) +{ + itk::OStringStream oss; + oss<<"--"<<param->GetKey()<<" ("<<param->GetName()<<")"<< std::endl; + + if( std::string(param->GetDescription()).size() != 0 ) + { + oss<<"\t Description: "<<param->GetDescription()<<std::endl; + } + + if( m_Application->HasValue( param->GetKey() ) ) + oss << "\t Default value: "<<m_Application->GetParameterAsString( param->GetKey() )<< std::endl; + else + oss << "\t Default value: none"<<std::endl; + + if( !m_Parser->IsAttributExists( std::string("--").append(param->GetKey()), m_Expression) ) + { + if ( !m_Application->HasValue( param->GetKey() ) ) + oss << "\t Status: MISSING"<< std::endl; + else + oss << "\t Status: DEFAULT VALUE"<< std::endl; + } + else if( m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression).size() == 0 ) + { + oss << "\t Status: NO VALUE ASSOCIATED"<< std::endl; + } + else + { + oss << "\t Status: USER VALUE ("; + oss << m_Parser->GetAttributAsString( std::string("--").append(param->GetKey()), m_Expression ) <<")"; + oss << std::endl; + } + + return oss.str(); +} + } } diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h index 7d5e42a95aa36c497df2c002449bfa5d67baefd0..53976f3eba2514675a9bbdc870f44db3a2df65bb 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h @@ -69,10 +69,10 @@ public: * - Load the paths * - Load the application using the ApplicationRegistry */ - void Load(); + bool Load(); /** same as Load method but set the expression before. */ - void Load( const std::string & exp ); + bool Load( const std::string & exp ); /** Launch the process, using the Execute application method * The method will check if the user asked for help (looking at --help key) before loading parameter and launching process. @@ -97,7 +97,7 @@ protected: virtual ~CommandLineLauncher(); /** Load the executable path. It looks for the key --modulePath, extract and interpret as path the following strings. */ - void LoadPath(); + bool 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. */ @@ -106,6 +106,10 @@ protected: /** Parse the user expression, extract the key and the associated string and set it as value of each corresonding application parameter. */ CommandLineLauncher::ParamResultType LoadParameters(); + + /** Create and display the help of the application */ + std::string DisplayParameterHelp( const Parameter::Pointer & param ); + /** Method used to determine the type of a a parameter (trying a dynamic_cast). */ //template <class TParameterType> //bool CanCreateParameter(Parameter * param); diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx index b7357df2875ebad7ba750ec754d32f56f69a7382..e0e8478007b0a092e4707c95c7651a97a791b92e 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx @@ -172,13 +172,22 @@ CommandLineParser::GetModuleName( std::string & modName, const std::string & exp std::vector<std::string> CommandLineParser::GetAttribut( const std::string & key, const std::string & exp ) { - std::size_t found = std::string(exp).find(key); + std::string keySpaced = key; + keySpaced.append(" "); + std::size_t found = std::string(exp).find(keySpaced); if( found == std::string::npos ) { itkExceptionMacro("No key \""<<key<<"\" found in \""<<exp<<"\"."); } + std::vector<std::string> res; std::string expFromKey = std::string(exp).substr(found+key.size(), std::string(exp).size()); + + if( expFromKey.size() == 0 ) + { + return res; + } + std::string tempModKey = expFromKey; // remove other key in the string if there's any if( expFromKey.find("--") != std::string::npos) @@ -197,7 +206,6 @@ CommandLineParser::GetAttribut( const std::string & key, const std::string & exp } // Remove space at the begining of the string and cast into std::vector<std::string> - std::vector<std::string> res; for(unsigned int i=0; i<spaceSplitted.size(); i++) { while( spaceSplitted[i].size()>0 && spaceSplitted[i][0] == ' ' ) @@ -213,11 +221,21 @@ CommandLineParser::GetAttribut( const std::string & key, const std::string & exp std::string CommandLineParser::GetAttributAsString( const std::string & key, const std::string & exp ) { - std::string res; + std::string res(""); std::vector<std::string> values = this->GetAttribut( key, exp ); + + if( values.size() == 0 ) + { + return ""; + } + else if( values.size() == 1 && values[0] == " " ) + { + return ""; + } + for( unsigned int i=0; i<values.size(); i++) { - if( i<values.size() ) + if( i<values.size()-1 ) { res.append(values[i]); res.append(" "); @@ -234,8 +252,9 @@ CommandLineParser::GetAttributAsString( const std::string & key, const std::stri bool CommandLineParser::IsAttributExists( const std::string key, const std::string & exp ) { - std::cout<<"--------- "<<key<<" "<<exp<<std::endl; - std::size_t found = std::string(exp).find(key); + std::string keySpaced = key; + keySpaced.append(" "); + std::size_t found = std::string(exp).find(keySpaced); if( found == std::string::npos ) { return false; diff --git a/Testing/CommandLine/otbWrapperCommandLineLauncherTests.cxx b/Testing/CommandLine/otbWrapperCommandLineLauncherTests.cxx index c6d444876aff99cc2cc4b86cb8cdbcf321ae09ab..56a4bbee5fca825efcd8615270bd762c81548f88 100644 --- a/Testing/CommandLine/otbWrapperCommandLineLauncherTests.cxx +++ b/Testing/CommandLine/otbWrapperCommandLineLauncherTests.cxx @@ -35,8 +35,10 @@ int otbWrapperCommandLineLauncherTest(int argc, char* argv[]) typedef otb::Wrapper::CommandLineLauncher LauncherType; LauncherType::Pointer launcher = LauncherType::New(); - launcher->Load( argv[1]); - launcher->Execute(); + if (launcher->Load( argv[1]) == true ) + { + launcher->ExecuteAndWriteOutput(); + } return EXIT_SUCCESS; }