cbeust / jcommander

Command line parsing framework for Java
Apache License 2.0
1.94k stars 332 forks source link

@Parameters(`parametersValidators = MYPARAMETERSVALIDATOR.class`) #539

Closed mkarg closed 1 year ago

mkarg commented 2 years ago

I frequently had the need to check parameters for validity, but the validity was not static. So I wished there would be a rule that could be pointed to instead of static flags like password or required. The idea is that an instance of the given MYRULE.class is created, then it is given the values of all parameters, then it computes the outcome, and the outcome of the rule is either throwing an exception (when the rule is broken) or doing nothing. Just some use cases where this would be handy:

mkarg commented 2 years ago

@cbeust WDYT? Do you like the idea? If so, I would be happy to provide a PR. :-)

cbeust commented 2 years ago

I was wondering if this is really necessary considering what the doc says about global parameter validation:

https://jcommander.org/#_global_parameter_validation

mkarg commented 2 years ago

I would say it is a shortcoming of JCommander to simply offload this task to the application. The nice thing with JCommander is that one can have everything dealing with CLI in one single place: annotations on the parameters. I would really love to get rid of all those checks in my application code and move it to such rules instead. This also allows to make rules reusable between applications, BTW. In real world, the CLI code I wrote in the past two years would look much cleaner if rules would exists.

mkarg commented 2 years ago

We would be happy to provide a PR that replaces that annoying part of the documentation by a real support of rules in @Parameter and @Parameters.

cbeust commented 2 years ago

Alright, send a PR!

mkarg commented 1 year ago

Alright, send a PR!

@cbeust Working on it. Maybe a dumb question, but how to run all tests at once? gradlew test produces an HTML file telling me zero of zero tests done. What am I doing wrong? 🤔

mkarg commented 1 year ago

ℹ️ In contrast to the original comment, this issue was implemented by PR #542 using different names than originally proposed. The new interface is called IParametersValidator (instead of IRule), and the annotation field is named parametersValidators (instead of rules).