From 9aefda37d2b7b6a8d1214aa0ae1cefaaeb33f4a6 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Fri, 22 Nov 2024 07:24:42 +0100 Subject: [PATCH 1/2] Added support for print config argument to reuse the name of the config argument. --- CHANGELOG.rst | 14 ++++++++++++++ jsonargparse/_actions.py | 3 +++ jsonargparse/_core.py | 2 +- jsonargparse_tests/test_core.py | 8 ++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fee10a5b..5335733b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,20 @@ The semantic versioning only considers the public API as described in paths are considered internals and can change in minor and patch releases. +v4.35.0 (2024-12-??) +-------------------- + +Added +^^^^^ +- Support for ``print config`` argument to reuse the name of the config argument + by using ``%s`` (`#??? `__). + +Deprecated +^^^^^^^^^^ +- From v5.0.0 the print config argument will by default reuse the name of the + config argument as ``--print_%s`` instead of being always ``--print_config``. + + v4.34.1 (2024-11-??) -------------------- diff --git a/jsonargparse/_actions.py b/jsonargparse/_actions.py index 938d4946..637c87c3 100644 --- a/jsonargparse/_actions.py +++ b/jsonargparse/_actions.py @@ -181,6 +181,9 @@ def _ensure_single_config_argument(container, action): @staticmethod def _add_print_config_argument(container, action): if isinstance(action, ActionConfigFile) and getattr(container, "_print_config", None) is not None: + if "%s" in container._print_config: + container._print_config = container._print_config % action.dest + assert container._print_config.startswith("--") container.add_argument(container._print_config, action=_ActionPrintConfig) @staticmethod diff --git a/jsonargparse/_core.py b/jsonargparse/_core.py index 548e5b66..54bf759f 100644 --- a/jsonargparse/_core.py +++ b/jsonargparse/_core.py @@ -219,7 +219,7 @@ def __init__( formatter_class: Class for printing help messages. logger: Configures the logger, see :class:`.LoggerProperty`. version: Program version which will be printed by the --version argument. - print_config: Add this as argument to print config, set None to disable. + print_config: Name for print config argument, ``%s`` is replaced by config dest, set None to disable. parser_mode: Mode for parsing config files: ``'yaml'``, ``'jsonnet'`` or ones added via :func:`.set_loader`. dump_header: Header to include as comment when dumping a config object. default_config_files: Default config file locations, e.g. ``['~/.config/myapp/*.yaml']``. diff --git a/jsonargparse_tests/test_core.py b/jsonargparse_tests/test_core.py index 17906e5b..e82711d9 100644 --- a/jsonargparse_tests/test_core.py +++ b/jsonargparse_tests/test_core.py @@ -741,6 +741,14 @@ def test_print_config_empty_default_config_file(print_parser, tmp_cwd): assert yaml.safe_load(out) == {"g1": {"v2": "2"}, "g2": {"v3": None}, "v1": 1} +def test_print_config_reuse_name(): + parser = ArgumentParser(exit_on_error=False, print_config="--print_%s") + parser.add_argument("--conf", action="config") + parser.add_argument("--x", default=1) + out = get_parse_args_stdout(parser, ["--print_conf"]) + assert yaml.safe_load(out) == {"x": 1} + + def test_default_config_files(parser, subtests, tmp_cwd): default_config_file = tmp_cwd / "defaults.yaml" default_config_file.write_text("op1: from default config file\n") From 049157bfb64d5702056b10c7b6efb36db7bf5795 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Fri, 22 Nov 2024 07:34:30 +0100 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5335733b..5164b5ce 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -18,12 +18,13 @@ v4.35.0 (2024-12-??) Added ^^^^^ - Support for ``print config`` argument to reuse the name of the config argument - by using ``%s`` (`#??? `__). + by using ``%s`` (`#630 `__). Deprecated ^^^^^^^^^^ - From v5.0.0 the print config argument will by default reuse the name of the - config argument as ``--print_%s`` instead of being always ``--print_config``. + config argument as ``--print_%s`` instead of being always ``--print_config`` + (`#630 `__). v4.34.1 (2024-11-??)