You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by je...@bnf.fr on 2009/02/27 10:02:21 UTC

[CLI] matching option and options value with a java enum

Hello,

I'm currently using commons-cli 1, and I want to store arguments in enum
form.
I have 2 use case

For the first one, I have a set of args for an option.
-extract argValue1
-extract argValue2
becomes
MyExtractEnum.argValue1
MyExtractEnum.argValue2

Sometime I also want to match a set of options of an Optiongroup with enum
For instance
-ident|-import
with
MyIEnum.ident
or
MyEnum.import


So I've developped two methods doing that.

I don't know if it's would be usefull for some other  people.
If yes, you can see here the corresponding code. (The second one was coded
quite rapidly, perhaps Exception trapping should be better handled).


    /** This method provides conversion from an command line argument
     *  to the matching enum.<br/>
     *  For instance, if in command line we have --extract sparnew, <br/>
     *      <code>OptionValue2Enum("extract",<br/>
     *                               ExtractionModeEnum.class,<br/>
       *                             "stupid default value");,<br/></code>
       *                            will return
ExtractionModeEnum.sparnew<br/>
     * @param <T> enum class pattern
     * @param optionName corresponding optionName, for instance extract
     * @param enumType enum to be converted to.
     * @param defaultValue the default value for this option if the option
is present but not a value
     * @return value in enum form, null if the option is not set
     * @throws ParseException if value does not correspond to enum values.
     */
    private <T extends Enum<T>> T OptionValue2Enum(String optionName,
Class<T> enumType, String defaultValue) throws ParseException {
        if (commandLine.hasOption(optionName)) {
                  String ex = commandLine.getOptionValue(optionName,
defaultValue);
                  ex = ex.trim();
                  try {
                        return Enum.valueOf(enumType, ex);
                  } catch (IllegalArgumentException e) {
                        throw new ParseException("The argument" +
optionName + "is not correctly valued: "
                                                                  + ex + "
found.");
                  }
            } else {
                  return null;
            }
      }



    /** This method checks a command line parameter. Based on enum
definition,<br/>
     *  checks if each value is present in command line as an option.<br/>
     * If yes,  returns it. If none is found, returns the default value.
<br/>
     *  For instance, if in command line can be <code>-ident</code> or
<code>-inodeint</code>,<br/>
     *  IdentificationActivationEnum must contains 2 values ident and
noident<br/>
     *  if comand line contains -ident<br/>
     *   <br/>
     *      <code>OptionValue2Enum(IdentificationActivationEnum.class,<br/>
       *                             "noident");,<br/></code>
       *                            will return
ExtractionModeEnum.ident<br/>
     * @param enumType enum class describing the different options
     * @param defaultValue if parameter not found, the default value
     * @return option set in command line
     * @throws ParseException
     */
    private <T extends Enum<T>> T Option2Enum(Class<T> enumType, String
defaultValue) throws ParseException {
      for (Enum<T> e : enumType.getEnumConstants()) {
            if (commandLine.hasOption(e.toString())) {
                        return Enum.valueOf(enumType, e.toString());
            }
      }
            return Enum.valueOf(enumType, defaultValue);
      }
Cordialement,
-----------------------------------------------
Jérôme Dupont
Bibliothèque Nationale de France
Département des Systèmes d'Information
Tour T3 - Quai François Mauriac
75706 Paris Cedex 13
téléphone: 33 (0)1 53 79 45 40
e-mail: jerome.dupont@bnf.fr
-----------------------------------------------




Avant d'imprimer, pensez à l'environnement. 
Consider the environment before printing this mail.   
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org