Skip to content

Is it possible to refer to ParentCommand by interface? #2216

Closed Answered by remkop
kyrrek asked this question in Q&A
Discussion options

You must be logged in to vote

Only fields can be annotated with @ParentCommand, so you cannot directly use it in the interface itself.
One idea is to create a getParentCommand method in your interface, and have the implementation classes implement it like this:

    @ParentCommand
    private MyClass parent; // picocli injects reference to parent command
 
    public MyClass getParentCommand() {
        return parent;
    }

You would still have some duplicate code, but it is fairly minimal (essentially just wiring).

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kyrrek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants