helpDesc = current.getParameter(HELP_OPTION);
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); } } }
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()); } }
helpDesc = current.getParameter(HELP_OPTION);
@Override public Object invoke(Resolver resolver, T command) throws InvocationException, SyntaxException { // Get the option from the top match ParameterMatch<OptionDescriptor> help = null; for (InvocationMatch<T> current = match;current != null && help == null;current = current.owner()) { help = current.getParameter(HELP_OPTION); } // if (help == null && invoker != null) { return invoker.invoke(resolver, command); } else { return new Help<T>(delegate); } } };
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()))); }
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); } } } }
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); } } }
ParameterMatch match = _match.getParameter(parameter); if (match != null) { v = match.computeValue();