omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
https://jsonargparse.readthedocs.io
MIT License
314 stars 42 forks source link

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

Closed dn-scribe closed 5 months ago

dn-scribe commented 6 months ago

šŸ› 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

mauvilsa commented 5 months ago

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