Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for print config argument to reuse the name of the config argument #630

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ 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`` (`#630 <https://github.com/omni-us/jsonargparse/pull/630>`__).

Changed
^^^^^^^
- Argument groups created from dataclass-like that have zero configurable
Expand All @@ -29,6 +34,9 @@ Deprecated
- ``add_dataclass_arguments`` is deprecated and will be removed in v5.0.0.
Instead use ``add_class_arguments`` (`#634
<https://github.com/omni-us/jsonargparse/pull/634>`__).
- 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``
(`#630 <https://github.com/omni-us/jsonargparse/pull/630>`__).


v4.34.1 (2024-12-02)
Expand Down
3 changes: 3 additions & 0 deletions jsonargparse/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,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
Expand Down
2 changes: 1 addition & 1 deletion jsonargparse/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,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']``.
Expand Down
8 changes: 8 additions & 0 deletions jsonargparse_tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading