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

No built-in way to access parent command from subcommand? #247

Closed
michaelpj opened this issue Dec 14, 2017 · 7 comments
Closed

No built-in way to access parent command from subcommand? #247

michaelpj opened this issue Dec 14, 2017 · 7 comments

Comments

@michaelpj
Copy link

A natural thing to do with parent commands is define options that will apply to all the subcommands. However, it's not terribly convenient to access the parent command from the subcommand: you have to rely on whatever creates the CommandLine to thread everything around.

Similarly, the provided IParseResultHandlers don't make it easy to pass things around.

I think what would be most convenient for me would be for a subcommand to receive an instance of its parent command as a constructor argument. That way everything is well-typed and it's easy to access the options of parent commands.

@remkop
Copy link
Owner

remkop commented Dec 14, 2017

You raise a good point.

I’m thinking to provide an additional @ParentCommand annotation to address this.

A reference to the parent command will be injected to any fields annotated with @ParentCommand. This also provides type safety while not imposing any constraints on subcommand constructors.

Would that work for you?

@michaelpj
Copy link
Author

Yes, that should do the job.

(Incidentally, I don't think the requirements on constructors (there must be a zero-arg constructor) for commands are documented anywhere.)

@remkop
Copy link
Owner

remkop commented Dec 15, 2017

Ok. Thanks for pointing out the docs can be improved.

By the way, do zero-arg constructors work for you or are you also interested in using a factory like requested in #169 ?

@michaelpj
Copy link
Author

Zero-arg constructors are fine for me at the moment, although it would be kind of neat to be able to use a factory.

@remkop
Copy link
Owner

remkop commented Dec 17, 2017

Pushed the new @ParentCommand feature to master.
Please verify.

@remkop
Copy link
Owner

remkop commented Dec 17, 2017

Reopening: still need to update the user manual.

@remkop remkop reopened this Dec 17, 2017
remkop added a commit that referenced this issue Dec 17, 2017
@remkop remkop closed this as completed in 46673d9 Dec 17, 2017
remkop added a commit that referenced this issue Dec 18, 2017
- #83 additional built-in type converters;
- #247 @ParentCommand annotation and general subcommand documentation
@remkop
Copy link
Owner

remkop commented Dec 21, 2017

This issue has been addressed in picocli 2.2, which is now available on JCenter and Maven Central.
See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants