ArgumentParser { @SuppressWarnings("unchecked") public static <T> T parse(String args[], Class<T> argProxyInterface) { checkProxyInterfaces(argProxyInterface); if (!validateArguments(args, argProxyInterface)) throw new IllegalArgumentException("Passed args must be valid!"); Map<String, Object> arguments = new HashMap<String, Object>(); for (Method method : argProxyInterface.getMethods()) { String parameterName = methodNameToParameter(method.getName()); String valueString = CmdLineUtil.getParameter(parameterName, args); if (valueString == null) { OptionalParameter optionalParam = method.getAnnotation(OptionalParameter.class); if (optionalParam.defaultValue().length() > 0) valueString = optionalParam.defaultValue(); else valueString = null; } Class<?> returnType = method.getReturnType(); Object value; if (valueString != null) { ArgumentFactory factory = argumentFactories.get(returnType); if (factory == null) throw new IllegalStateException("factory for '" + returnType + "' must not be null"); value = factory.parseArgument(method, parameterName, valueString); } else value = null; arguments.put(method.getName(), value); } return (T) java.lang.reflect.Proxy.newProxyInstance( argProxyInterface.getClassLoader(), new Class[]{argProxyInterface}, new ArgumentProxy(arguments)); } private  ArgumentParser(); @SuppressWarnings({"unchecked"}) static String createUsage(Class<T> argProxyInterface); static String createUsage(Class<T>... argProxyInterfaces); @SuppressWarnings({"unchecked"}) static boolean validateArguments(String args[], Class<T> argProxyInterface); static boolean validateArguments(String args[], Class<T>... argProxyInterfaces); @SuppressWarnings({"unchecked"}) static String validateArgumentsLoudly(String args[], Class<T> argProxyInterface); static String validateArgumentsLoudly(String args[], Class<T>... argProxyInterfaces); @SuppressWarnings("unchecked") static T parse(String args[], Class<T> argProxyInterface); static String[] filter(String args[], Class<T> argProxyInterface);  }
@Test public void testSimpleArguments() { String argsString = "-encoding UTF-8 -alphaNumOpt false"; SimpleArguments args = ArgumentParser.parse(argsString.split(" "), SimpleArguments.class); assertEquals("UTF-8", args.getEncoding()); assertEquals(Integer.valueOf(100), args.getIterations()); assertEquals(null, args.getCutoff()); assertEquals(false, args.getAlphaNumOpt()); }