LCOV - code coverage report
Current view: top level - apps - gdalalg_main.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 33 52 63.5 %
Date: 2025-03-28 11:40:40 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             : *
       3             : * Project:  GDAL
       4             : * Purpose:  gdal "main" command
       5             : * Author:   Even Rouault <even dot rouault at spatialys.com>
       6             : *
       7             : ******************************************************************************
       8             : * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
       9             : *
      10             : * SPDX-License-Identifier: MIT
      11             : ****************************************************************************/
      12             : 
      13             : #include "gdalalg_main.h"
      14             : 
      15             : //! @cond Doxygen_Suppress
      16             : 
      17             : /************************************************************************/
      18             : /*                  GDALMainAlgorithm::GDALMainAlgorithm()              */
      19             : /************************************************************************/
      20             : 
      21         108 : GDALMainAlgorithm::GDALMainAlgorithm()
      22         108 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      23             : {
      24         648 :     for (const std::string &subAlgName :
      25        1404 :          GDALGlobalAlgorithmRegistry::GetSingleton().GetNames())
      26             :     {
      27             :         const auto pInfo =
      28         648 :             GDALGlobalAlgorithmRegistry::GetSingleton().GetInfo(subAlgName);
      29         648 :         if (pInfo)
      30         648 :             RegisterSubAlgorithm(*pInfo);
      31             :     }
      32             : 
      33         216 :     SetCallPath({NAME});
      34             : 
      35             :     m_longDescription = "'gdal <FILENAME>' can also be used as a shortcut for "
      36             :                         "'gdal info <FILENAME>'.\n"
      37             :                         "And 'gdal read <FILENAME> ! ...' as a shortcut for "
      38         108 :                         "'gdal pipeline <FILENAME> ! ...'.";
      39             : 
      40         108 :     SetDisplayInJSONUsage(false);
      41         108 : }
      42             : 
      43             : /************************************************************************/
      44             : /*              GDALMainAlgorithm::ParseCommandLineArguments()          */
      45             : /************************************************************************/
      46             : 
      47          39 : bool GDALMainAlgorithm::ParseCommandLineArguments(
      48             :     const std::vector<std::string> &args)
      49             : {
      50             :     // Detect shortest form of pipeline:
      51             :     // "gdal read in.tif ! .... ! write out.tif"
      52          39 :     if (args.size() >= 2 && args[0] == "read")
      53             :     {
      54             :         m_subAlg =
      55           0 :             GDALGlobalAlgorithmRegistry::GetSingleton().Instantiate("pipeline");
      56           0 :         if (m_subAlg)
      57             :         {
      58           0 :             bool ret = m_subAlg->ParseCommandLineArguments(args);
      59           0 :             if (ret)
      60             :             {
      61           0 :                 m_selectedSubAlg = &(m_subAlg->GetActualAlgorithm());
      62           0 :                 std::vector<std::string> callPath(m_callPath);
      63           0 :                 callPath.push_back("vector");
      64           0 :                 m_selectedSubAlg->SetCallPath(callPath);
      65           0 :                 return true;
      66             :             }
      67           0 :             else if (strstr(CPLGetLastErrorMsg(),
      68           0 :                             "has both raster and vector content"))
      69             :             {
      70           0 :                 m_showUsage = false;
      71           0 :                 return false;
      72             :             }
      73             :         }
      74             : 
      75           0 :         return GDALAlgorithm::ParseCommandLineArguments(args);
      76             :     }
      77             :     // Generic case: "gdal {subcommand} arguments"
      78             :     // where subcommand is a known subcommand
      79          39 :     else if (args.size() >= 1 && InstantiateSubAlgorithm(args[0]))
      80             :     {
      81          34 :         return GDALAlgorithm::ParseCommandLineArguments(args);
      82             :     }
      83             :     // Otherwise check if that is the shortest form of "gdal read mydataset"
      84             :     // where "read" is omitted: "gdal in.tif"
      85             :     else
      86             :     {
      87             :         VSIStatBufL sStat;
      88           8 :         for (const auto &arg : args)
      89             :         {
      90           4 :             if (VSIStatL(arg.c_str(), &sStat) == 0)
      91             :             {
      92             :                 m_subAlg =
      93           2 :                     GDALGlobalAlgorithmRegistry::GetSingleton().Instantiate(
      94           1 :                         "info");
      95           1 :                 if (m_subAlg)
      96             :                 {
      97           1 :                     bool ret = m_subAlg->ParseCommandLineArguments(args);
      98           1 :                     if (ret)
      99             :                     {
     100           1 :                         m_selectedSubAlg = &(m_subAlg->GetActualAlgorithm());
     101           1 :                         std::vector<std::string> callPath(m_callPath);
     102           1 :                         callPath.push_back(m_selectedSubAlg->GetArg("layer")
     103             :                                                ? "vector"
     104             :                                                : "raster");
     105           1 :                         m_selectedSubAlg->SetCallPath(callPath);
     106           1 :                         return true;
     107             :                     }
     108           0 :                     else if (strstr(CPLGetLastErrorMsg(),
     109           0 :                                     "has both raster and vector content"))
     110             :                     {
     111           0 :                         m_showUsage = false;
     112           0 :                         return false;
     113             :                     }
     114             :                 }
     115             :             }
     116             :         }
     117             : 
     118           4 :         return GDALAlgorithm::ParseCommandLineArguments(args);
     119             :     }
     120             : }
     121             : 
     122             : /************************************************************************/
     123             : /*                   GDALMainAlgorithm::GetUsageForCLI()                */
     124             : /************************************************************************/
     125             : 
     126             : std::string
     127           6 : GDALMainAlgorithm::GetUsageForCLI(bool shortUsage,
     128             :                                   const UsageOptions &usageOptions) const
     129             : {
     130           6 :     if (m_selectedSubAlg)
     131           3 :         return m_selectedSubAlg->GetUsageForCLI(shortUsage, usageOptions);
     132           3 :     if (m_showUsage)
     133           3 :         return GDALAlgorithm::GetUsageForCLI(shortUsage, usageOptions);
     134           0 :     return std::string();
     135             : }
     136             : 
     137             : //! @endcond

Generated by: LCOV version 1.14