Index: lutinutil/src/java/org/codelutin/util/ArgumentsParser.java diff -u lutinutil/src/java/org/codelutin/util/ArgumentsParser.java:1.5 lutinutil/src/java/org/codelutin/util/ArgumentsParser.java:1.6 --- lutinutil/src/java/org/codelutin/util/ArgumentsParser.java:1.5 Thu Dec 23 18:19:15 2004 +++ lutinutil/src/java/org/codelutin/util/ArgumentsParser.java Wed Dec 27 15:56:44 2006 @@ -24,9 +24,9 @@ * Created: 22 août 2003 * * @author Benjamin Poussin -* @version $Revision: 1.5 $ +* @version $Revision: 1.6 $ * -* Mise a jour: $Date: 2004/12/23 18:19:15 $ +* Mise a jour: $Date: 2006/12/27 15:56:44 $ * par : $Author: bpoussin $ */ @@ -39,12 +39,12 @@ /** * Parser d'option de la ligne de commande. -* IDEA: ajouter le type de l'argument a parser (int, double, String, boolean, ) +* IDEA: ajouter le type de l'argument a parser (int, double, String, boolean, Class, File, ...) * et mettre des methodes pour récupérer directement ce type. */ public class ArgumentsParser extends java.lang.Object { /** La liste des arguments qui ne sont pas des options */ - protected ArrayList arguments = new ArrayList(); + protected ArrayList arguments = new ArrayList(); /** le nom à afficher pour l'usage */ protected String name = ""; @@ -52,9 +52,10 @@ * la liste des options, la cle est une option, et la valeur est la liste * des options qui ont été lu grâce à celle-ci. */ - protected HashMap options = new HashMap(); + protected HashMap> options = + new HashMap>(); /** liste a plat de toutes les options lus. */ - protected ArrayList parsedOptions = new ArrayList(); + protected ArrayList parsedOptions = new ArrayList(); /** * Constructor for the ArgumentsParser object @@ -78,27 +79,9 @@ *@see #addOption(Option) */ public ArgumentsParserOption addOption(String optionName, - String description, String[] acceptedOption, int argument) { + String description, int argument, String ... acceptedOption) { return addOption(new ArgumentsParserDefaultOption - (optionName, description, acceptedOption, argument)); - } - - /** - * Permet de d'ajouter une option facilement au parser, sans avoir besoin de - * créer un objet Option. - * - *@param name le nom de cette option. - *@param acceptedOption le marqueur acceptable pour cette option. exemple - * -o, --option ou --options - *@param argument le nombre d'argument que cette option a - *@param description description de cette option - *@return retourne l'option créer et qui sera utilisé - *@see #addOption(Option) - */ - public ArgumentsParserOption addOption(String optionName, String description, - String acceptedOption, int argument) { - return addOption(new ArgumentsParserDefaultOption( - optionName, description, new String[]{acceptedOption}, argument)); + (optionName, description, argument, acceptedOption)); } /** @@ -108,7 +91,7 @@ *@return retourne l'option passé en paramètre */ public ArgumentsParserOption addOption(ArgumentsParserOption option) { - options.put(option, new ArrayList()); + options.put(option, new ArrayList()); return option; } @@ -123,29 +106,29 @@ */ public String checkCoherence() { StringBuffer result = new StringBuffer(); - HashSet optionNames = new HashSet(); - HashSet used = new HashSet(); - for (Iterator i = options.keySet().iterator(); i.hasNext();) { - ArgumentsParserOption option = (ArgumentsParserOption) i.next(); + HashSet optionNames = new HashSet(); + HashSet used = new HashSet(); + + for (ArgumentsParserOption option : options.keySet()) { if (optionNames.contains(option.getName())) { result.append("option name " + option.getName() - + " is allready used\n"); + + " is allready used\n"); } optionNames.add(option.getName()); - String[] acceptedOption = option.getAcceptedOption(); - for (int a = 0; a < acceptedOption.length; a++) { - if (used.contains(acceptedOption[a])) { - result.append("Option " + acceptedOption[a] + " in " - + option.getName() + " is allready used\n"); + String[] acceptedOptions = option.getAcceptedOption(); + for (String acceptedOption : acceptedOptions) { + if (used.contains(acceptedOption)) { + result.append("Option " + acceptedOption + " in " + + option.getName() + " is allready used\n"); } - used.add(acceptedOption[a]); + used.add(acceptedOption); } if (option.getRepetitionMax() > 0 && option.getRepetitionMin() > option.getRepetitionMax()) { result.append("repetition min is over repetition max for: " - + option.getName() + "\n"); + + option.getName() + "\n"); option.setRepetitionMax(option.getRepetitionMin()); } } @@ -163,20 +146,19 @@ * erreur est détectée. */ boolean checkParsing() throws ArgumentsParserException { - for (Iterator i = options.keySet().iterator(); i.hasNext();) { - ArgumentsParserOption option = (ArgumentsParserOption) i.next(); - ArrayList list = (ArrayList) options.get(option); + for (ArgumentsParserOption option : options.keySet()) { + ArrayList list = options.get(option); if (option.getRepetitionMin() > 0 - && list.size() < option.getRepetitionMin()) { + && list.size() < option.getRepetitionMin()) { throw new ArgumentsParserException( - "Error option repeted less than accepted need " - + option.getRepetitionMin() + " find " + list.size() + ": " - + option); + "Error option repeted less than accepted need " + + option.getRepetitionMin() + " find " + list.size() + ": " + + option); } if (option.getRepetitionMax() > 0 - && list.size() > option.getRepetitionMax()) { + && list.size() > option.getRepetitionMax()) { throw new ArgumentsParserException( - "Error option repeted more than accepted: " + option); + "Error option repeted more than accepted: " + option); } } return true; @@ -188,7 +170,7 @@ * *@return la liste des arguments non lu par les options. */ - public ArrayList getArguments() { + public ArrayList getArguments() { return arguments; } @@ -218,11 +200,11 @@ *@return la valeur de l'option si elle existe, null sinon */ public String [] getParsedOption(String optionName) { - ArrayList list = getParsedOptions(optionName); + ArrayList list = getParsedOptions(optionName); if (list.size() == 0){ return null; } else { - return ((ArgumentsParserOption)list.get(0)).getArguments(); + return list.get(0).getArguments(); } } @@ -232,7 +214,7 @@ * *@return la liste de toutes les options lus */ - public ArrayList getParsedOptions() { + public ArrayList getParsedOptions() { return parsedOptions; } @@ -244,12 +226,11 @@ * aucune option de ce nom existe ou si cette option n'a pas été trouvé * sur la ligne de commande */ - public ArrayList getParsedOptions(String optionName){ - ArrayList result = new ArrayList(); - for (Iterator i = options.keySet().iterator(); i.hasNext(); ) { - ArgumentsParserOption option = (ArgumentsParserOption) i.next(); + public ArrayList getParsedOptions(String optionName){ + ArrayList result = new ArrayList(); + for (ArgumentsParserOption option : options.keySet()) { if (optionName.equals(option.getName())) { - result = (ArrayList) options.get(option); + result = options.get(option); } } return result; @@ -266,11 +247,10 @@ * ou null si aucune option ne peut lire l'argument courant. */ ArgumentsParserOption lookingForOption(String[] args, int index) { - for (Iterator i = options.keySet().iterator(); i.hasNext(); ) { - ArgumentsParserOption option = (ArgumentsParserOption) i.next(); + for (ArgumentsParserOption option : options.keySet()) { ArgumentsParserOption copy = option.copy(); if (copy.parse(args, index)) { - ArrayList list = (ArrayList) options.get(option); + ArrayList list = options.get(option); parsedOptions.add(copy); list.add(copy); return copy; @@ -288,16 +268,16 @@ */ public static void main(String[] args) throws Exception { ArgumentsParser ap = new ArgumentsParser("ArgumentsParserTest"); - ap.addOption("project", "le nom du projet", "-p", 1) + ap.addOption("project", "le nom du projet", 1, "-p") .setRepetitionMin(1); - ap.addOption("lib", "les librairies", "-l", 1) + ap.addOption("lib", "les librairies", 1, "-l") .setRepetitionMin(2).setRepetitionMax(1); - ap.addOption("truc", "les dummy", "-t", 1); + ap.addOption("truc", "les dummy", 1, "-t"); ap.addOption("package", "le package utilisé", - new String[]{"-k", "--package"}, 1).setRepetitionMax(10); + 1, "-k", "--package").setRepetitionMax(10); System.out.println(ap.usage()); System.out.println(); @@ -365,8 +345,7 @@ public String usage() { StringBuffer result = new StringBuffer(); result.append("usage: " + getName() + " "); - for (Iterator i = options.keySet().iterator(); i.hasNext();) { - ArgumentsParserOption option = (ArgumentsParserOption) i.next(); + for (ArgumentsParserOption option : options.keySet()) { if (option.getRepetitionMin() <= 0) { result.append("["); } else { @@ -390,8 +369,7 @@ } } result.append("\n\n"); - for (Iterator oi = options.keySet().iterator(); oi.hasNext();) { - ArgumentsParserOption option = (ArgumentsParserOption) oi.next(); + for (ArgumentsParserOption option : options.keySet()) { result.append(option.getName()); result.append("("); String[] acceptedOption = option.getAcceptedOption(); Index: lutinutil/src/java/org/codelutin/util/ArgumentsParserDefaultOption.java diff -u lutinutil/src/java/org/codelutin/util/ArgumentsParserDefaultOption.java:1.2 lutinutil/src/java/org/codelutin/util/ArgumentsParserDefaultOption.java:1.3 --- lutinutil/src/java/org/codelutin/util/ArgumentsParserDefaultOption.java:1.2 Thu Dec 23 18:19:15 2004 +++ lutinutil/src/java/org/codelutin/util/ArgumentsParserDefaultOption.java Wed Dec 27 15:56:44 2006 @@ -17,7 +17,7 @@ *@author Benjamin Poussin * * Copyright Code Lutin - *@version $Revision: 1.2 $ Mise a jour: $Date: 2004/12/23 18:19:15 $ par : $Author: bpoussin $ + *@version $Revision: 1.3 $ Mise a jour: $Date: 2006/12/27 15:56:44 $ par : $Author: bpoussin $ */ package org.codelutin.util; @@ -41,7 +41,7 @@ /** le nombre d'argument de cette option */ int argument = 0; /** la liste des arguments */ - ArrayList argumentList = new ArrayList(); + ArrayList argumentList = new ArrayList(); /** * le nombre de fois minimum que l'option peut-être répétée sur la ligne de * commande @@ -49,10 +49,10 @@ int repetitionMin = 0; /** * le nombre de fois maximum que l'option peut-être répétée sur la ligne de - * commande + * commande: 1 par defaut */ - int repetitionMax = 0; - /** TODO Description of the Field */ + int repetitionMax = 1; + /** Description of the argument */ String description = ""; /** @@ -62,7 +62,7 @@ *@param description TODO Description of the Parameter */ public ArgumentsParserDefaultOption(String name, String description, - String[] acceptedOption, int argument) { + int argument, String ... acceptedOption) { this.name = name; this.description = description; this.acceptedOption = acceptedOption; @@ -79,7 +79,7 @@ *@return TODO Description of the Return Value */ public ArgumentsParserOption copy() { - return new ArgumentsParserDefaultOption(name, description, acceptedOption, argument).setRepetitionMin(getRepetitionMin()).setRepetitionMax(getRepetitionMax()); + return new ArgumentsParserDefaultOption(name, description, argument, acceptedOption).setRepetitionMin(getRepetitionMin()).setRepetitionMax(getRepetitionMax()); } /** @@ -180,9 +180,9 @@ } /** - * Description of the Method + * return numbers of args for this option * - *@return Description of the Return Value + * @return numbers of args for this option */ public int count() { return argument;