diff --git a/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandManager.java b/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandManager.java index 7ced202..0988daf 100644 --- a/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandManager.java +++ b/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandManager.java @@ -31,6 +31,7 @@ import cloud.commandframework.exceptions.InvalidSyntaxException; import cloud.commandframework.exceptions.NoPermissionException; import cloud.commandframework.exceptions.NoSuchCommandException; +import cloud.commandframework.execution.FilteringCommandSuggestionProcessor; import cloud.commandframework.keys.CloudKey; import java.util.ArrayList; import java.util.Arrays; @@ -87,6 +88,9 @@ public SpringCommandManager( this.commandSenderMapper = commandSenderMapper; this.suggestionFactory = super.suggestionFactory().mapped(CloudCompletionProposal::fromSuggestion); this.parameterInjectorRegistry().registerInjectionService(new SpringInjectionService<>(applicationContext)); + this.commandSuggestionProcessor(new FilteringCommandSuggestionProcessor<>( + FilteringCommandSuggestionProcessor.Filter.startsWith(true).andTrimBeforeLastSpace() + )); this.registerDefaultExceptionHandlers(); } diff --git a/example/src/main/java/org/incendo/cloud/spring/example/commands/AddCatCommand.java b/example/src/main/java/org/incendo/cloud/spring/example/commands/AddCatCommand.java index 6f5fd5d..68add1f 100644 --- a/example/src/main/java/org/incendo/cloud/spring/example/commands/AddCatCommand.java +++ b/example/src/main/java/org/incendo/cloud/spring/example/commands/AddCatCommand.java @@ -26,7 +26,9 @@ import cloud.commandframework.Command; import cloud.commandframework.CommandBean; import cloud.commandframework.CommandProperties; +import cloud.commandframework.arguments.aggregate.AggregateCommandParser; import cloud.commandframework.arguments.flags.CommandFlag; +import cloud.commandframework.arguments.parser.ArgumentParseResult; import cloud.commandframework.arguments.suggestion.SuggestionProvider; import cloud.commandframework.context.CommandContext; import cloud.commandframework.meta.CommandMeta; @@ -42,6 +44,7 @@ import org.springframework.stereotype.Component; import static cloud.commandframework.CommandDescription.commandDescription; +import static cloud.commandframework.arguments.standard.IntegerParser.integerParser; import static cloud.commandframework.arguments.standard.StringParser.stringParser; @Component @@ -74,21 +77,28 @@ public AddCatCommand(final @NonNull CatService catService) { protected Command.@NonNull Builder configure( final Command.@NonNull Builder builder ) { - return builder.literal("add") - .required("name", stringParser(), SuggestionProvider.blocking((ctx, in) -> List.of( + final AggregateCommandParser catParser = AggregateCommandParser.builder() + .withComponent("name", stringParser(), SuggestionProvider.blocking((ctx, in) -> List.of( CloudCompletionProposal.of("Missy").displayText("Missy (A cute cat name)"), CloudCompletionProposal.of("Donald").displayText("Donald (Old man name = CUTE!)"), CloudCompletionProposal.of("Fluffy").displayText("Fluffy (A classic :))") ))) + .withComponent("age", integerParser(0)) + .withDirectMapper(Cat.class, (cmxCtx, ctx) -> ArgumentParseResult.success(new Cat(ctx.get("name"), + ctx.get("age")))) + .build(); + + return builder.literal("add") + .required("cat", catParser) .flag(CommandFlag.builder("override")) .commandDescription(commandDescription("Add a cat")); } @Override public void execute(final @NonNull CommandContext commandContext) { - final String name = commandContext.get("name"); + final Cat cat = commandContext.get("cat"); final boolean override = commandContext.flags().hasFlag("override"); - final Cat cat = this.catService.addCat(name, override); + this.catService.addCat(cat, override); // We can either write the output to the context: commandContext.set(SpringCommandManager.OUTPUT, String.format("Added cat: %s", cat.name())); diff --git a/example/src/main/java/org/incendo/cloud/spring/example/commands/ListCatCommand.java b/example/src/main/java/org/incendo/cloud/spring/example/commands/ListCatCommand.java index bf2efbe..6f4e560 100644 --- a/example/src/main/java/org/incendo/cloud/spring/example/commands/ListCatCommand.java +++ b/example/src/main/java/org/incendo/cloud/spring/example/commands/ListCatCommand.java @@ -61,6 +61,6 @@ public ListCatCommand(final @NonNull CatService catService) { @CommandMethod("cat list") public void listCats(final @NonNull SpringCommandSender sender) { sender.writeLine("Cats"); - this.catService.cats().forEach(cat -> sender.writeLine(String.format("- %s", cat.name()))); + this.catService.cats().forEach(cat -> sender.writeLine(String.format("- %s (Age: %d)", cat.name(), cat.age()))); } } diff --git a/example/src/main/java/org/incendo/cloud/spring/example/model/Cat.java b/example/src/main/java/org/incendo/cloud/spring/example/model/Cat.java index 4548f57..6999627 100644 --- a/example/src/main/java/org/incendo/cloud/spring/example/model/Cat.java +++ b/example/src/main/java/org/incendo/cloud/spring/example/model/Cat.java @@ -23,13 +23,15 @@ // package org.incendo.cloud.spring.example.model; +import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; /** * A kitty. * * @param name the name of the cat + * @param age the age of the cat */ -public record Cat(@NonNull String name) { +public record Cat(@NonNull String name, @NonNegative int age) { } diff --git a/example/src/main/java/org/incendo/cloud/spring/example/service/CatService.java b/example/src/main/java/org/incendo/cloud/spring/example/service/CatService.java index ebc1f2d..18c1f4c 100644 --- a/example/src/main/java/org/incendo/cloud/spring/example/service/CatService.java +++ b/example/src/main/java/org/incendo/cloud/spring/example/service/CatService.java @@ -47,17 +47,14 @@ public CatService(final @NonNull CatRepository catRepository) { /** * Adds a cat with the given {@code name}. * - * @param name the cat + * @param cat the cat * @param override whether to override an existing cat - * @return the cat */ - public Cat addCat(final @NonNull String name, final boolean override) { - final Cat cat = new Cat(name); + public void addCat(final @NonNull Cat cat, final boolean override) { if (override) { - this.removeCat(name); + this.removeCat(cat.name()); } this.catRepository.addCat(cat); - return cat; } /**