picocli icon indicating copy to clipboard operation
picocli copied to clipboard

Mixin-with-prefix capability?

Open alexturc opened this issue 7 months ago • 4 comments

Discussed in https://github.com/remkop/picocli/discussions/1808

Originally posted by rcauble September 11, 2022 Hi,

I have a use case where I have 2 commands with each their own set of settings:

class Command1Settings {
    @CommandLine.Option(names = "--foo")
    private String foo;
    @CommandLine.Option(names = "--bar")
    private String bar;
}

@CommandLine.Command(name = "command1")
public static final class Command1  {
    @CommandLine.Mixin
    private Command1Settings settings = new Command1Settings();

    public void run() {
        String results1 = Command1Runner.run(settings);
        System.out.println(results1);
    }
}

class Command2Settings {
    @CommandLine.Option(names = "--foo")
    private String foo;
    @CommandLine.Option(names = "--bar")
    private String bar;
}

@CommandLine.Command(name = "command2")
 public static final class Command2  {
    @CommandLine.Mixin
    private Command2Settings settings = new Command2Settings();
    public void run() {
        String results2 = Command2Runner.run(settings);
         System.out.println(results2);
    }
}

And I am trying to create a 3rd command that computes a diff of the other 2. As you can see above, though there is overlap in the options of the 2 commands and so I'm looking for a MixinWithPrefix capability so that I can do something like this:

@CommandLine.Command(name = "diff")
public static final class Diff  {
    @CommandLine.MixinWithPrefix("first-")
    private Command1Settings settings1 = new Command1Settings();
    @CommandLine.MixinWithPrefix("second-")
    private Command1Settings settings2 = new Command2Settings();
    public void run() {
        String results1 = Command1Runner.run(settings1);
        String results2 = Command2Runner.run(settings2);
         System.out.println(diff(results1, results2));
    }
}

And then this will define a new command where the user can do:

diff --first-foo thing1 --first-bar thing2 --second-foo thing3 --second-bar thing4

To add an additional constraint for context, Command1 and Command2 live in different libraries from the differ and so I can't simply arrange for the names to be distinct as they are not necessarily owned by me.

Any suggestions?

alexturc avatar Jun 25 '24 18:06 alexturc