File: gdalalg_main.cpp

package info (click to toggle)
gdal 3.12.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92,292 kB
  • sloc: cpp: 1,223,498; ansic: 206,434; python: 26,278; java: 6,001; xml: 4,769; sh: 3,855; cs: 2,513; yacc: 1,306; makefile: 214
file content (175 lines) | stat: -rw-r--r-- 6,063 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/******************************************************************************
*
* Project:  GDAL
* Purpose:  gdal "main" command
* Author:   Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/

#include "gdalalg_main.h"

#include "gdal_priv.h"

//! @cond Doxygen_Suppress

#ifndef _
#define _(x) (x)
#endif

/************************************************************************/
/*                  GDALMainAlgorithm::GDALMainAlgorithm()              */
/************************************************************************/

GDALMainAlgorithm::GDALMainAlgorithm()
    : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
{
    for (const std::string &subAlgName :
         GDALGlobalAlgorithmRegistry::GetSingleton().GetNames())
    {
        const auto pInfo =
            GDALGlobalAlgorithmRegistry::GetSingleton().GetInfo(subAlgName);
        if (pInfo)
            RegisterSubAlgorithm(*pInfo);
    }

    SetCallPath({NAME});

    AddArg("version", 0, _("Display GDAL version and exit"), &m_version)
        .SetHiddenForAPI();
    AddArg("drivers", 0, _("Display driver list as JSON document"), &m_drivers);

    AddOutputStringArg(&m_output);

    m_longDescription = "'gdal <FILENAME>' can also be used as a shortcut for "
                        "'gdal info <FILENAME>'.\n"
                        "And 'gdal read <FILENAME> ! ...' as a shortcut for "
                        "'gdal pipeline <FILENAME> ! ...'.";

    SetDisplayInJSONUsage(false);
}

/************************************************************************/
/*              GDALMainAlgorithm::ParseCommandLineArguments()          */
/************************************************************************/

bool GDALMainAlgorithm::ParseCommandLineArguments(
    const std::vector<std::string> &args)
{
    // Detect shortest form of pipeline:
    // "gdal read in.tif ! .... ! write out.tif"
    if (args.size() >= 2 && args[0] == "read")
    {
        m_subAlg =
            GDALGlobalAlgorithmRegistry::GetSingleton().Instantiate("pipeline");
        if (m_subAlg)
        {
            bool ret = m_subAlg->ParseCommandLineArguments(args);
            if (ret)
            {
                m_selectedSubAlg = &(m_subAlg->GetActualAlgorithm());
                std::vector<std::string> callPath(m_callPath);
                callPath.push_back("vector");
                m_selectedSubAlg->SetCallPath(callPath);
                return true;
            }
            else if (strstr(CPLGetLastErrorMsg(),
                            "has both raster and vector content"))
            {
                m_showUsage = false;
                return false;
            }
        }

        return GDALAlgorithm::ParseCommandLineArguments(args);
    }
    else if (args.size() == 1 && args[0].size() >= 2 && args[0][0] == '-' &&
             args[0][1] == '-')
    {
        return GDALAlgorithm::ParseCommandLineArguments(args);
    }

    // Generic case: "gdal {subcommand} arguments"
    // where subcommand is a known subcommand
    else if (args.size() >= 1)
    {
        const auto nCounter = CPLGetErrorCounter();
        if (InstantiateSubAlgorithm(args[0]))
            return GDALAlgorithm::ParseCommandLineArguments(args);
        if (CPLGetErrorCounter() == nCounter + 1 &&
            strstr(CPLGetLastErrorMsg(), "Do you mean"))
        {
            return false;
        }
    }

    // Otherwise check if that is the shortest form of "gdal read mydataset"
    // where "read" is omitted: "gdal in.tif"
    {
        VSIStatBufL sStat;
        for (const auto &arg : args)
        {
            if (VSIStatL(arg.c_str(), &sStat) == 0)
            {
                m_subAlg =
                    GDALGlobalAlgorithmRegistry::GetSingleton().Instantiate(
                        "info");
                if (m_subAlg)
                {
                    bool ret = m_subAlg->ParseCommandLineArguments(args);
                    if (ret)
                    {
                        m_selectedSubAlg = &(m_subAlg->GetActualAlgorithm());
                        std::vector<std::string> callPath(m_callPath);
                        callPath.push_back(m_selectedSubAlg->GetArg("layer")
                                               ? "vector"
                                               : "raster");
                        m_selectedSubAlg->SetCallPath(callPath);
                        return true;
                    }
                    else if (strstr(CPLGetLastErrorMsg(),
                                    "has both raster and vector content"))
                    {
                        m_showUsage = false;
                        return false;
                    }
                }
            }
        }

        return GDALAlgorithm::ParseCommandLineArguments(args);
    }
}

/************************************************************************/
/*                   GDALMainAlgorithm::GetUsageForCLI()                */
/************************************************************************/

std::string
GDALMainAlgorithm::GetUsageForCLI(bool shortUsage,
                                  const UsageOptions &usageOptions) const
{
    if (m_selectedSubAlg)
        return m_selectedSubAlg->GetUsageForCLI(shortUsage, usageOptions);
    if (m_showUsage)
        return GDALAlgorithm::GetUsageForCLI(shortUsage, usageOptions);
    return std::string();
}

/************************************************************************/
/*                    GDALMainAlgorithm::RunImpl()                      */
/************************************************************************/

bool GDALMainAlgorithm::RunImpl(GDALProgressFunc, void *)
{
    if (m_drivers)
    {
        m_output = GDALPrintDriverList(GDAL_OF_KIND_MASK, true);
    }
    return true;
}

//! @endcond