@Command public String home() { return System.getProperty("user.home"); }
@Command public void gc() { Runtime.getRuntime().gc(); }
@Command public int processors() { return Runtime.getRuntime().availableProcessors(); }
@Command public static void exit() { if (exitHook != null) { exitHook.run(); } Environment.ENVIRONMENT_THREAD_LOCAL.remove(); throw new ExitException(); }
@Command("list") public String doList(@Option({"pattern", "p"}) Pattern pattern) { if (pattern == null) pattern = Pattern.compile(".*"); final StringBuilder sb = new StringBuilder(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { final String key = entry.getKey().toString(); if (pattern.matcher(key).matches()) { sb.append(key).append(" = ").append(entry.getValue()).append("\n"); } } return sb.toString(); } }
@Command public void exit() throws StopException { throw new StopException(); } }
@Command("set") public String doSet(@Option({"key", "k"}) String key, @Option({"value", "v"}) String value) { final Object old = properties.setProperty(key, value); final StringBuilder sb = new StringBuilder(); sb.append("set ").append(key).append(" to ").append(value); sb.append("\n"); if (old != null) { sb.append("old value: ").append(old); sb.append("\n"); } return sb.toString(); }
@Command public void exit() throws StopException { throw new StopException(); } }
@Command public String home(@Exists @Directory File file) { return System.setProperty("user.home", file.getAbsolutePath()); }
@Command public String dir(@Exists @Directory File file) { return System.setProperty("user.dir", file.getAbsolutePath()); }
@Command public String env() { final PrintString out = new PrintString(); final Map<String, String> env = System.getenv(); for (String key : new TreeSet<String>(env.keySet())) { out.printf("%s = %s%n", key, env.get(key)); } return out.toString(); }
@Command public String whoami() { return context.getCallerPrincipal() == null ? "Unknown" : context.getCallerPrincipal().getName(); } }
@Command public StreamingOutput cat(@IsFile @Readable final File file) { return new StreamingOutput() { @Override public void write(final OutputStream os) throws IOException { IO.copy(file, os); } }; }
@Command public String help(final String name) { final Cmd cmd = commands.get(name); if (cmd == null) { return String.format("No such command: %s%n", name); } final PrintString out = new PrintString(); cmd.help(out); return out.toString(); }
@Command public String properties() { final PrintString out = new PrintString(); System.getProperties().list(out); return out.toString(); }
@Command public static void history(final CliEnvironment env, @Out final PrintStream out) { for (final History.Entry entry : env.history()) { out.println(String.format("[%3d] %s", entry.index() + 1, entry.value())); } }
@Command public String maxMemory(@Option({"unit","u"}) @Default("MEGABYTES") SizeUnit unit) { final long bytes = Runtime.getRuntime().maxMemory(); final Size size = new Size(unit.convert(bytes, SizeUnit.BYTES), unit); return size.toString(); }
@Command public String totalMemory(@Option({"unit","u"}) @Default("MEGABYTES") SizeUnit unit) { final long bytes = Runtime.getRuntime().totalMemory(); final Size size = new Size(unit.convert(bytes, SizeUnit.BYTES), unit); return size.toString(); }
@Command public String freeMemory(@Option({"unit", "u"}) @Default("MEGABYTES") SizeUnit unit) { final long bytes = Runtime.getRuntime().freeMemory(); final Size size = new Size(unit.convert(bytes, SizeUnit.BYTES), unit); return size.toString(); }
@Command public String hello(@Option("name") @Default("World") final String name, @Option("language") @Default("EN") final Language language) { return String.format("%s, %s!", language.getSalutation(), name); }