From 8a4314d4c59651989eca2aff22c625107b6facf0 Mon Sep 17 00:00:00 2001
From: Thomas Feuvrier <thomas.feuvrier@c-s.fr>
Date: Mon, 13 Feb 2006 16:24:35 +0000
Subject: [PATCH] =?UTF-8?q?Initialisation=20de=20la=20classe,=20permettant?=
 =?UTF-8?q?=20de=20g=C3=A9rer=20la=20ligne=20de=20commande=20d'un=20progra?=
 =?UTF-8?q?mme?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Code/Common/otbCommandLineArgumentParser.cxx | 170 +++++++++++++++++++
 Code/Common/otbCommandLineArgumentParser.h   | 124 ++++++++++++++
 Code/Common/otbCommandLineArgumentParser.txx |  28 +++
 3 files changed, 322 insertions(+)
 create mode 100755 Code/Common/otbCommandLineArgumentParser.cxx
 create mode 100755 Code/Common/otbCommandLineArgumentParser.h
 create mode 100755 Code/Common/otbCommandLineArgumentParser.txx

diff --git a/Code/Common/otbCommandLineArgumentParser.cxx b/Code/Common/otbCommandLineArgumentParser.cxx
new file mode 100755
index 0000000000..b59d7343cd
--- /dev/null
+++ b/Code/Common/otbCommandLineArgumentParser.cxx
@@ -0,0 +1,170 @@
+#include "otbCommandLineArgumentParser.h"
+
+#include <assert.h>
+#include <iostream>
+
+namespace otb
+{
+
+void 
+CommandLineArgumentParser
+::AddOption(const char *name, const int nParameters, const char * comment)
+{
+  // Create a structure for the command
+  OptionType option;
+  option.CommonName = std::string(name);
+  option.CommentName = std::string(comment);
+  option.NumberOfParameters = nParameters;
+
+  // Add the option to the map
+  m_OptionMap[std::string(name)] = option;
+}
+
+void 
+CommandLineArgumentParser
+::AddSynonim(const char *option, const char *synonim)
+{
+  std::string strOption(option);
+  std::string strSynonim(synonim);
+
+  // The option should exist!
+  assert(m_OptionMap.find(strOption) != m_OptionMap.end());
+
+  // Create a new option object
+  OptionType o;
+  o.NumberOfParameters = m_OptionMap[strOption].NumberOfParameters;
+  o.CommentName = m_OptionMap[strOption].CommentName;
+  o.CommonName = strOption;
+
+  // Insert the option into the map
+  m_OptionMap[strSynonim] = o;
+}
+
+bool 
+CommandLineArgumentParser
+::TryParseCommandLine(int argc, char *argv[], 
+                      CommandLineArgumentParseResult &outResult,
+                      bool failOnUnknownTrailingParameters)
+{
+  // Clear the result
+  outResult.Clear();
+
+  m_ProgramName = std::string(argv[0]);
+
+  // Go through the arguments
+  for(int i=1; i < argc; i++)
+    {
+    // Get the next argument
+    std::string arg(argv[i]);
+
+    // Check if the argument is known
+    if(m_OptionMap.find(arg) == m_OptionMap.end())
+      {
+      if(failOnUnknownTrailingParameters)
+        {
+        // Unknown argument found
+        std::cerr << "Unrecognized command line option '" << arg << "'" << std::endl;
+        return false;
+        }
+      else return true;
+      }
+
+    // Check if the number of parameters is correct
+    int nParameters = m_OptionMap[arg].NumberOfParameters;
+    if(i+nParameters >= argc) 
+      {
+      // Too few parameters
+      std::cerr << "Too few parameters to command line option '" << arg << "'" << std::endl;
+      return false;
+      }
+
+    // Tell the result that the option has been encountered
+    outResult.AddOption(m_OptionMap[arg].CommonName,nParameters);
+
+    // Pass in the parameters
+    for(int j=0;j<nParameters;j++,i++)
+      outResult.AddParameter(m_OptionMap[arg].CommonName,std::string(argv[i+1]));
+    
+    }
+
+  // Everything is good
+  return true;
+}
+
+void
+CommandLineArgumentParser
+::PrintUsage(std::ostream& os/*, itk::Indent indent*/)const
+{
+        os << " Usage : "<<m_ProgramName<<std::endl;
+        OptionMapType::const_iterator iterMap = m_OptionMap.begin();
+        
+//        iterMap.first
+/*        while ( ! iterMap.end() )
+        {
+                os << iterMap<<std::endl;
+                ++iterMap;
+        }*/
+
+}
+
+
+// --------- CommandLineArgumentParseResult  ----------------------------------------
+void
+CommandLineArgumentParseResult
+::PrintSelf(std::ostream& os/*, itk::Indent indent*/)const
+{
+
+
+}
+
+
+
+
+
+
+
+
+bool 
+CommandLineArgumentParseResult
+::IsOptionPresent(const char *option)
+{
+  return (m_OptionMap.find(std::string(option)) != m_OptionMap.end());
+}
+
+const char * 
+CommandLineArgumentParseResult
+::GetOptionParameter(const char *option, unsigned int number)
+{
+  assert(IsOptionPresent(option));
+  assert(number < m_OptionMap[std::string(option)].size());
+
+  return m_OptionMap[std::string(option)][number].c_str();
+}
+
+
+void  
+CommandLineArgumentParseResult
+::Clear()
+{
+  m_OptionMap.clear();
+}
+
+void  
+CommandLineArgumentParseResult
+::AddOption(const std::string &option, int nParms)
+{
+  ParameterArrayType pat;
+  pat.reserve(nParms);
+  m_OptionMap[option] = pat;
+}
+
+void  
+CommandLineArgumentParseResult
+::AddParameter(const std::string &option, const std::string &parameter)
+{
+  m_OptionMap[option].push_back(parameter);  
+}
+
+}
+
+
diff --git a/Code/Common/otbCommandLineArgumentParser.h b/Code/Common/otbCommandLineArgumentParser.h
new file mode 100755
index 0000000000..271c55c87b
--- /dev/null
+++ b/Code/Common/otbCommandLineArgumentParser.h
@@ -0,0 +1,124 @@
+/*=========================================================================
+
+  Programme :   OTB (ORFEO ToolBox)
+  Auteurs   :   CS - T.Feuvrier
+  Language  :   C++
+  Date      :   10 février 2006
+  Version   :   
+  Role      :   Classe permettant de géer les paramètres d'une ligne de commande d'un programme
+  $Id$
+
+=========================================================================*/
+#ifndef __otbCommandLineArgumentParser_h_
+#define __otbCommandLineArgumentParser_h_
+
+#if defined(_MSC_VER)
+#pragma warning ( disable : 4786 )
+#pragma warning ( disable : 4503 )
+#endif
+
+#include <vector>
+#include <string>
+#include <list>
+#include <map>
+
+#include "itkIndent.h"
+
+namespace otb
+{
+
+/**
+ * \class CommandLineArgumentParseResult
+ * \brief Objet retourné par lCommandLineArgumentParser
+ * \see CommandLineArgumentParser
+ */
+class CommandLineArgumentParseResult
+{
+public:
+  /** Check whether the option was passed in or not */
+  bool IsOptionPresent(const char *option);
+
+  /** Get one of the parameters to the option */
+  const char *GetOptionParameter(const char *option, unsigned int number = 0);
+  
+  void PrintSelf(std::ostream& os/*, itk::Indent indent*/) const;
+
+  template< typename TypeValeur >
+  TypeValeur GetOptionParameterNumericValue(const char *option, unsigned int number)const;
+
+
+private:
+  typedef std::vector< std::string > ParameterArrayType;
+  typedef std::map< std::string, ParameterArrayType> OptionMapType;
+
+  void Clear();
+  void AddOption(const std::string &option, int nParms);
+  void AddParameter(const std::string &option, const std::string &parameter);
+
+  OptionMapType m_OptionMap;
+
+  friend class CommandLineArgumentParser;
+};
+
+/**
+ * \class CommandLineArgumentParser
+ * \brief Utilisé pour parser une ligne de commande contenant des arguments et la traduit en liste de paramètres.
+ * Usage:
+ * \code
+ *    // Initialise le parser
+ *    CommandLineArgumentParser parser;
+ *    parser.AddOption("-f",1);
+ *    parser.AddSynonim("-f","--filename");
+ *    parser.AddOption("-v",0);
+ *    parser.AddSynonim("-v","--verbose");
+ *
+ *    // Utilise le parser
+ *    CommandLineArgumentParseResult result;
+ *    if(parser.TryParseCommandLine(argc,argv,result)) {
+ *       if(result.IsOptionPresent("-f"))
+ *          cout << "Filename " << result.GetOptionParameter("-f") << endl;
+ *       ...
+ *    } 
+ * \endcode      
+ */
+class CommandLineArgumentParser
+{
+public:
+  /** Add an option with 0 or more parameters (words that follow it) */
+  void AddOption(const char *name, const int nParameters, const char * comment);
+  
+  /** Add a different string that envokes the same option (--file and -f) */  
+  void AddSynonim(const char *option, const char *synonim);
+
+  /** Try processing a command line.  Returns false if something breaks */
+  bool TryParseCommandLine(int argc, char *argv[], 
+                           CommandLineArgumentParseResult &outResult,
+                           bool failOnUnknownTrailingParameters = true);
+
+  void PrintUsage(std::ostream& os/*, itk::Indent indent*/) const;
+
+private:
+  // Synonim list type
+  typedef std::list< std::string > NameListType;
+  typedef struct 
+    {
+    std::string CommonName;
+    std::string CommentName;
+    unsigned int NumberOfParameters;
+    } OptionType;
+  typedef std::map< std::string, OptionType> OptionMapType;
+  
+  OptionMapType m_OptionMap;
+
+
+  std::string m_ProgramName;
+};
+
+
+#ifndef OTB_MANUAL_INSTANTIATION
+#include "otbCommandLineArgumentParser.txx"
+#endif
+
+}
+
+#endif // __otbCommandLineArgumentParser_h_
diff --git a/Code/Common/otbCommandLineArgumentParser.txx b/Code/Common/otbCommandLineArgumentParser.txx
new file mode 100755
index 0000000000..31510b34ae
--- /dev/null
+++ b/Code/Common/otbCommandLineArgumentParser.txx
@@ -0,0 +1,28 @@
+#ifndef _otbCommandLineArgumentParser_txx
+#define _otbCommandLineArgumentParser_txx
+
+#include "otbCommandLineArgumentParser.h"
+
+#include "itkMacro.h"
+
+namespace otb
+{
+
+
+template< typename TypeValeur >
+TypeValeur
+CommandLineArgumentParseResult
+::GetOptionParameterNumericValue(const char *option, unsigned int number)const
+{
+  const char * parametre = this->GetOptionParameter(option, number);
+  TypeValeur lValeur;
+  ::itk::OStringStream flux;
+  flux << parametre;
+  flux >> lValeur;
+  return lValeur;
+}
+
+
+} // end namespace otb
+
+#endif
-- 
GitLab