Skip to content

Commit

Permalink
gdal_translate: use GDALArgumentParser
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Mar 13, 2024
1 parent 15a4f4f commit 2a0b9d4
Show file tree
Hide file tree
Showing 4 changed files with 512 additions and 496 deletions.
100 changes: 6 additions & 94 deletions apps/gdal_translate_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,69 +38,11 @@
/* Usage() */
/* ******************************************************************** */

static void Usage(bool bIsError, const char *pszErrorMsg = nullptr,
bool bShort = true) CPL_NO_RETURN;

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

static void Usage()
{
fprintf(
bIsError ? stderr : stdout,
"Usage: gdal_translate [--help] [--help-general] [--long-usage]\n"
" [-ot "
"{Byte/Int8/Int16/UInt16/UInt32/Int32/UInt64/Int64/Float32/Float64/\n"
" CInt16/CInt32/CFloat32/CFloat64}] [-strict]\n"
" [-if <format>]... [-of <format>]\n"
" [-b <band>] [-mask <band>] [-expand {gray|rgb|rgba}]\n"
" [-outsize <xsize>[%%]|0 <ysize>[%%]|0] [-tr <xres> <yres>]\n"
" [-ovr <level>|AUTO|AUTO-<n>|NONE]\n"
" [-r "
"{nearest,bilinear,cubic,cubicspline,lanczos,average,mode}]\n"
" [-unscale] [-scale[_bn] [<src_min> <src_max> [<dst_min> "
"<dst_max>]]]... "
"[-exponent[_bn] <exp_val>]...\n"
" [-srcwin <xoff> <yoff> <xsize> <ysize>] [-epo] [-eco]\n"
" [-projwin <ulx> <uly> <lrx> <lry>] [-projwin_srs <srs_def>]\n"
" [-a_srs <srs_def>] [-a_coord_epoch <epoch>]\n"
" [-a_ullr <ulx> <uly> <lrx> <lry>] [-a_nodata <value>]\n"
" [-a_gt <gt0> <gt1> <gt2> <gt3> <gt4> <gt5>]\n"
" [-a_scale <value>] [-a_offset <value>]\n"
" [-nogcp] [-gcp <pixel> <line> <easting> <northing> "
"[<elevation>]]...\n"
" |-colorinterp{_bn} {red|green|blue|alpha|gray|undefined}]\n"
" |-colorinterp {red|green|blue|alpha|gray|undefined},...]\n"
" [-mo <META-TAG>=<VALUE>]... [-dmo "
"<DOMAIN:META-TAG>=<VALUE>]... [-q] [-sds]\n"
" [-co <NAME>=<VALUE>]... [-stats] [-norat] [-noxmp]\n"
" [-oo <NAME>=<VALUE>]...\n"
" <src_dataset> <dst_dataset>\n");

if (!bShort)
{
printf("\n%s\n\n", GDALVersionInfo("--version"));
printf("The following format drivers are configured and support "
"output:\n");
for (int iDr = 0; iDr < GDALGetDriverCount(); iDr++)
{
GDALDriverH hDriver = GDALGetDriver(iDr);

if (GDALGetMetadataItem(hDriver, GDAL_DCAP_RASTER, nullptr) !=
nullptr &&
(GDALGetMetadataItem(hDriver, GDAL_DCAP_CREATE, nullptr) !=
nullptr ||
GDALGetMetadataItem(hDriver, GDAL_DCAP_CREATECOPY, nullptr) !=
nullptr))
{
printf(" %s: %s\n", GDALGetDriverShortName(hDriver),
GDALGetDriverLongName(hDriver));
}
}
}

if (pszErrorMsg != nullptr)
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
fprintf(stderr, "%s\n", GDALTranslateGetParserUsage().c_str());

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

/************************************************************************/
Expand All @@ -125,26 +67,6 @@ MAIN_START(argc, argv)
if (argc < 1)
exit(-argc);

for (int i = 0; argv != nullptr && argv[i] != nullptr; i++)
{
if (EQUAL(argv[i], "--utility_version"))
{
printf("%s was compiled against GDAL %s and is running against "
"GDAL %s\n",
argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
CSLDestroy(argv);
return 0;
}
else if (EQUAL(argv[i], "--help"))
{
Usage(false, nullptr);
}
else if (EQUAL(argv[i], "--long-usage"))
{
Usage(false, nullptr, FALSE);
}
}

/* -------------------------------------------------------------------- */
/* Set optimal setting for best performance with huge input VRT. */
/* The rationale for 450 is that typical Linux process allow */
Expand All @@ -171,17 +93,7 @@ MAIN_START(argc, argv)

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

if (sOptionsForBinary.osSource.empty())
{
Usage(true, "No source dataset specified.");
}

if (sOptionsForBinary.osDest.empty())
{
Usage(true, "No target dataset specified.");
Usage();
}

if (sOptionsForBinary.osDest == "/vsistdout/")
Expand Down Expand Up @@ -350,7 +262,7 @@ MAIN_START(argc, argv)
}

if (bUsageError == TRUE)
Usage(true);
Usage();
GDALClose(hDataset);
GDALTranslateOptionsFree(psOptions);

Expand All @@ -365,7 +277,7 @@ MAIN_START(argc, argv)
hOutDS = GDALTranslate(sOptionsForBinary.osDest.c_str(), hDataset,
psOptions, &bUsageError);
if (bUsageError == TRUE)
Usage(true);
Usage();
int nRetCode = hOutDS ? 0 : 1;

/* Close hOutDS before hDataset for the -f VRT case */
Expand Down
Loading

0 comments on commit 2a0b9d4

Please sign in to comment.