001    /*--------------------------------------------------------------------------+
002    $Id: CmdLine.java 26283 2010-02-18 11:18:57Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.options;
019    
020    import java.util.ArrayList;
021    
022    import edu.tum.cs.commons.string.StringUtils;
023    
024    /**
025     * A very simple class for parsing command line parameters.
026     * <p>
027     * A typical command line looks like this:
028     * <p>
029     * <code>-dir src -count occurrences TEST -dde</code>
030     * <p>
031     * 
032     * In this example the minus symbol ('-') is the <i>parameter prefix </i>,
033     * <code>dir</code>,<code>count</code> and <code>dde</code> are
034     * <i>parameters </i>. Whereas <code>dir</code> has a single <i>value </i>
035     * <code>src</code> and <code>count</code> has the two <i>value </i>
036     * <code>occurrences</code> and <code>TEST</code>.
037     * <p>
038     * Typical method calls would have the following results. <table>
039     * <tr>
040     * <th>method call</th>
041     * <th>result</th>
042     * </tr>
043     * <tr>
044     * <td><code>hasParameter("dde")</code></td>
045     * <td><code>true</code></td>
046     * </tr>
047     * <tr>
048     * <td><code>hasParameterAndValue("dde")</code></td>
049     * <td><code>false</code></td>
050     * </tr>
051     * <tr>
052     * <td><code>hasParameter("TEST")</code></td>
053     * <td><code>false</code></td>
054     * </tr>
055     * <tr>
056     * <td><code>getValue("src")</code></td>
057     * <td>"dir"</td>
058     * </tr>
059     * <tr>
060     * <td><code>getValue("count")</code></td>
061     * <td>"occurrences"</td>
062     * </tr>
063     * <tr>
064     * <td><code>getValues("count")</code></td>
065     * <td>["occurrences", "TEST"]</td>
066     * </tr>
067     * </table>
068     * 
069     * @deprecated Use the CommandLine class instead.
070     * 
071     * @author Florian Deissenboeck
072     * @author $Author: juergens $
073     * 
074     * @version $Rev: 26283 $
075     * @levd.rating GREEN Hash: 2C62BA90E8DC1F4D02B2E9BD96EB6A5E
076     * 
077     */
078    @Deprecated
079    public class CmdLine {
080        /** Parameter store. */
081        private final String[] parameters;
082    
083        /** The prefix. */
084        private final String parameterPrefix;
085    
086        /**
087         * Create new <code>CmdLine</code> -object from command line arguments.
088         * Parameter prefix is "-".
089         * 
090         * @param params
091         *            command line arguments as provided in <code>main()</code>
092         *            -method.
093         */
094        public CmdLine(String[] params) {
095            this(params, "-");
096    
097        }
098    
099        /**
100         * Create new <code>CmdLine</code> -object from command line arguments.
101         * 
102         * @param params
103         *            command line arguments as provided in <code>main()</code>
104         *            -method.
105         * @param parameterPrefix
106         *            parameter prefix
107         */
108        public CmdLine(String[] params, String parameterPrefix) {
109            this.parameters = params;
110            this.parameterPrefix = parameterPrefix;
111    
112        }
113    
114        /**
115         * Get number of parameters.
116         * 
117         * @return number of parameters.
118         */
119        public int getParameterCount() {
120            return parameters.length;
121        }
122    
123        /**
124         * Get the values for a parameter.
125         * 
126         * @param parameterName
127         *            name of the parameter.
128         * @return the values associated with this parameter. If the parameter is
129         *         not present or doesn't habe a value <code>null</code> is
130         *         returned.
131         */
132        public String[] getValues(String parameterName) {
133            if (!hasParameter(parameterName)) {
134                return null;
135            }
136            int index = StringUtils.indexOf(parameters, parameterPrefix
137                    + parameterName);
138            ArrayList<String> result = new ArrayList<String>();
139            for (int i = index + 1; i < parameters.length; i++) {
140                String current = parameters[i].trim();
141                if (!isValue(current)) {
142                    break;
143                }
144                result.add(current);
145            }
146            if (result.size() == 0) {
147                return null;
148            }
149            return result.toArray(new String[0]);
150        }
151    
152        /**
153         * Get the value for a parameter.
154         * 
155         * @param parameterName
156         *            name of the parameter.
157         * @return the value associated with this parameter. If the parameter is not
158         *         present or doesn't habe a value <code>null</code> is returned.
159         */
160        public String getValue(String parameterName) {
161            if (!hasParameter(parameterName)) {
162                return null;
163            }
164            int index = StringUtils.indexOf(parameters, parameterPrefix
165                    + parameterName);
166            String result = parameters[index + 1].trim();
167            if (!isValue(result)) {
168                return null;
169            }
170            return result;
171        }
172    
173        /**
174         * Checks if this command line has a certain parameter.
175         * 
176         * @param parameterName
177         *            name of the parameter
178         * @return <code>true</code> if parameter is present, <code>false</code>
179         *         otherwise.
180         */
181        public boolean hasParameter(String parameterName) {
182            int index = StringUtils.indexOf(parameters, parameterPrefix
183                    + parameterName);
184            return (index != -1);
185        }
186    
187        /**
188         * Checks if this command line has a certain parameter with at least one
189         * value.
190         * 
191         * @param parameterName
192         *            name of the parameter
193         * @return <code>true</code> if parameter and value is present,
194         *         <code>false</code> otherwise.
195         */
196        public boolean hasParameterAndValue(String parameterName) {
197            int index = StringUtils.indexOf(parameters, parameterPrefix
198                    + parameterName);
199            if (index < 0) {
200                return false;
201            }
202            if (index >= parameters.length - 1) {
203                return false;
204            }
205            return isValue(parameters[index + 1]);
206        }
207    
208        /**
209         * Check is a certain string is a value, i.e. is no parameter.
210         * 
211         * @param string
212         *            the string in question.
213         * @return <code>true</code> it is a value, <code>false</code>
214         *         otherwise.
215         */
216        private boolean isValue(String string) {
217            return (string.trim().indexOf(parameterPrefix) != 0);
218        }
219    
220    }