Skip to content

Commit

Permalink
Merge pull request #8620 from rouault/fix_8619
Browse files Browse the repository at this point in the history
gdal_calc.py: restore specifc --format behaviour (master only fix, fixes #8619)
  • Loading branch information
rouault authored Oct 27, 2023
2 parents b564656 + 15b4b2f commit b5f8da5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion autotest/pyscripts/test_gdal_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 25 additions & 2 deletions swig/python/gdal-utils/osgeo_utils/auxiliary/gdal_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b5f8da5

Please sign in to comment.