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)); } }
InvocationMatch<T> current = new InvocationMatch<T>(descriptor); Event.Option optionEvent = (Event.Option)event; OptionDescriptor desc = optionEvent.getParameter(); Iterable<OptionMatch> options = current.options(); OptionMatch option = null; for (OptionMatch om : options) { option = new OptionMatch(desc, optionEvent.getToken().getName(), bilto(optionEvent.getValues())); current.option(option); } else if (event instanceof Event.Subordinate) { current = current.subordinate(((Event.Subordinate)event).getDescriptor().getName()); } else if (event instanceof Event.Argument) { Event.Argument argumentEvent = (Event.Argument)event; bilto(argumentEvent.getValues()) ); if (argumentEvent.getCommand() == current.getDescriptor()) { current.argument(match); } else { throw new AssertionError(); rest.append(token.getRaw()); current.setRest(rest.toString());
public Object invoke(T command) throws InvocationException, SyntaxException { CommandInvoker<T, ?> invoker = getInvoker(); if (invoker != null) { return invoker.invoke(command); } else { return null; } }
for (InvocationMatch<T> current = match;current != null && helpDesc == null;current = current.owner()) { helpDesc = current.getParameter(HELP_OPTION);
private <T> Test(Class<T> type, String s) { try { CommandDescriptor<Instance<T>> command = CommandFactory.DEFAULT.create(type); InvocationMatcher<Instance<T>> parser = command.matcher(); InvocationMatch<Instance<T>> match = parser.parse(s); // if (match.owner() == null) { this.sharedOptionMatches = new LinkedList<OptionMatch>(); this.optionMatches = new LinkedList<OptionMatch>(match.options()); this.argumentMatches = new LinkedList<ArgumentMatch>(match.arguments()); this.rest = match.getRest(); this.methodName = null; } else { this.sharedOptionMatches = new LinkedList<OptionMatch>(match.owner().options()); this.optionMatches = new LinkedList<OptionMatch>(match.options()); this.argumentMatches = new LinkedList<ArgumentMatch>(match.arguments()); this.rest = match.getRest(); this.methodName = match.getDescriptor().getName(); } } catch (Exception e) { AssertionFailedError afe = new AssertionFailedError(); afe.initCause(e); throw afe; } }
@Override public void printSynopsisSection(CommandDescriptor<?> descriptor, Appendable stream) throws IOException { man.printSynopsisSection(descriptor, stream); // Extra stream section if (match.getDescriptor().getSubordinates().isEmpty()) { stream.append("STREAM\n"); stream.append(Util.MAN_TAB); printFQN(descriptor, stream); stream.append(" <").append(commandMatch.getConsumedType().getName()).append(", ").append(commandMatch.getProducedType().getName()).append('>'); stream.append("\n\n"); } } };
public void testOption() throws Exception { // CommandDescriptor<Instance<A>> desc = CommandFactory.DEFAULT.create(A.class); HelpDescriptor<Instance<A>> helpDesc = new HelpDescriptor<Instance<A>>(desc); OptionDescriptor optionDesc = helpDesc.getOption("-h"); assertNotNull(optionDesc); OptionDescriptor foo = helpDesc.getOption("-f"); assertNotNull(foo); InvocationMatcher<Instance<A>> matcher = helpDesc.matcher(); // InvocationMatch<Instance<A>> match = matcher.parse("--help"); ParameterMatch<OptionDescriptor> helpMatch = match.getParameter(optionDesc); assertNotNull(helpMatch); CommandInvoker<Instance<A>, ?> invoker = match.getInvoker(); Help help = (Help)invoker.invoke(Util.wrap(new A())); assertNotNull(help); assertSame(desc, help.getDescriptor().getDelegate()); // match = matcher.parse(""); invoker = match.getInvoker(); invoker.invoke(Util.wrap(new A())); match = matcher.parse("-f foo_value bar"); invoker = match.getInvoker(); assertEquals("invoked:foo_value", invoker.invoke(Util.wrap(new A()))); }
@Override public CommandInvoker<Void, Object> getInvoker() throws CommandException { List<String> chunks = Utils.chunks(match.getRest()); String[] args = chunks.toArray(new String[chunks.size()]); return GroovyScriptShellCommand.this.getInvoker(args); }
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); } }
static void bind(InvocationMatch<?> match, Iterable<ParameterDescriptor> parameters, Object target, Object[] args) throws SyntaxException, InvocationException { for (ParameterDescriptor parameter : parameters) { ParameterMatch parameterMatch = match.getParameter(parameter); Object value = parameterMatch != null ? parameterMatch.computeValue() : null; if (value == null) { if (parameter.getDeclaredType().isPrimitive() || parameter.isRequired()) { if (parameter instanceof ArgumentDescriptor) { ArgumentDescriptor argument = (ArgumentDescriptor)parameter; throw new SyntaxException("Missing argument " + argument.getName()); } else { OptionDescriptor option = (OptionDescriptor)parameter; throw new SyntaxException("Missing option " + option.getNames()); } } } else { ((Binding)parameter).set(target, args, value); } } }
bind(match.owner(), owner.getParameters(), command, Util.EMPTY_ARGS);
match.getDescriptor().print(format, buffer); return buffer.toString();
public void testMain1() throws Exception { CommandDescriptor<Instance<b>> desc = CommandFactory.DEFAULT.create(b.class); HelpDescriptor<Instance<b>> helpDesc = new HelpDescriptor<Instance<b>>(desc); OptionDescriptor optionDesc = helpDesc.getOption("-h"); assertNotNull(optionDesc); InvocationMatcher<Instance<b>> matcher = helpDesc.matcher(); // InvocationMatch<Instance<b>> match = matcher.parse("--help"); ParameterMatch<OptionDescriptor> helpMatch = match.getParameter(optionDesc); assertNotNull(helpMatch); CommandInvoker<Instance<b>, ?> invoker = match.getInvoker(); Help help = (Help)invoker.invoke(Util.wrap(new b())); assertNotNull(help); CommandDescriptor mainDescriptor = help.getDescriptor(); assertEquals("b", mainDescriptor.getName()); assertSame(null, mainDescriptor.getOwner()); } }
public void open2(final CommandContext<P> consumer) { invocationContext = new InvocationContextImpl<P>(consumer); command.pushContext(invocationContext); command.unmatched = invoker.getMatch().getRest(); }
for (InvocationMatch<T> current = match;current != null && helpDesc == null;current = current.owner()) { helpDesc = current.getParameter(HELP_OPTION);
void configure(InvocationMatch<T> classMatch, T command) throws InvocationException, SyntaxException { for (ParameterDescriptor parameter : getParameters()) { ParameterMatch match = classMatch.getParameter(parameter); if (match == null) { if (parameter.isRequired()) { if (parameter instanceof ArgumentDescriptor) { ArgumentDescriptor argument = (ArgumentDescriptor)parameter; throw new SyntaxException("Missing argument " + argument.getName()); } else { OptionDescriptor option = (OptionDescriptor)parameter; throw new SyntaxException("Missing option " + option.getNames()); } } } else { Object value = match.computeValue(); Field f = ((ClassFieldBinding)parameter.getBinding()).getField(); try { f.setAccessible(true); f.set(command, value); } catch (Exception e) { throw new InvocationException(e.getMessage(), e); } } } }
bind(match.owner(), owner.getParameters(), command, Util.EMPTY_ARGS);
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); } } }
InvocationMatch<T> current = new InvocationMatch<T>(descriptor); InvocationMatch<T> sub = current.subordinate(mainName); if (sub != null) { current = sub; Event.Option optionEvent = (Event.Option)event; OptionDescriptor desc = optionEvent.getParameter(); Iterable<OptionMatch> options = current.options(); OptionMatch option = null; for (OptionMatch om : options) { option = new OptionMatch(desc, optionEvent.getToken().getName(), bilto(optionEvent.getValues())); current.option(option); } else if (event instanceof Event.Subordinate) { current = current.subordinate(((Event.Subordinate)event).getDescriptor().getName()); } else if (event instanceof Event.Argument) { Event.Argument argumentEvent = (Event.Argument)event; bilto(argumentEvent.getValues()) ); if (argumentEvent.getCommand() == current.getDescriptor()) { current.argument(match); } else { throw new AssertionError(); current.setRest(rest.toString());
org.crsh.cli.impl.invocation.CommandInvoker<Instance<T>,?> invoker = match.getInvoker();