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

Defaults are missing in the namespace of the sub subcommand #29

Closed
obaranov opened this issue Dec 4, 2020 · 4 comments
Closed

Defaults are missing in the namespace of the sub subcommand #29

obaranov opened this issue Dec 4, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@obaranov
Copy link

obaranov commented Dec 4, 2020

Hi,

I've been trying to design cli with the several levels of subcommands. I want something like that:

app subcommand1 subcommand2 --arg1=foo

The problem is that when I add an argument with the default value into the subcommand2, this argument is not getting populated in the subcommand2 namespace.

Here is my code to verify that:

parser = ArgumentParser()
subcommand = ArgumentParser()
# this is a default argument for subcommand
subcommand.add_argument('--sub-foo', default="bar")

subcommand_subcommand = ArgumentParser()
# this is a default argument for subcommand2. This one will not be populated!
subcommand_subcommand.add_argument('--sub-sub-foo', default="bar")

root_subcommands = parser.add_subcommands()
root_subcommands.add_subcommand('subcommand1', subcommand)

sub_subcommands = subcommand.add_subcommands()
sub_subcommands.add_subcommand("subcommand2", subcommand_subcommand)


args = parser.parse_args(['subcommand1', 'subcommand2'])
print(args)

As the result I'm getting that:

Namespace(__cwd__=['....'], subcommand='subcommand1', subcommand1=Namespace(sub_foo='bar', subcommand='subcommand2'))

You can see here that the subcommand2 Namespace is missing in the result. For the subcommand1 namespace you see that the default argument is present (sub_foo='bar').

Is that a bug, or am I doing something wrong here?

Best regards,
Oleksii

@mauvilsa
Copy link
Member

mauvilsa commented Dec 4, 2020

Is that a bug, or am I doing something wrong here?

I had not considered the case of several levels of subcommands. Not a bug, simply not yet implemented. Certainly makes sense that several levels of subcommands works as you expected. I will look into it to figure out how difficult it would be to implement.

@mauvilsa mauvilsa added the enhancement New feature or request label Dec 4, 2020
mauvilsa added a commit that referenced this issue Dec 7, 2020
- Fixed type hint of skip in signatures, being more a general Container.
@mauvilsa
Copy link
Member

mauvilsa commented Dec 7, 2020

I have fixed the issue with the defaults. Though I don't yet consider this to be an officially supported feature. Thus for the time being I will not create a new release of the package. Please clone the repo to try it out.

As a side note. The subcommands should be added in the order of the levels, just like in your example. First subcommand1 and then subcommand2. If they are added in a different order, there wouldn't be any error, but some things don't work as expected, in particular parsing environment variables and the help messages.

@obaranov
Copy link
Author

obaranov commented Dec 7, 2020

Works fine for me now. Thank you for the quick fix :)

@mauvilsa
Copy link
Member

mauvilsa commented Dec 9, 2020

This is now included in release v3.1.0.

@mauvilsa mauvilsa closed this as completed Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants