DBVIEW
src/org/dbview/utils/args4j/SpecialStringOptionHandler.java
00001 /*
00002         DbView - Graph Visualization
00003     Copyright (C) 2012  Denis BEURIVE
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 /**
00020  * @author Denis Beurive
00021  */
00022 
00023 package org.dbview.utils.args4j;
00024 
00025 import org.kohsuke.args4j.CmdLineException;
00026 import org.kohsuke.args4j.CmdLineParser;
00027 import org.kohsuke.args4j.OptionDef;
00028 import org.kohsuke.args4j.spi.Parameters;
00029 import org.kohsuke.args4j.spi.Setter;
00030 import org.kohsuke.args4j.spi.OptionHandler;;
00031 
00032 /**
00033  * This class implements a special kind of options' values.
00034  * Options' values of this kind can not start with a dash.
00035  * For example, the option's value "-toto" is forbidden.
00036  * This rule has been set, so that it is possible to differentiate between an option (that starts with a dash) and a value.
00037  * @author Denis BEURIVE
00038  * @see args4j documentation.
00039  */
00040 public class SpecialStringOptionHandler extends OptionHandler<String>
00041 {
00042     /**
00043      * Constructor.
00044      * @param parser Args4J command line parser.
00045      * @param option See documentation for Args4J.
00046      * @param setter See documentation for Args4J.
00047      */
00048     public SpecialStringOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super String> setter)
00049     {
00050         super(parser, option, setter);
00051     }
00052 
00053     /**
00054      * This method sets the parsed argument.
00055      * @param in_parameters List of strings that represents the parameters for the option.
00056      * @warning Keep in mind that this method may set the value null.
00057      * @return See documentation for Args4J.
00058      */
00059     public int parseArguments(Parameters in_parameters) throws CmdLineException
00060     {
00061         String option_value = in_parameters.getParameter(0);
00062 
00063         if (null == option_value) { setter.addValue(null); return 1; }
00064 
00065         if (option_value.startsWith("-"))
00066         { throw new CmdLineException(this.owner, "Invalid option's value. Option values can not start with a dash. Dashes are used to specify options. If the option's value you need to specify starts with a dash, then you must escape it. Exemple \"-myValue\" becomes \"\\-myValue\"."); }
00067         option_value = option_value.replaceFirst("^\\\\", "");
00068         setter.addValue(option_value);
00069         return 1;
00070     }
00071 
00072     /**
00073      * See documentation for Args4J.
00074      * @return See documentation for Args4J.
00075      */
00076     public String getDefaultMetaVariable()
00077     {
00078         return "VAL";
00079     }
00080 }