diff --git a/Code/Common/otbCommandLineArgumentParser.cxx b/Code/Common/otbCommandLineArgumentParser.cxx index 972bf3dc308010696c908042a01da3658de5b392..f9b88b1adbf3213a03b663b3face7eb446813e3c 100755 --- a/Code/Common/otbCommandLineArgumentParser.cxx +++ b/Code/Common/otbCommandLineArgumentParser.cxx @@ -71,16 +71,19 @@ std::string CommandLineArgumentParseResult ::GetParameterString(std::string option, unsigned int number)const { - if( this->IsOptionPresent(option) == false ) - { - itkExceptionMacro(<<"GetParameterString(): The following '"<<option<<"' option is unknown !!"); - } - - OptionMapType::const_iterator it = m_OptionMap.begin(); - it = m_OptionMap.find(option); - ParameterArrayType pat = (*it).second; - std::string lString = pat[number]; - return ( lString ); + if( this->IsOptionPresent(option) == false ) + { + itk::OStringStream msg; + msg<<"GetParameterString(): The following '"<<option<<"' option is unknown !!"; + CommandLineArgumentParserArgumentErrorException e(__FILE__, __LINE__); + e.SetDescription(msg.str().c_str()); + throw e; + } + OptionMapType::const_iterator it = m_OptionMap.begin(); + it = m_OptionMap.find(option); + ParameterArrayType pat = (*it).second; + std::string lString = pat[number]; + return ( lString ); } std::string @@ -234,20 +237,26 @@ CommandLineArgumentParser bool IsHelp = outResult->IsOptionPresent("--help"); if ( IsHelp == true ) { - PrintUsage(std::cout); - itkExceptionMacro(<<"ParseCommandLine(): Help Parser"); + PrintUsage(std::cout); + CommandLineArgumentParserHelpException e(__FILE__, __LINE__); + e.SetDescription("ParseCommandLine(): Help Parser"); + throw e; } bool IsVersion = outResult->IsOptionPresent("--version"); if ( IsVersion == true ) { PrintVersion(std::cout); - itkExceptionMacro(<<"ParseCommandLine(): Version Parser"); + CommandLineArgumentParserHelpException e(__FILE__, __LINE__); + e.SetDescription("ParseCommandLine(): Version Parser"); + throw e; } tryParse = TryParseCommandLine(argc, argv, outResult, true, failOnUnknownTrailingParameters); if ( (tryParse == false) ) { PrintUsage(std::cerr); - itkExceptionMacro(<<"ParseCommandLine() argument Error"); + CommandLineArgumentParserArgumentErrorException e(__FILE__, __LINE__); + e.SetDescription("ParseCommandLine() argument Error"); + throw e; } } diff --git a/Code/Common/otbCommandLineArgumentParser.h b/Code/Common/otbCommandLineArgumentParser.h index c3e4bc124f62329b859f85fdee7dcf1dc46d0b41..088cec64fd1cca21e1a9353877e307ed121fbae5 100755 --- a/Code/Common/otbCommandLineArgumentParser.h +++ b/Code/Common/otbCommandLineArgumentParser.h @@ -32,6 +32,79 @@ #include "itkProcessObject.h" #include "otbMacro.h" + +/** \class CommandLineArgumentParserHelpException + * \brief This exception is thrown when the help menu is displayed. + */ +class ITK_EXPORT CommandLineArgumentParserHelpException + : public itk::ExceptionObject +{ +public: + /** Run-time information. */ + itkTypeMacro(CommandLineArgumentParserHelpException , ExceptionObject ); + + /** Constructor. */ + CommandLineArgumentParserHelpException(const char *file, unsigned int line, + const char* message = "Help:", + const char* loc = "Unknown" ) : + ExceptionObject(file, line, message, loc) + {} + /** Constructor. */ + CommandLineArgumentParserHelpException(const std::string &file, unsigned int line, + const char* message = "Help:", + const char* loc = "Unknown" ) : + ExceptionObject(file, line, message, loc) + {} +}; + +/** \class CommandLineArgumentParserVersionException + * \brief This exception is thrown when the version is displayed. + */ +class ITK_EXPORT CommandLineArgumentParserVersionException + : public itk::ExceptionObject +{ +public: + /** Run-time information. */ + itkTypeMacro(CommandLineArgumentParserVersionException , ExceptionObject ); + + /** Constructor. */ + CommandLineArgumentParserVersionException(const char *file, unsigned int line, + const char* message = "Version:", + const char* loc = "Unknown" ) : + ExceptionObject(file, line, message, loc) + {} + /** Constructor. */ + CommandLineArgumentParserVersionException(const std::string &file, unsigned int line, + const char* message = "Version:", + const char* loc = "Unknown" ) : + ExceptionObject(file, line, message, loc) + {} +}; + +/** \class CommandLineArgumentParserArgumentErrorException + * \brief This exception is thrown when the version is displayed. + */ +class ITK_EXPORT CommandLineArgumentParserArgumentErrorException + : public itk::ExceptionObject +{ +public: + /** Run-time information. */ + itkTypeMacro(CommandLineArgumentParserArgumentErrorException , ExceptionObject ); + + /** Constructor. */ + CommandLineArgumentParserArgumentErrorException(const char *file, unsigned int line, + const char* message = "Argument error:", + const char* loc = "Unknown" ) : + ExceptionObject(file, line, message, loc) + {} + /** Constructor. */ + CommandLineArgumentParserArgumentErrorException(const std::string &file, unsigned int line, + const char* message = "Argument error:", + const char* loc = "Unknown" ) : + ExceptionObject(file, line, message, loc) + {} +}; + namespace otb { diff --git a/Testing/Code/Common/otbTestCommandLineArgumentParserHelp.cxx b/Testing/Code/Common/otbTestCommandLineArgumentParserHelp.cxx index 75b62e08f2b0fefaa1a7b3d695ada1fbb745ea53..83e42dfe3f9b78732cec69ba025e4c17f920987c 100644 --- a/Testing/Code/Common/otbTestCommandLineArgumentParserHelp.cxx +++ b/Testing/Code/Common/otbTestCommandLineArgumentParserHelp.cxx @@ -24,20 +24,27 @@ // Test de sortie en erreur int otbTestCommandLineArgumentParserHelp( int argc, char * argv[] ) { - // Parse command line parameters - typedef otb::CommandLineArgumentParser ParserType; - ParserType::Pointer parser = ParserType::New(); + try + { + // Parse command line parameters + typedef otb::CommandLineArgumentParser ParserType; + ParserType::Pointer parser = ParserType::New(); + + parser->AddOption("-image","Nom d'une image","-i",1,true); + + typedef otb::CommandLineArgumentParseResult ParserResultType; + ParserResultType::Pointer parseResult = ParserResultType::New(); + + parser->ParseCommandLine(argc,argv,parseResult) ; - parser->AddOption("-image","Nom d'une image","-i",1,true); - typedef otb::CommandLineArgumentParseResult ParserResultType; - ParserResultType::Pointer parseResult = ParserResultType::New(); - - parser->ParseCommandLine(argc,argv,parseResult) ; - - - std::cout << "Image : "<<parseResult->GetParameterString("-image")<<std::endl; - + std::cout << "Image : "<<parseResult->GetParameterString("-image")<<std::endl; + } + catch(CommandLineArgumentParserHelpException & err) + { + std::cerr<<err; + return EXIT_SUCCESS; + } return EXIT_FAILURE; } diff --git a/Testing/Code/Common/otbTestCommandLineArgumentParserWithError.cxx b/Testing/Code/Common/otbTestCommandLineArgumentParserWithError.cxx index 49fccac03918deb99d3e5386af7029861cb06502..f08623971fe01ebd4f0fe6f1fd8d4179aaf1e9dd 100644 --- a/Testing/Code/Common/otbTestCommandLineArgumentParserWithError.cxx +++ b/Testing/Code/Common/otbTestCommandLineArgumentParserWithError.cxx @@ -35,24 +35,31 @@ int otbTestCommandLineArgumentParserWithError( int argc, char * argv[] ) typedef otb::CommandLineArgumentParseResult ParserResultType; ParserResultType::Pointer parseResult = ParserResultType::New(); - parser->ParseCommandLine(argc,argv,parseResult) ; - + try + { + + parser->ParseCommandLine(argc,argv,parseResult) ; - std::cout << "Image : "<<parseResult->GetParameterString("-image")<<std::endl; - //unsigned int lEntier = otb::GetParameter<unsigned int>(parseResult,"-entier"); - unsigned int lEntier = parseResult->GetParameterUInt("-entier"); - std::cout << "Entier : "<<lEntier<<std::endl; - if( parseResult->IsOptionPresent("-deuxentiers")) + + std::cout << "Image : "<<parseResult->GetParameterString("-image")<<std::endl; + //unsigned int lEntier = otb::GetParameter<unsigned int>(parseResult,"-entier"); + unsigned int lEntier = parseResult->GetParameterUInt("-entier"); + std::cout << "Entier : "<<lEntier<<std::endl; + if( parseResult->IsOptionPresent("-deuxentiers")) + { + //unsigned int lEntierDeux = otb::GetParameter<unsigned int>(parseResult,"-deuxentiers",1); + unsigned int lEntierDeux = parseResult->GetParameterUInt("-deuxentiers",1); + std::cout << "Entier : "<<lEntierDeux<<std::endl; + } + //double lDouble = otb::GetParameter<double>(parseResult,"-double"); + double lDouble = parseResult->GetParameterDouble("-double"); + std::cout << "Double : "<<lDouble<<std::endl; + } + catch(CommandLineArgumentParserArgumentErrorException & err) { - //unsigned int lEntierDeux = otb::GetParameter<unsigned int>(parseResult,"-deuxentiers",1); - unsigned int lEntierDeux = parseResult->GetParameterUInt("-deuxentiers",1); - std::cout << "Entier : "<<lEntierDeux<<std::endl; + std::cerr<<err; + return EXIT_SUCCESS; } - //double lDouble = otb::GetParameter<double>(parseResult,"-double"); - double lDouble = parseResult->GetParameterDouble("-double"); - std::cout << "Double : "<<lDouble<<std::endl; - - return EXIT_FAILURE; }