@Override public String convert(String value) throws Exception { if (!possibilities.contains(value)) { throw new ErrorMessage("Expected one of %s, but got %s", possibilities, value); } return value; } }
io.err.println(e.getMessage()); Throwable cause = e.getCause(); e.printStackTrace(io.err); return e.getErrorCode(); } catch (Exception e) { io.err.printfln("An unexpected error occurred: %s", e.getMessage());
@Test(groups = "fast") public void testDuplicate() { CliCommand.Builder command = new CliCommand.Builder("test", "A test"); command.addOption("-v"); try { parser(command, "-v", "foo", "-v=bar"); Assert.fail("expected exception"); } catch (ErrorMessage e) { Assert.assertEquals(e.getMessage(), "Duplicate options: -v=foo, -v=bar"); } }
@Test(groups = "fast") public void testMissing() { CliCommand.Builder command = new CliCommand.Builder("test", "A test"); command.addOption("-v"); try { parser(command); Assert.fail("expected exception"); } catch (ErrorMessage e) { Assert.assertEquals(e.getMessage(), "Missing required option: -v"); } }
@Override public boolean hasNext() { if (pending) { return true; } try { line = reader.readLine(); } catch (IOException e) { throw new ErrorMessage(e, "Error while running %s", name); } pending = true; return line != null; }
@Test(groups = "fast") public void testUnexpected() { CliCommand.Builder command = new CliCommand.Builder("test", "A test"); command.addOption("-a").withDefault(null); command.addOption("-b").withDefault(null); try { parser(command, "-x", "test", "-a", "hello"); Assert.fail("expected exception"); } catch (ErrorMessage e) { Assert.assertEquals(e.getMessage(), "Unexpected parameters: -x test"); } }
@Override public int read() { try { return reader.read(); } catch (IOException e) { throw new ErrorMessage(e, "Error reading input"); } }
@Test(groups = "fast") public void testDuplicate() { CliCommand.Builder command = new CliCommand.Builder("test", "A test"); command.addOption("-v"); try { parser(command, "-v", "foo", "-v=bar"); Assert.fail("expected exception"); } catch (ErrorMessage e) { Assert.assertEquals(e.getMessage(), "Duplicate options: -v=foo, -v=bar"); } }
@Override public String readLine() { try { return reader.readLine(); } catch (IOException e) { throw new ErrorMessage(e, "Error reading input"); } }
@Test(groups = "fast") public void testMissing() { CliCommand.Builder command = new CliCommand.Builder("test", "A test"); command.addOption("-v"); try { parser(command); Assert.fail("expected exception"); } catch (ErrorMessage e) { Assert.assertEquals(e.getMessage(), "Missing required option: -v"); } }
@Override public void send(String content) { OutputStream outputStream = process.getOutputStream(); try { outputStream.write(content.getBytes(StandardCharsets.UTF_8)); outputStream.flush(); } catch (IOException e) { throw new ErrorMessage(e, "Error while sending content to %s", name); } }
@Test(groups = "fast") public void testUnexpected() { CliCommand.Builder command = new CliCommand.Builder("test", "A test"); command.addOption("-a").withDefault(null); command.addOption("-b").withDefault(null); try { parser(command, "-x", "test", "-a", "hello"); Assert.fail("expected exception"); } catch (ErrorMessage e) { Assert.assertEquals(e.getMessage(), "Unexpected parameters: -x test"); } }
public <T> List<T> getTrailing(CliConverter<T> converter) { List<T> result = new ArrayList<>(); for (String value : trailing) { try { result.add(converter.convert(value)); } catch (Exception e) { throw new ErrorMessage(e, "Failed to parse %s", value); } } return result; }
@Override public int waitFor(File outFile) { try (OutputStream out = new FileOutputStream(outFile)) { return waitFor(out); } catch (IOException e) { throw new ErrorMessage(e, "Error saving to %s", outFile); } }
public <T> T get(String option, CliConverter<T> converter) { if (!switches.contains(option)) { throw new IllegalStateException( String.format("Expected option name to be one of %s, but got %s", switches, option) ); } String value = values.get(option); try { return converter.convert(value); } catch (Exception e) { throw new ErrorMessage(e, "Failed to parse %s %s", option, value); } }
@Override public int waitFor() { stdout.background(); try { int result = process.waitFor(); stdoutFuture.get(); stderrFuture.get(); return result; } catch (InterruptedException e) { throw new ErrorMessage(e, "Interrupted while waiting for: %s", name); } catch (ExecutionException e) { throw new ErrorMessage(e, "Error while waiting for: %s", name); } finally { close(); } }
@Override public int waitFor(OutputStream out) { try { InputStream in = getStdOut(); byte[] buffer = new byte[4096]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } } catch (IOException e) { throw new ErrorMessage(e, "Error while waiting for %s", name); } try { return process.waitFor(); } catch (InterruptedException e) { throw new ErrorMessage(e, "Interrupted while waiting for %s", name); } finally { close(); } }
public <T> List<T> getMulti(String option, CliConverter<T> converter) { List<String> values = multiValues.get(option); if (values == null || values.isEmpty()) { // value must have been specified less than twice T value = get(option, converter); return value == null ? Collections.<T>emptyList() : Collections.singletonList(value); } List<T> result = new ArrayList<>(); for (String value : values) { try { result.add(converter.convert(value)); } catch (Exception e) { throw new ErrorMessage(e, "Failed to parse %s %s", option, value); } } return result; }
return processBuilder.start(); } catch (IOException e) { throw new ErrorMessage(e, "Error running: %s", command);
out.flush(); throw new ErrorMessage(join("\n", errors));