Skip to content

Commit 69b7ac3

Browse files
committed
Introduce the notion of a terminal parameter.
It is roughly what GenericArguments#optional did in API 7, but happens one element earlier - a terminal element is parsed then control passes to an executor, rather than seeing there is nothing to parse.
1 parent cf1e100 commit 69b7ac3

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

src/main/java/org/spongepowered/api/command/parameter/Parameter.java

+48-33
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package org.spongepowered.api.command.parameter;
2626

2727
import com.google.common.reflect.TypeToken;
28+
import org.checkerframework.checker.nullness.qual.NonNull;
2829
import org.checkerframework.checker.nullness.qual.Nullable;
2930
import org.spongepowered.api.CatalogType;
3031
import org.spongepowered.api.Sponge;
@@ -738,41 +739,22 @@ static <T> Parameter.Value.Builder<T> literal(Class<T> returnType, T returnedVal
738739
}
739740

740741
/**
741-
* Parses the next element(s) in the {@link CommandContext}
742+
* Gets whether this parameter can be considered terminal.
742743
*
743-
* @param reader The {@link ArgumentReader.Mutable} containing the strings
744-
* that need to be parsed
745-
* @param context The {@link CommandContext.Builder} that contains the
746-
* current state of the execution
747-
* @throws ArgumentParseException thrown if the parameter could not be
748-
* parsed
749-
*/
750-
void parse(ArgumentReader.Mutable reader, CommandContext.Builder context) throws ArgumentParseException;
751-
752-
/**
753-
* Returns potential completions of the current tokenized argument.
754-
*
755-
* @param reader The {@link ArgumentReader} containing the strings that need
756-
* to be parsed
757-
* @param context The {@link CommandContext} that contains the
758-
* current state of the execution.
759-
* @return The potential completions.
760-
* @throws ArgumentParseException thrown if the parameter could not be
761-
* parsed
762-
*/
763-
List<String> complete(ArgumentReader.Immutable reader, CommandContext context) throws ArgumentParseException;
764-
765-
/**
766-
* Gets the usage of this parameter.
744+
* <p>A terminal parameter will pass control to the command's associated
745+
* {@link CommandExecutor} if the parameter consumes the end of an input
746+
* string.</p>
767747
*
768-
* @param cause The {@link CommandCause} that requested the usage
769-
* @return The usage
748+
* @return true if terminal, else false.
770749
*/
771-
Text getUsage(CommandCause cause);
750+
boolean isTerminal();
772751

773752
/**
774753
* Gets whether this parameter is optional.
775754
*
755+
* <p>An optional parameter will not throw an exception if it cannot parse
756+
* an input, instead passing control to another parameter.</p>
757+
*
776758
* @return true if optional, else false.
777759
*/
778760
boolean isOptional();
@@ -814,7 +796,7 @@ interface Builder extends ResettableBuilder<Key<?>, Builder> {
814796
* @param <T> The type of the value represented by the key
815797
* @return The built {@link Key}
816798
*/
817-
<T> Key<T> build(String key, TypeToken<T> typeToken);
799+
<T> Key<T> build(@NonNull String key, @NonNull TypeToken<T> typeToken);
818800
}
819801

820802
}
@@ -868,6 +850,39 @@ interface Value<T> extends Parameter {
868850
*/
869851
Predicate<CommandCause> getRequirement();
870852

853+
/**
854+
* Parses the next element(s) in the {@link CommandContext}
855+
*
856+
* @param reader The {@link ArgumentReader.Mutable} containing the strings
857+
* that need to be parsed
858+
* @param context The {@link CommandContext.Builder} that contains the
859+
* current state of the execution
860+
* @throws ArgumentParseException thrown if the parameter could not be
861+
* parsed
862+
*/
863+
void parse(ArgumentReader.@NonNull Mutable reader, CommandContext.@NonNull Builder context) throws ArgumentParseException;
864+
865+
/**
866+
* Returns potential completions of the current tokenized argument.
867+
*
868+
* @param reader The {@link ArgumentReader} containing the strings that need
869+
* to be parsed
870+
* @param context The {@link CommandContext} that contains the
871+
* current state of the execution.
872+
* @return The potential completions.
873+
* @throws ArgumentParseException thrown if the parameter could not be
874+
* parsed
875+
*/
876+
List<String> complete(ArgumentReader.@NonNull Immutable reader, @NonNull CommandContext context) throws ArgumentParseException;
877+
878+
/**
879+
* Gets the usage of this parameter.
880+
*
881+
* @param cause The {@link CommandCause} that requested the usage
882+
* @return The usage
883+
*/
884+
Text getUsage(CommandCause cause);
885+
871886
/**
872887
* Builds a {@link Parameter} from constituent components.
873888
*/
@@ -1159,15 +1174,15 @@ interface SequenceBuilder extends ResettableBuilder<Parameter, SequenceBuilder>
11591174
*
11601175
* @return This builder, for chaining
11611176
*/
1162-
SequenceBuilder optional();
1177+
SequenceBuilder terminal();
11631178

11641179
/**
11651180
* Sets that this parameter is weak optional and will be ignored if it
11661181
* cannot parse the next element(s).
11671182
*
11681183
* @return This builder, for chaining
11691184
*/
1170-
SequenceBuilder optionalWeak();
1185+
SequenceBuilder optional();
11711186

11721187
/**
11731188
* Defines the next parameter in the parameter sequence.
@@ -1228,15 +1243,15 @@ interface FirstOfBuilder extends ResettableBuilder<Parameter, FirstOfBuilder> {
12281243
*
12291244
* @return This builder, for chaining
12301245
*/
1231-
FirstOfBuilder optional();
1246+
FirstOfBuilder terminal();
12321247

12331248
/**
12341249
* Sets that this parameter is weak optional and will be ignored if it
12351250
* cannot parse the next element(s).
12361251
*
12371252
* @return This builder, for chaining
12381253
*/
1239-
FirstOfBuilder optionalWeak();
1254+
FirstOfBuilder optional();
12401255

12411256
/**
12421257
* Adds a parameter that can be used to parse an argument. Parameters

0 commit comments

Comments
 (0)