@Override protected void addParameter(ParameterDescriptor parameter) throws IntrospectionException, NullPointerException, IllegalArgumentException { super.addParameter(parameter); }
private HelpDescriptor(HelpDescriptor<T> owner, CommandDescriptorImpl<T> delegate) throws IntrospectionException { super(delegate.getName(), delegate.getDescription()); // for (ParameterDescriptor parameter : delegate.getParameters()) { addParameter(parameter); } // Override the help parameter only for the root level // otherwise it may be repeated several times if (owner == null) { addParameter(HELP_OPTION); } // Wrap subordinates LinkedHashMap<String, HelpDescriptor<T>> subordinates = new LinkedHashMap<String, HelpDescriptor<T>>(); for (CommandDescriptorImpl<T> subordinate : delegate.getSubordinates().values()) { subordinates.put(subordinate.getName(), new HelpDescriptor<T>(this, subordinate)); } // this.owner = owner; this.delegate = delegate; this.subordinates = subordinates; }
public CommandInvoker<T> getInvoker() { return descriptor.getInvoker(this); }
private static <T> void handle(Class<T> commandClass, String line) throws Exception { CommandDescriptorImpl<T> descriptor = CommandFactory.DEFAULT.create(commandClass); descriptor = HelpDescriptor.create(descriptor); InvocationMatcher<T> matcher = descriptor.invoker("main"); InvocationMatch<T> match = matcher.match(line); T instance = commandClass.newInstance(); Object o = match.invoke(instance); if (o != null) { System.out.println(o); } } }
public InvocationMatch<T> subordinate(String name) { CommandDescriptorImpl<T> subordinate = descriptor.getSubordinate(name); if (subordinate != null) { return new InvocationMatch<T>(this, subordinate); } else { return null; } }
public final CompletionMatch complete(RuntimeContext context, String line) { // WTF CompletionMatcher analyzer = descriptor.completer("main"); // Completer completer = this instanceof Completer ? (Completer)this : null; // this.context = context; try { return analyzer.match(completer, line); } catch (CompletionException e) { log.log(Level.SEVERE, "Error during completion of line " + line, e); return new CompletionMatch(Delimiter.EMPTY, Completion.create()); } finally { this.context = null; } }
public CommandInvoker<?, ?> resolveInvoker(String name, Map<String, ?> options, List<?> args) { if (options.containsKey("h") || options.containsKey("help")) { throw new UnsupportedOperationException("Implement me"); } else { InvocationMatcher matcher = descriptor.invoker("main"); InvocationMatch<CRaSHCommand> match = null; try { match = matcher.match(name, options, args); } catch (org.crsh.cli.SyntaxException e) { throw new org.crsh.command.SyntaxException(e.getMessage()); } return resolveInvoker(match); } }
@Override public void run() { try { CommandDescriptorImpl<Agent> c = CommandFactory.DEFAULT.create(Agent.class); InvocationMatcher<Agent> matcher = c.invoker("main"); InvocationMatch<Agent> match = matcher.match(agentArgs); match.invoke(new Agent(inst)); } catch (Exception e) { e.printStackTrace(); } } };
@Override public CommandInvoker<T> getInvoker(final InvocationMatch<T> match) { final CommandInvoker<T> invoker = delegate.getInvoker(match); return new CommandInvoker<T>() { @Override
@Override protected void addParameter(ParameterDescriptor parameter) throws IntrospectionException { // Check we can add the option if (parameter instanceof OptionDescriptor) { OptionDescriptor option = (OptionDescriptor)parameter; Set<String> blah = new HashSet<String>(); for (String optionName : option.getNames()) { blah.add((optionName.length() == 1 ? "-" : "--") + optionName); } for (MethodDescriptor<T> method : methods.values()) { Set<String> diff = new HashSet<String>(method.getOptionNames()); diff.retainAll(blah); if (diff.size() > 0) { throw new IntrospectionException("Cannot add method " + method.getName() + " because it has common " + " options with its class: " + diff); } } } // super.addParameter(parameter); }
public static void main(String[] args) throws Exception { StringBuilder line = new StringBuilder(); for (int i = 0;i < args.length;i++) { if (i > 0) { line.append(' '); } Delimiter.EMPTY.escape(args[i], line); } // CRaSH main = new CRaSH(); InvocationMatcher<CRaSH> matcher = main.descriptor.invoker("main"); InvocationMatch<CRaSH> match = matcher.match(line.toString()); match.invoke(new CRaSH()); } }
public CommandInvoker<?, ?> resolveInvoker(String line) { InvocationMatcher analyzer = descriptor.invoker("main"); InvocationMatch<CRaSHCommand> match; try { match = analyzer.match(line); } catch (org.crsh.cli.SyntaxException e) { throw new org.crsh.command.SyntaxException(e.getMessage()); } return resolveInvoker(match); }
public final String describe(String line, DescriptionFormat mode) { InvocationMatcher analyzer = descriptor.invoker("main");
@Override public Command createCommand(String command) { // if (command.startsWith("scp ")) { try { command = command.substring(4); SCPAction action = new SCPAction(); CommandDescriptorImpl<SCPAction> descriptor = CommandFactory.DEFAULT.create(SCPAction.class); InvocationMatcher<SCPAction> analyzer = descriptor.invoker("main"); InvocationMatch<SCPAction> match = analyzer.match(command); match.invoke(Resolver.EMPTY, action); if (Boolean.TRUE.equals(action.isSource())) { return new SourceCommand(action.getTarget(), Boolean.TRUE.equals(action.isRecursive())); } else if (Boolean.TRUE.equals(action.isSink())) { return new SinkCommand(action.getTarget(), Boolean.TRUE.equals(action.isRecursive())); } else { return new FailCommand("Cannot execute command " + command); } } catch (CLIException e) { return new FailCommand("Cannot execute command " + command, e); } } else { return new FailCommand("Cannot execute command " + command); } } }