Newer
Older

Sébastien Dinot
committed
/*
* Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
Thomas Feuvrier
committed
#include "otbCommandLineArgumentParser.h"
#include <assert.h>
#include <iostream>
#include "otbConfigure.h" // For OTB_VERSION_STRING generated by ccmake
Thomas Feuvrier
committed
namespace otb
{
// --------- CommandLineArgumentParseResult
// --------- ----------------------------------------
CommandLineArgumentParseResult::CommandLineArgumentParseResult() {}
CommandLineArgumentParseResult::~CommandLineArgumentParseResult() {}
::PrintSelf(std::ostream& itkNotUsed(os), itk::Indent itkNotUsed(indent)) const
::IsOptionPresent(const std::string& option) const
{
return (m_OptionMap.find(option) != m_OptionMap.end());
}
{
return (this->IsOptionPresent("--InputImage"));
}
bool CommandLineArgumentParseResult
{
return (this->IsOptionPresent("--OutputImage"));
}
{
return (this->IsOptionPresent("--OTBTesting"));
}
std::string CommandLineArgumentParseResult
::GetParameterString(const std::string& option, unsigned int number) const
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 CommandLineArgumentParseResult
::GetInputImage(void) const
{
return (GetParameterString("--InputImage"));
}
Thomas Feuvrier
committed
std::string CommandLineArgumentParseResult
::GetOutputImage(void) const
{
return (GetParameterString("--OutputImage"));
}
::GetNumberOfParameters(const std::string& option)
{
assert(IsOptionPresent(option));
return (m_OptionMap[option].size());
}
void CommandLineArgumentParseResult
::Clear()
{
m_OptionMap.clear();
}
{
m_OptionMap[option].push_back(parameter);
}
// --------- CommandLineArgumentParser ----------------------------------------
CommandLineArgumentParser
::CommandLineArgumentParser()
{
AddOption("--help", "Help", "-h", 0, false);
AddOption("--version", "Version", "-v", 0, false);
AddOptionNParams("--OTBTesting", "Testing purposes only.", "-OTBTesting", false);
CommandLineArgumentParser
::~CommandLineArgumentParser() {}
void CommandLineArgumentParser
::AddInputImage(bool obligatory)
{
AddOption("--InputImage", "input image file name ", "-in", 1, obligatory);
Thomas Feuvrier
committed
void CommandLineArgumentParser
::AddOutputImage(bool obligatory)
{
AddOption("--OutputImage", "output image file name ", "-out", 1, obligatory);
::AddOption(const std::string& name, const std::string& comment, const std::string& synonym, int nParameters, bool obligatory)
Thomas Feuvrier
committed
// Create a structure for the command
OptionType option;
option.CommonName = name;
option.Description = comment;
option.NumberOfParameters = nParameters;
option.NumberOfParametersFixed = true;
option.Obligatory = obligatory;
option.Finded = false;
Thomas Feuvrier
committed
// Add the option to the map
Thomas Feuvrier
committed
::AddOptionNParams(const std::string& name, const std::string& comment, const std::string& synonym, bool obligatory)
OptionType option;
option.CommonName = name;
option.Description = comment;
option.NumberOfParameters = -1;
option.NumberOfParametersFixed = false;
option.Obligatory = obligatory;
option.Finded = false;
Thomas Feuvrier
committed
Thomas Feuvrier
committed
Thomas Feuvrier
committed
::ParseCommandLine(int argc, char *argv[],
CommandLineArgumentParseResult * outResult,
bool tryParse = TryParseCommandLine(argc, argv, outResult, false, failOnUnknownTrailingParameters);
bool IsHelp = outResult->IsOptionPresent("--help");
PrintUsage(std::cout);
CommandLineArgumentParserHelpException e(__FILE__, __LINE__);
e.SetDescription("ParseCommandLine(): Help Parser");
throw e;
bool IsVersion = outResult->IsOptionPresent("--version");
PrintVersion(std::cout);
CommandLineArgumentParserHelpException e(__FILE__, __LINE__);
e.SetDescription("ParseCommandLine(): Version Parser");
throw e;
tryParse = TryParseCommandLine(argc, argv, outResult, true, failOnUnknownTrailingParameters);
PrintUsage(std::cerr);
CommandLineArgumentParserArgumentErrorException e(__FILE__, __LINE__);
e.SetDescription("ParseCommandLine() argument Error");
throw e;
Thomas Feuvrier
committed
}
bool CommandLineArgumentParser::TryParseCommandLine(int argc, char *argv[],
CommandLineArgumentParseResult * outResult,
bool reportFailedMsg,
bool failOnUnknownTrailingParameters)
Thomas Feuvrier
committed
{
Thomas Feuvrier
committed
int i;
Thomas Feuvrier
committed
m_ProgramName = std::string(argv[0]);
Thomas Feuvrier
committed
std::string arg(argv[i]);
// Check if the argument is known
std::cerr << "The following '" << arg << "' option is unknown !!" << std::endl;
Thomas Feuvrier
committed
return false;
// If the number of parameters is predefined
// Check if the number of parameters is correct
int nParameters = m_OptionList[index].NumberOfParameters;
std::cerr << "Missing one (or more) parameter(s) for the following '" << arg << "' option." << std::endl;
// Tell the result that the option has been encountered
outResult->AddOption(m_OptionList[index].CommonName);
// Pass in the parameters
for (int j = 0; j < nParameters; ++j, ++i)
{
outResult->AddParameter(m_OptionList[index].CommonName, std::string(argv[i + 1]));
}
}
// If the number of parameters is not defined, read until the next option or the end of argv
else
// Tell the result that the option has been encountered
outResult->AddOption(m_OptionList[index].CommonName);
bool goOnFlag(true);
if (strArgv[0] == '-' )
// 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;
}
Thomas Feuvrier
committed
// Check that all required arguments are specified
for (unsigned int cpt = 0; cpt < m_OptionList.size(); ++cpt)
if ((m_OptionList[cpt].Obligatory == true) && (m_OptionList[cpt].Finded == false))
std::cerr << "'" << m_OptionList[cpt].CommonName << "' argument is required !!!" << std::endl;
Thomas Feuvrier
committed
return true;
}
bool CommandLineArgumentParser::IsNumber(const std::string& text)
// Don't test the sign (-)
unsigned int i = 1;
while (text[i])
{
if( text[i] != '.' && text[i] != ',')
{
if(!isdigit(text[i]))
{
return false;
}
}
}
return true;
}
bool CommandLineArgumentParser::FindOption(const std::string& option, int& index)
Thomas Feuvrier
committed
{
if ((m_OptionList[cpt].CommonName == strOption) || (m_OptionList[cpt].Synonym == strOption))
Thomas Feuvrier
committed
}
Thomas Feuvrier
committed
{
os << std::endl;
if (!m_ProgramDescription.empty())
os << m_ProgramDescription << std::endl << std::endl;
// Compute the max width for option display
int largeur = m_OptionList[i].CommonName.size() + m_OptionList[i].Synonym.size();
// Check that all required arguments are present on the command line
int largeur = m_OptionList[i].CommonName.size() + m_OptionList[i].Synonym.size();
for (int b = largeur; b < largeurmax; b++)
os << " ";
os << " : " << m_OptionList[i].Description;
if (m_OptionList[i].NumberOfParametersFixed == true)
Romain Garrigues
committed
{
case 0:
break;
case 1:
os << " (" << m_OptionList[i].NumberOfParameters << " parameter)";
break;
default:
os << " (" << m_OptionList[i].NumberOfParameters << " parameters)";
break;
Romain Garrigues
committed
}
Thomas Feuvrier
committed
}