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); }
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 InvocationMatcher<T> matcher() { return new InvocationMatcher<T>(this); }
if (options != null && options.size() > 0) { for (Map.Entry<String, ?> option : options.entrySet()) { matcher = matcher.option(option.getKey(), Collections.singletonList(option.getValue())); matcher = matcher.subordinate(subordinate); matcher = matcher.option(option.getKey(), Collections.singletonList(option.getValue())); match = matcher.arguments(arguments != null ? arguments : Collections.emptyList());
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); }
private static <T> void handle(Class<T> commandClass, String line) throws Exception { ObjectCommandDescriptor<T> descriptor = CommandFactory.DEFAULT.create(commandClass); HelpDescriptor<Instance<T>> helpDescriptor = HelpDescriptor.create(descriptor); InvocationMatcher<Instance<T>> matcher = helpDescriptor.matcher(); InvocationMatch<Instance<T>> match = matcher.parse(line); final T instance = commandClass.newInstance(); Object o = match.invoke(Util.wrap(instance)); if (o != null) { System.out.println(o); } } }
public void testBooleanParameter() throws Exception { class A implements Runnable { @Option(names = "o") boolean o; public void run() {} } CommandDescriptor<Instance<A>> desc = CommandFactory.DEFAULT.create(A.class); InvocationMatcher<Instance<A>> analyzer = desc.matcher(); // A a = new A(); analyzer.parse("-o").invoke(Util.wrap(a)); assertEquals(true, a.o); a = new A(); analyzer.option("o", Collections.singletonList(true)).arguments(Collections.emptyList()).invoke(Util.wrap(a)); assertEquals(true, a.o); }
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 <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); }
if (om.getParameter().equals(desc)) { List<LiteralValue> v = new ArrayList<LiteralValue>(om.getValues()); v.addAll(bilto(optionEvent.getValues())); List<String> names = new ArrayList<String>(om.getNames()); names.add(optionEvent.getToken().getName()); option = new OptionMatch(desc, optionEvent.getToken().getName(), bilto(optionEvent.getValues())); argumentEvent.getFrom(), argumentEvent.getTo(), bilto(argumentEvent.getValues()) ); if (argumentEvent.getCommand() == current.getDescriptor()) {
public InvocationMatcher<T> option(String optionName, List<?> optionValue) throws SyntaxException { return options(Collections.<String, List<?>>singletonMap(optionName, optionValue)); }
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<Instance<CRaSH>> matcher = main.descriptor.matcher(); InvocationMatch<Instance<CRaSH>> match = matcher.parse(line.toString()); match.invoke(Util.wrap(main)); } }
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); }
if (options != null && options.size() > 0) { for (Map.Entry<String, ?> option : options.entrySet()) { matcher = matcher.option(option.getKey(), Collections.singletonList(option.getValue())); matcher = matcher.subordinate(subordinate); matcher = matcher.option(option.getKey(), Collections.singletonList(option.getValue())); match = matcher.arguments(arguments != null ? arguments : Collections.emptyList());
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); } }
if (om.getParameter().equals(desc)) { List<LiteralValue> v = new ArrayList<LiteralValue>(om.getValues()); v.addAll(bilto(optionEvent.getValues())); List<String> names = new ArrayList<String>(om.getNames()); names.add(optionEvent.getToken().getName()); option = new OptionMatch(desc, optionEvent.getToken().getName(), bilto(optionEvent.getValues())); argumentEvent.getFrom(), argumentEvent.getTo(), bilto(argumentEvent.getValues()) ); if (argumentEvent.getCommand() == current.getDescriptor()) {
public InvocationMatcher<T> option(String optionName, List<?> optionValue) throws SyntaxException { return options(Collections.<String, List<?>>singletonMap(optionName, optionValue)); }
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); }
@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(); } } };
matcher.parse("-t -- portal:collaboration:/Documents").invoke(Util.wrap(scp)); assertEquals(true, scp.t); assertEquals("portal:collaboration:/Documents", scp.target); scp = new SCP(); matcher.option("t", Collections.singletonList(true)).arguments(Collections.singletonList("portal:collaboration:/Documents")).invoke(Util.wrap(scp)); assertEquals(true, scp.t); assertEquals("portal:collaboration:/Documents", scp.target); matcher.parse("-t").invoke(Util.wrap(scp)); fail(); } catch (CLIException e) { matcher.option("t", Collections.singletonList(true)).arguments(Collections.emptyList()).invoke(Util.wrap(scp)); fail(); } catch (CLIException e) {