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);
SwidTagGateway gateway;
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();
System.out.println(commander.printHelpExamples());
} else if (commander.isVersion()) {
@ -57,8 +64,7 @@ public class Main {
}
validator.validateSwidtagFile(verifyFile);
} else {
System.out.println("Need a RIM file to validate!");
System.exit(1);
exitWithErrorCode("A RIM file was not found for validation.");
}
} else {
gateway = new SwidTagGateway();
@ -92,13 +98,11 @@ public class Main {
gateway.setDefaultCredentials(true);
gateway.setJksTruststoreFile(SwidTagConstants.DEFAULT_KEYSTORE_FILE);
} 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.");
System.exit(1);
}
if (rimEventLog.isEmpty()) {
System.out.println("Error: a support RIM is required!");
System.exit(1);
exitWithErrorCode("A support RIM is required.");
} else {
gateway.setRimEventLog(rimEventLog);
}
@ -110,18 +114,28 @@ public class Main {
gateway.setTimestampArgument(timestampArguments.get(1));
}
} else {
System.exit(1);
exitWithErrorCode("The provided timestamp argument(s) " +
"is/are not valid.");
}
}
gateway.generateSwidTag(commander.getOutFile());
break;
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
* the VERSION file expected with an rpm installation.

View File

@ -12,6 +12,8 @@ import java.util.List;
*/
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.")
private boolean help;
@Parameter(names = {"-c", "--create \"base\""}, order = 0,
@ -58,6 +60,10 @@ public class Commander {
"\tRFC3339 [yyyy-MM-ddThh:mm:ssZ]\n\tRFC3852 <counterSignature.bin>")
private List<String> timestampArguments = new ArrayList<String>(2);
public List<String> getUnknownOptions() {
return unknownOptions;
}
public boolean isHelp() {
return help;
}