Picocli: Validation of parameters

Created on 20 Feb 2018  路  9Comments  路  Source: remkop/picocli

First off: I really like your command line parser. Very nice, very good.

Is there support for parameter validations, or best practice how to do them in the context of picocli.

doc

All 9 comments

Thanks for the positive feedback! Let me know what parts of it you like particularly, so I can do more of that.

About validation, picocli currently does not have support for validation. I thought about supporting it but decided not to. I could change my mind about this, but I could not see how much value it would have to provide a picocli interface for this. It seems just as simple to have applications do validation without implementing a picocli interface.

Additionally, many applications would likely need to do validation on the combination of multiple options. It would be hard for picocli to provide for this.

I'm open to ideas though, but this is my current thinking.

What I like on picocli (currently) the most is the ease of

  • converting the command line parameters to custom data types
  • using sub commands
  • reducing boiler plate code by using parseWithHandlers

And especially for the last point I think parameter validation would be a good extension to picocli.
If I have to add within the run or call method my own validation it will increase the boiler plate code a lot. Imagine a command with subcommand and many subcommands are using the same custom data types. Now I have to add this validation in each run / call block, whereas I would like to declare the validation and let the framework do its job.

I think there are (at least) two modes for validations: One would be the validation of each parameter on its one. This could be done via annotation or by providing a similar mechanism as the registerConverter.
Then there would also be a validation where more then one parameters are validated together, here
some callback method would be necessary.

The additional advantage to put the validation into picocli would be the improved messages, as picocli
could report which options / parameters have raised an validation error. If I do it by hand I have to take
care that my exception message always matches the names within the annotations. This would somehow
invalidate the DRY principle.

I have also some other ideas for improvement in mind and try to sketch them later this days or week.

I'd be happy to discuss further. If you plan to do any prototyping for a proof of concept, please use the master branch. The 2.x branch is intended for bugfixes and small enhancements only.

Quick update: you can do validation in annotations using this standard: https://beanvalidation.org/

I'm planning to write an article on how to do this.

It has been a while, but I created this wiki page about using JSR-380 BeanValidation to validate user input in picocli: https://github.com/remkop/picocli/wiki/JSR-380-BeanValidation

The picocli-examples module now has a runnable example.

I also agree that this would be a useful addition.

One example situation for this would be if you have an int field, I want to only accept values that are greater than or equal to 1.

The wiki page has an example of how to accomplish this with the @Min annotation.

I鈥檒l add an entry to the user manual on using jsr-380 bean validation.

This is now included in the picocli user manual:
https://picocli.info/#_validation

Was this page helpful?
0 / 5 - 0 ratings