Skip to content

Commit

Permalink
gdal: advertize --version and --drivers switches
Browse files Browse the repository at this point in the history
Fixes #11360
  • Loading branch information
rouault committed Dec 2, 2024
1 parent c48c12c commit 554ef14
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion autotest/cpp/test_gdal_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2895,7 +2895,7 @@ TEST_F(test_gdal_algorithm, algorithm_c_api)

char **argNames = GDALAlgorithmGetArgNames(hAlg.get());
ASSERT_NE(argNames, nullptr);
EXPECT_EQ(CSLCount(argNames), 10);
EXPECT_EQ(CSLCount(argNames), 12);
CSLDestroy(argNames);

EXPECT_EQ(GDALAlgorithmGetArg(hAlg.get(), "non_existing"), nullptr);
Expand Down
5 changes: 3 additions & 2 deletions gcore/gdal_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3778,12 +3778,13 @@ int CPL_STDCALL GDALGeneralCmdLineProcessor(int nArgc, char ***ppapszArgv,
/* --formats */
/* --------------------------------------------------------------------
*/
else if (EQUAL(papszArgv[iArg], "--formats"))
else if (EQUAL(papszArgv[iArg], "--formats") ||
EQUAL(papszArgv[iArg], "--drivers"))
{
if (nOptions == 0)
nOptions = GDAL_OF_RASTER;

bool bJSON = false;
bool bJSON = EQUAL(papszArgv[iArg], "--drivers");
for (int i = 1; i < nArgc; i++)
{
if (strcmp(papszArgv[i], "-json") == 0 ||
Expand Down
39 changes: 28 additions & 11 deletions gcore/gdalalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,11 +715,18 @@ GDALAlgorithm::GDALAlgorithm(const std::string &name,
.SetOnlyForCLI()
.SetCategory(GAAC_COMMON)
.AddAction([this]() { m_specialActionRequested = true; });
AddArg("version", 0, _("Display GDAL version and exit"), &m_dummyBoolean)
.SetOnlyForCLI()
.SetCategory(GAAC_COMMON);
AddArg("json-usage", 0, _("Display usage as JSON document and exit"),
&m_JSONUsageRequested)
.SetOnlyForCLI()
.SetCategory(GAAC_COMMON)
.AddAction([this]() { m_specialActionRequested = true; });
AddArg("drivers", 0, _("Display driver list as JSON document and exit"),
&m_dummyBoolean)
.SetOnlyForCLI()
.SetCategory(GAAC_COMMON);
}

/************************************************************************/
Expand Down Expand Up @@ -2222,18 +2229,27 @@ GDALAlgorithm::GetUsageForCLI(bool shortUsage,
osRet += osPath;

bool hasNonPositionals = false;
for (const auto &arg : m_args)
{
if (!arg->IsHiddenForCLI() && !arg->IsPositional())
hasNonPositionals = true;
}

if (HasSubAlgorithms())
{
if (m_callPath.size() == 1)
{
osRet += " <COMMAND>\n";
osRet += "where <COMMAND> is one of:\n";
osRet += " <COMMAND>";
if (hasNonPositionals)
osRet += " [OPTIONS]";
osRet += "\nwhere <COMMAND> is one of:\n";
}
else
{
osRet += " <SUBCOMMAND>\n";
osRet += "where <SUBCOMMAND> is one of:\n";
osRet += " <SUBCOMMAND>";
if (hasNonPositionals)
osRet += " [OPTIONS]";
osRet += "\nwhere <SUBCOMMAND> is one of:\n";
}
size_t maxNameLen = 0;
for (const auto &subAlgName : GetSubAlgorithmNames())
Expand Down Expand Up @@ -2271,15 +2287,16 @@ GDALAlgorithm::GetUsageForCLI(bool shortUsage,
}
osRet += '\n';
}

if (shortUsage && hasNonPositionals)
{
osRet += "\nTry '";
osRet += osPath;
osRet += " --help' for help.\n";
}
}
else
{
for (const auto &arg : m_args)
{
if (!arg->IsHiddenForCLI() && !arg->IsPositional())
hasNonPositionals = true;
}

if (!m_args.empty())
{
if (hasNonPositionals)
Expand Down Expand Up @@ -2313,7 +2330,7 @@ GDALAlgorithm::GetUsageForCLI(bool shortUsage,
osRet += '\n';
}

if (!m_args.empty())
if (!m_args.empty() && !shortUsage)
{
std::vector<std::pair<GDALAlgorithmArg *, std::string>> options;
size_t maxOptLen;
Expand Down
1 change: 1 addition & 0 deletions gcore/gdalalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,7 @@ class CPL_DLL GDALAlgorithmRegistry
bool m_specialActionRequested = false;
bool m_helpRequested = false;
bool m_JSONUsageRequested = false;
bool m_dummyBoolean = false; // Used for --version
std::vector<std::unique_ptr<GDALAlgorithmArg>> m_args{};
std::map<std::string, GDALAlgorithmArg *> m_mapLongNameToArg{};
std::map<std::string, GDALAlgorithmArg *> m_mapShortNameToArg{};
Expand Down

0 comments on commit 554ef14

Please sign in to comment.