Do not show full help when providing an invalid option

Closes #9956
This commit is contained in:
Dominik Guhr 2022-02-03 11:25:54 +01:00 committed by Stian Thorgersen
parent 0471ec4941
commit 0f3293cf00
3 changed files with 41 additions and 6 deletions

View file

@ -282,7 +282,7 @@ public final class Picocli {
CommandLine cmd = new CommandLine(spec);
cmd.setExecutionExceptionHandler(new ExecutionExceptionHandler());
cmd.setParameterExceptionHandler(new ShortErrorMessageHandler());
cmd.setHelpFactory(new HelpFactory());
cmd.getHelpSectionMap().put(SECTION_KEY_COMMAND_LIST, new SubCommandListRenderer());
cmd.setErr(new PrintWriter(System.err, true));

View file

@ -0,0 +1,28 @@
package org.keycloak.quarkus.runtime.cli;
import picocli.CommandLine;
import picocli.CommandLine.IParameterExceptionHandler;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.UnmatchedArgumentException;
import picocli.CommandLine.Help;
import picocli.CommandLine.Model.CommandSpec;
import java.io.PrintWriter;
public class ShortErrorMessageHandler implements IParameterExceptionHandler {
public int handleParseException(ParameterException ex, String[] args) {
CommandLine cmd = ex.getCommandLine();
PrintWriter writer = cmd.getErr();
writer.println(cmd.getColorScheme().errorText(ex.getMessage()));
UnmatchedArgumentException.printSuggestions(ex, writer);
CommandSpec spec = cmd.getCommandSpec();
writer.printf("Try '%s --help' for more information on the available options.%n", spec.qualifiedName());
return cmd.getExitCodeExceptionMapper() != null
? cmd.getExitCodeExceptionMapper().getExitCode(ex)
: spec.exitCodeOnInvalidInput();
}
}

View file

@ -17,14 +17,14 @@
package org.keycloak.it.cli;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.CLITest;
import io.quarkus.test.junit.main.Launch;
import io.quarkus.test.junit.main.LaunchResult;
import io.quarkus.test.junit.main.QuarkusMainLauncher;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@CLITest
public class OptionValidationTest {
@ -32,12 +32,19 @@ public class OptionValidationTest {
@Test
@Launch({"build", "--db"})
public void failMissingOptionValue(LaunchResult result) {
Assertions.assertTrue(result.getErrorOutput().contains("Missing required value for option '--db' (vendor). Expected values are: h2-file, h2-mem, mariadb, mssql, mysql, oracle, postgres"));
assertTrue(result.getErrorOutput().contains("Missing required value for option '--db' (vendor). Expected values are: h2-file, h2-mem, mariadb, mssql, mysql, oracle, postgres"));
}
@Test
@Launch({"build", "--db", "foo", "bar"})
public void failMultipleOptionValue(LaunchResult result) {
Assertions.assertTrue(result.getErrorOutput().contains("Option '--db' expects a single value (vendor) Expected values are: h2-file, h2-mem, mariadb, mssql, mysql, oracle, postgres"));
assertTrue(result.getErrorOutput().contains("Option '--db' expects a single value (vendor) Expected values are: h2-file, h2-mem, mariadb, mssql, mysql, oracle, postgres"));
}
@Test
@Launch({"build", "--nosuch"})
public void failUnknownOption(LaunchResult result) {
assertEquals("Unknown option: '--nosuch'\n" +
"Try 'kc.sh build --help' for more information on the available options.", result.getErrorOutput());
}
}