use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.
the class ChildCommandsTest method testSimpleChildCommandIsSuppressedOnError.
@Test
public void testSimpleChildCommandIsSuppressedOnError() throws CommandException {
final AtomicBoolean parentExecuted = new AtomicBoolean();
final AtomicBoolean childExecuted = new AtomicBoolean();
final CommandSpec spec = CommandSpec.builder().children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder().arguments(GenericArguments.literal(Text.of("test"), "test")).executor((src, args) -> {
childExecuted.set(true);
return CommandResult.builder().successCount(1).build();
}).build())).arguments(GenericArguments.literal(Text.of("t"), "child")).executor((src, args) -> {
parentExecuted.set(true);
return CommandResult.success();
}).build();
final SimpleDispatcher execute = new SimpleDispatcher();
execute.register(spec, "parent");
execute.process(mock(CommandSource.class), "parent child");
assertFalse(childExecuted.get());
assertTrue(parentExecuted.get());
}
use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.
the class ChildCommandsTest method testErrorOnNonExistentChildWithNoOtherParameters.
@Test
public void testErrorOnNonExistentChildWithNoOtherParameters() throws CommandException {
final CommandSpec spec = CommandSpec.builder().children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder().executor((src, args) -> CommandResult.builder().successCount(1).build()).build())).childArgumentParseExceptionFallback(false).executor((src, args) -> CommandResult.success()).build();
final SimpleDispatcher execute = new SimpleDispatcher();
execute.register(spec, "parent");
try {
execute.process(mock(CommandSource.class), "parent wrong");
} catch (ArgumentParseException ex) {
assertEquals("Input command wrong was not a valid subcommand!\nwrong\n^", ex.getMessage());
}
}
use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.
the class ChildCommandsTest method testEmptyChildrenWorksWithArgument.
@Test
public void testEmptyChildrenWorksWithArgument() throws CommandException {
final AtomicBoolean parent = new AtomicBoolean();
final CommandSpec spec = CommandSpec.builder().arguments(GenericArguments.optional(GenericArguments.string(Text.of("a")))).children(ImmutableMap.<List<String>, CommandSpec>of()).executor((s, c) -> {
parent.set(true);
return CommandResult.success();
}).build();
final SimpleDispatcher execute = new SimpleDispatcher();
execute.register(spec, "emptyparentwith");
execute.process(mock(CommandSource.class), "emptyparentwith child");
assertTrue(parent.get());
}
use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.
the class CommandFlagsTest method testFlaggedCommand.
@Test
public void testFlaggedCommand() throws CommandException {
CommandSpec command = CommandSpec.builder().arguments(flags().flag("a").valueFlag(integer(t("quot")), "q").buildWith(string(t("key")))).executor((src, args) -> {
assertEquals(true, args.getOne("a").get());
assertEquals(42, args.getOne("quot").get());
assertEquals("something", args.getOne("key").get());
return CommandResult.builder().successCount(3).build();
}).build();
process(command, "-a -q 42 something");
process(command, "-aq 42 something");
process(command, "-a something -q 42");
}
use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.
the class ChildCommandElementExecutor method parse.
@Override
public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException {
if (this.fallbackExecutor != null && !args.hasNext()) {
if (this.fallbackElements != null) {
// there might be optionals to take account of that would parse this successfully.
this.fallbackElements.parse(source, args, context);
}
// execute the fallback regardless in this scenario.
return;
}
Object state = args.getState();
final String key = args.next();
Optional<CommandMapping> optionalCommandMapping = this.dispatcher.get(key, source);
if (optionalCommandMapping.isPresent()) {
final CommandMapping mapping = optionalCommandMapping.get();
try {
if ((mapping.getCallable() instanceof CommandSpec)) {
CommandSpec spec = ((CommandSpec) mapping.getCallable());
spec.populateContext(source, args, context);
} else {
if (args.hasNext()) {
args.next();
}
context.putArg(getUntranslatedKey() + "_args", args.getRaw().substring(args.getRawPosition()));
while (args.hasNext()) {
args.next();
}
}
// Success, add to context now so that we don't execute the wrong executor in the first place.
context.putArg(getUntranslatedKey(), mapping);
} catch (ArgumentParseException ex) {
// If we get here, fallback to the elements, if they exist.
args.setState(state);
if (this.fallbackOnFail && this.fallbackElements != null) {
this.fallbackElements.parse(source, args, context);
return;
}
// Get the usage
args.next();
if (ex instanceof ArgumentParseException.WithUsage) {
// This indicates a previous child failed, so we just prepend our child
throw new ArgumentParseException.WithUsage(ex, Text.of(key, " ", ((ArgumentParseException.WithUsage) ex).getUsage()));
}
throw new ArgumentParseException.WithUsage(ex, Text.of(key, " ", mapping.getCallable().getUsage(source)));
}
} else {
// Not a child, so let's continue with the fallback.
if (this.fallbackExecutor != null && this.fallbackElements != null) {
args.setState(state);
this.fallbackElements.parse(source, args, context);
} else {
// so specifying it implicitly means we have a child command to execute.
throw args.createError(t("Input command %s was not a valid subcommand!", key));
}
}
}
Aggregations