Skip to content

Commit

Permalink
fix(cli): print help on missing parameters
Browse files Browse the repository at this point in the history
Missing required positional parameters caused just a stacktrace print.

Now such cases print the error message acompanied by command's help.

Examples:

* `./kestra flow namespace update`
* `./kestra flow dot`
  • Loading branch information
yuri1969 authored and loicmathieu committed Feb 3, 2025
1 parent 6251359 commit ae05de4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cli/src/main/java/io/kestra/cli/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ protected static ApplicationContext applicationContext(Class<?> mainClass,
.environments(Environment.CLI);

CommandLine cmd = new CommandLine(mainClass, CommandLine.defaultFactory());
continueOnParsingErrors(cmd);

CommandLine.ParseResult parseResult = cmd.parseArgs(args);
List<CommandLine> parsedCommands = parseResult.asCommandLineList();

CommandLine commandLine = parsedCommands.get(parsedCommands.size() - 1);
CommandLine commandLine = parsedCommands.getLast();
Class<?> cls = commandLine.getCommandSpec().userObject().getClass();

if (AbstractCommand.class.isAssignableFrom(cls)) {
Expand All @@ -114,15 +115,17 @@ protected static ApplicationContext applicationContext(Class<?> mainClass,
.stream()
.filter(argSpec -> ((Field) argSpec.userObject()).getName().equals("serverPort"))
.findFirst()
.ifPresent(argSpec -> {
properties.put("micronaut.server.port", argSpec.getValue());
});
.ifPresent(argSpec -> properties.put("micronaut.server.port", argSpec.getValue()));

builder.properties(properties);
}
return builder.build();
}

private static void continueOnParsingErrors(CommandLine cmd) {
cmd.getCommandSpec().parser().collectErrors(true);
}

@SuppressWarnings("unchecked")
private static <T> T getPropertiesFromMethod(Class<?> cls, String methodName, Object instance) {
try {
Expand Down
16 changes: 16 additions & 0 deletions cli/src/test/java/io/kestra/cli/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@ void testServerCommandHelp(String serverType) {
assertThat(out.toString(), startsWith("Usage: kestra server " + serverType));
}
}

@Test
void missingRequiredParamsPrintHelpInsteadOfException() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setErr(new PrintStream(out));

final String[] argsWithMissingParams = new String[]{"flow", "namespace", "update"};

try (ApplicationContext ctx = App.applicationContext(App.class, argsWithMissingParams)) {
new CommandLine(App.class, new MicronautFactory(ctx)).execute(argsWithMissingParams);

assertThat(out.toString(), startsWith("Missing required parameters: "));
assertThat(out.toString(), containsString("Usage: kestra flow namespace update "));
assertThat(out.toString(), not(containsString("MissingParameterException: ")));
}
}
}

0 comments on commit ae05de4

Please sign in to comment.