-
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
Is it possible to create a single value Option typed as byte[] ? #1394
Comments
Yes picocli considers arrays to be multi-value options, so even when arity=1, users would be able to specify the Probably the easiest thing to do is to capture the user-specified token as a string, and convert to a byte array in the application (the Alternatively, you can have an |
I do not like so much this idea, because I like to have the ITypeConverter which act as validator and converter. This allow to write less code and also have a good error message automatically. (With the model you propose I need to create a ITypeConverter which acts as validator then later in my code do the conversion)
I will give a try it, but it feel like a workaround 😞 I would really appreciate that picocli provide an Option parameter which allow to force Array/List/... as single value option. (Not the first time I want to do this kind of thing). |
Thinking about this more, the This keeps most of the benefits you mention about the type converter: good error message and centralized testing. Example code: class MyCommand {
private byte[] hexa;
@Option(names = { "-h", "--hexa" }, required = true, description = "a hexadecimal string")
void setHexa(String value) throws Exception {
hexa = Hex.decodeHex(value.toCharArray());
}
} Picocli currently distinguishes between single-value and multi-value options by their Java type. I think this makes sense, and is intuitive for programmers trying to build command line applications. Of course there are exceptions, like your use case. Sometimes byte arrays or character arrays are conceptually one value. |
I'm not sure this is so uncommon. At least in my experience this is not the case. 3 applications and 5 option (not only hexa decimal option) where I need to do that. I use different way to make his work ( setter as you propose or I create a class wrapper)
I still feel that adding a new Option argument would be better : @Option(required = true,
names = { "-h", "--hexa" },
description = { //
"an hexadecimal string" },
converter = HexadecimalConverter.class,
kind = "single", // or single="true" or forcesinglevalue="true" or something like this ...
)
byte[] hexa; I'm not sure as this is not my case but I also guess that the opposite could be useful, I mean "force an option to be multiple value and store it in something which is not an Array/Map/Collection" ... But maybe this is doable with |
Sorry, but I am not convinced. 😅 The amount of code is about the same, it just moves from the separate converter class to the setter method. Most importantly, I like the simplicity of the current picocli design, and I don't feel that introducing a |
Here is just a simplified use case to make it easier to understand. When I see my code with setter and converter, I feel this really less elegant and consistent.
I don't know if you are saying that this would make the picocli code more complicated or if you are talking about picocli API ? I feel that you don't like this idea and so not really open to this change so no need to insist. |
#1408 is a drawback of the setter workaround 😞 |
Another idea is to use |
Yep thx to let me know that. 🙏 But this works only for byte[] and I have several other kind of case, like :
But yes, in a general way, creating a Wrapper like this works : public class Wrapper<T> {
private T value;
public Wrapper(T value) {
this.value = value;
}
public T value(){
return value;
} I just still feel this is less elegant/more verbose than just a param in the @option annotation. :-) |
I would like to create something like this :
My converter looks like this :
But I get this kind of error:
java.lang.IllegalArgumentException: argument type mismatch.
I think this is because I convert the option in byte[] then picocli want to assign this value to byte[0]. (as it consider any array as a Multiple value option)
I tried to play with arity="1" or type="byte[].class" without success...
Any idea ?
The text was updated successfully, but these errors were encountered: