Skip to content
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

Multiple converter for Map #2117

Open
sbernard31 opened this issue Sep 19, 2023 · 4 comments
Open

Multiple converter for Map #2117

sbernard31 opened this issue Sep 19, 2023 · 4 comments

Comments

@sbernard31
Copy link
Contributor

I would like to do something like :

@Option(
        names =  { "-mo", "--my-option } ,
        description = { "syntax is : - attrKey1=attrValue1,attrKey2=attrValue2" },
        split = ",")
public Map<Type1, Type2> myOption;

Note there is no default converter for Type1 and Type2.

Ideally :

  1. I would like to add converter to this option for both type.
  2. I would like to validate that attrValue is a valid value for the given attrKey.

I don't know what is the recommended way ?

From a user perspective, I would like to be able to just add a converter which respect this API :

public interface IMapTypeConverter<K,V> {
  // Maybe there is better idea than Map.entry ?
   Map.entry<K,V> convert(String key, String value) throws Exception;
}

(I share the idea just in case)

I guess solutions with current API could be :

  • Register converters to the command (using CommandLine.registerConverter) and do post validation (in run() when using Runnable) ?
  • OR use a collection of <Map.entry<K,V> (or a decicated class) with only one converter but I need to split by "=" by myself ?

Any advice or recommendation?

@remkop
Copy link
Owner

remkop commented Sep 19, 2023

I would use CommandLine.registerConverter to register type converters for the key and value types.

Validation can be done in the run method like you said, or you can have an annotated setter method instead of an annotated Map field. That allows you to do the validation in the setter method. However picocli will call this method multiple times; using the run method is probably simpler.

This section in the user manual has examples for both ways to do validation.

@sbernard31
Copy link
Contributor Author

Thx for quick answer 🙏

Any opinion about the IMapTypeConverter idea ?

@remkop
Copy link
Owner

remkop commented Sep 19, 2023

To be honest I am not enthusiastic about adding API for something that can already be accomplished with the existing functionality.

@sbernard31
Copy link
Contributor Author

I guess there is a lot of feature which are just about making some use case more easier/straightforward to use.

E.g. : CommandLine.registerConverter vs converter = MyConverter.class seems like redundant feature.

But I can understand that you could think that this use case is not common enough to deserve dedicate feature.

Anyway, thx again for your help 🙏

(On my side, I get my answers so, feel free to close the issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants