Change usage text of redirects based on if it's root or not
This commit is contained in:
parent
a67b4b51b0
commit
2d7644dce3
2 changed files with 21 additions and 4 deletions
|
@ -32,7 +32,7 @@ public class CommandDispatcher<S> {
|
||||||
private static final String USAGE_REQUIRED_CLOSE = ")";
|
private static final String USAGE_REQUIRED_CLOSE = ")";
|
||||||
private static final String USAGE_OR = "|";
|
private static final String USAGE_OR = "|";
|
||||||
|
|
||||||
private final RootCommandNode<S> root = new RootCommandNode<>();
|
private final RootCommandNode<S> root;
|
||||||
private final Predicate<CommandNode<S>> hasCommand = new Predicate<CommandNode<S>>() {
|
private final Predicate<CommandNode<S>> hasCommand = new Predicate<CommandNode<S>>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean test(final CommandNode<S> input) {
|
public boolean test(final CommandNode<S> input) {
|
||||||
|
@ -40,9 +40,18 @@ public class CommandDispatcher<S> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public void register(final LiteralArgumentBuilder<S> command) {
|
public CommandDispatcher(final RootCommandNode<S> root) {
|
||||||
|
this.root = root;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandDispatcher() {
|
||||||
|
this(new RootCommandNode<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public LiteralCommandNode<S> register(final LiteralArgumentBuilder<S> command) {
|
||||||
final LiteralCommandNode<S> build = command.build();
|
final LiteralCommandNode<S> build = command.build();
|
||||||
root.addChild(build);
|
root.addChild(build);
|
||||||
|
return build;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int execute(final String input, final S source) throws CommandException {
|
public int execute(final String input, final S source) throws CommandException {
|
||||||
|
@ -126,7 +135,8 @@ public class CommandDispatcher<S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.getRedirect() != null) {
|
if (node.getRedirect() != null) {
|
||||||
result.add(prefix.isEmpty() ? node.getUsageText() + ARGUMENT_SEPARATOR + "..." : prefix + ARGUMENT_SEPARATOR + "...");
|
final String redirect = node.getRedirect() == root ? "..." : "-> " + node.getRedirect().getUsageText();
|
||||||
|
result.add(prefix.isEmpty() ? node.getUsageText() + ARGUMENT_SEPARATOR + redirect : prefix + ARGUMENT_SEPARATOR + redirect);
|
||||||
} else if (!node.getChildren().isEmpty()) {
|
} else if (!node.getChildren().isEmpty()) {
|
||||||
for (final CommandNode<S> child : node.getChildren()) {
|
for (final CommandNode<S> child : node.getChildren()) {
|
||||||
getAllUsage(child, source, result, prefix.isEmpty() ? child.getUsageText() : prefix + ARGUMENT_SEPARATOR + child.getUsageText());
|
getAllUsage(child, source, result, prefix.isEmpty() ? child.getUsageText() : prefix + ARGUMENT_SEPARATOR + child.getUsageText());
|
||||||
|
@ -159,7 +169,8 @@ public class CommandDispatcher<S> {
|
||||||
|
|
||||||
if (!deep) {
|
if (!deep) {
|
||||||
if (node.getRedirect() != null) {
|
if (node.getRedirect() != null) {
|
||||||
return self + ARGUMENT_SEPARATOR + "...";
|
final String redirect = node.getRedirect() == root ? "..." : "-> " + node.getRedirect().getUsageText();
|
||||||
|
return self + ARGUMENT_SEPARATOR + redirect;
|
||||||
} else {
|
} else {
|
||||||
final Collection<CommandNode<S>> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList());
|
final Collection<CommandNode<S>> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList());
|
||||||
if (children.size() == 1) {
|
if (children.size() == 1) {
|
||||||
|
|
|
@ -91,6 +91,10 @@ public class CommandDispatcherUsagesTest {
|
||||||
literal("j")
|
literal("j")
|
||||||
.redirect(subject.getRoot())
|
.redirect(subject.getRoot())
|
||||||
);
|
);
|
||||||
|
subject.register(
|
||||||
|
literal("k")
|
||||||
|
.redirect(get("h"))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CommandNode<Object> get(final String command) {
|
private CommandNode<Object> get(final String command) {
|
||||||
|
@ -141,6 +145,7 @@ public class CommandDispatcherUsagesTest {
|
||||||
"i 1",
|
"i 1",
|
||||||
"i 2",
|
"i 2",
|
||||||
"j ...",
|
"j ...",
|
||||||
|
"k -> h",
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,6 +162,7 @@ public class CommandDispatcherUsagesTest {
|
||||||
.put(get("h"), "h [1|2|3]")
|
.put(get("h"), "h [1|2|3]")
|
||||||
.put(get("i"), "i [1|2]")
|
.put(get("i"), "i [1|2]")
|
||||||
.put(get("j"), "j ...")
|
.put(get("j"), "j ...")
|
||||||
|
.put(get("k"), "k -> h")
|
||||||
.build()
|
.build()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue