@Command public static void jsed(final String command, @In final InputStream in, @Out final PrintStream out) { if (!requireNonNull(command, "Please provide an option like: jsed s/foo/bar/g").startsWith("s/")) { throw new IllegalArgumentException("Only substitution commands supported."); } final int slash1 = command.indexOf('/'); final int slash2 = command.indexOf('/', slash1 + 1); final int slash3 = command.indexOf('/', slash2 + 1); if (slash1 < 0 || slash2 < 0 || slash3 < 0) { throw new IllegalArgumentException("Wrong pattern '" + command + "', use s/pattern/replacement/[g]"); } final String pattern = command.substring(slash1 + 1, slash2); final String substitution = command.substring(slash2 + 1, slash3); final boolean global = command.endsWith("/g"); final Pattern compiled = Pattern.compile(pattern); try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { String line; while ((line = reader.readLine()) != null) { out.println(global ? compiled.matcher(line).replaceAll(substitution) : compiled.matcher(line).replaceFirst(substitution)); } } catch (final IOException e) { throw new IllegalStateException(e); } }
@Command public static void pretty(@In final InputStream in, @Out final PrintStream out) throws IOException { final String content = IO.slurp(in); // note: optimizing it to not load in memory the content is good but then optiomise http cmd as well if (content.startsWith("{") || content.startsWith("[")) { JSonP.format(content, out); } else { // xml? try { final TransformerFactory factory = TransformerFactory.newInstance(); final Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); final StreamResult result = new StreamResult(new StringWriter()); transformer.transform(new StreamSource(new StringReader(content)), result); out.write(result.getWriter().toString().getBytes("UTF-8")); } catch (final TransformerException e) { out.write(content.getBytes("UTF-8")); } } out.write(lineSeparator().getBytes()); try { in.close(); } catch (final Exception e) { // no-op } }
@Command public static void wc(@Option("l") @Default("true") final boolean line, @Option("c") @Default("false") final boolean characters, @In final InputStream in, @Out final PrintStream out) { int count = 0; if (characters) { try (Reader reader = new InputStreamReader(in)) { while (reader.read() >= 0) { count++; } } catch (final IOException e) { throw new IllegalStateException(e); } } else if (line) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { while (reader.readLine() != null) { count++; } } catch (final IOException e) { throw new IllegalStateException(e); } } else { throw new IllegalStateException("wc needs at least one active option"); } out.println(count); }
@Option("regex") @Default("false") final boolean isRegex, @Option("i") @Default("true") final boolean insensitive, @In final InputStream in, @Out final PrintStream out) { if (pattern == null) {