Unknown options are caught and reported to the user instead of throwing an exception

This commit is contained in:
chubtub 2024-02-01 17:46:44 -05:00
parent 81575ee08a
commit eb77981e41
2 changed files with 29 additions and 9 deletions

View File

@ -21,8 +21,15 @@ public class Main {
jc.parse(args); jc.parse(args);
SwidTagGateway gateway; SwidTagGateway gateway;
ReferenceManifestValidator validator; ReferenceManifestValidator validator;
List<String> unknownOpts = commander.getUnknownOptions();
if (commander.isHelp()) { if (!unknownOpts.isEmpty()) {
StringBuilder sb = new StringBuilder("Unknown options encountered: ");
for (String opt : unknownOpts) {
sb.append(opt + ", ");
}
exitWithErrorCode(sb.substring(0,sb.lastIndexOf(",")));
} else if (commander.isHelp()) {
jc.usage(); jc.usage();
System.out.println(commander.printHelpExamples()); System.out.println(commander.printHelpExamples());
} else if (commander.isVersion()) { } else if (commander.isVersion()) {
@ -57,8 +64,7 @@ public class Main {
} }
validator.validateSwidtagFile(verifyFile); validator.validateSwidtagFile(verifyFile);
} else { } else {
System.out.println("Need a RIM file to validate!"); exitWithErrorCode("A RIM file was not found for validation.");
System.exit(1);
} }
} else { } else {
gateway = new SwidTagGateway(); gateway = new SwidTagGateway();
@ -92,13 +98,11 @@ public class Main {
gateway.setDefaultCredentials(true); gateway.setDefaultCredentials(true);
gateway.setJksTruststoreFile(SwidTagConstants.DEFAULT_KEYSTORE_FILE); gateway.setJksTruststoreFile(SwidTagConstants.DEFAULT_KEYSTORE_FILE);
} else { } else {
System.out.println("A private key (-k) and public certificate (-p) " + exitWithErrorCode("A private key (-k) and public certificate (-p) " +
"are required, or the default key (-d) must be indicated."); "are required, or the default key (-d) must be indicated.");
System.exit(1);
} }
if (rimEventLog.isEmpty()) { if (rimEventLog.isEmpty()) {
System.out.println("Error: a support RIM is required!"); exitWithErrorCode("A support RIM is required.");
System.exit(1);
} else { } else {
gateway.setRimEventLog(rimEventLog); gateway.setRimEventLog(rimEventLog);
} }
@ -110,18 +114,28 @@ public class Main {
gateway.setTimestampArgument(timestampArguments.get(1)); gateway.setTimestampArgument(timestampArguments.get(1));
} }
} else { } else {
System.exit(1); exitWithErrorCode("The provided timestamp argument(s) " +
"is/are not valid.");
} }
} }
gateway.generateSwidTag(commander.getOutFile()); gateway.generateSwidTag(commander.getOutFile());
break; break;
default: default:
System.out.println("No create type given, nothing to do"); exitWithErrorCode("No create type given, nothing to do");
} }
} }
} }
} }
/**
* Use cases that exit with an error code are redirected here.
*/
private static void exitWithErrorCode(String errorMessage) {
//TODO: log errorMessage
System.out.println(errorMessage);
System.exit(1);
}
/** /**
* This method parses the version number from the jar filename in the absence of * This method parses the version number from the jar filename in the absence of
* the VERSION file expected with an rpm installation. * the VERSION file expected with an rpm installation.

View File

@ -12,6 +12,8 @@ import java.util.List;
*/ */
public class Commander { public class Commander {
@Parameter(description = "This parameter catches all unrecognized arguments.")
private List<String> unknownOptions = new ArrayList<>();
@Parameter(names = {"-h", "--help"}, help = true, description = "Print this help text.") @Parameter(names = {"-h", "--help"}, help = true, description = "Print this help text.")
private boolean help; private boolean help;
@Parameter(names = {"-c", "--create \"base\""}, order = 0, @Parameter(names = {"-c", "--create \"base\""}, order = 0,
@ -58,6 +60,10 @@ public class Commander {
"\tRFC3339 [yyyy-MM-ddThh:mm:ssZ]\n\tRFC3852 <counterSignature.bin>") "\tRFC3339 [yyyy-MM-ddThh:mm:ssZ]\n\tRFC3852 <counterSignature.bin>")
private List<String> timestampArguments = new ArrayList<String>(2); private List<String> timestampArguments = new ArrayList<String>(2);
public List<String> getUnknownOptions() {
return unknownOptions;
}
public boolean isHelp() { public boolean isHelp() {
return help; return help;
} }