From 3829cd6706ab6ee9d22f4684b135b5e7eca1d6b6 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 22 Aug 2022 18:07:35 +0200 Subject: [PATCH] Add AbstractSWCommand.OptionalValue.onlyUINIG --- src/de/steamwar/command/AbstractSWCommand.java | 5 +++++ src/de/steamwar/command/CommandPart.java | 10 ++++++++-- src/de/steamwar/command/SWCommandUtils.java | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index aa33a64..5a4a10a 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -386,6 +386,11 @@ public abstract class AbstractSWCommand { * Will pe parsed against the TypeMapper specified by the parameter or annotation. */ String value(); + + /** + * The method name stands for 'onlyUseIfNoneIsGiven'. + */ + boolean onlyUINIG() default false; } @Retention(RetentionPolicy.RUNTIME) diff --git a/src/de/steamwar/command/CommandPart.java b/src/de/steamwar/command/CommandPart.java index d5a5220..14b4079 100644 --- a/src/de/steamwar/command/CommandPart.java +++ b/src/de/steamwar/command/CommandPart.java @@ -23,7 +23,6 @@ import lombok.AllArgsConstructor; import lombok.Setter; import java.lang.reflect.Array; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -52,6 +51,9 @@ class CommandPart { @Setter private boolean ignoreAsArgument = false; + @Setter + private boolean onlyUseIfNoneIsGiven = false; + public CommandPart(AbstractSWCommand command, AbstractTypeMapper typeMapper, AbstractValidator validator, AbstractGuardChecker guardChecker, Class varArgType, String optional, GuardCheckType guardCheckType) { this.command = command; this.typeMapper = typeMapper; @@ -113,7 +115,11 @@ class CommandPart { } if (!validArgument.success) { if (!ignoreAsArgument) { - current.add(typeMapper.map(sender, EMPTY_ARRAY, optional)); + if (!onlyUseIfNoneIsGiven) { + current.add(typeMapper.map(sender, EMPTY_ARRAY, optional)); + } else if(startIndex >= args.length) { + current.add(typeMapper.map(sender, EMPTY_ARRAY, optional)); + } } if (next != null) { next.generateArgumentArray(errors, current, sender, args, startIndex); diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 69e0b8c..e1266bd 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -101,6 +101,7 @@ public class SWCommandUtils { AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class); CommandPart commandPart = new CommandPart<>(command, typeMapper, validator, guardChecker, varArgType, optionalValue != null ? optionalValue.value() : null, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND); + commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG()); if (current != null) { current.setNext(commandPart); }