Skip to content

Commit

Permalink
Command line utilities: use usual conventions for placeholder (<value…
Browse files Browse the repository at this point in the history
…>), exclusive argument ({a|b}) and repeated arguments ([-foo <bar>]...)
  • Loading branch information
rouault committed Sep 24, 2023
1 parent 8d7bb0a commit bb49e41
Show file tree
Hide file tree
Showing 82 changed files with 1,481 additions and 1,292 deletions.
2 changes: 1 addition & 1 deletion apps/dumpoverviews.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static bool DumpBand(GDALDatasetH hBaseDS, GDALRasterBandH hBand,
static void Usage()

{
printf("Usage: dumpoverviews [-masks] <filename> [overview]*\n");
printf("Usage: dumpoverviews [-masks] <filename> [<overview>]...\n");
exit(1);
}

Expand Down
31 changes: 16 additions & 15 deletions apps/gdal_contour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,18 @@ static bool ArgIsNumeric(const char *pszArg)
/* Usage() */
/************************************************************************/

static void Usage(const char *pszErrorMsg = nullptr)
static void Usage(bool bIsError, const char *pszErrorMsg = nullptr)

{
printf(
fprintf(
bIsError ? stderr : stdout,
"Usage: gdal_contour [--help] [--help-general]\n"
" [-b <band>] [-a <attribute_name>] [-amin "
"<attribute_name>] [-amax <attribute_name>]\n"
" [-3d] [-inodata] [-snodata n] [-f <formatname>] "
" [-3d] [-inodata] [-snodata <n>] [-f <formatname>] "
"[-i <interval>]\n"
" [[-dsco <NAME=VALUE>] ...] [[-lco <NAME=VALUE>] "
"...]\n"
" [-dsco <NAME>=<VALUE>]... "
"[-lco <NAME>=<VALUE>]...\n"
" [-off <offset>] [-fl <level> <level>...] [-e "
"<exp_base>]\n"
" [-nln <outlayername>] [-q] [-p]\n"
Expand All @@ -70,7 +71,7 @@ static void Usage(const char *pszErrorMsg = nullptr)
if (pszErrorMsg != nullptr)
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);

exit(1);
exit(bIsError ? 1 : 0);
}

static void CreateElevAttrib(const char *pszElevAttrib, OGRLayerH hLayer)
Expand All @@ -92,8 +93,8 @@ static void CreateElevAttrib(const char *pszElevAttrib, OGRLayerH hLayer)
do \
{ \
if (i + nExtraArg >= argc) \
Usage(CPLSPrintf("%s option requires %d argument(s)", argv[i], \
nExtraArg)); \
Usage(true, CPLSPrintf("%s option requires %d argument(s)", \
argv[i], nExtraArg)); \
} while (false)

MAIN_START(argc, argv)
Expand Down Expand Up @@ -152,7 +153,7 @@ MAIN_START(argc, argv)
return 0;
}
else if (EQUAL(argv[i], "--help"))
Usage();
Usage(false);
else if (EQUAL(argv[i], "-a"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
Expand Down Expand Up @@ -196,8 +197,8 @@ MAIN_START(argc, argv)
else if (EQUAL(argv[i], "-fl"))
{
if (i >= argc - 1)
Usage(CPLSPrintf("%s option requires at least 1 argument",
argv[i]));
Usage(true, CPLSPrintf("%s option requires at least 1 argument",
argv[i]));
while (i < argc - 1 &&
nFixedLevelCount < static_cast<int>(sizeof(adfFixedLevels) /
sizeof(double)) &&
Expand Down Expand Up @@ -263,22 +264,22 @@ MAIN_START(argc, argv)
pszDstFilename = argv[i];
}
else
Usage("Too many command options.");
Usage(true, "Too many command options.");
}

if (dfInterval == 0.0 && nFixedLevelCount == 0 && dfExpBase == 0.0)
{
Usage("Neither -i nor -fl nor -e are specified.");
Usage(true, "Neither -i nor -fl nor -e are specified.");
}

if (pszSrcFilename == nullptr)
{
Usage("Missing source filename.");
Usage(true, "Missing source filename.");
}

if (pszDstFilename == nullptr)
{
Usage("Missing destination filename.");
Usage(true, "Missing destination filename.");
}

if (strcmp(pszDstFilename, "/vsistdout/") == 0 ||
Expand Down
23 changes: 12 additions & 11 deletions apps/gdal_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,27 @@
#include <memory>
#include <vector>

static void Usage()
static void Usage(bool bIsError)

{
printf(
fprintf(
bIsError ? stderr : stdout,
"Usage: gdal_create [--help] [--help-general]\n"
" [-of <format>]\n"
" [-outsize <xsize< <ysize>]\n"
" [-outsize <xsize> <ysize>]\n"
" [-bands <count>]\n"
" [-burn <value>]*\n"
" [-burn <value>]...\n"
" [-ot "
"{Byte/Int8/Int16/UInt16/UInt32/Int32/UInt64/Int64/Float32/Float64/\n"
" CInt16/CInt32/CFloat32/CFloat64}] [-strict]\n"
" [-a_srs <srs_def>] [-a_ullr <ulx> <uly> <lrx> <lry>] "
"[-a_nodata <value>]\n"
" [-mo \"META-TAG=VALUE\"]* [-q]\n"
" [-co \"NAME=VALUE\"]*\n"
" [-mo <META-TAG>=<VALUE>]... [-q]\n"
" [-co <NAME>=<VALUE>]...\n"
" [-if <input_dataset>]\n"
" <out_dataset>\n");

exit(1);
exit(bIsError ? 1 : 0);
}

/************************************************************************/
Expand Down Expand Up @@ -124,7 +125,7 @@ MAIN_START(argc, argv)
}
else if (EQUAL(argv[i], "--help"))
{
Usage();
Usage(false);
}
else if (i < argc - 1 &&
(EQUAL(argv[i], "-of") || EQUAL(argv[i], "-f")))
Expand Down Expand Up @@ -237,7 +238,7 @@ MAIN_START(argc, argv)
CPLError(CE_Failure, CPLE_NotSupported, "Unknown option name '%s'",
argv[i]);
CSLDestroy(argv);
Usage();
Usage(true);
}
else if (pszFilename == nullptr)
{
Expand All @@ -248,13 +249,13 @@ MAIN_START(argc, argv)
CPLError(CE_Failure, CPLE_NotSupported,
"Too many command options '%s'", argv[i]);
CSLDestroy(argv);
Usage();
Usage(true);
}
}
if (pszFilename == nullptr)
{
CSLDestroy(argv);
Usage();
Usage(true);
}

double adfGeoTransform[6] = {0, 1, 0, 0, 0, 1};
Expand Down
36 changes: 19 additions & 17 deletions apps/gdal_footprint_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,25 @@
/* Usage() */
/************************************************************************/

static void Usage(const char *pszErrorMsg = nullptr)
static void Usage(bool bIsError, const char *pszErrorMsg = nullptr)

{
printf("Usage: gdal_footprint [--help] [--help-general]\n"
" [-b band]* [-combine_bands union|intersection]\n"
" [-oo NAME=VALUE]* [-ovr <index>]\n"
" [-srcnodata \"value [value...]\"]\n"
" [-t_cs pixel|georef] [-t_srs <srs_def>] [-split_polys]\n"
" [-convex_hull] [-densify <value>] [-simplify <value>]\n"
" [-min_ring_area <value>] [-max_points <value>|unlimited]\n"
" [-of ogr_format] [-lyr_name dst_layername]\n"
" [-dsco name=value]* [-lco name=value]* [-overwrite] [-q]\n"
" <src_filename> <dst_filename>\n");
fprintf(bIsError ? stderr : stdout,
"Usage: gdal_footprint [--help] [--help-general]\n"
" [-b <band>]... [-combine_bands union|intersection]\n"
" [-oo <NAME>=<VALUE>]... [-ovr <index>]\n"
" [-srcnodata \"<value>[ <value>]...\"]\n"
" [-t_cs pixel|georef] [-t_srs <srs_def>] [-split_polys]\n"
" [-convex_hull] [-densify <value>] [-simplify <value>]\n"
" [-min_ring_area <value>] [-max_points <value>|unlimited]\n"
" [-of <ogr_format>] [-lyr_name <dst_layername>]\n"
" [-dsco <name>=<value>]... [-lco <name>=<value>]... "
"[-overwrite] [-q]\n"
" <src_filename> <dst_filename>\n");

if (pszErrorMsg != nullptr)
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
exit(1);
exit(bIsError ? 1 : 0);
}

/************************************************************************/
Expand Down Expand Up @@ -88,7 +90,7 @@ MAIN_START(argc, argv)
}
else if (EQUAL(argv[i], "--help"))
{
Usage();
Usage(false);
}
}

Expand All @@ -100,7 +102,7 @@ MAIN_START(argc, argv)

if (psOptions == nullptr)
{
Usage();
Usage(true);
}

if (!(sOptionsForBinary.bQuiet))
Expand All @@ -109,10 +111,10 @@ MAIN_START(argc, argv)
}

if (sOptionsForBinary.osSource.empty())
Usage("No input file specified.");
Usage(true, "No input file specified.");

if (!sOptionsForBinary.bDestSpecified)
Usage("No output file specified.");
Usage(true, "No output file specified.");

/* -------------------------------------------------------------------- */
/* Open input file. */
Expand Down Expand Up @@ -218,7 +220,7 @@ MAIN_START(argc, argv)
GDALDatasetH hRetDS = GDALFootprint(sOptionsForBinary.osDest.c_str(),
hDstDS, hInDS, psOptions, &bUsageError);
if (bUsageError == TRUE)
Usage();
Usage(true);
int nRetCode = hRetDS ? 0 : 1;

GDALClose(hInDS);
Expand Down
45 changes: 25 additions & 20 deletions apps/gdal_grid_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,30 @@
/* Usage() */
/************************************************************************/

static void Usage(const char *pszErrorMsg = nullptr)
static void Usage(bool bIsError, const char *pszErrorMsg = nullptr)

{
printf(
fprintf(
bIsError ? stderr : stdout,
"Usage: gdal_grid [--help] [--help-general]\n"
" [-oo NAME=VALUE]*\n"
" [-oo <NAME>=<VALUE>]...\n"
" [-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/\n"
" CInt16/CInt32/CFloat32/CFloat64}]\n"
" [-of format] [-co \"NAME=VALUE\"]\n"
" [-zfield field_name] [-z_increase increase_value] [-z_multiply "
"multiply_value]\n"
" [-a_srs srs_def] [-spat xmin ymin xmax ymax]\n"
" [-clipsrc <xmin ymin xmax ymax>|WKT|datasource|spat_extent]\n"
" [-clipsrcsql sql_statement] [-clipsrclayer layer]\n"
" [-clipsrcwhere expression]\n"
" [-l layername]* [-where expression] [-sql select_statement]\n"
" [-txe xmin xmax] [-tye ymin ymax] [-tr xres yres] [-outsize xsize "
"ysize]\n"
" [-a algorithm[:parameter1=value1]*]"
" [-of <format>] [-co <NAME>=<VALUE>]...\n"
" [-zfield <field_name>] [-z_increase <increase_value>] "
"[-z_multiply "
"<multiply_value>]\n"
" [-a_srs <srs_def>] [-spat <xmin> <ymin> <xmax> <ymax>]\n"
" [-clipsrc <xmin> <ymin> <xmax> "
"<ymax>|<WKT>|<datasource>|spat_extent]\n"
" [-clipsrcsql <sql_statement>] [-clipsrclayer <layer>]\n"
" [-clipsrcwhere <expression>]\n"
" [-l <layername>]... [-where <expression>] [-sql "
"<select_statement>]\n"
" [-txe <xmin> <xmax>] [-tye <ymin> <ymax>] [-tr <xres> <yres>] "
"[-outsize <xsize> "
"<ysize>]\n"
" [-a <algorithm>[:<parameter1>=<value1>]...]"
" [-q]\n"
" <src_datasource> <dst_filename>\n"
"\n"
Expand Down Expand Up @@ -88,7 +93,7 @@ static void Usage(const char *pszErrorMsg = nullptr)
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);

GDALDestroyDriverManager();
exit(1);
exit(bIsError ? 1 : 0);
}

/************************************************************************/
Expand Down Expand Up @@ -123,7 +128,7 @@ MAIN_START(argc, argv)
}
else if (EQUAL(argv[i], "--help"))
{
Usage();
Usage(false);
}
}

Expand All @@ -135,7 +140,7 @@ MAIN_START(argc, argv)

if (psOptions == nullptr)
{
Usage();
Usage(true);
}

if (!(sOptionsForBinary.bQuiet))
Expand All @@ -144,9 +149,9 @@ MAIN_START(argc, argv)
}

if (sOptionsForBinary.osSource.empty())
Usage("No input file specified.");
Usage(true, "No input file specified.");
if (!sOptionsForBinary.bDestSpecified)
Usage("No output file specified.");
Usage(true, "No output file specified.");

/* -------------------------------------------------------------------- */
/* Open input file. */
Expand All @@ -163,7 +168,7 @@ MAIN_START(argc, argv)
GDALDatasetH hOutDS = GDALGrid(sOptionsForBinary.osDest.c_str(), hInDS,
psOptions, &bUsageError);
if (bUsageError == TRUE)
Usage();
Usage(true);
int nRetCode = hOutDS ? 0 : 1;

GDALClose(hInDS);
Expand Down
Loading

0 comments on commit bb49e41

Please sign in to comment.