Skip to content

Commit 96087b2

Browse files
authored
Fix environment variable names not shown in help for positional arguments when default_env is true (#763)
1 parent 5fed4ed commit 96087b2

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Fixed
3131
<https://github.com/omni-us/jsonargparse/pull/756>`__).
3232
- Some deprecations not shown in the API documentation (`#760
3333
<https://github.com/omni-us/jsonargparse/pull/760>`__).
34+
- Environment variable names not shown in help for positional arguments when
35+
``default_env`` is true (`#763
36+
<https://github.com/omni-us/jsonargparse/pull/763>`__).
3437

3538
Changed
3639
^^^^^^^

jsonargparse/_formatters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def _format_action_invocation(self, action: Action) -> str:
247247
if parser.default_env:
248248
value = f"ENV: {get_env_var(self, action)}\n\n {value}"
249249
return value
250-
if action.option_strings == [] or not parser.default_env:
250+
if not parser.default_env:
251251
return super()._format_action_invocation(action)
252252
extr = ""
253253
if not isinstance(action, (_ActionHelpClassPath, _ActionPrintConfig, ShtabAction, _HelpAction)):

jsonargparse_tests/test_core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ def test_parse_env_config(parser):
228228
pytest.raises(ArgumentError, lambda: parser.parse_env({"APP_CFG": '{"undefined": True}'}))
229229

230230

231+
def test_parse_env_positional():
232+
parser = ArgumentParser(prog="app", exit_on_error=False, default_env=True)
233+
parser.add_argument("pos", type=int)
234+
with patch.dict(os.environ, {"APP_POS": "1"}):
235+
assert parser.parse_env() == Namespace(pos=1)
236+
with pytest.raises(ArgumentError, match="Got value: 1.1"):
237+
parser.parse_env({"APP_POS": "1.1"})
238+
239+
231240
def test_parse_env_positional_nargs_plus(parser):
232241
parser.env_prefix = "app"
233242
parser.add_argument("req", nargs="+")

jsonargparse_tests/test_formatters.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ def test_help_action_config_file(parser):
3232
assert "APP_PRINT_CONFIG" not in help_str
3333

3434

35+
def test_help_positional(parser):
36+
parser.add_argument("pos")
37+
help_str = get_parser_help(parser)
38+
assert "ARG: pos" in help_str
39+
assert "ENV: APP_POS" in help_str
40+
41+
3542
def test_help_required_and_default(parser):
3643
parser.add_argument("--v1", help="Option v1.", default="v1", required=True)
3744
help_str = get_parser_help(parser)

0 commit comments

Comments
 (0)