001// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org> 002// 003// SPDX-License-Identifier: Apache-2.0 004 005package sop.cli.picocli; 006 007import picocli.CommandLine; 008import sop.exception.SOPGPException; 009 010public class SOPExceptionExitCodeMapper implements CommandLine.IExitCodeExceptionMapper { 011 012 @Override 013 public int getExitCode(Throwable exception) { 014 if (exception instanceof SOPGPException) { 015 return ((SOPGPException) exception).getExitCode(); 016 } 017 if (exception instanceof CommandLine.UnmatchedArgumentException) { 018 CommandLine.UnmatchedArgumentException ex = (CommandLine.UnmatchedArgumentException) exception; 019 // Unmatched option of subcommand (eg. `generate-key -k`) 020 if (ex.isUnknownOption()) { 021 return SOPGPException.UnsupportedOption.EXIT_CODE; 022 } 023 // Unmatched subcommand 024 return SOPGPException.UnsupportedSubcommand.EXIT_CODE; 025 } 026 // Invalid option (eg. `--label Invalid`) 027 if (exception instanceof CommandLine.ParameterException) { 028 return SOPGPException.UnsupportedOption.EXIT_CODE; 029 } 030 031 // Others, like IOException etc. 032 return 1; 033 } 034}