Skip to content

Commit 1f901fe

Browse files
asmacdoclaude
andauthored
786 rm extra env vars (#787)
* Exclude version action env var in helptext When using `default_env=True`, the `action="version"` arguments no longer gets an unused environment variable in helptext. * Rm subcommand ARG/ENV help when default_env=True When using `default_env=True`, subcommands in the help output no longer show unusable ARG or ENV PREFIX_<subcommand>. --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 36679f3 commit 1f901fe

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ The semantic versioning only considers the public API as described in
1111
:ref:`api-ref`. Components not mentioned in :ref:`api-ref` or different import
1212
paths are considered internals and can change in minor and patch releases.
1313

14+
v4.42.1 (unreleased)
15+
--------------------
16+
17+
Fixed
18+
^^^^^
19+
- Prevent extra environment variables in helptext when default_env=True, for
20+
version actions and subcommands(`#787
21+
<https://github.com/omni-us/jsonargparse/pull/787>`__).
1422

1523
v4.42.0 (2025-10-14)
1624
--------------------

jsonargparse/_formatters.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
Action,
99
HelpFormatter,
1010
_HelpAction,
11+
_SubParsersAction,
12+
_VersionAction,
1113
)
1214
from io import StringIO
1315
from string import Template
@@ -249,8 +251,11 @@ def _format_action_invocation(self, action: Action) -> str:
249251
return value
250252
if not parser.default_env:
251253
return super()._format_action_invocation(action)
254+
# Subcommand choices (individual subcommands in the list) don't get ARG: prefix or ENV: line
255+
if isinstance(action, _SubParsersAction._ChoicesPseudoAction):
256+
return super()._format_action_invocation(action)
252257
extr = ""
253-
if not isinstance(action, (_ActionHelpClassPath, _ActionPrintConfig, ShtabAction, _HelpAction)):
258+
if not isinstance(action, (_ActionHelpClassPath, _ActionPrintConfig, ShtabAction, _HelpAction, _VersionAction)):
254259
extr += "\n ENV: " + get_env_var(self, action)
255260
return "ARG: " + super()._format_action_invocation(action) + extr
256261

jsonargparse_tests/test_formatters.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ def test_help_basics(parser):
2222
assert "APP_HELP" not in help_str
2323

2424

25+
def test_help_action_version(parser):
26+
parser.add_argument("--version", action="version", version="1.0.0")
27+
help_str = get_parser_help(parser)
28+
assert "ARG: --version" in help_str
29+
assert "APP_VERSION" not in help_str
30+
assert "show program's version number and exit" in help_str
31+
32+
2533
def test_help_action_config_file(parser):
2634
parser.add_argument("-c", "--cfg", help="Config in yaml/json.", action="config")
2735
help_str = get_parser_help(parser)
@@ -140,6 +148,25 @@ def test_help_default_config_files_with_required(tmp_path, parser):
140148
assert "from config" in help_str
141149

142150

151+
def test_help_subcommands_with_default_env(parser):
152+
subcommands = parser.add_subcommands()
153+
subparser1 = ArgumentParser()
154+
subparser2 = ArgumentParser()
155+
subcommands.add_subcommand("greet", subparser1, help="Greet someone")
156+
subcommands.add_subcommand("farewell", subparser2, help="Say goodbye")
157+
help_str = get_parser_help(parser)
158+
# Individual subcommands should NOT have ARG: prefix or ENV: lines
159+
assert "ARG: greet" not in help_str
160+
assert "ARG: farewell" not in help_str
161+
assert "ENV: APP_GREET" not in help_str
162+
assert "ENV: APP_FAREWELL" not in help_str
163+
# But the subcommand names and help should still be present
164+
assert "greet" in help_str
165+
assert "Greet someone" in help_str
166+
assert "farewell" in help_str
167+
assert "Say goodbye" in help_str
168+
169+
143170
def test_format_usage(parser):
144171
parser.add_argument("--v1")
145172
with patch.dict(os.environ, {"COLUMNS": "200"}):

0 commit comments

Comments
 (0)