private HelpDescriptor(HelpDescriptor<T> owner, CommandDescriptor<T> delegate) throws IntrospectionException { super(delegate.getName(), delegate.getDescription()); for (ParameterDescriptor parameter : delegate.getParameters()) { addParameter(parameter); if (owner == null) { add = !(getOptionNames().contains("-h") || getOptionNames().contains("--help")); for (CommandDescriptor<T> sub : delegate.getSubordinates().values()) { if (sub.getOptionNames().contains("-h") || getOptionNames().contains("--help")) { add = false; for (CommandDescriptor<T> subordinate : delegate.getSubordinates().values()) { subordinates.put(subordinate.getName(), new HelpDescriptor<T>(this, subordinate));
public final CommandMatch<?, ?> resolveCommand(String line) throws CommandException { CommandDescriptor<T> descriptor = getDescriptor(); InvocationMatcher<T> analyzer = descriptor.matcher(); InvocationMatch<T> match; try { match = analyzer.parse(line); } catch (org.crsh.cli.impl.SyntaxException e) { throw new CommandException(ErrorKind.SYNTAX, "Syntax exception when evaluating "+ getDescriptor().getName(), e); } return resolve(match); }
public void printDetailsSection(CommandDescriptor<?> command, Appendable stream) throws IOException { if (command.getSubordinates().isEmpty()) { List<String[]> tt = collectParametersTuples(command); int length = 0; for (String[] s : tt) { length = Math.max(s[0].length(), length); } String format = " %1$-" + length + "s %2$s\n"; for (String[] tuple : tt) { Formatter formatter = new Formatter(stream); formatter.format(format, tuple[0], tuple[1]); } } else { stream.append("The most commonly used ").append(command.getName()).append(" commands are:\n"); String format = " %1$-16s %2$s\n"; for (CommandDescriptor<?> subordinate : command.getSubordinates().values()) { Formatter formatter = new Formatter(stream); formatter.format(format, subordinate.getName(), subordinate.getUsage()); } } stream.append("\n\n"); } }
/** * Find an command option by its name, this will look through the command hierarchy. * * @param name the option name * @return the option or null */ public final OptionDescriptor resolveOption(String name) { OptionDescriptor option = getOption(name); if (option == null) { CommandDescriptor<T> owner = getOwner(); if (owner != null) { option = owner.resolveOption(name); } } return option; }
protected void printFQNWithOptions(CommandDescriptor<?> command, Appendable stream) throws IOException { CommandDescriptor<?> owner = command.getOwner(); if (owner != null) { printFQNWithOptions(owner, stream); stream.append(' '); } stream.append(command.getName()); for (OptionDescriptor option : command.getOptions()) { stream.append(' '); option.printUsage(stream); } }
public final void printMan(Appendable writer) throws IOException { int depth = getDepth(); switch (depth) { case 0: { Map<String, ? extends CommandDescriptor<T>> methods = getSubordinates(); if (methods.size() == 1) { methods.values().iterator().next().printMan(writer); } else { writer.append(Util.MAN_TAB).append(getName()); if (getUsage().length() > 0) { writer.append(" - ").append(getUsage()); writer.append(Util.MAN_TAB).append(getName()); for (OptionDescriptor option : getOptions()) { writer.append(" "); option.printUsage(writer); String man = getDescription().getMan(); if (man.length() > 0) { writer.append("DESCRIPTION\n"); if (getOptions().size() > 0) { writer.append("PARAMETERS\n"); for (OptionDescriptor option : getOptions()) { writer.append(Util.MAN_TAB); option.printUsage(writer); writer.append(Util.MAN_TAB).append(method.getName()); String methodText = method.getDescription().getBestEffortMan();
public final void printUsage(Appendable writer) throws IOException { int depth = getDepth(); switch (depth) { case 0: { Map<String, ? extends CommandDescriptor<T>> methods = getSubordinates(); if (methods.size() == 1) { methods.values().iterator().next().printUsage(writer); } else { writer.append("usage: ").append(getName()); for (OptionDescriptor option : getOptions()) { option.printUsage(writer); writer.append("The most commonly used ").append(getName()).append(" commands are:\n"); String format = " %1$-16s %2$s\n"; for (CommandDescriptor<T> method : methods.values()) { Formatter formatter = new Formatter(writer); formatter.format(format, method.getName(), method.getUsage()); CommandDescriptor<T> owner = getOwner(); int length = 0; List<String> parameterUsages = new ArrayList<String>(); List<String> parameterBilto = new ArrayList<String>(); boolean printName = !owner.getSubordinates().keySet().equals(MAIN_SINGLETON); writer.append("usage: ").append(owner.getName()); for (OptionDescriptor option : owner.getOptions()) { writer.append(" "); StringBuilder sb = new StringBuilder();
protected void printSubordinates(CommandDescriptor<?> command, Appendable stream) throws IOException { for (CommandDescriptor<?> subordinate : command.getSubordinates().values()) { stream.append(Util.MAN_TAB).append(subordinate.getName()); String methodText = subordinate.getDescription().getBestEffortMan(); if (methodText.length() > 0) { stream.append("\n"); Util.indent(Util.MAN_TAB_EXTRA, methodText, stream); } stream.append("\n\n"); } }
@Override public void run() { try { CommandDescriptor<Instance<Agent>> c = CommandFactory.DEFAULT.create(Agent.class); InvocationMatcher<Instance<Agent>> matcher = c.matcher(); InvocationMatch<Instance<Agent>> match = matcher.parse(agentArgs); match.invoke(Util.wrap(new Agent(inst))); } catch (Exception e) { e.printStackTrace(); } } };
@Override public CompletionMatch complete() throws CompletionException { org.crsh.cli.spi.Completion.Builder builder = org.crsh.cli.spi.Completion.builder(prefix); for (CommandDescriptor<?> m : descriptor.getSubordinates().values()) { String name = m.getName(); if (name.startsWith(prefix)) { builder.add(name.substring(prefix.length()), true); } } return new CompletionMatch(delimiter, builder.build()); } }
public void printUsageSection(CommandDescriptor<?> command, Appendable stream) throws IOException { stream.append("usage: "); printFQNWithOptions(command, stream); if (command.getSubordinates().size() > 0) { stream.append(" COMMAND [ARGS]"); } else { for (ArgumentDescriptor argument : command.getArguments()) { stream.append(' '); argument.printUsage(stream); } } stream.append("\n\n"); }
/** * Print the full qualified name of the command. * * @param command the command * @param stream the output * @throws IOException any io exception */ protected void printFQN(CommandDescriptor<?> command, Appendable stream) throws IOException { CommandDescriptor<?> owner = command.getOwner(); if (owner != null) { printFQN(owner, stream); stream.append(' '); } stream.append(command.getName()); }
public void testCommandDescription() throws IntrospectionException { class A { } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); assertEquals("", c.getUsage()); assertEquals(0, c.getArguments().size()); assertEquals(0, c.getOptions().size()); }
public void testMethodDescription() throws Exception { class A { @Usage("method_usage") @Man("method_man") @Command void m() {} } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); CommandDescriptor<Instance<A>> m = c.getSubordinate("m"); assertEquals("method_usage", m.getUsage()); assertEquals("method_usage", m.getDescription().getUsage()); assertEquals("method_man", m.getDescription().getMan()); }
/** * Returns a specified subordinate. * * @param name the subordinate name * @return the subordinate command or null */ public final CommandDescriptor<T> getSubordinate(String name) { return getSubordinates().get(name); }
public void testMethodNameOverride() throws Exception { class A { @Named("foo") @Command public void bar() {} } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); assertEquals("A", c.getName()); assertNotNull(c.getSubordinate("foo")); assertEquals("foo", c.getSubordinate("foo").getName()); }
public ProducerCommandMatch(ClassShellCommand<T> shellCommand, CommandInvoker<Instance<T>, ?> invoker, Class<P> producedType) { super(shellCommand); // this.invoker = invoker; this.producedType = producedType; this.name = shellCommand.getDescriptor().getName(); }
private List<String[]> collectParametersTuples(CommandDescriptor<?> command) throws IOException { CommandDescriptor<?> owner = command.getOwner(); List<String[]> tuples; Collection<? extends ParameterDescriptor> parameters; if (owner != null) { tuples = collectParametersTuples(owner); parameters = command.getOptions(); } else { tuples = new ArrayList<String[]>(); parameters = command.getParameters(); } for (ParameterDescriptor parameter : parameters) { StringBuilder sb = new StringBuilder(); parameter.printUsage(sb); String usage = sb.toString(); tuples.add(new String[]{usage, parameter.getUsage()}); } return tuples; }
/** * Provide completions for the specified arguments. * * @param context the command context * @param line the original command line arguments * @return the completions */ public final CompletionMatch complete(RuntimeContext context, String line) throws CommandException { CompletionMatcher matcher = getDescriptor().completer(); Completer completer = getCompleter(context); try { return matcher.match(completer, line); } catch (CompletionException e) { // command.log.log(Level.SEVERE, "Error during completion of line " + line, e); return new CompletionMatch(Delimiter.EMPTY, Completion.create()); } }