Add default arguments

This commit is contained in:
Alexander Söderberg 2020-09-08 19:30:45 +02:00
parent 11d40bdd87
commit e623d72be7
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
42 changed files with 205 additions and 35 deletions

View file

@ -1,4 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~
~ MIT License
~
~ Copyright (c) 2020 IntellectualSites
~
~ 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.
~
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@ -9,7 +35,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>commands-bukkit</artifactId>
<artifactId>cloud-bukkit</artifactId>
<repositories>
<repository>
@ -27,7 +53,7 @@
</dependency>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>commands-core</artifactId>
<artifactId>cloud-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

View file

@ -29,13 +29,13 @@
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Commands</artifactId>
<artifactId>cloud</artifactId>
<groupId>com.intellectualsites</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>commands-core</artifactId>
<artifactId>cloud-core</artifactId>
</project>

View file

@ -25,7 +25,8 @@ package com.intellectualsites.commands;
import com.intellectualsites.commands.components.CommandSyntaxFormatter;
import com.intellectualsites.commands.components.StandardCommandSyntaxFormatter;
import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.context.CommandContextFactory;
import com.intellectualsites.commands.context.StandardCommandContextFactory;
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
import com.intellectualsites.commands.execution.CommandResult;
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
@ -46,13 +47,15 @@ import java.util.function.Function;
*/
public abstract class CommandManager<C extends CommandSender> {
private final CommandContextFactory<C> commandContextFactory = new StandardCommandContextFactory<>();
private final CommandExecutionCoordinator<C> commandExecutionCoordinator;
private final CommandRegistrationHandler commandRegistrationHandler;
private final CommandTree<C> commandTree;
private CommandSyntaxFormatter<C> commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>();
protected CommandManager(@Nonnull final Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
public CommandManager(@Nonnull final Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
this.commandTree = CommandTree.newTree(this, commandRegistrationHandler);
this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree);
@ -68,8 +71,7 @@ public abstract class CommandManager<C extends CommandSender> {
*/
@Nonnull
public CompletableFuture<CommandResult> executeCommand(@Nonnull final C commandSender, @Nonnull final String input) {
final CommandContext<C> context = new CommandContext<>(commandSender);
return this.commandExecutionCoordinator.coordinateExecution(context, tokenize(input));
return this.commandExecutionCoordinator.coordinateExecution(this.commandContextFactory.create(commandSender), tokenize(input));
}
/**
@ -82,8 +84,7 @@ public abstract class CommandManager<C extends CommandSender> {
*/
@Nonnull
public List<String> suggest(@Nonnull final C commandSender, @Nonnull final String input) {
final CommandContext<C> context = new CommandContext<>(commandSender);
return this.commandTree.getSuggestions(context, tokenize(input));
return this.commandTree.getSuggestions(this.commandContextFactory.create(commandSender), tokenize(input));
}
@Nonnull

View file

@ -107,7 +107,11 @@ public class CommandTree<C extends CommandSender> {
}
if (child.getValue() != null) {
if (commandQueue.isEmpty()) {
if (child.isLeaf()) {
if (child.getValue().hasDefaultValue()) {
commandQueue.add(child.getValue().getDefaultValue());
} else if (!child.getValue().isRequired()) {
return Optional.ofNullable(child.getValue().getOwningCommand());
} else if (child.isLeaf()) {
/* Not enough arguments */
throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter()
.apply(Objects.requireNonNull(

View file

@ -45,7 +45,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
/**
* Indicates whether or not the component is required
* or not. All components prior to any other required
* or not. All c$ git ls-files | xargs wc -lomponents prior to any other required
* component must also be required, such that the predicate
* ( c_i required)({c_0, ..., c_i-1} required) holds true,
* where {c_0, ..., c_n-1} is the set of command components.
@ -61,17 +61,27 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
* into the corresponding command type
*/
private final ComponentParser<C, T> parser;
/**
* Default value, will be empty if none was supplied
*/
private final String defaultValue;
private Command<C> owningCommand;
public CommandComponent(final boolean required, @Nonnull final String name,
@Nonnull final ComponentParser<C, T> parser) {
@Nonnull final ComponentParser<C, T> parser, @Nonnull final String defaultValue) {
this.required = required;
this.name = Objects.requireNonNull(name, "Name may not be null");
if (!NAME_PATTERN.asPredicate().test(name)) {
throw new IllegalArgumentException("Name must be alphanumeric");
}
this.parser = Objects.requireNonNull(parser, "Parser may not be null");
this.defaultValue = defaultValue;
}
public CommandComponent(final boolean required, @Nonnull final String name,
@Nonnull final ComponentParser<C, T> parser) {
this(required, name, parser, "");
}
/**
@ -179,6 +189,25 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
}
}
/**
* Get the default value
*
* @return Default value
*/
@Nonnull public String getDefaultValue() {
return this.defaultValue;
}
/**
* Check if the component has a default value
*
* @return {@code true} if the component has a default value, {@code false} if not
*/
public boolean hasDefaultValue() {
return !this.isRequired() &&
!this.getDefaultValue().isEmpty();
}
/**
* Mutable builder for {@link CommandComponent} instances
@ -191,6 +220,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
protected String name;
protected boolean required = true;
protected ComponentParser<C, T> parser;
protected String defaultValue = "";
protected Builder() {
}
@ -237,6 +267,23 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
return this;
}
/**
* Indicates that the component is optional.
* All components prior to any other required
* component must also be required, such that the predicate
* ( c_i required)({c_0, ..., c_i-1} required) holds true,
* where {c_0, ..., c_n-1} is the set of command components.
*
* @param defaultValue Default value that will be used if none was supplied
* @return Builder instance
*/
@Nonnull
public Builder<C, T> asOptionalWithDefault(@Nonnull final String defaultValue) {
this.defaultValue = defaultValue;
this.required = false;
return this;
}
/**
* Set the component parser
*
@ -256,7 +303,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
*/
@Nonnull
public CommandComponent<C, T> build() {
return new CommandComponent<>(this.required, this.name, this.parser);
return new CommandComponent<>(this.required, this.name, this.parser, this.defaultValue);
}
}

View file

@ -39,8 +39,8 @@ public class ByteComponent<C extends CommandSender> extends CommandComponent<C,
private final byte min;
private final byte max;
private ByteComponent(final boolean required, @Nonnull final String name, final byte min, final byte max) {
super(required, name, new ByteParser<>(min, max));
private ByteComponent(final boolean required, @Nonnull final String name, final byte min, final byte max, final String defaultValue) {
super(required, name, new ByteParser<>(min, max), defaultValue);
this.min = min;
this.max = max;
}
@ -81,7 +81,7 @@ public class ByteComponent<C extends CommandSender> extends CommandComponent<C,
@Nonnull
@Override
public ByteComponent<C> build() {
return new ByteComponent<>(this.required, this.name, this.min, this.max);
return new ByteComponent<>(this.required, this.name, this.min, this.max, this.defaultValue);
}
}
@ -153,12 +153,12 @@ public class ByteComponent<C extends CommandSender> extends CommandComponent<C,
@Override
public boolean hasMin() {
return this.getMin().byteValue() == Byte.MIN_VALUE;
return this.getMin().byteValue() != Byte.MIN_VALUE;
}
@Override
public boolean hasMax() {
return this.getMax().byteValue() == Byte.MAX_VALUE;
return this.getMax().byteValue() != Byte.MAX_VALUE;
}
@Override

View file

@ -39,8 +39,8 @@ public class IntegerComponent<C extends CommandSender> extends CommandComponent<
private final int min;
private final int max;
private IntegerComponent(final boolean required, @Nonnull final String name, final int min, final int max) {
super(required, name, new IntegerParser<>(min, max));
private IntegerComponent(final boolean required, @Nonnull final String name, final int min, final int max, final String defaultValue) {
super(required, name, new IntegerParser<>(min, max), defaultValue);
this.min = min;
this.max = max;
}
@ -57,6 +57,10 @@ public class IntegerComponent<C extends CommandSender> extends CommandComponent<
return IntegerComponent.<C>newBuilder().named(name).asOptional().build();
}
@Nonnull public static <C extends CommandSender> CommandComponent<C, Integer> optional(@Nonnull final String name, final int defaultNum) {
return IntegerComponent.<C>newBuilder().named(name).asOptionalWithDefault(Integer.toString(defaultNum)).build();
}
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, Integer> {
@ -76,7 +80,7 @@ public class IntegerComponent<C extends CommandSender> extends CommandComponent<
@Nonnull
@Override
public IntegerComponent<C> build() {
return new IntegerComponent<>(this.required, this.name, this.min, this.max);
return new IntegerComponent<>(this.required, this.name, this.min, this.max, this.defaultValue);
}
}
@ -143,12 +147,12 @@ public class IntegerComponent<C extends CommandSender> extends CommandComponent<
@Override
public boolean hasMin() {
return this.getMin().intValue() == Integer.MIN_VALUE;
return this.getMin().intValue() != Integer.MIN_VALUE;
}
@Override
public boolean hasMax() {
return this.getMax().intValue() == Integer.MAX_VALUE;
return this.getMax().intValue() != Integer.MAX_VALUE;
}
@Override

View file

@ -35,7 +35,7 @@ import java.util.Optional;
*
* @param <C> Command sender type
*/
public class CommandContext<C extends CommandSender> {
public final class CommandContext<C extends CommandSender> {
private final Map<String, Object> internalStorage = new HashMap<>();
private final C commandSender;

View file

@ -0,0 +1,44 @@
//
// MIT License
//
// Copyright (c) 2020 IntellectualSites
//
// 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 com.intellectualsites.commands.context;
import com.intellectualsites.commands.sender.CommandSender;
import javax.annotation.Nonnull;
/**
* Factory for {@link CommandContext} instances
*/
public interface CommandContextFactory<C extends CommandSender> {
/**
* Create a new command context
*
* @param sender Command sender
* @return Command context
*/
@Nonnull
CommandContext<C> create(@Nonnull C sender);
}

View file

@ -0,0 +1,38 @@
//
// MIT License
//
// Copyright (c) 2020 IntellectualSites
//
// 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 com.intellectualsites.commands.context;
import com.intellectualsites.commands.sender.CommandSender;
import javax.annotation.Nonnull;
public final class StandardCommandContextFactory<C extends CommandSender> implements CommandContextFactory<C> {
@Nonnull
@Override
public CommandContext<C> create(@Nonnull final C sender) {
return new CommandContext<>(sender);
}
}

View file

@ -24,6 +24,7 @@
package com.intellectualsites.commands;
import com.intellectualsites.commands.components.StaticComponent;
import com.intellectualsites.commands.components.standard.IntegerComponent;
import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.exceptions.NoPermissionException;
import com.intellectualsites.commands.sender.CommandSender;
@ -36,8 +37,6 @@ import java.util.Collections;
import java.util.LinkedList;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
class CommandTreeTest {
private static CommandManager<CommandSender> commandManager;
@ -48,7 +47,10 @@ class CommandTreeTest {
commandManager.registerCommand(commandManager.commandBuilder("test")
.withComponent(StaticComponent.required("one")).build())
.registerCommand(commandManager.commandBuilder("test")
.withComponent(StaticComponent.required("two")).withPermission("no").build());
.withComponent(StaticComponent.required("two")).withPermission("no").build())
.registerCommand(commandManager.commandBuilder("test")
.withComponent(StaticComponent.required("opt"))
.withComponent(IntegerComponent.optional("num", 15)).build());
}
@Test
@ -58,6 +60,10 @@ class CommandTreeTest {
Assertions.assertTrue(command.isPresent());
Assertions.assertThrows(NoPermissionException.class, () -> commandManager.getCommandTree().parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(
Arrays.asList("test", "two"))));
commandManager.getCommandTree().parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("test", "opt")))
.ifPresent(c -> c.getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender())));
commandManager.getCommandTree().parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("test", "opt", "12")))
.ifPresent(c -> c.getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender())));
}
@Test

View file

@ -8,7 +8,7 @@
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>commands-jline</artifactId>
<artifactId>cloud-jline</artifactId>
<dependencies>
<dependency>
<groupId>org.jline</groupId>
@ -17,7 +17,7 @@
</dependency>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>Commands</artifactId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

10
pom.xml
View file

@ -5,22 +5,22 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.intellectualsites</groupId>
<artifactId>Commands</artifactId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<!--<module>commands-jline</module>-->
<module>commands-bukkit</module>
<module>commands-core</module>
<module>cloud-bukkit</module>
<module>cloud-core</module>
</modules>
<packaging>pom</packaging>
<inceptionYear>2020</inceptionYear>
<licenses>
<license>
<name>MIT License</name>
<url>https://raw.githubusercontent.com/Sauilitired/Commands/master/LICENSE</url>
<url>https://raw.githubusercontent.com/Sauilitired/cloud/master/LICENSE</url>
</license>
</licenses>
<name>Commands</name>
<name>cloud</name>
<description>Commands for Java</description>
<properties>
<java.version>1.8</java.version>