public static boolean intToBool(int input) { if (input < 0 || input > 1) { throw new IllegalParameterException("input must be 0 or 1"); } // Note we designate 1 as true and 0 as false though some may disagree return input == 1; }
@Override public Help invoke(T command) throws InvocationException, SyntaxException { return new Help<T>(HelpDescriptor.this); } };
public static <T> HelpDescriptor<T> create(CommandDescriptor<T> descriptor) throws IntrospectionException { return new HelpDescriptor<T>(descriptor); }
public ClassShellCommand(Class<T> clazz) throws IntrospectionException { CommandFactory factory = new CommandFactory(getClass().getClassLoader()); this.clazz = clazz; this.descriptor = HelpDescriptor.create(factory.create(clazz)); }
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()); } }
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); } } }
addParameter(parameter); add = !(getOptionNames().contains("-h") || getOptionNames().contains("--help")); for (CommandDescriptor<T> sub : delegate.getSubordinates().values()) { if (sub.getOptionNames().contains("-h") || getOptionNames().contains("--help")) { add = false; addParameter(HELP_OPTION); subordinates.put(subordinate.getName(), new HelpDescriptor<T>(this, subordinate));
throw new IntrospectionException("Command name cannot be null"); } else { for (int i = 0;i < nameLength;i++) { if (i == 0) { if (!Character.isLetter(c)) { throw new IntrospectionException("Invalid command name <" + name + "> does not start with a letter"); throw new IntrospectionException("Invalid command name <" + name + "> char " + c + " at position " + i + " is now allowed");
multiplicity = Multiplicity.MULTI; } else { throw new IllegalValueTypeException(); throw new IllegalValueTypeException(); throw new IllegalValueTypeException(); throw new IllegalValueTypeException(); valueType = factory.get(declaredType); if (valueType == null) { throw new IllegalValueTypeException("Type " + declaredType.getName() + " is not handled at the moment");
@Override public String toString() { try { StringBuilder sb = new StringBuilder(); descriptor.printUsage(sb); return sb.toString(); } catch (IOException e) { throw new AssertionError(e); } } }
public GroovyScriptShellCommand(Class<T> clazz) throws IntrospectionException { // CommandFactory factory = new CommandFactory(getClass().getClassLoader()); boolean hasExplicitReturn; try { clazz.getDeclaredField(ScriptLastStatementTransformer.FIELD_NAME); hasExplicitReturn = true; } catch (NoSuchFieldException e) { hasExplicitReturn = false; } // this.clazz = clazz; this.descriptor = HelpDescriptor.create(factory.create(clazz)); this.hasExplicitReturn = hasExplicitReturn; }
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()))); }
private static void checkChar(String s, int index, BitSet authorized) throws IllegalParameterException { if (!authorized.get(s.charAt(index))) { throw new IllegalParameterException("Option name " + s + " cannot contain " + s.charAt(index) + " at position " + index); } }
@Override protected void addParameter(ParameterDescriptor parameter) throws IntrospectionException { // Check we can add the option if (parameter instanceof OptionDescriptor) { OptionDescriptor option = (OptionDescriptor)parameter; Set<String> blah = new HashSet<String>(); for (String optionName : option.getNames()) { blah.add((optionName.length() == 1 ? "-" : "--") + optionName); } for (MethodDescriptor<T> method : methods.values()) { Set<String> diff = new HashSet<String>(method.getOptionNames()); diff.retainAll(blah); if (diff.size() > 0) { throw new IntrospectionException("Cannot add method " + method.getName() + " because it has common " + " options with its class: " + diff); } } } // super.addParameter(parameter); }
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); }
throw new IllegalParameterException(); for (String name : names) { if (name == null) { throw new IllegalParameterException("Option name must not be null"); throw new IllegalParameterException("Option name cannot be empty"); throw new IllegalParameterException("Option name " + name + " cannot start with " + name.charAt(0));
name = "-" + optionName; if (shortOptionNames.contains(name)) { throw new IntrospectionException("Duplicate option " + name); } else { shortOptionNames.add(name); name = "--" + optionName; if (longOptionNames.contains(name)) { throw new IntrospectionException(); } else { longOptionNames.add(name); if (argument.getMultiplicity() == Multiplicity.MULTI) { if (listArgument) { throw new IntrospectionException();
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 <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 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()); }