Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,20 @@ private String unquoteAndUnescapeQuoted(String s) {
}

private boolean isBooleanOption(String commandName, String currentWord) {
return Optional.ofNullable(commandRegistry.getCommandByName(commandName))
List<CommandOption> declaredOptions = Optional.ofNullable(commandRegistry.getCommandByName(commandName))
.map(Command::getOptions)
.orElse(List.of())
.stream()
.filter(o -> o.isOptionEqual(currentWord))
.anyMatch(o -> o.type() == boolean.class || o.type() == Boolean.class);
.toList();
if (declaredOptions.isEmpty()) {
return isHelpOption(currentWord);
}
return declaredOptions.stream().anyMatch(o -> o.type() == boolean.class || o.type() == Boolean.class);
}

private boolean isHelpOption(String word) {
return word.equals("--help") || word.equals("-h");
}

private boolean isBooleanValue(String rawValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,25 @@ void testParseWithHelpOption(String input) {
assertEquals("true", parsedInput.options().get(0).value());
}

@ParameterizedTest
@ValueSource(strings = { "mycommand mysubcommand --help", "mycommand mysubcommand -h",
"mycommand --help arg1 --option value1", "mycommand -h arg1" })
void testParseWithImplicitHelpOption(String input) {
// given
Command command = createCommand("mycommand", "My test command");
command.getOptions().add(CommandOption.with().longName("option").shortName('o').build());
commandRegistry.registerCommand(command);
Command subCommand = createCommand("mycommand mysubcommand", "My test sub command");
subCommand.getOptions().add(CommandOption.with().longName("option").shortName('o').build());
commandRegistry.registerCommand(subCommand);

// when
ParsedInput parsedInput = parser.parse(input);

// then
assertEquals("true", parsedInput.options().get(0).value());
}

private static Command createCommand(String name, String description) {
return new AbstractCommand(name, description) {
@Override
Expand Down
Loading