-
Notifications
You must be signed in to change notification settings - Fork 425
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 n-ary converter #1305
Comments
Hi @martlin2cz, thanks for raising this. Yes, this is possible. This can be accomplished with a parameter consumer. Something like this: @Parameters(parameterConsumer = PointConverter.class)
private Point position;
static class PointConverter implements IParameterConsumer {
public void consumeParameters(Stack<String> args, ArgSpec argSpec, CommandSpec commandSpec) {
if (args.size() < 2) {
throw new ParameterException(commandSpec.commandLine(),
"Missing coordinates for Point. Please specify 2 coordinates."));
}
int x = Integer.parseInt(args.pop());
int y = Integer.parseInt(args.pop());
argSpec.setValue(new Point(x, y));
}
} I will update the docs to add a link from the type conversion section. |
@martlin2cz I went ahead and added this section to the user manual: https://picocli.info/#_multi_parameter_type_converters Can you verify and suggest improvements, if any? |
Maybe the example would be better with a more explanatory |
Hello @remkop, this is good news that there do exists some solution. Thanks for the doc update, I didn't know the parameter consumer can be used this way. But, I'm thinking, wouldn't there still be possibility of some convient method in case like the mine? I mean in which the arity is fixed number. It would be nice to have the arity checked by the picocli automatically. Maybe just some abstract helper class like:
Would this be possible? |
It is always difficult to decide what belongs in the library and what belongs in applications. The library currently offers the building blocks to build n-ary converters that do custom validation based on arity, or all kinds of other validations that we have not though of yet. My initial take it to leave it like this for a while, at least until we see multiple requests and a clearer picture emerges of what additional features would help accomplish additional use cases. Does that make sense? Taking a step back, how do you like picocli so far? Any particular likes and/or dislikes? |
Okay, agree. Thanks. |
This reverts commit 4823583.
…ary type converter" This reverts commit 7b493cc.
This reverts commit 4823583.
…ary type converter" This reverts commit 7b493cc.
Hello,
I would like to get following command created:
I have prepared the
Command
class with the parameters declaration as follows:However, I would like to get the
coords
list automatically converted to my data class. Currently, I have to do it by hand:I was thinking about utilizing the
ITypeConverter
, like so:looking like:
But, the
ITypeConverter
maps 1 parameter to 1 object, not all of them into one.Quite obviously, there would be new converter interface needed to be created (something like
IMultiTypeConverter
).BTW is there any other workaround?
Thanks,
Martin
The text was updated successfully, but these errors were encountered: