@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 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 <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); }
@Override List<String> invoke(String line) throws Exception { BC1 object = new BC1(); desc1.matcher().parse(line).invoke(Util.wrap(object)); return object.s; } };
@Override List<String> invoke(String line) throws Exception { BC2 object = new BC2(); desc2.matcher().parse(line).invoke(Util.wrap(object)); return object.s; } };
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); } }
public void testImplicitSubordinateOption() throws Exception { CommandDescriptor<Instance<M>> desc = CommandFactory.DEFAULT.create(M.class); M m = new M(); desc.matcher().parse("-o foo").invoke(Util.wrap(m)); assertEquals("foo", m.opt); m = new M(); desc.matcher().option("o", Collections.singletonList("foo")).arguments(Collections.emptyList()).invoke(Util.wrap(m)); assertEquals("foo", m.opt); }
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 void testSpecifySubordinateOptionAfterSubordinate() throws Exception { CommandDescriptor<Instance<L>> desc = CommandFactory.DEFAULT.create(L.class); L l = new L(); desc.matcher().parse("cmd -o foo").invoke(Util.wrap(l)); assertEquals("foo", l.opt); l = new L(); desc.matcher().subordinate("cmd").option("o", Collections.singletonList("foo")).arguments(Collections.emptyList()).invoke(Util.wrap(l)); assertEquals("foo", l.opt); }
public void testSpecifyClassOptionAfterSubordinate() throws Exception { CommandDescriptor<Instance<K>> desc = CommandFactory.DEFAULT.create(K.class); K k = new K(); desc.matcher().parse("cmd -o foo").invoke(Util.wrap(k)); assertEquals(null, k.opt); k = new K(); desc.matcher().subordinate("cmd").option("o", Collections.singletonList("foo")).arguments(Collections.emptyList()).invoke(Util.wrap(k)); assertEquals(null, k.opt); }
public void testMainMethodInvocation() throws Exception { CommandDescriptor<Instance<B>> desc = CommandFactory.DEFAULT.create(B.class); // B b = new B(); desc.matcher().parse("").invoke(Util.wrap(b)); assertEquals(1, b.count); b = new B(); desc.matcher().arguments(Collections.emptyList()).invoke(Util.wrap(b)); assertEquals(1, b.count); }
public void testSpecifyClassOptionBeforeSubordinate() throws Exception { CommandDescriptor<Instance<K>> desc = CommandFactory.DEFAULT.create(K.class); K k = new K(); desc.matcher().parse("-o foo cmd").invoke(Util.wrap(k)); assertEquals("foo", k.opt); k = new K(); desc.matcher().option("o", Collections.singletonList("foo")).subordinate("cmd").arguments(Collections.emptyList()).invoke(Util.wrap(k)); assertEquals("foo", k.opt); }
public void testSpecifySubordinateOptionBeforeSubordinate() throws Exception { CommandDescriptor<Instance<L>> desc = CommandFactory.DEFAULT.create(L.class); L l = new L(); desc.matcher().parse("-o foo cmd").invoke(Util.wrap(l)); assertEquals(null, l.opt); l = new L(); desc.matcher().option("o", Collections.singletonList("foo")).subordinate("cmd").arguments(Collections.emptyList()).invoke(Util.wrap(l)); assertEquals(null, l.opt); }
public void testError() throws Exception { CommandDescriptor<Instance<J>> desc = CommandFactory.DEFAULT.create(J.class); // J j = new J(); InvocationMatch<Instance<J>> matcher = desc.matcher().parse("foo"); try { matcher.invoke(Util.wrap(j)); fail(); } catch (Error e) { assertEquals("fooerror", e.getMessage()); } }
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()); }
public void testValue() throws Exception { // CommandDescriptor<Instance<G>> desc = new CommandFactory(MatcherTestCase.class.getClassLoader()).create(G.class); // G g = new G(); desc.matcher().parse("foo -o a").invoke(Util.wrap(g)); assertEquals(new Custom("a"), g.o); g = new G(); desc.matcher().subordinate("foo").option("o", Collections.singletonList("a")).arguments(Collections.emptyList()).invoke(Util.wrap(g)); assertEquals(new Custom("a"), g.o); }
public void testInvocationTypeConversionInjection() throws Exception { CommandDescriptor<Instance<D>> desc = CommandFactory.DEFAULT.create(D.class); // D d = new D(); desc.matcher().parse("a -o 5").invoke(Util.wrap(d)); assertEquals((Integer)5, d.i); // d = new D(); desc.matcher().parse("b -o 5").invoke(Util.wrap(d)); assertEquals((Integer)5, d.i); }
public void testQuoted() throws Exception { CommandDescriptor<Instance<E>> desc = CommandFactory.DEFAULT.create(E.class); // E e = new E(); desc.matcher().parse("a -o a").invoke(Util.wrap(e)); assertEquals("a", e.i); // e = new E(); desc.matcher().parse("a -o \"a\"").invoke(Util.wrap(e)); assertEquals("\"a\"", e.i); }
public void testException() throws Exception { CommandDescriptor<Instance<H>> desc = CommandFactory.DEFAULT.create(H.class); // H h = new H(); InvocationMatch<Instance<H>> matcher = desc.matcher().parse("foo"); try { matcher.invoke(Util.wrap(h)); fail(); } catch (CLIException e) { assertEquals(Exception.class, e.getCause().getClass()); assertEquals("fooexception", e.getCause().getMessage()); } }
public void testRuntimeException() throws Exception { CommandDescriptor<Instance<I>> desc = CommandFactory.DEFAULT.create(I.class); // I i = new I(); InvocationMatch<Instance<I>> matcher = desc.matcher().parse("foo"); try { matcher.invoke(Util.wrap(i)); fail(); } catch (CLIException e) { assertEquals(RuntimeException.class, e.getCause().getClass()); assertEquals("fooruntimeexception", e.getCause().getMessage()); } }