@Parameters(`parametersValidators = MYPARAMETERSVALIDATOR.class`)
mkarg opened this issue · 7 comments
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' ).
I was wondering if this is really necessary considering what the doc says about 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!