Skip to content

extra env vars in help text when using default_env=True #786

@asmacdo

Description

@asmacdo

🐛 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 exits
  • ENV: CLI_SUBCOMMAND - Subcommand selection via environment variable is unusual and may not be desirable
  • ENV: 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 selection

This would:

  1. Suppress the corresponding ENV: lines in help output
  2. 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=False with selective enable_env=True for 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions