feat(core): add repeatable flags (#378)

implements #209.
This commit is contained in:
Alexander Söderberg 2022-06-14 17:21:51 +02:00 committed by Jason
parent d3864414aa
commit ec535dad7f
9 changed files with 459 additions and 80 deletions

View file

@ -28,6 +28,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.BiFunction;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
@ -94,4 +95,13 @@ public @interface Flag {
* @since 1.6.0
*/
@NonNull String permission() default "";
/**
* Whether the flag can be repeated.
*
* @return whether the flag can be repeated
* @since 1.7.0
*/
@API(status = API.Status.STABLE, since = "1.7.0")
boolean repeatable() default false;
}

View file

@ -30,6 +30,7 @@ import cloud.commandframework.arguments.flags.CommandFlag;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.arguments.parser.ParserRegistry;
import cloud.commandframework.permission.Permission;
import io.leangen.geantyref.GenericTypeReflector;
import io.leangen.geantyref.TypeToken;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@ -65,15 +66,34 @@ final class FlagExtractor implements Function<@NonNull Method, Collection<@NonNu
}
final Flag flag = parameter.getAnnotation(Flag.class);
final String flagName = this.annotationParser.processString(flag.value());
final CommandFlag.Builder<Void> builder = this.commandManager
CommandFlag.Builder<Void> builder = this.commandManager
.flagBuilder(this.annotationParser.processString(flagName))
.withDescription(ArgumentDescription.of(this.annotationParser.processString(flag.description())))
.withAliases(this.annotationParser.processStrings(flag.aliases()))
.withPermission(Permission.of(this.annotationParser.processString(flag.permission())));
if (flag.repeatable()) {
builder = builder.asRepeatable();
}
if (parameter.getType().equals(boolean.class)) {
flags.add(builder.build());
} else {
final TypeToken<?> token = TypeToken.get(parameter.getType());
final TypeToken<?> token;
if (flag.repeatable() && Collection.class.isAssignableFrom(parameter.getType())) {
token = TypeToken.get(GenericTypeReflector.getTypeParameter(
parameter.getParameterizedType(),
Collection.class.getTypeParameters()[0]
));
} else {
token = TypeToken.get(parameter.getType());
}
if (token.equals(TypeToken.get(boolean.class))) {
flags.add(builder.build());
continue;
}
final Collection<Annotation> annotations = Arrays.asList(parameter.getAnnotations());
final ParserRegistry<?> registry = this.commandManager.parserRegistry();
final ArgumentParser<?, ?> parser;

View file

@ -126,8 +126,10 @@ public class MethodCommandExecutionHandler<C> implements CommandExecutionHandler
} else if (parameter.isAnnotationPresent(Flag.class)) {
final Flag flag = parameter.getAnnotation(Flag.class);
final String flagName = this.annotationParser.processString(flag.value());
if (parameter.getType() == boolean.class) {
if (parameter.getType().equals(boolean.class)) {
arguments.add(flagContext.isPresent(flagName));
} else if (flag.repeatable() && parameter.getType().isAssignableFrom(List.class)) {
arguments.add(flagContext.getAll(flagName));
} else {
arguments.add(flagContext.getValue(flagName, null));
}

View file

@ -0,0 +1,79 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.annotations.feature;
import cloud.commandframework.CommandManager;
import cloud.commandframework.annotations.AnnotationParser;
import cloud.commandframework.annotations.CommandMethod;
import cloud.commandframework.annotations.Flag;
import cloud.commandframework.annotations.TestCommandManager;
import cloud.commandframework.annotations.TestCommandSender;
import cloud.commandframework.arguments.parser.StandardParameters;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.execution.CommandResult;
import cloud.commandframework.internal.CommandRegistrationHandler;
import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.meta.SimpleCommandMeta;
import io.leangen.geantyref.TypeToken;
import java.util.Collection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static com.google.common.truth.Truth.assertThat;
class RepeatableFlagTest {
private CommandManager<TestCommandSender> commandManager;
@BeforeEach
void setup() {
this.commandManager = new TestCommandManager();
final AnnotationParser<TestCommandSender> annotationParser = new AnnotationParser<>(
this.commandManager,
TestCommandSender.class,
p -> SimpleCommandMeta.empty()
);
annotationParser.parse(new TestClassA());
}
@Test
void testRepeatableFlagParsing() {
// Act
final CommandResult<TestCommandSender> result = this.commandManager.executeCommand(
new TestCommandSender(),
"test --flag one --flag two --flag three"
).join();
// Assert
assertThat(result.getCommandContext().flags().getAll("flag")).containsExactly("one", "two", "three");
}
public static final class TestClassA {
@CommandMethod("test")
public void command(@Flag(value = "flag", repeatable = true) Collection<String> flags) {
}
}
}