From de2d04fc7f2e305bc2e43986dd4b430226639b4c Mon Sep 17 00:00:00 2001
From: Cyrille Valladeau <cyrille.valladeau@c-s.fr>
Date: Mon, 12 Sep 2011 16:24:20 +0200
Subject: [PATCH] ENH : commandline helper in progress

---
 Code/Core/otbWrapperApplication.cxx           |  34 ++++
 Code/Core/otbWrapperApplication.h             |  22 +++
 .../otbWrapperCommandLineLauncher.cxx         | 187 +++++-------------
 .../otbWrapperCommandLineParser.cxx           |  21 ++
 .../CommandLine/otbWrapperCommandLineParser.h |   1 +
 Example/otbRescale.cxx                        |   4 +-
 6 files changed, 134 insertions(+), 135 deletions(-)

diff --git a/Code/Core/otbWrapperApplication.cxx b/Code/Core/otbWrapperApplication.cxx
index 4cb75d1189..880eca355c 100644
--- a/Code/Core/otbWrapperApplication.cxx
+++ b/Code/Core/otbWrapperApplication.cxx
@@ -397,6 +397,12 @@ std::string Application::GetParameterDescription(std::string parameter)
   return param->GetDescription();
 }
 
+void Application::SetParameterDescription(std::string parameter, std::string desc)
+{
+  Parameter* param = GetParameterByKey(parameter);
+  param->SetDescription(desc);
+}
+
 int Application::GetParameterInt(std::string parameter)
 {
   int ret = 0;
@@ -539,6 +545,34 @@ VectorDataType* Application::GetParameterVectorData(std::string parameter)
 }
 
 
+std::string Application::GetParameterAsString(std::string paramKey)
+{
+  std::string ret;
+  ParameterType type = this->GetParameterType( paramKey );
+
+  if( type == ParameterType_String || type == ParameterType_Filename || type == ParameterType_Directory || 
+      type == ParameterType_InputImage || type == ParameterType_InputComplexImage || type == ParameterType_InputVectorData 
+      || type == ParameterType_OutputImage || type == ParameterType_OutputVectorData  )
+    {
+      ret = this->GetParameterString( paramKey );
+    }
+  else if ( type == ParameterType_Int || type == ParameterType_Radius || type == ParameterType_Choice )
+    {
+      itk::OStringStream oss;
+      oss << this->GetParameterInt( paramKey );
+      ret = oss.str();
+    }
+  else if( type == ParameterType_Float )
+    {
+      itk::OStringStream oss;
+      oss << this->GetParameterFloat( paramKey );
+      ret = oss.str();
+    }
+
+  //TODO: exception if not found ?
+  return ret;
+}
+
 void
 Application::AddChoice(std::string paramKey, std::string paramName)
 {
diff --git a/Code/Core/otbWrapperApplication.h b/Code/Core/otbWrapperApplication.h
index 2222283178..99ac117499 100644
--- a/Code/Core/otbWrapperApplication.h
+++ b/Code/Core/otbWrapperApplication.h
@@ -107,6 +107,9 @@ public:
   /* Returns the description of a parameter */
   std::string GetParameterDescription(std::string paramKey);
 
+  /* Set the description of a parameter */
+  void SetParameterDescription(std::string paramKey, std::string dec);
+
   /* Enable the use of an optional parameter. Returns the previous state */
   void EnableParameter(std::string paramKey);
 
@@ -243,10 +246,29 @@ public:
   /* GetParameterInt
    *
    * Can be called for types :
+
    * \li ParameterType_InputVectorData
    */
   VectorDataType* GetParameterVectorData(std::string parameter);
 
+ /* Get the parameter as a std::string
+   *
+   * Can be called for types :
+   * \li ParameterType_Float
+   * \li ParameterType_Int
+   * \li ParameterType_Choice
+   * \li ParameterType_Radius
+   * \li ParameterType_String
+   * \li ParameterType_Filename
+   * \li ParameterType_Directory
+   * \li ParameterType_InputImage
+   * \li ParameterType_InputComplexImage
+   * \li ParameterType_InputVectorData
+   * \li ParameterType_OutputImage
+   * \li ParameterType_OutputVectorData
+   */
+  std::string GetParameterAsString(std::string paramKey);
+
   /* Get the list of all parameters
    */
   std::vector<std::string> GetParametersKeys(bool recursive = true);
diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
index 37201b298a..e3d84fd4fd 100644
--- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
+++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
@@ -274,14 +274,26 @@ CommandLineLauncher::LoadParameters()
 void
 CommandLineLauncher::DisplayHelp()
 {
+  std::cerr<<std::endl;
   std::cerr<<"====================== HELP CONTEXT ======================"<<std::endl;
-  std::cerr<<"Name: "<<m_Application->GetName()<<std::endl;
-  std::cerr<<"Description: "<<m_Application->GetDescription()<<std::endl;
-  std::cerr<<"Parameters: "<<std::endl;
+  std::cerr<<"NAME: "<<m_Application->GetName()<<std::endl;
+  std::cerr<<"DESCRIPTION: "<<m_Application->GetDescription()<<std::endl;
+  std::cerr<<"PARAMETERS: "<<std::endl;
+
   ParameterGroup::Pointer paramGr = m_Application->GetParameterList();
   const unsigned int nbOfParam = paramGr->GetNumberOfParameters();
-  
-  bool addMandaTag = true;
+
+
+  std::cerr<<"=== Mandatory parameters: "<<std::endl;
+  std::cerr<<"--modPath (Executables paths)"<<std::endl;
+  std::cerr<<"\t   Description: Paths to the executable library."<<std::endl;
+  if( !m_Parser->IsAttributExists( "--modPath", m_Expression ) )
+    std::cerr<<"\t        Status: ENVIRONEMENT PATH"<<std::endl;
+  else if( m_Path == "") 
+    std::cerr<< "\t       Status: NO VALUE ASSOCIATED "<<m_Path<<std::endl;
+  else
+    std::cerr<< "\t       Status: USER VALUE: "<<m_Path<<std::endl;
+
   // Mandatory parameters
   for( unsigned int i=0; i<nbOfParam; i++ )
     {
@@ -291,44 +303,41 @@ CommandLineLauncher::DisplayHelp()
         {
           itk::OStringStream oss;
           oss<<"--"<<param->GetKey()<<" ("<<param->GetName()<<")"<< std::endl;
+
           if( std::string(param->GetDescription()).size() != 0 )
             {
-              oss<<"\t Description: "<<param->GetDescription()<<")."<<std::endl;
+              oss<<"\t   Description: "<<param->GetDescription()<<std::endl;
             }
-          //oss << "\t Default value: "<<param->GetDefaultValue()<< std::endl;
+          if( m_Application->HasValue( param->GetKey() ) )
+            oss << "\t Default value: "<<m_Application->GetParameterAsString( param->GetKey() )<< std::endl;
+          else
+            oss << "\t Default value: none"<<std::endl;
+
+
+          std::cout<<"sssiiiizzzee: "<<m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression).size()<<std::endl;
           if( !m_Parser->IsAttributExists( std::string("--").append(param->GetKey()), m_Expression) )
             {
-              oss << "\t Status: MISSING."<< std::endl;
+              std::cout<<"exist pas "<<std::string("--").append(param->GetKey())<<" => "<<m_Application->HasValue( param->GetKey())<<std::endl;
+
+              if ( !m_Application->HasValue( param->GetKey() ) )
+                oss << "\t        Status: MISSING"<< std::endl;
+              else
+                oss << "\t        Status: DEFAULT VALUE"<< std::endl;
             }
           else if( m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression).size() == 0 )
             {
-              oss << "\t Status: NO VALUE ASSOCIATED."<< std::endl;
+              oss << "\t        Status: NO VALUE ASSOCIATED"<< std::endl;
             }
           else
             {
-              oss << "\t Status: USER VALUE: ";
-              std::vector<std::string> values = m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression);
-              for( unsigned int i=0; i<values.size(); i++)
-                {
-                  if( i<values.size() )
-                    {
-                      oss<<values[i]<<" ";
-                    }
-                  else
-                    {
-                      oss<<values[i];
-                    }
-                }
+              oss << "\t        Status: USER VALUE: ";
+              oss << m_Parser->GetAttributAsString( std::string("--").append(param->GetKey()), m_Expression );
               oss << std::endl;
             }
 
-          if( addMandaTag == true )
-            {
-              std::cerr<<"=== Mandatory parameters: "<<std::endl;
-              addMandaTag = false;
-            }
           std::cerr<< oss.str();
         }
+std::cerr<<std::endl;
     }
  
   bool addOptionTag = true;
@@ -340,131 +349,43 @@ CommandLineLauncher::DisplayHelp()
       if( param->GetMandatory() != true )
         {
           itk::OStringStream oss;
-          oss<<"--"<<param->GetKey()<<" ("<<param->GetName()<<")"<< std::endl;
-          if( std::string(param->GetDescription()).size() != 0 )
+          oss<<"--"<<param->GetKey()<<std::endl;
+          oss<<"\t          Name: "<<param->GetName()<< std::endl;
+
+                    if( std::string(param->GetDescription()).size() != 0 )
             {
-              oss<<"\t Description: "<<param->GetDescription()<<")."<<std::endl;
+              oss<<"\t   Description: "<<param->GetDescription()<<std::endl;
             }
-          //oss << "\t Default value: "<<param->GetDefaultValue()<< std::endl;
+          if( m_Application->HasValue( param->GetKey() ) )
+            oss << "\t Default value: "<<m_Application->GetParameterAsString( param->GetKey() )<< std::endl;
+          else
+            oss << "\t Default value: none"<<std::endl;
           if( !m_Parser->IsAttributExists( std::string("--").append(param->GetKey()), m_Expression) )
             {
-              oss << "\t Status: NOT USED."<< std::endl;
+              if ( !m_Application->HasValue( param->GetKey() ) )
+                oss << "\t        Status: MISSING"<< std::endl;
+              else
+                oss << "\t        Status: DEFAULT VALUE"<< std::endl;
             }
           else if( m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression).size() == 0 )
             {
-              oss << "\t Status: NO VALUE ASSOCIATED."<< std::endl;
+              oss << "\t        Status: NO VALUE ASSOCIATED"<< std::endl;
             }
           else
             {
-              oss << "\t Status: USER VALUE: ";
-              std::vector<std::string> values = m_Parser->GetAttribut( std::string("--").append(param->GetKey()), m_Expression);
-              for( unsigned int i=0; i<values.size(); i++)
-                {
-                  if( i<values.size() )
-                    {
-                      oss<<values[i]<<" ";
-                    }
-                  else
-                    {
-                      oss<<values[i];
-                    }
-                }
+              oss << "\t        Status: USER VALUE: ";
+              oss << m_Parser->GetAttributAsString( std::string("--").append(param->GetKey()), m_Expression );
               oss << std::endl;
             }
 
-          if( addOptionTag == true )
-            {
-              std::cerr<<"=== Optional parameters: "<<std::endl;
-              addOptionTag = false;
-            }
-          std::cerr<< oss.str() << std::endl;
+          std::cerr<< oss.str();
         }
-    }
-
-  std::cerr<<"Detected user paths: "<<m_Path<<std::endl;
-
-}
-
-/*
-template <class TParameterType>
-void
-CommandLineLauncher::SetValueToParameter(Parameter * param, const std::string & val )
-{
-  if( !this->CanCreateParameter<TParameterType>( param ) )
-    {
-      itkExceptionMacro("Impossible cast to add value to the parameter "<<param->GetKey()<<".");
-    }
-  
-  dynamic_cast<TParameterType *>(param)->SetValue( val );
-}
-
-template <class TParameterType>
-void
-CommandLineLauncher::SetValueToParameter(Parameter * param, const std::vector<std::string> & val )
-{
-  if( !this->CanCreateParameter<TParameterType>( param ) )
-    {
-      itkExceptionMacro("Impossible cast to add value to the parameter "<<param->GetKey()<<".");
-    }
-  
-  dynamic_cast<TParameterType *>(param)->SetValue( val );
-}
-
-template <class TParameterType>
-void
-CommandLineLauncher::SetFileNameToParameter(Parameter * param, const std::string & val )
-{
-  if( !this->CanCreateParameter<TParameterType>( param ) )
-    {
-      itkExceptionMacro("Impossible cast to add value to the parameter "<<param->GetKey()<<".");
-    }
-  
-  dynamic_cast<TParameterType *>(param)->SetFileName( val );
-}
-
-template <class TParameterType>
-void
-CommandLineLauncher::SetFileNameToParameter(Parameter * param, const std::vector<std::string> & val )
-{
-  if( !this->CanCreateParameter<TParameterType>( param ) )
-    {
-      itkExceptionMacro("Impossible cast to add value to the parameter "<<param->GetKey()<<".");
-    }
-  
-  dynamic_cast<TParameterType *>(param)->SetFileNameList( val );
-}
+ 
 
-template <class TParameterType>
-void
-CommandLineLauncher::SetFromFileNameToParameter(Parameter * param, const std::string & val )
-{
-  if( !this->CanCreateParameter<TParameterType>( param ) )
-    {
-      itkExceptionMacro("Impossible cast to add value to the parameter "<<param->GetKey()<<".");
     }
-  
-  dynamic_cast<TParameterType *>(param)->SetFromFileName( val );
-}
 
-template <class TParameterType>
-void
-CommandLineLauncher::SetFromFileNameToParameter(Parameter * param, const std::vector<std::string> & val )
-{
-  if( !this->CanCreateParameter<TParameterType>( param ) )
-    {
-      itkExceptionMacro("Impossible cast to add value to the parameter "<<param->GetKey()<<".");
-    }
-  
-  dynamic_cast<TParameterType *>(param)->SetListFromFileName( val );
 }
 
-template <class TParameterType>
-bool
-CommandLineLauncher::CanCreateParameter( Parameter * param )
-{
-  return dynamic_cast<TParameterType *>(param) != 0;
-}
-*/
 }
 }
 
diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx
index 4c5f5e2b23..b7357df287 100644
--- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx
+++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx
@@ -210,10 +210,31 @@ CommandLineParser::GetAttribut( const std::string & key, const std::string & exp
    return res; 
 }
 
+std::string
+CommandLineParser::GetAttributAsString( const std::string & key, const std::string & exp )
+{
+  std::string res;
+  std::vector<std::string> values = this->GetAttribut( key, exp );
+  for( unsigned int i=0; i<values.size(); i++)
+    {
+      if( i<values.size() )
+        {
+          res.append(values[i]);
+          res.append(" ");
+        }
+      else
+        {
+          res.append(values[i]);
+        }
+    }
+  return res;
+}
+
 
 bool
 CommandLineParser::IsAttributExists( const std::string key, const std::string & exp  )
 {
+  std::cout<<"--------- "<<key<<"    "<<exp<<std::endl;
   std::size_t found = std::string(exp).find(key);
   if( found == std::string::npos )
     {
diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h
index 24dafa5d83..53450caaf2 100644
--- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h
+++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h
@@ -59,6 +59,7 @@ public:
                   
 
   std::vector<std::string> GetAttribut( const std::string & key, const std::string & exp );
+  std::string GetAttributAsString( const std::string & key, const std::string & exp );
   
   bool IsAttributExists(const std::string key, const std::string & exp );
 
diff --git a/Example/otbRescale.cxx b/Example/otbRescale.cxx
index 9ccbd7f790..b0af740e80 100644
--- a/Example/otbRescale.cxx
+++ b/Example/otbRescale.cxx
@@ -68,9 +68,9 @@ private:
     AddParameter(ParameterType_Float,      "outmin", "Output min value");
     AddParameter(ParameterType_Float,      "outmax", "Output max value");
     SetParameterFloat("outmin", 0);
-    //SetDescription( "outmin", "Minimum value of the output image." );
+    SetParameterDescription( "outmin", "Minimum value of the output image." );
     SetParameterFloat("outmax", 255);
-    //SetDescription( "outmax", "Maximum value of the output image." );
+    SetParameterDescription( "outmax", "Maximum value of the output image." );
   }
 
   void DoUpdateParameters()
-- 
GitLab