Index: lutinutil/src/java/org/codelutin/util/OptionParser.java diff -u lutinutil/src/java/org/codelutin/util/OptionParser.java:1.5 lutinutil/src/java/org/codelutin/util/OptionParser.java:1.6 --- lutinutil/src/java/org/codelutin/util/OptionParser.java:1.5 Wed Nov 28 20:57:16 2007 +++ lutinutil/src/java/org/codelutin/util/OptionParser.java Thu Nov 29 22:25:29 2007 @@ -29,7 +29,9 @@ import java.io.IOException; import java.io.Writer; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -78,7 +80,7 @@ } } - final org.apache.commons.logging.Log log; + protected final org.apache.commons.logging.Log log; /** le context du parseur (contenant les données temporaires du parsing). */ protected final ParserContext context; @@ -90,7 +92,7 @@ * la liste des options, la cle est le nom de l'option, et la valeur * est la liste des options qui ont été lues grâce à celle-ci. */ - protected final HashMap> options; + protected final Map> options; /** * La liste des arguments qui ne sont pas des options insérés dans leur @@ -98,6 +100,8 @@ */ protected final List arguments; + protected final Map actions; + public OptionParser() { // find the fullyQualifiedFactoryName via OptionParserA annotation @@ -107,12 +111,12 @@ throw new IllegalArgumentException("could not found annotation " + OptionParserA.class + " in Parser " + this); } - log = LogFactory.getLog(getClass()); - + this.log = LogFactory.getLog(getClass()); this.definitions = new LinkedHashMap(); this.arguments = new ArrayList(); this.options = new HashMap>(); + this.actions = new HashMap(); try { Map map; OptionDefinitionA defA; @@ -134,16 +138,36 @@ } keys1.clear(); map.clear(); - // create parser context context = new ParserContext(this.definitions); } catch (NoSuchFieldException e) { throw new IllegalArgumentException(e); } + } + /** + * Register an action associated to an Option. + * + * @param optionKey option key + * @param action action to realise for the option + */ + public void registerAction(String optionKey, OptionAction action) { + if (!definitions.containsKey(optionKey)) { + throw new IllegalArgumentException("could not found this option " + optionKey); + } + if (actions.containsKey(optionKey)) { + throw new IllegalArgumentException("already action registred for this option " + optionKey); + } + actions.put(optionKey, action); } + /** + * Launch parsing of some arguments. + * + * @param args arguments to parse + * @throws OptionParserException if any problem while parsing + */ public void doParse(String... args) throws OptionParserException { if (args.length > 0) { // detect OptionContexts @@ -220,8 +244,8 @@ // close the current optionContexts and add it to result context.addOptionContext(optionContext); - - optionContext = new OptionContext(newOption, argument, i); + optionContext = new OptionContext(newOption, + actions.get(newOption.getName()),argument,i); continue; } if (optionContext == null || !optionContext.addArgument(context, argument, i)) { @@ -286,17 +310,10 @@ position++; } } - // finally instanciate option and store it - Option option; + // finally instanciate option and store it String key = optionContext.definition.getName(); - - if (arguments.isEmpty()) { - option = new Option(optionContext.alias); - } else { - option = new Option(optionContext.alias, - arguments.toArray(new OptionArgument[arguments.size()])); - } + Option option = optionContext.newOption(arguments); List