Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dn-scribe opened this issue Mar 9, 2024 · 1 comment · Fixed by #466
Closed
Labels
bug Something isn't working

Comments

@dn-scribe
Copy link

🐛 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
@mauvilsa
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants