LCOV - code coverage report
Current view: top level - apps - gdalalg_main.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 32 51 62.7 %
Date: 2025-01-18 12:42:00 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          73 : GDALMainAlgorithm::GDALMainAlgorithm()
      22          73 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      23             : {
      24         365 :     for (const std::string &subAlgName :
      25         803 :          GDALGlobalAlgorithmRegistry::GetSingleton().GetNames())
      26             :     {
      27             :         const auto pInfo =
      28         365 :             GDALGlobalAlgorithmRegistry::GetSingleton().GetInfo(subAlgName);
      29         365 :         if (pInfo)
      30         365 :             RegisterSubAlgorithm(*pInfo);
      31             :     }
      32             : 
      33             :     m_longDescription = "'gdal <FILENAME>' can also be used as a shortcut for "
      34             :                         "'gdal info <FILENAME>'.\n"
      35             :                         "And 'gdal read <FILENAME> ! ...' as a shortcut for "
      36          73 :                         "'gdal pipeline <FILENAME> ! ...'.";
      37             : 
      38          73 :     SetDisplayInJSONUsage(false);
      39          73 : }
      40             : 
      41             : /************************************************************************/
      42             : /*              GDALMainAlgorithm::ParseCommandLineArguments()          */
      43             : /************************************************************************/
      44             : 
      45          15 : bool GDALMainAlgorithm::ParseCommandLineArguments(
      46             :     const std::vector<std::string> &args)
      47             : {
      48             :     // Detect shortest form of pipeline:
      49             :     // "gdal read in.tif ! .... ! write out.tif"
      50          15 :     if (args.size() >= 2 && args[0] == "read")
      51             :     {
      52             :         m_subAlg =
      53           0 :             GDALGlobalAlgorithmRegistry::GetSingleton().Instantiate("pipeline");
      54           0 :         if (m_subAlg)
      55             :         {
      56           0 :             bool ret = m_subAlg->ParseCommandLineArguments(args);
      57           0 :             if (ret)
      58             :             {
      59           0 :                 m_selectedSubAlg = &(m_subAlg->GetActualAlgorithm());
      60           0 :                 std::vector<std::string> callPath(m_callPath);
      61           0 :                 callPath.push_back("vector");
      62           0 :                 m_selectedSubAlg->SetCallPath(callPath);
      63           0 :                 return true;
      64             :             }
      65           0 :             else if (strstr(CPLGetLastErrorMsg(),
      66           0 :                             "has both raster and vector content"))
      67             :             {
      68           0 :                 m_showUsage = false;
      69           0 :                 return false;
      70             :             }
      71             :         }
      72             : 
      73           0 :         return GDALAlgorithm::ParseCommandLineArguments(args);
      74             :     }
      75             :     // Generic case: "gdal {subcommand} arguments"
      76             :     // where subcommand is a known subcommand
      77          15 :     else if (args.size() >= 1 && InstantiateSubAlgorithm(args[0]))
      78             :     {
      79          10 :         return GDALAlgorithm::ParseCommandLineArguments(args);
      80             :     }
      81             :     // Otherwise check if that is the shortest form of "gdal read mydataset"
      82             :     // where "read" is omitted: "gdal in.tif"
      83             :     else
      84             :     {
      85             :         VSIStatBufL sStat;
      86           8 :         for (const auto &arg : args)
      87             :         {
      88           4 :             if (VSIStatL(arg.c_str(), &sStat) == 0)
      89             :             {
      90             :                 m_subAlg =
      91           2 :                     GDALGlobalAlgorithmRegistry::GetSingleton().Instantiate(
      92           1 :                         "info");
      93           1 :                 if (m_subAlg)
      94             :                 {
      95           1 :                     bool ret = m_subAlg->ParseCommandLineArguments(args);
      96           1 :                     if (ret)
      97             :                     {
      98           1 :                         m_selectedSubAlg = &(m_subAlg->GetActualAlgorithm());
      99           1 :                         std::vector<std::string> callPath(m_callPath);
     100           1 :                         callPath.push_back(m_selectedSubAlg->GetArg("layer")
     101             :                                                ? "vector"
     102             :                                                : "raster");
     103           1 :                         m_selectedSubAlg->SetCallPath(callPath);
     104           1 :                         return true;
     105             :                     }
     106           0 :                     else if (strstr(CPLGetLastErrorMsg(),
     107           0 :                                     "has both raster and vector content"))
     108             :                     {
     109           0 :                         m_showUsage = false;
     110           0 :                         return false;
     111             :                     }
     112             :                 }
     113             :             }
     114             :         }
     115             : 
     116           4 :         return GDALAlgorithm::ParseCommandLineArguments(args);
     117             :     }
     118             : }
     119             : 
     120             : /************************************************************************/
     121             : /*                   GDALMainAlgorithm::GetUsageForCLI()                */
     122             : /************************************************************************/
     123             : 
     124             : std::string
     125           4 : GDALMainAlgorithm::GetUsageForCLI(bool shortUsage,
     126             :                                   const UsageOptions &usageOptions) const
     127             : {
     128           4 :     if (m_selectedSubAlg)
     129           1 :         return m_selectedSubAlg->GetUsageForCLI(shortUsage, usageOptions);
     130           3 :     if (m_showUsage)
     131           3 :         return GDALAlgorithm::GetUsageForCLI(shortUsage, usageOptions);
     132           0 :     return std::string();
     133             : }
     134             : 
     135             : //! @endcond

Generated by: LCOV version 1.14