sop-java/sop-java-picocli/src/main/java/sop/cli/picocli/SOPExecutionExceptionHandler.java

34 lines
1.1 KiB
Java
Raw Normal View History

2022-01-11 13:46:05 +01:00
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.cli.picocli;
import picocli.CommandLine;
public class SOPExecutionExceptionHandler implements CommandLine.IExecutionExceptionHandler {
@Override
public int handleExecutionException(Exception ex, CommandLine commandLine, CommandLine.ParseResult parseResult) {
2022-01-11 13:46:05 +01:00
int exitCode = commandLine.getExitCodeExceptionMapper() != null ?
commandLine.getExitCodeExceptionMapper().getExitCode(ex) :
commandLine.getCommandSpec().exitCodeOnExecutionException();
2022-01-11 13:46:05 +01:00
CommandLine.Help.ColorScheme colorScheme = commandLine.getColorScheme();
// CHECKSTYLE:OFF
if (ex.getMessage() != null) {
commandLine.getErr().println(colorScheme.errorText(ex.getMessage()));
2022-11-06 14:41:52 +01:00
} else {
commandLine.getErr().println(ex.getClass().getName());
}
if (SopCLI.stacktrace) {
ex.printStackTrace(commandLine.getErr());
2022-01-11 13:46:05 +01:00
}
// CHECKSTYLE:ON
return exitCode;
}
}