omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables

Home Page:https://jsonargparse.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

subcommands of subcommands fail parsing when the top parser uses env - default_env=True

dn-scribe opened this issue Β· comments

πŸ› Bug report

In cases where there in an hierarchy of commands, and use of envs - either by default_env=True or by using `export JSONARGPARSE_DEFAULT_ENV=true', parsing fails.
Here is an example:

from jsonargparse import CLI,ArgumentParser, ActionConfigFile

parser = ArgumentParser(description="CLI tool for discovering assets on Dockerhub",
                        default_env=True)
parser.add_argument('--config', action=ActionConfigFile)

a_parser = ArgumentParser()
a_parser.add_argument('--a_arg', default='a_arg', help='A argument')

b_parser = ArgumentParser()
b_parser.add_argument('--b_arg', default='b_arg', help='B argument')

c_parser = ArgumentParser()
c_parser.add_argument('--c_arg', default='c_arg', help='C argument')

subcommands = parser.add_subcommands()
subcommands.add_subcommand('a', a_parser)
subcommands.add_subcommand('c', c_parser)

a_subs = a_parser.add_subcommands()
a_subs.add_subcommand('sub', b_parser)

args = parser.parse_args()
print(args)

Running:

python a.py a sub
usage: a.py [options] a [-h] [--a_arg A_ARG] {sub} ...
error: expected "subcommand" to be one of {sub}, but it was not provided.

When disabling both environment options (env and code) it works fine:

export JSONARGPARSE_DEFAULT_ENV=false
❯ python a.py a sub
Namespace(config=None, subcommand='a', a=Namespace(a_arg='a_arg', subcommand='sub', sub=Namespace(b_arg='b_arg')))

To reproduce

See above.

Expected behavior

Succeed in parsing.

Environment

  • jsonargparse version (e.g., 4.8.0): 4.27.5
  • Python version (e.g., 3.9): 3.10.10
  • How jsonargparse was installed (e.g. pip install jsonargparse[all]): `pip install jsonargparse'
  • OS (e.g., Linux): Mac Sonoma

@dn-scribe thank you for reporting! This should be fixed with #466.