Add String[] args

This commit is contained in:
Alexander Söderberg 2020-10-09 20:44:17 +02:00
parent 16623969ad
commit fcd269b6e7
No known key found for this signature in database
GPG key ID: FACEA5B0F4C1BF80
7 changed files with 176 additions and 0 deletions

View file

@ -39,6 +39,7 @@ import cloud.commandframework.exceptions.NoSuchCommandException;
import cloud.commandframework.permission.CommandPermission;
import cloud.commandframework.permission.OrPermission;
import cloud.commandframework.types.tuples.Pair;
import io.leangen.geantyref.GenericTypeReflector;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -452,6 +453,14 @@ public final class CommandTree<C> {
}
// END: Flags
// START: Array arguments
if (child.getValue() != null && GenericTypeReflector.erase(child.getValue().getValueType().getType()).isArray()) {
while (commandQueue.size() > 1) {
commandQueue.remove();
}
}
// END: Array arguments
if (child.getValue() != null) {
if (commandQueue.isEmpty()) {
return Collections.emptyList();

View file

@ -67,6 +67,8 @@ public final class DelegatingCommandSuggestionEngine<C> implements CommandSugges
@NonNull final String input
) {
final @NonNull LinkedList<@NonNull String> inputQueue = new CommandInputTokenizer(input).tokenize();
/* Store a copy of the input queue in the context */
context.store("__raw_input__", new LinkedList<>(inputQueue));
final List<String> suggestions;
if (this.commandManager.preprocessContext(context, inputQueue) == State.ACCEPTED) {
suggestions = this.commandManager.getCommandSuggestionProcessor().apply(

View file

@ -35,6 +35,7 @@ import cloud.commandframework.arguments.standard.FloatArgument;
import cloud.commandframework.arguments.standard.IntegerArgument;
import cloud.commandframework.arguments.standard.ShortArgument;
import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.arguments.standard.StringArrayArgument;
import cloud.commandframework.arguments.standard.UUIDArgument;
import io.leangen.geantyref.GenericTypeReflector;
import io.leangen.geantyref.TypeToken;
@ -112,6 +113,7 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
(double) options.get(StandardParameters.RANGE_MAX, Double.MAX_VALUE)
));
this.registerParserSupplier(TypeToken.get(Character.class), options -> new CharArgument.CharacterParser<C>());
this.registerParserSupplier(TypeToken.get(String[].class), options -> new StringArrayArgument.StringArrayParser<>());
/* Make this one less awful */
this.registerParserSupplier(TypeToken.get(String.class), options -> {
final boolean greedy = options.get(StandardParameters.GREEDY, false);

View file

@ -0,0 +1,122 @@
//
// MIT License
//
// Copyright (c) 2020 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.arguments.standard;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.context.CommandContext;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
import java.util.Queue;
import java.util.function.BiFunction;
/**
* This is a command argument type that essentially mimics {@link StringArgument#greedy(String)},
* but then splits the input string into a string array. The input string will be split at
* every blank space.
*
* @param <C> Command sender type
*/
public final class StringArrayArgument<C> extends CommandArgument<C, String[]> {
private StringArrayArgument(
final boolean required,
final @NonNull String name,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
) {
super(
required,
name,
new StringArrayParser<>(),
"",
TypeToken.get(String[].class),
suggestionsProvider
);
}
/**
* Create a new required string array argument
*
* @param name Argument name
* @param suggestionsProvider Suggestions provider
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull StringArrayArgument<C> of(
final @NonNull String name,
final @NonNull BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
) {
return new StringArrayArgument<>(
true,
name,
suggestionsProvider
);
}
/**
* Create a new optional string array argument
*
* @param name Argument name
* @param suggestionsProvider Suggestions provider
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull StringArrayArgument<C> optional(
final @NonNull String name,
final @NonNull BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
) {
return new StringArrayArgument<>(
false,
name,
suggestionsProvider
);
}
/**
* Parser that parses input into a string array
*
* @param <C> Command sender type
*/
public static final class StringArrayParser<C> implements ArgumentParser<C, String[]> {
@Override
public @NonNull ArgumentParseResult<String @NonNull []> parse(
@NonNull final CommandContext<@NonNull C> commandContext,
@NonNull final Queue<@NonNull String> inputQueue
) {
final String[] result = new String[inputQueue.size()];
for (int i = 0; i < result.length; i++) {
result[i] = inputQueue.remove();
}
return ArgumentParseResult.success(result);
}
}
}

View file

@ -30,6 +30,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
@ -205,6 +206,15 @@ public final class CommandContext<C> {
return this.<T>getOptional(key).orElse(defaultValue);
}
/**
* Get the raw input. This should only be used when {@link #isSuggestions()} is {@code true}
*
* @return Raw input in token form
*/
public @NonNull LinkedList<@NonNull String> getRawInput() {
return this.getOrDefault("__raw_input__", new LinkedList<>());
}
/**
* Create an argument timing for a specific argument
*