Fix quoted strings

This commit is contained in:
Alexander Söderberg 2020-09-22 18:45:26 +02:00
parent dbd546dd20
commit a6db68fa66
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
3 changed files with 139 additions and 8 deletions

View file

@ -63,7 +63,7 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
} }
/** /**
* Create a new required command argument * Create a new required single string command argument
* *
* @param name Argument name * @param name Argument name
* @param <C> Command sender type * @param <C> Command sender type
@ -71,11 +71,24 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
*/ */
@Nonnull @Nonnull
public static <C> CommandArgument<C, String> required(@Nonnull final String name) { public static <C> CommandArgument<C, String> required(@Nonnull final String name) {
return StringArgument.<C>newBuilder(name).asRequired().build(); return StringArgument.<C>newBuilder(name).single().asRequired().build();
} }
/** /**
* Create a new optional command argument * Create a new required command argument
*
* @param name Argument name
* @param stringMode String mode
* @param <C> Command sender type
* @return Created argument
*/
@Nonnull
public static <C> CommandArgument<C, String> required(@Nonnull final String name, @Nonnull final StringMode stringMode) {
return StringArgument.<C>newBuilder(name).withMode(stringMode).asRequired().build();
}
/**
* Create a new optional single string command argument
* *
* @param name Argument name * @param name Argument name
* @param <C> Command sender type * @param <C> Command sender type
@ -83,7 +96,20 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
*/ */
@Nonnull @Nonnull
public static <C> CommandArgument<C, String> optional(@Nonnull final String name) { public static <C> CommandArgument<C, String> optional(@Nonnull final String name) {
return StringArgument.<C>newBuilder(name).asOptional().build(); return StringArgument.<C>newBuilder(name).single().asOptional().build();
}
/**
* Create a new optional command argument
*
* @param name Argument name
* @param stringMode String mode
* @param <C> Command sender type
* @return Created argument
*/
@Nonnull
public static <C> CommandArgument<C, String> optional(@Nonnull final String name, @Nonnull final StringMode stringMode) {
return StringArgument.<C>newBuilder(name).withMode(stringMode).asOptional().build();
} }
/** /**
@ -127,6 +153,12 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
super(String.class, name); super(String.class, name);
} }
@Nonnull
private Builder<C> withMode(@Nonnull final StringMode stringMode) {
this.stringMode = stringMode;
return this;
}
/** /**
* Set the string mode to greedy * Set the string mode to greedy
* *
@ -234,7 +266,7 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
boolean finished = false; boolean finished = false;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
final String string = inputQueue.peek(); String string = inputQueue.peek();
if (string == null) { if (string == null) {
break; break;
@ -243,7 +275,7 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
if (this.stringMode == StringMode.QUOTED) { if (this.stringMode == StringMode.QUOTED) {
if (!started) { if (!started) {
if (string.startsWith("\"")) { if (string.startsWith("\"")) {
sj.add(string.substring(1)); string = string.substring(1);
started = true; started = true;
} else { } else {
return ArgumentParseResult.failure(new StringParseException(string, StringMode.QUOTED)); return ArgumentParseResult.failure(new StringParseException(string, StringMode.QUOTED));
@ -256,7 +288,8 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
} }
} }
sj.add(inputQueue.remove()); sj.add(string);
inputQueue.remove();
} }
if (this.stringMode == StringMode.QUOTED && (!started || !finished)) { if (this.stringMode == StringMode.QUOTED && (!started || !finished)) {

View file

@ -31,7 +31,10 @@ import javax.annotation.Nonnull;
public class TestCommandManager extends CommandManager<TestCommandSender> { public class TestCommandManager extends CommandManager<TestCommandSender> {
protected TestCommandManager() { /**
* Construct a new test command manager
*/
public TestCommandManager() {
super(CommandExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler()); super(CommandExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler());
} }

View file

@ -0,0 +1,95 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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.arguments.standard;
import com.intellectualsites.commands.CommandManager;
import com.intellectualsites.commands.TestCommandManager;
import com.intellectualsites.commands.TestCommandSender;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class StringArgumentTest {
private static final String[] storage = new String[2];
private static CommandManager<TestCommandSender> manager;
@BeforeAll
static void setup() {
manager = new TestCommandManager();
manager.command(manager.commandBuilder("quoted")
.argument(StringArgument.required("message1", StringArgument.StringMode.QUOTED))
.argument(StringArgument.required("message2"))
.handler(c -> {
final String message1 = c.getRequired("message1");
final String message2 = c.getRequired("message2");
storage[0] = message1;
storage[1] = message2;
})
.build());
manager.command(manager.commandBuilder("single")
.argument(StringArgument.required("message"))
.handler(c -> {
final String message = c.getRequired("message");
storage[0] = message;
})
.build());
manager.command(manager.commandBuilder("greedy")
.argument(StringArgument.required("message", StringArgument.StringMode.GREEDY))
.handler(c -> {
final String message = c.getRequired("message");
storage[0] = message;
})
.build());
}
private static void clear() {
storage[0] = storage[1] = null;
}
@Test
void testSingle() {
clear();
manager.executeCommand(new TestCommandSender(), "single string");
Assertions.assertEquals("string", storage[0]);
}
@Test
void testQuotes() {
clear();
manager.executeCommand(new TestCommandSender(), "quoted \"quoted string\" unquoted").join();
Assertions.assertEquals("quoted string", storage[0]);
Assertions.assertEquals("unquoted", storage[1]);
}
@Test
void testGreedy() {
clear();
manager.executeCommand(new TestCommandSender(), "greedy greedy string content").join();
Assertions.assertEquals("greedy string content", storage[0]);
}
}