diff --git a/Code/Common/otbCommandLineArgumentParser.cxx b/Code/Common/otbCommandLineArgumentParser.cxx index 46a307578401b8bf7e3f4485c2ea51f8c29724bc..82d5951b270176df84a99bc58d3a2779899d847e 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 d9694515338181f3c7b69e1610dd2f62b67ee661..ca0eb07a94e2f1a8f069dc38ff73160d68d08eb5 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 9630fc3714d515ffa323c91e629e4a349469e294..3154ff08827babd976f2e3c985c231dc9c57c0fb 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 )