✨ Improve syntax hinting for flags
This commit is contained in:
parent
f7bc31df27
commit
50e510e141
2 changed files with 49 additions and 4 deletions
|
|
@ -25,6 +25,8 @@ package cloud.commandframework.arguments;
|
||||||
|
|
||||||
import cloud.commandframework.CommandTree;
|
import cloud.commandframework.CommandTree;
|
||||||
import cloud.commandframework.arguments.compound.CompoundArgument;
|
import cloud.commandframework.arguments.compound.CompoundArgument;
|
||||||
|
import cloud.commandframework.arguments.compound.FlagArgument;
|
||||||
|
import cloud.commandframework.arguments.flags.CommandFlag;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
|
|
@ -66,10 +68,27 @@ public class StandardCommandSyntaxFormatter<C> implements CommandSyntaxFormatter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stringBuilder.append(suffix);
|
stringBuilder.append(suffix);
|
||||||
} else if (commandArgument.isRequired()) {
|
|
||||||
stringBuilder.append("<").append(commandArgument.getName()).append(">");
|
|
||||||
} else {
|
} else {
|
||||||
stringBuilder.append("[").append(commandArgument.getName()).append("]");
|
String name = commandArgument.getName();
|
||||||
|
|
||||||
|
if (commandArgument instanceof FlagArgument) {
|
||||||
|
final StringBuilder flagBuilder = new StringBuilder();
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
final Iterator<CommandFlag<?>> flagIterator = ((FlagArgument<C>) commandArgument).getFlags().iterator();
|
||||||
|
while (flagIterator.hasNext()) {
|
||||||
|
flagBuilder.append("--").append(flagIterator.next().getName());
|
||||||
|
if (flagIterator.hasNext()) {
|
||||||
|
flagBuilder.append(" | ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
name = flagBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commandArgument.isRequired()) {
|
||||||
|
stringBuilder.append("<").append(name).append(">");
|
||||||
|
} else {
|
||||||
|
stringBuilder.append("[").append(name).append("]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (iterator.hasNext()) {
|
if (iterator.hasNext()) {
|
||||||
|
|
@ -115,6 +134,20 @@ public class StandardCommandSyntaxFormatter<C> implements CommandSyntaxFormatter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stringBuilder.append(suffix);
|
stringBuilder.append(suffix);
|
||||||
|
} else if (argument instanceof FlagArgument) {
|
||||||
|
final StringBuilder flagBuilder = new StringBuilder();
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
final Iterator<CommandFlag<?>> flagIterator = ((FlagArgument<C>) argument).getFlags().iterator();
|
||||||
|
while (flagIterator.hasNext()) {
|
||||||
|
flagBuilder.append("--").append(flagIterator.next().getName());
|
||||||
|
if (flagIterator.hasNext()) {
|
||||||
|
flagBuilder.append(" | ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stringBuilder.append(" ")
|
||||||
|
.append(prefix)
|
||||||
|
.append(flagBuilder)
|
||||||
|
.append(suffix);
|
||||||
} else {
|
} else {
|
||||||
stringBuilder.append(" ")
|
stringBuilder.append(" ")
|
||||||
.append(prefix)
|
.append(prefix)
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ import java.util.function.BiFunction;
|
||||||
*
|
*
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
*/
|
*/
|
||||||
public class FlagArgument<C> extends CommandArgument<C, Object> {
|
public final class FlagArgument<C> extends CommandArgument<C, Object> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dummy object that indicates that flags were parsed successfully
|
* Dummy object that indicates that flags were parsed successfully
|
||||||
|
|
@ -58,6 +58,7 @@ public class FlagArgument<C> extends CommandArgument<C, Object> {
|
||||||
|
|
||||||
private static final String FLAG_ARGUMENT_NAME = "flags";
|
private static final String FLAG_ARGUMENT_NAME = "flags";
|
||||||
|
|
||||||
|
private final Collection<@NonNull CommandFlag<?>> flags;
|
||||||
/**
|
/**
|
||||||
* Construct a new flag argument
|
* Construct a new flag argument
|
||||||
*
|
*
|
||||||
|
|
@ -68,8 +69,19 @@ public class FlagArgument<C> extends CommandArgument<C, Object> {
|
||||||
FLAG_ARGUMENT_NAME,
|
FLAG_ARGUMENT_NAME,
|
||||||
new FlagArgumentParser<>(flags.toArray(new CommandFlag<?>[0])),
|
new FlagArgumentParser<>(flags.toArray(new CommandFlag<?>[0])),
|
||||||
Object.class);
|
Object.class);
|
||||||
|
this.flags = flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the flags registered in the argument
|
||||||
|
*
|
||||||
|
* @return Unmodifiable view of flags
|
||||||
|
*/
|
||||||
|
public @NonNull Collection<@NonNull CommandFlag<?>> getFlags() {
|
||||||
|
return Collections.unmodifiableCollection(this.flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static final class FlagArgumentParser<C> implements ArgumentParser<C, Object> {
|
public static final class FlagArgumentParser<C> implements ArgumentParser<C, Object> {
|
||||||
|
|
||||||
private final CommandFlag<?>[] flags;
|
private final CommandFlag<?>[] flags;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue