🐛 Fix multiple chained optionals not working correctly (#73)

* 🐛 Fix multiple chained optionals not working correctly

There is a problem where the child arguments never forwarded their commands correctly. This will now fix itself when necessary.

* 📚 Add CHANGELOG entry for change
This commit is contained in:
Alexander Söderberg 2020-10-18 21:36:58 +02:00 committed by GitHub
parent d6cdeca1c3
commit 6c63b47e5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View file

@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixes
- Fixes chained optionals not allowing the command to be executed when more than one optional is omitted
## [1.0.1] - 2020-10-14

View file

@ -287,7 +287,20 @@ public final class CommandTree<C> {
if (child.getValue().hasDefaultValue()) {
commandQueue.add(child.getValue().getDefaultValue());
} else if (!child.getValue().isRequired()) {
return Pair.of(this.cast(child.getValue().getOwningCommand()), null);
if (child.getValue().getOwningCommand() == null) {
/*
* If there are multiple children with different owning commands then it's ambiguous and
* not allowed, therefore we're able to pick any child command, as long as we can find it
*/
Node<CommandArgument<C, ?>> node = child;
while (!node.isLeaf()) {
node = node.getChildren().get(0);
if (node.getValue() != null && node.getValue().getOwningCommand() != null) {
child.getValue().setOwningCommand(node.getValue().getOwningCommand());
}
}
}
return Pair.of(child.getValue().getOwningCommand(), null);
} else if (child.isLeaf()) {
if (root.getValue() != null && root.getValue().getOwningCommand() != null) {
final Command<C> command = root.getValue().getOwningCommand();

View file

@ -143,6 +143,13 @@ class CommandTreeTest {
.addPreprocessor(RegexPreprocessor.of("[A-Za-z]{3,5}"))
)
);
/* Build command for testing multiple optionals */
manager.command(
manager.commandBuilder("optionals")
.argument(StringArgument.optional("opt1"))
.argument(StringArgument.optional("opt2"))
);
}
@Test
@ -301,6 +308,11 @@ class CommandTreeTest {
);
}
@Test
void testOptionals() {
manager.executeCommand(new TestCommandSender(), "optionals").join();
}
public static final class SpecificCommandSender extends TestCommandSender {