From 993f51dd0697e0b70e74e456c22585b0a0be3328 Mon Sep 17 00:00:00 2001
From: Julien Michel <julien.michel@cnes.fr>
Date: Mon, 19 Jun 2017 17:17:12 +0200
Subject: [PATCH] ENH: Modify behaviour of -help so as to get the long
 description of app and params

---
 .../include/otbWrapperCommandLineLauncher.h   |   7 +-
 .../src/otbWrapperCommandLineLauncher.cxx     | 119 ++++++++++++++----
 2 files changed, 102 insertions(+), 24 deletions(-)

diff --git a/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h b/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h
index 36b0b0de53..50c39f6cd5 100644
--- a/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h
+++ b/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h
@@ -109,7 +109,10 @@ public:
   bool BeforeExecute();
 
   /** Create and display the help of the application */
-  void DisplayHelp();
+  void DisplayHelp(bool longHelp=false);
+
+   /** Create and display the long help of the application */
+  void DisplayLongHelp();
 
   /** Performs specific action for testing environment */
   void LoadTestEnv();
@@ -141,7 +144,7 @@ protected:
 
   /** Create and display the help of the application */
   std::string DisplayParameterHelp( const Parameter::Pointer & param,
-                                    const std::string paramKey );
+                                    const std::string paramKey, bool longHelp = false);
 
   /** Check if each key is unique in the expression. */
   bool CheckUnicity();
diff --git a/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx b/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx
index d1cc388ebd..279130d5ca 100644
--- a/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx
+++ b/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx
@@ -197,7 +197,36 @@ bool CommandLineLauncher::BeforeExecute()
   // if help is asked...
   if (m_Parser->IsAttributExists("-help", m_VExpression) == true)
     {
-    this->DisplayHelp();
+    std::vector<std::string> val;
+    val = m_Parser->GetAttribut("-help", m_VExpression);
+
+    if(val.empty())
+      {      
+      this->DisplayHelp(true);
+      }
+    else
+      {
+      const unsigned int nbOfParam = appKeyList.size();
+      
+      m_MaxKeySize = std::string("progress").size();
+      for (unsigned int i = 0; i < nbOfParam; i++)
+        {
+        if (m_Application->GetParameterRole(appKeyList[i]) != Role_Output)
+          {
+          if( m_MaxKeySize < appKeyList[i].size() )
+            m_MaxKeySize = appKeyList[i].size();
+          }
+        }
+      
+      for(std::vector<std::string>::const_iterator it = val.begin(); it!=val.end();++it)
+        {
+        Parameter::Pointer param = m_Application->GetParameterByKey(*it);
+        if (param->GetRole() != Role_Output)
+          {
+          std::cerr << this->DisplayParameterHelp(param, *it,true)<<std::endl;
+          }
+        }
+      }
     return false;
     }
 
@@ -549,11 +578,11 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters()
                 {
                 dynamic_cast<EmptyParameter *> (param.GetPointer())->SetActive(false);
                 }
-             else
-              {
-              std::cerr << "ERROR: Wrong parameter value: " << paramKey << std::endl;
-              return WRONGPARAMETERVALUE;
-              }
+              else
+                {
+                std::cerr << "ERROR: Wrong parameter value: " << paramKey << std::endl;
+                return WRONGPARAMETERVALUE;
+                }
             }
         // Update the flag UserValue
         param->SetUserValue(true);
@@ -685,19 +714,38 @@ void CommandLineLauncher::LinkWatchers(itk::Object * itkNotUsed(caller), const i
     }
 }
 
-void CommandLineLauncher::DisplayHelp()
+void CommandLineLauncher::DisplayHelp(bool longHelp)
 {
-  std::cerr << "This is the "<<m_Application->GetName() << " application, version " << OTB_VERSION_STRING <<std::endl;
+  std::cerr<<std::endl;
+  std::cerr << "This is the "<<m_Application->GetDocName() << " ("<<m_Application->GetName()<<") application, version " << OTB_VERSION_STRING <<std::endl<<std::endl;
 
   std::cerr << m_Application->GetDescription() << std::endl;
-  std::cerr<<std::endl;
 
-  std::string link = m_Application->GetDocLink();
-  if (!link.empty())
-  {
-    std::cerr << "Complete documentation: " << link << std::endl;
+  if(longHelp)
+    { 
+    std::cerr<<"Tags: ";
+
+    std::vector<std::string> tags = m_Application->GetDocTags();
+    
+    for(std::vector<std::string>::const_iterator it = tags.begin(); it!=tags.end();++it)
+      {
+      std::cerr<<*it<<" ";
+      }
+    std::cerr<<std::endl;
+  
+    std::cerr<<std::endl;
+    std::cerr<<m_Application->GetDocLongDescription() << std::endl;
     std::cerr<<std::endl;
-  }
+    }
+  else
+    {
+    std::string link = m_Application->GetDocLink();
+    if (!link.empty())
+      {
+      std::cerr << "Complete documentation: " << link << " or -help" <<std::endl;
+      std::cerr<<std::endl;
+      }
+    }
 
   std::cerr << "Parameters: " << std::endl;
 
@@ -723,31 +771,47 @@ void CommandLineLauncher::DisplayHelp()
 
   for (unsigned int i = 0; i < nbOfParam; i++)
     {
-      Parameter::Pointer param = m_Application->GetParameterByKey(appKeyList[i]);
-      if (param->GetRole() != Role_Output)
-        {
-        std::cerr << this->DisplayParameterHelp(param, appKeyList[i]);
-        }
+    Parameter::Pointer param = m_Application->GetParameterByKey(appKeyList[i]);
+    if (param->GetRole() != Role_Output)
+      {
+      std::cerr << this->DisplayParameterHelp(param, appKeyList[i]);
+      }
     }
 
   std::cerr<<std::endl;
   //std::string cl(m_Application->GetCLExample());
 
+
+  std::cerr<<"Use -help param1 [... paramN] to see detailed documentation of those parameters."<<std::endl;
+  std::cerr<<std::endl;
+  
   std::cerr << "Examples: " << std::endl;
   std::cerr << m_Application->GetCLExample() << std::endl;
 
-}
+  if(longHelp)
+    {
+    std::cerr<<"Authors: "<<std::endl<<m_Application->GetDocAuthors() << std::endl;
 
+    std::cerr<<std::endl;
+    std::cerr<<"Limitations: "<<std::endl<<m_Application->GetDocLimitations() << std::endl;
+
+    std::cerr<<std::endl;
+    std::cerr<<"See also: "<<std::endl<<m_Application->GetDocSeeAlso() << std::endl;
+    std::cerr<<std::endl;
+
+    }  
+
+}
 
 void CommandLineLauncher::LoadTestEnv()
 {
   //Set seed for rand and itk mersenne twister
   //srand(1);
- // itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->SetSeed(121212);
+  // itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->SetSeed(121212);
 }
 
 
-std::string CommandLineLauncher::DisplayParameterHelp(const Parameter::Pointer & param, const std::string paramKey)
+std::string CommandLineLauncher::DisplayParameterHelp(const Parameter::Pointer & param, const std::string paramKey, bool longHelp)
 {
   // Display the type the type
   ParameterType type = m_Application->GetParameterType(paramKey);
@@ -924,6 +988,17 @@ std::string CommandLineLauncher::DisplayParameterHelp(const Parameter::Pointer &
   oss<<")";
 
   oss << std::endl;
+
+  if(longHelp)
+    {
+    oss << "        ";
+    for(unsigned int i=0; i<m_MaxKeySize;++i)
+      oss<<" ";
+    oss<<"                   ";
+    oss<<m_Application->GetParameterDescription(paramKey)<<std::endl;
+
+    }
+  
   return oss.str();
 }
 
-- 
GitLab