Handle OptionException immediately

This commit is contained in:
Chris Beams 2020-04-26 19:30:35 +02:00
parent dee5e4cf7e
commit e10e29a211
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73

View file

@ -28,6 +28,7 @@ import io.grpc.StatusRuntimeException;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import java.text.DecimalFormat;
@ -77,8 +78,13 @@ public class CliMain {
var passwordOpt = parser.accepts("password", "rpc server password")
.withRequiredArg();
OptionSet options = null;
try {
var options = parser.parse(args);
options = parser.parse(args);
} catch (OptionException ex) {
err.println("Error: " + ex.getMessage());
exit(EXIT_FAILURE);
}
if (options.has(helpOpt)) {
printHelp(parser, out);
@ -110,6 +116,7 @@ public class CliMain {
exit(EXIT_FAILURE);
}
try {
var channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
var credentials = new PasswordCallCredentials(password);
@ -140,9 +147,6 @@ public class CliMain {
exit(EXIT_FAILURE);
}
}
} catch (OptionException ex) {
err.println("Error: " + ex.getMessage());
exit(EXIT_FAILURE);
} catch (StatusRuntimeException ex) {
Throwable t = ex.getCause() == null ? ex : ex.getCause();
err.println("Error: " + t.getMessage().replace("UNAUTHENTICATED: ", ""));