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

Allow empty mixin (Not urgent) #1665

Closed
nakanaka7 opened this issue Apr 24, 2022 · 5 comments
Closed

Allow empty mixin (Not urgent) #1665

nakanaka7 opened this issue Apr 24, 2022 · 5 comments

Comments

@nakanaka7
Copy link

nakanaka7 commented Apr 24, 2022

I found a mixin without any annotations (@ Option, @ Parameters, etc) will invoke picocli.CommandLine$InitializationException. I tried using such "empty" mixin in the following situation.
real situation

public class BaseCommand implements Runnable {
  @Command
  private void subcommandA(@Mixin MixinA mixinA) {
    runSubcommand(mixinA)
  }
  @Command
  private void subcommandB(@Mixin MixinB mixinB) {
    runSubcommand(mixinB)
  }
  @Command
  private void subcommandC(@Mixin MixinC mixinC) {
    runSubcommand(mixinC)
  }
     .......... 

  private void runSubcommand(IMixin mixin) {
    //invoke Imixin method 
  }

IMixin is an interface. MixinA, MixinB, MixinC... are the implementation of IMixin. The implementations are basically real mixin, but a few case are empty mixins.
I could avoid the exception easily by creating the instance directly as following. But allowing such empty mixin will help code
consistency like above.

  @ Command
  private void subcommandD() {
      runSubcommand(new MixinD())
  }
@remkop
Copy link
Owner

remkop commented May 11, 2022

@nakanaka7 Have you tried annotating the empty mixin with just @Command?
That may prevent the InitializationException.

@nakanaka7
Copy link
Author

It works fine. Thank you.

@rsenden
Copy link
Contributor

rsenden commented Aug 17, 2022

@remkop I'm having a similar issue; in my case the mixin is not empty but provides generic option processing functionality based on mixee.options().

Good to know that the @Command annotation can be used as a workaround, but it feels a bit odd to annotate a mixin with @Command. Any chance that picocli can be improved to allow for mixins that do not have any options or parameters?

Alternatively, can we have a more appropriate annotation that is accepted by the validateCommandSpec method? Maybe we could just add ElementType.TYPE to the list of targets for the @Mixin annotation, to allow mixins to be explicitly declared as being a mixin, and update validateCommandSpec accordingly?

@remkop
Copy link
Owner

remkop commented Aug 17, 2022

@rsenden
The @Command annotation is not a workaround, this is the design. It allows you to mix in command attributes like header, footer, version, etc. All command attributes can be mixed in, so creating another separate annotation would then have to also have all these attributes…

Reusing the @Mixin annotation would also mean that this annotation would need all attributes from the @Command annotation…

(I actually don’t understand what problem we are trying to solve here…) 😅

@rsenden
Copy link
Contributor

rsenden commented Aug 17, 2022

@remkop I wasn't (actively) aware that you could use the @Command annotation to mix in command attributes, so for that purpose it indeed makes sense to have an @Command annotation on mixins.

In cases like described in this issue, we are not trying to mix in command attributes though, so in that regards it makes less sense to add an empty @Command annotation. For the uneducated reader, this may look like you are defining a command rather than a mixin.

Anyway, I can live with adding the @Command annotation to such mixins, I'll probably add a comment to explain why though ;)

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

No branches or pull requests

3 participants