Support explicit arity min max with @Option

- @Option now has arityMin/arityMax which if defined,
  non-negative, are used instead of arity.
- Fixes #731
This commit is contained in:
Janne Valkealahti
2023-05-01 08:36:41 +01:00
parent c36aa41878
commit 9d27466c6a
6 changed files with 225 additions and 40 deletions

View File

@@ -74,6 +74,28 @@ public @interface Option {
* Define option arity.
*
* @return option arity
* @see #arityMin()
* @see #arityMax()
*/
OptionArity arity() default OptionArity.NONE;
/**
* Define option arity min. If Defined non-negative will be used instead of
* {@link #arity()}. If {@code arityMax} is not set non-negative it is set to
* same as this.
*
* @return option arity min
* @see #arity()
*/
int arityMin() default -1;
/**
* Define option arity max. If Defined non-negative will be used instead of
* {@link #arity()}. If {@code arityMin} is not set non-negative it is set to
* zero.
*
* @return option arity max
* @see #arity()
*/
int arityMax() default -1;
}

View File

@@ -251,7 +251,22 @@ class CommandRegistrationFactoryBean implements FactoryBean<CommandRegistration>
optionSpec.shortNames(shortNames.toArray(new Character[0]));
optionSpec.position(mp.getParameterIndex());
optionSpec.description(so.description());
if (so.arity() != OptionArity.NONE) {
int arityMin = so.arityMin();
int arityMax = so.arityMax();
if (arityMin > -1) {
if (arityMax < arityMin) {
arityMax = arityMin;
}
}
else if (arityMax > -1) {
if (arityMin < 0) {
arityMin = 0;
}
}
if (arityMin > -1 && arityMax > -1) {
optionSpec.arity(arityMin, arityMax);
}
else if (so.arity() != OptionArity.NONE) {
optionSpec.arity(so.arity());
}
else {