diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/Picocli.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/Picocli.java index 381db8e893..398ea59061 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/Picocli.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/Picocli.java @@ -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)); diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/ShortErrorMessageHandler.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/ShortErrorMessageHandler.java new file mode 100644 index 0000000000..8cafd4c693 --- /dev/null +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/ShortErrorMessageHandler.java @@ -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(); + } +} diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/OptionValidationTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/OptionValidationTest.java index 7ef4e18d35..14785de1c9 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/OptionValidationTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/OptionValidationTest.java @@ -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()); } }