How to properly add a flag to command args?

Hey. I feel a bit stupid asking this, but I can’t find any good examples of how to add an optional flag to the rest of your command arguments.
Currently, I have this CommandSpec:

CommandSpec banSpec = CommandSpec.builder()
                .arguments(
                        GenericArguments.onlyOne(GenericArguments.string(Text.of("player"))),
                        GenericArguments.optional(GenericArguments.remainingJoinedStrings(Text.of("reason"))),
                        GenericArguments.flags().flag("-ip", "i").buildWith(GenericArguments.none()))
                .executor(new Command())
                .build();

The expected behaviour: if “-i” is specified in the command arguments, args.hasAny("i") will return true. This doesn’t happen. Thanks for your help.

The buildWith needs to wrap your other arguments. So:

CommandSpec banSpec = CommandSpec.builder()
                .arguments(
                    GenericArguments.flags().flag("-ip", "i").buildWith(
                        GenericArguments.onlyOne(GenericArguments.string(Text.of("player"))),
                        GenericArguments.optional(GenericArguments.remainingJoinedStrings(Text.of("reason"))))
                .executor(new Command())
                .build();
1 Like

Thaaanks, but… buildWith has only one argument, thus meaning I can put only one CommandElement here. How to put multiple in?

Oh nevermind, figured it out by myself, thanks to other plugins examples. GenericArguments.seq() has CommandElement varargs, so you can put multiple elements.

Ahh, yes, I did forget that, glad you figured it out though! :slight_smile:

1 Like