From 773a47db41db0f4c1750c57d9d283a9698d7ce06 Mon Sep 17 00:00:00 2001 From: Otmane Lahlou <otmane.lahlou@c-s.fr> Date: Tue, 7 Dec 2010 15:13:36 +0100 Subject: [PATCH] BUG : negative real number were considered as arguments --- Code/Common/otbCommandLineArgumentParser.cxx | 35 ++++++++++++++++++-- Code/Common/otbCommandLineArgumentParser.h | 1 + Testing/Code/Common/CMakeLists.txt | 7 +++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Code/Common/otbCommandLineArgumentParser.cxx b/Code/Common/otbCommandLineArgumentParser.cxx index 46a3075784..82d5951b27 100644 --- a/Code/Common/otbCommandLineArgumentParser.cxx +++ b/Code/Common/otbCommandLineArgumentParser.cxx @@ -302,9 +302,18 @@ bool CommandLineArgumentParser::TryParseCommandLine(int argc, char *argv[], if (argv[i + 1] != NULL) { std::string strArgv = std::string(argv[i + 1]); - if (strArgv[0] == '-') + if (strArgv[0] == '-' ) { - goOnFlag = false; + // Test if the string is an argument or a real + if(!this->IsNumber(strArgv)) + { + goOnFlag = false; + } + else // If not an argument add it to the option list + { + outResult->AddParameter(m_OptionList[index].CommonName, strArgv); + ++i; + } } else { @@ -337,6 +346,28 @@ bool CommandLineArgumentParser::TryParseCommandLine(int argc, char *argv[], return true; } + +bool CommandLineArgumentParser::IsNumber(std::string text) +{ + if(text.empty()) + return false; + + // Don't test the sign (-) + unsigned int i = 1; + while (text[i]) + { + if( text[i] != '.' && text[i] != ',') + { + if(!isdigit(text[i])) + { + return false; + } + } + i++; + } + return true; +} + bool CommandLineArgumentParser::FindOption(const std::string& option, int& index) { //Look through the option list diff --git a/Code/Common/otbCommandLineArgumentParser.h b/Code/Common/otbCommandLineArgumentParser.h index d969451533..ca0eb07a94 100644 --- a/Code/Common/otbCommandLineArgumentParser.h +++ b/Code/Common/otbCommandLineArgumentParser.h @@ -263,6 +263,7 @@ private: void PrintUsage(std::ostream& os) const; void PrintVersion(std::ostream& os) const; bool FindOption(const std::string&, int& index); + bool IsNumber(std::string text); /** Try processing a command line. Returns false if something breaks */ bool TryParseCommandLine(int argc, char *argv[], diff --git a/Testing/Code/Common/CMakeLists.txt b/Testing/Code/Common/CMakeLists.txt index 9630fc3714..3154ff0882 100644 --- a/Testing/Code/Common/CMakeLists.txt +++ b/Testing/Code/Common/CMakeLists.txt @@ -83,7 +83,7 @@ ADD_TEST(coTuCmdLineArgParser_EmptyList ${COMMON_TESTS1} ADD_TEST(coTuCmdLineArgParser_List1Arg ${COMMON_TESTS1} otbTestCommandLineArgumentParserList - -image image.png -entier 150 -double 12.25 -deuxentiers 78 56 -doubles 1.0) + -image image.png -entier 150 -double 12.25 -deuxentiers 78 56 -doubles 1.0 2.5) ADD_TEST(coTuCmdLineArgParser_List2Arg ${COMMON_TESTS1} otbTestCommandLineArgumentParserList @@ -93,6 +93,11 @@ ADD_TEST(coTuCmdLineArgParser_List2ArgBis ${COMMON_TESTS1} otbTestCommandLineArgumentParserList -image image.png -entier 150 -double 12.25 -doubles 1.0 2.0 -deuxentiers 78 56 ) + +ADD_TEST(coTuCmdLineArgParser_List3ArgWithError ${COMMON_TESTS1} + otbTestCommandLineArgumentParserList + -image image.png -entier 150 -double 12.25 -deuxentiers 78 56 -doubles 1.0 2.5 -3.5) + #ADD_TEST(coTuCmdLineArgParserGUIDeuxEntiers_ExactNumberArg ${COMMON_TESTS1} # otbTestCommandLineArgumentParserGUI # -image image.png -entier 150 -double 12.25 -deuxentiers 78 56 ) -- GitLab