Command Arguments

Below is a calculator command.

@Commander(
        name = "calculate",
        description = "test calculate command",
        alias = {"cal", "c"}
)
public class TestCalculateCommand implements CommandNode {

    @Override
    public void execute(CommandSender commandSender) {
    }
}
@Commander(
        name = "add",
        description = "calculate add command",
        alias = {"ad", "plus", "a"}
)
public class TestCalculateAddCommand implements CommandNode {

    @CommandArg(order = 0)
    private int one;

    @CommandArg(order = 1)
    private int two;

    @Override
    public void execute(CommandSender commandSender) {
        commandSender.sendMessage(one+" + "+two+" = "+(one + two));
    }
}

Well you should know they are plus calculation and minus calculation commands, accept with two integer arguments. The help message will show as below:

  • /calculate add <one> <two>

  • /calculate minus <one> <two>

order decided the order of arguments.

Now, we try edit something in @CommandArg

The help message will now show as below:

  • /calculate add <first value> [second value]

  • /calculate minus <first value> [second value]

<arg> is a compulsory argument while [arg] is an optional argument (optional = true). you can see that property two has been assigned as 22, so when command sender didn't input the second value, the default value of second value will be 22.

Remain Arguments

After parsing declared command arguments, the remaining arguments will be ignored. If you want to take them, just declare a variable with type List<String> and annotating with @RemainArgs

when you input /calculate add 1 1 a b c d e the output result will be

1 + 1 = 2

remainArgs: [a, b, c, d, e]

最后更新于