jcommander
jcommander copied to clipboard
@Parameters(`parametersValidators = MYPARAMETERSVALIDATOR.class`)
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:
- Parameter X is required only if parameter Y is provided (like
--log-file /var/log/x
is required only when--log-level
is provided). - Parameter X is mutual exclusive to parameter Y (like
--quiet
cannot be used with--verbose
). - The values of X and Y need to be compatible (like `---create-bean-of-type Integer.class --initialize-with-value 125' ).
@cbeust WDYT? Do you like the idea? If so, I would be happy to provide a PR. :-)
I was wondering if this is really necessary considering what the doc says about global parameter validation:
https://jcommander.org/#_global_parameter_validation
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.
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
.
Alright, send a PR!
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? 🤔
ℹ️ 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
).