-
Notifications
You must be signed in to change notification settings - Fork 431
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
ITypeConverter for custom generics (type constructors) #2156
Comments
Is the problem that you want to use Scala collection types for multi-value options and positional parameters? Picocli internally uses reflection to see if multi-value fields implement the However, I have one idea that should allow you to use picocli-annotated For example (PSEUDO CODE):
Can you give this a try? |
Thanks a lot for the response and for the possible workaround! Although the described approach would work in theory it won't fly for us in practice. The reason is that we chose picocli primarily because of (sub)command methods and we intend to use those for ~90% of commands (we try to rewrite quite a big cli app from python to scala). For the sake of anyone who might see this thread in the future, I'm attaching our solution, which is quite good (allows to use any scala type and ensures converter is present at compile time) but is also cumbersome (requires wrapping all parameters) and has significant drawbacks (parsing errors are thrown during command execution, not parsing). If picocli had some lower level API that would allow us to plug in more directly into the parser, it would be great.
|
@Krever Glad you found an efficient workaround. Are you okay if I close this ticket? To be honest, I don't see myself working on API to support non-java Collection and Map-like data structures. |
Hey @remkop, I understand, it's fine to close the issue. I think it might significantly limit the adoption from Scala (having working native collections is rather important). However, at the same time, I see value in focus (on Java/Kotlin) and understand the lack of resources to add this significant change. Thanks a lot for the responses :) |
Thanks to @remkop for the workaround; I was able to use a similar approach to populate a Guava Multimap: Multimap<UUID, String> examples = MultimapBuilder.linkedHashKeys().linkedHashSetValues().build();
@Option(
names = "--examples",
paramLabel = "<uuid>=<string>"
)
public void setExamples(Map<UUID, String> newValues) {
for (Map.Entry<UUID, String> entry : newValues.entrySet()) {
examples.put(entry.getKey(), entry.getValue());
}
} Unlike the suggestion given above, this code doesn't ever clear the multimap, but this seems to work fine if PicoCLI doesn't need to remove anything--parsing arguments should just be additive. |
Hey @remkop, I got back to this problem and thought of an alternative workaround. What do you think about traversing CommandSpec and injecting custom The problem I currently have with this approach:
I would appreciate any hint or info that this approach won't fly for some reason. I was also considering modifying the field with reflection, hacky as damn but should work I think. Alternatively I was also thinking about plugging myself somewhere around ITypeInfo and recognizing scala types in isOptional, isCollection but this seemed much more complicated. |
I think there's an OptionSpec.Builder constructor that takes an existing OptionSpec, and similarly for PositionalParamSpec.Builder (constructor that takes a PositionalParamSpec). |
Yes, that's it. Thank you! I haven't looked carefully enough, sorry! |
Hey! I'm trying to use picocli with Scala and one of the main limitations is inability to define converters from scala stdlib. While simple types are ok, the problem arises with generic types (called type constructors in scala).
Scala has its own
scala.List
,scala.Map
andscala.Option
. Having to use java variants is a bit suboptimal.This might be related to #1804 but Im not sure if interface described there would suffice, so I'm raising a separate ticket.
The text was updated successfully, but these errors were encountered: