Resolve "If the CLI is invoked without any parameter, the usage is shown twice"

This commit is contained in:
Philip Helger 2025-08-27 18:14:09 +02:00 committed by Renzo Kottmann
parent 22910c1ca0
commit 5e57c3e978
2 changed files with 11 additions and 4 deletions

View file

@ -25,7 +25,6 @@ import org.fusesource.jansi.AnsiRenderer.Code;
import de.kosit.validationtool.cmd.report.Line; import de.kosit.validationtool.cmd.report.Line;
import de.kosit.validationtool.impl.Printer; import de.kosit.validationtool.impl.Printer;
import picocli.CommandLine; import picocli.CommandLine;
import picocli.CommandLine.ParseResult; import picocli.CommandLine.ParseResult;
@ -79,11 +78,11 @@ public class CommandLineApplication {
final CommandLine commandLine = new CommandLine(new CommandLineOptions()); final CommandLine commandLine = new CommandLine(new CommandLineOptions());
try { try {
commandLine.setExecutionExceptionHandler(CommandLineApplication::logExecutionException); commandLine.setExecutionExceptionHandler(CommandLineApplication::logExecutionException);
commandLine.execute(args); final int cmdlineRetVal = commandLine.execute(args);
if (commandLine.isUsageHelpRequested()) { if (commandLine.isUsageHelpRequested() || cmdlineRetVal == CommandLine.ExitCode.USAGE) {
resultStatus = ReturnValue.HELP_REQUEST; resultStatus = ReturnValue.HELP_REQUEST;
} else { } else {
resultStatus = ObjectUtils.defaultIfNull(commandLine.getExecutionResult(), ReturnValue.PARSING_ERROR); resultStatus = ObjectUtils.getIfNull(commandLine.getExecutionResult(), ReturnValue.PARSING_ERROR);
if (resultStatus.isError()) { if (resultStatus.isError()) {
commandLine.usage(System.out); commandLine.usage(System.out);
} }

View file

@ -84,6 +84,14 @@ public class CommandlineApplicationTest {
assertThat(outputLines.stream().filter(l -> l.startsWith(CommandLineOptions.SYNOSIS_HEADING))).hasSize(1); assertThat(outputLines.stream().filter(l -> l.startsWith(CommandLineOptions.SYNOSIS_HEADING))).hasSize(1);
} }
@Test
public void testNoArguments() {
final String[] args = {};
CommandLineApplication.mainProgram(args);
assertThat(CommandLine.getErrorOutput()).isNotEmpty();
checkForHelp(CommandLine.getErrorLines());
}
@Test @Test
public void testRequiredScenarioFile() { public void testRequiredScenarioFile() {
final String[] args = { "arguments", "egal welche", "argumente drin sind" }; final String[] args = { "arguments", "egal welche", "argumente drin sind" };