mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-05 15:26:02 +00:00
fix child command issues
This commit is contained in:
@@ -6,9 +6,10 @@ import com.dfsek.terra.api.command.ExecutionState;
|
||||
import com.dfsek.terra.api.command.TerraCommandManager;
|
||||
import com.dfsek.terra.api.command.annotation.Argument;
|
||||
import com.dfsek.terra.api.command.annotation.Command;
|
||||
import com.dfsek.terra.api.command.annotation.Subcommand;
|
||||
import com.dfsek.terra.api.command.exception.CommandException;
|
||||
import com.dfsek.terra.api.command.exception.InvalidArgumentsException;
|
||||
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
||||
import com.dfsek.terra.commands.StructureCommand;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -19,9 +20,14 @@ public class CommandTest {
|
||||
@Test
|
||||
public void subcommand() throws CommandException {
|
||||
CommandManager manager = new TerraCommandManager();
|
||||
manager.register("structure", StructureCommand.class);
|
||||
manager.register("test", DemoParentCommand.class);
|
||||
|
||||
manager.execute("structure", Arrays.asList("export"));
|
||||
manager.execute("test", Arrays.asList("subcommand1", "first", "2"));
|
||||
manager.execute("test", Arrays.asList("subcommand2", "first", "2"));
|
||||
manager.execute("test", Arrays.asList("s1", "first", "2", "3.4"));
|
||||
manager.execute("test", Arrays.asList("s2", "first", "2"));
|
||||
manager.execute("test", Arrays.asList("sub1", "first", "2", "3.4"));
|
||||
manager.execute("test", Arrays.asList("sub2", "first", "2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -33,6 +39,18 @@ public class CommandTest {
|
||||
manager.execute("test", Arrays.asList("first", "2", "3.4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void argsBeforeFlags() throws CommandException {
|
||||
CommandManager manager = new TerraCommandManager();
|
||||
manager.register("test", DemoCommand.class);
|
||||
|
||||
try {
|
||||
manager.execute("test", Arrays.asList("first", "-flag", "2"));
|
||||
fail();
|
||||
} catch(InvalidArgumentsException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requiredArgsFirst() throws CommandException {
|
||||
CommandManager manager = new TerraCommandManager();
|
||||
@@ -45,30 +63,32 @@ public class CommandTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Command(arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg1", type = int.class),
|
||||
@Argument(value = "arg2", type = double.class, required = false)
|
||||
})
|
||||
@Command(
|
||||
arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg1", type = int.class),
|
||||
@Argument(value = "arg2", type = double.class, required = false)
|
||||
})
|
||||
public static final class DemoCommand implements CommandTemplate {
|
||||
|
||||
@Override
|
||||
public void execute(ExecutionState state) {
|
||||
System.out.println(state.getArgument("arg0", String.class));
|
||||
System.out.println(state.getArgument("arg1", String.class));
|
||||
System.out.println(state.getArgument("arg1", int.class));
|
||||
try {
|
||||
System.out.println(state.getArgument("arg2", String.class));
|
||||
System.out.println(state.getArgument("arg2", double.class));
|
||||
} catch(IllegalArgumentException e) {
|
||||
System.out.println("arg2 undefined.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Command(arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg2", type = double.class, required = false), // optional arguments must be last. this command is invalid.
|
||||
@Argument(value = "arg1", type = int.class)
|
||||
})
|
||||
@Command(
|
||||
arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg2", type = double.class, required = false), // optional arguments must be last. this command is invalid.
|
||||
@Argument(value = "arg1", type = int.class)
|
||||
})
|
||||
public static final class DemoInvalidCommand implements CommandTemplate {
|
||||
|
||||
@Override
|
||||
@@ -76,4 +96,57 @@ public class CommandTest {
|
||||
throw new Error("this should never be reached");
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg1", type = int.class),
|
||||
@Argument(value = "arg2", type = double.class, required = false),
|
||||
},
|
||||
subcommands = {
|
||||
@Subcommand(
|
||||
value = "subcommand1",
|
||||
aliases = {"s1", "sub1"},
|
||||
clazz = DemoChildCommand.class
|
||||
),
|
||||
@Subcommand(
|
||||
value = "subcommand2",
|
||||
aliases = {"s2", "sub2"},
|
||||
clazz = DemoChildCommand.class // Duplicate command intentional.
|
||||
)
|
||||
})
|
||||
public static final class DemoParentCommand implements CommandTemplate {
|
||||
|
||||
@Override
|
||||
public void execute(ExecutionState state) {
|
||||
System.out.println("Parent command");
|
||||
System.out.println(state.getArgument("arg0", String.class));
|
||||
System.out.println(state.getArgument("arg1", int.class));
|
||||
try {
|
||||
System.out.println(state.getArgument("arg2", double.class));
|
||||
} catch(IllegalArgumentException e) {
|
||||
System.out.println("arg2 undefined.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg1", type = int.class),
|
||||
@Argument(value = "arg2", type = double.class, required = false),
|
||||
})
|
||||
public static final class DemoChildCommand implements CommandTemplate {
|
||||
@Override
|
||||
public void execute(ExecutionState state) {
|
||||
System.out.println("Child command");
|
||||
System.out.println(state.getArgument("arg0", String.class));
|
||||
System.out.println(state.getArgument("arg1", int.class));
|
||||
try {
|
||||
System.out.println(state.getArgument("arg2", double.class));
|
||||
} catch(IllegalArgumentException e) {
|
||||
System.out.println("arg2 undefined.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user