-
Notifications
You must be signed in to change notification settings - Fork 421
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
Add support for relative parameter indexes (was: Mixins and parameter indexes) #564
Comments
Yes, currently the application author needs to explicitly set the index of positional parameters and picocli will not modify this index. I don't think it is possible to implement exactly what you are asking for, but I may be able to offer a reasonable alternative. First, why can picocli not automatically increment the index for positional parameters? The alternative I have in mind is to allow applications to use variables for the index in the annotations. Variables will allow you to do something like this: public class CommonMixinOne {
@Parameters(index = "${sys:commonParam1}" paramLabel = "COMMON-PARAM-ONE")
private String commonMixinOneParam;
}
public class CommonMixinTwo {
@Parameters(index = "${sys:commonParam2}" paramLabel = "COMMON-PARAM-TWO")
private String commonMixinTwoParam;
}
@Command(name = "testCommand")
public class TestCommand {
@Mixin
private CommonMixinTwo myCommonMixinTwo;
@Parameters(index = "1", paramLabel = "TEST-COMMAND-PARAM")
private String testCommandParam;
@Mixin
private CommonMixinOne myCommonMixinOne;
public static void main(String... args) {
// in this particular application,
// the indices of the mixin parameters are set as below:
System.setProperty("commonParam2", "0");
System.setProperty("commonParam1", "2");
ParseResult result = CommandLine.parseArgs(new TestCommand(), args);
// ...
}
} |
Thanks @remkop for the response! It is helpful and I like your alternative to use variables. |
This is now available in master. See draft release notes for details. I’ll try to add a test for your use case before the release if I get a chance. |
I added a test but this still fails with a validation exception; when creating the |
Some ideas for addressing this:
We probably need something to postpone validation anyway: picocli will create a The relative index idea looks interesting but by itself won’t solve this problem. It may still be a useful feature to add later. |
This is related to #370: relative indexing. If an index can be defined as a relative index, like this:
Also, if the Perhaps |
picocli 4.3 will offer better support for positional parameters in reusable components: parameters can have a relative index. An index of A relative index can also be "anchored" to an absolute index. For example, an index of For example: static class CommonMixinOne {
@Parameters(index = "0+", paramLabel = "COMMON-PARAM-ONE")
private String commonMixinOneParam;
}
static class CommonMixinTwo {
@Parameters(index = "2+", paramLabel = "COMMON-PARAM-TWO")
private String commonMixinTwoParam;
}
@Test
// test for https://github.com/remkop/picocli/issues/564
public void testMixinsWithVariableIndex() {
@Command(name = "testCommand", description = "Example for issue 564")
class TestCommand {
@Mixin
private CommonMixinOne myCommonMixinOne;
@Parameters(index = "1", paramLabel = "TEST-COMMAND-PARAM")
private String testCommandParam;
@Mixin
private CommonMixinTwo myCommonMixinTwo;
}
CommandLine cmd = new CommandLine(new TestCommand());
//ParseResult result = cmd.parseArgs(args);
// ...
String expected = String.format("" +
"Usage: testCommand COMMON-PARAM-ONE TEST-COMMAND-PARAM COMMON-PARAM-TWO%n" +
"Example for issue 564%n" +
" COMMON-PARAM-ONE%n" +
" TEST-COMMAND-PARAM%n" +
" COMMON-PARAM-TWO%n");
String actual = cmd.getUsageMessage();
assertEquals(expected, actual);
} |
The indexes of Parameters inherited from Mixin's remain their original value. If for instance you have the following from the command line:
With the following code:
After parsing input args all of the Parameters will be at index zero and in this example the parameters TEST-COMMAND-PARAM and COMMON-PARAM-ONE from the command line will be unmatched. Also assigning any other index in the code will result in a:
Am I missing something and there is a way around this without having to drop the common Mixin code in to the main command? I realize this might be a strange use case but it seems like perhaps positional parameters should have their indexes incremented when Mixins are used.
The text was updated successfully, but these errors were encountered: