-
-
Notifications
You must be signed in to change notification settings - Fork 61
Description
🐛 Bug report
When using default_env=True, the action="version" argument gets environment variable support even though this doesn't make semantic sense. The version action is purely informational (prints and exits), similar to help, which is already auto-excluded from environment variable parsing.
Additionally, subcommand selection via environment variables is also questionable, though there may be legitimate use cases for this.
To reproduce
from jsonargparse import ArgumentParser
parser = ArgumentParser(
prog="cli",
description="Minimal CLI example",
default_env=True,
)
parser.add_argument("--version", action="version", version="1.0.0")
subcommands = parser.add_subcommands()
subparser = ArgumentParser()
subcommands.add_subcommand("greet", subparser, help="Greet someone")
parser.print_help()Output:
usage: cli [-h] [--version] {greet} ...
Minimal CLI example
options:
ARG: -h, --help Show this help message and exit.
ARG: --version
ENV: CLI_VERSION
show program's version number and exit
subcommands:
For more details of each subcommand, add it as an argument followed by
--help.
ENV: CLI_SUBCOMMAND
Available subcommands:
ARG: greet
ENV: CLI_GREET
Greet someone
Problems:
Heres a simlar cases too:
ENV: CLI_VERSION- Setting version via environment variable doesn't make semantic sense for a meta-action that just prints and exitsENV: CLI_SUBCOMMAND- Subcommand selection via environment variable is unusual and may not be desirableENV: CLI_GREET- Individual subcommands showing in the help as environment variables may be confusing
Note that -h, --help is already correctly excluded from environment variable parsing.
Expected behavior
Immediate fix (bug):
The action="version" argument should be automatically excluded from environment variable parsing, just like help actions already are.
More general solution (feature):
For broader control, it would be useful to have an enable_env parameter (similar to the existing enable_path parameter) that allows selectively disabling environment variable parsing:
parser = ArgumentParser(prog="cli", description="Minimal CLI example", default_env=True)
parser.add_argument("--version", action="version", version="1.0.0", enable_env=False)
subcommands = parser.add_subcommands(enable_env=False) # Disable for subcommand selectionThis would:
- Suppress the corresponding
ENV:lines in help output - Skip processing those environment variables during parsing
Use cases for enable_env:
- Meta arguments:
--version, custom help flags - Arguments where environment variable support would be confusing
- Inverse use case:
default_env=Falsewith selectiveenable_env=Truefor specific arguments
Environment
- jsonargparse version: latest (main branch)
- Python version: 3.x
- How jsonargparse was installed: git clone
- OS: Linux
@mauvilsa I'd be happy to put together a PR (especially for the the broader feature) if you agree this is worth addressing! It may be related to other default_env issues.