public Object invoke(T command) throws InvocationException, SyntaxException { CommandInvoker<T, ?> invoker = getInvoker(); if (invoker != null) { return invoker.invoke(command); } else { return null; } }
public final InvocationMatcher<T> matcher() { return new InvocationMatcher<T>(this); }
public InvocationMatcher<T> options(Map<String, List<?>> options) throws SyntaxException { TokenList tokens = new TokenList(this.tokens); for (Map.Entry<String, List<?>> option : options.entrySet()) { tokens.addOption(option.getKey(), option.getValue()); } return new InvocationMatcher<T>(descriptor, tokens); }
public <C> void assertPreserveHelpInSubCommand2(Class<C> clazz, C instance, String option) throws Exception { CommandDescriptor<Instance<C>> desc = HelpDescriptor.create(CommandFactory.DEFAULT.create(clazz)); InvocationMatcher<Instance<C>> matcher = desc.matcher(); InvocationMatch<Instance<C>> match = matcher. options(Collections.<String, List<?>>singletonMap(option, Collections.singletonList(Boolean.TRUE))).arguments(Collections.emptyList()). subordinate("sub"); CommandInvoker<Instance<C>, ?> invoker = match.getInvoker(); Object ret = invoker.invoke(Util.wrap(instance)); assertEquals("my help " + true, ret); } }
@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(); } } };
public InvocationMatcher<T> subordinate(String name) throws SyntaxException { TokenList tokens = new TokenList(this.tokens); if (name != null && name.length() > 0) { tokens.add(new Token.Literal.Word(tokens.last(), name)); } return new InvocationMatcher<T>(descriptor, tokens); }
private InvocationMatch<T> match(final Iterable<Token> tokens) throws SyntaxException { Tokenizer tokenizer = new Tokenizer() { /** . */ Iterator<Token> i = tokens.iterator(); @Override protected Token parse() { return i.hasNext() ? i.next() : null; } }; return match(tokenizer); }
public final <D extends ParameterDescriptor> ParameterMatch<D> getParameter(D parameter) { if (parameter instanceof OptionDescriptor) { return (ParameterMatch<D>)options.get(parameter); } else { for (ArgumentMatch argumentMatch : arguments) { if (argumentMatch.getParameter() == parameter) { return (ParameterMatch<D>)argumentMatch; } } return null; } }
public InvocationMatcher<T> option(String optionName, List<?> optionValue) throws SyntaxException { return options(Collections.<String, List<?>>singletonMap(optionName, optionValue)); }
@Override public void set(Object target, Object[] args, Object value) throws InvocationException { try { field.setAccessible(true); field.set(target, value); } catch (Exception e) { throw new InvocationException(e.getMessage(), e); } } }
public InvocationMatch<T> subordinate(String name) { CommandDescriptor<T> subordinate = descriptor.getSubordinate(name); if (subordinate != null) { return new InvocationMatch<T>(this, subordinate); } else { return null; } }
/** * Compute the value from the parameter metadata and the values list. * * @return the invocation value * @throws org.crsh.cli.impl.SyntaxException anything that would prevent the value from being computed */ public Object computeValue() throws SyntaxException { List<String> strings = getStrings(); return parameter.parse(strings); } }
public void add(Token token) { if (list.size() > 0) { list.add(new Token.Whitespace(last(), " ")); } list.add(token); }
public <C> void assertPreserveHelpInSubCommand1(Class<C> clazz, C instance, String option) throws Exception { CommandDescriptor<Instance<C>> desc = HelpDescriptor.create(CommandFactory.DEFAULT.create(clazz)); InvocationMatcher<Instance<C>> matcher = desc.matcher(); InvocationMatch<Instance<C>> match = matcher. subordinate("sub"). options(Collections.<String, List<?>>singletonMap(option, Collections.singletonList(Boolean.TRUE))).arguments(Collections.emptyList()); CommandInvoker<Instance<C>, ?> invoker = match.getInvoker(); Object ret = invoker.invoke(Util.wrap(instance)); assertEquals("my help " + true, ret); }
public <C> void assertPreserveHelp(Class<C> clazz, C instance, String option) throws Exception { CommandDescriptor<Instance<C>> desc = HelpDescriptor.create(CommandFactory.DEFAULT.create(clazz)); InvocationMatcher<Instance<C>> matcher = desc.matcher(); InvocationMatch<Instance<C>> match = matcher.options(Collections.<String, List<?>>singletonMap(option, Collections.singletonList(Boolean.TRUE))).arguments(Collections.emptyList()); CommandInvoker<Instance<C>, ?> invoker = match.getInvoker(); Object ret = invoker.invoke(Util.wrap(instance)); assertEquals("my help " + true, ret); }
public void testFoo() throws Exception { CommandDescriptor<Instance<A>> desc = HelpDescriptor.create(CommandFactory.DEFAULT.create(A.class)); InvocationMatcher<Instance<A>> matcher = desc.matcher(); InvocationMatch<Instance<A>> match = matcher.options(Collections.<String, List<?>>singletonMap("h", Collections.singletonList(Boolean.TRUE))).arguments(Collections.emptyList()); CommandInvoker<Instance<A>, ?> invoker = match.getInvoker(); Object ret = invoker.invoke(Util.wrap(new A())); assertTrue(ret instanceof Help); assertEquals(Help.class, invoker.getReturnType()); }