From 15b4b2f53456c9dec5a0ffe4a7e80a5a1163ebb4 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Fri, 27 Oct 2023 17:32:36 +0200 Subject: [PATCH] gdal_calc.py: restore specifc --format behaviour (master only fix, fixes #8619) --- autotest/pyscripts/test_gdal_calc.py | 2 +- .../osgeo_utils/auxiliary/gdal_argparse.py | 27 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/autotest/pyscripts/test_gdal_calc.py b/autotest/pyscripts/test_gdal_calc.py index 6d16d8f9e647..8a14ce74ae46 100755 --- a/autotest/pyscripts/test_gdal_calc.py +++ b/autotest/pyscripts/test_gdal_calc.py @@ -141,7 +141,7 @@ def test_gdal_calc_py_1(script_path): test_py_scripts.run_py_script( script_path, "gdal_calc", - f"-Z {infile} --Z_band=2 --calc=Z --overwrite --outfile {out[2]}", + f"-Z {infile} --Z_band=2 --calc=Z --overwrite --format GTiff --outfile {out[2]}", ) for i, checksum in zip( diff --git a/swig/python/gdal-utils/osgeo_utils/auxiliary/gdal_argparse.py b/swig/python/gdal-utils/osgeo_utils/auxiliary/gdal_argparse.py index 6216bfc6fcbd..15948ae27547 100644 --- a/swig/python/gdal-utils/osgeo_utils/auxiliary/gdal_argparse.py +++ b/swig/python/gdal-utils/osgeo_utils/auxiliary/gdal_argparse.py @@ -99,15 +99,38 @@ def __init__( # extend was introduced to the stdlib in Python 3.8 self.register("action", "extend", ExtendAction) + self.custom_format_arg = False + + def add_argument(self, *args, **kwargs): + if "--format" in args: + self.custom_format_arg = True + return super().add_argument(*args, **kwargs) + def parse_args(self, args=None, optfile_arg=None, **kwargs): if self.add_gdal_generic_options: from osgeo import gdal - args = gdal.GeneralCmdLineProcessor(["dummy"] + args) + args_for_gdal_general = [] + post_args = [] + # gdal_calc has a --format switch whose semantics is not the + # one of the generic --format + if self.custom_format_arg and args: + i = 0 + while i < len(args): + if args[i] == "--format": + post_args.append(args[i]) + post_args.append(args[i + 1]) + i += 1 + else: + args_for_gdal_general.append(args[i]) + i += 1 + else: + args_for_gdal_general = args + args = gdal.GeneralCmdLineProcessor(["dummy"] + args_for_gdal_general) if args is None: sys.exit(0) - args = args[1:] + args = args[1:] + post_args if ( args is not None