View Javadoc
1   package org.apache.fulcrum.yaafi.cli;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /**
23  * Extremely simple command line parsing class.
24  */
25  
26  public class Getopt
27  {
28      /** the prefix for determining command line parameters, e.g "-" or "--" */
29      private String prefix;
30  
31      /** the command line parameters */
32      private String[] args;
33  
34      /**
35       * Constructor
36       * @param args the command line parameters
37       */
38      public Getopt( String[] args )
39      {
40          this(args,"--");
41      }
42  
43      /**
44       * Constructor.
45       *
46       * @param args the command line parameters
47       * @param prefix the prefix for command line parameters
48       */
49      public Getopt( String[] args, String prefix )
50      {
51          this.prefix = prefix;
52  
53          if( args == null )
54          {
55              this.args = new String[0];
56          }
57          else
58          {
59              this.args = args;
60          }
61      }
62  
63      /**
64       * Does the command-line options exist?
65       *
66       * @param option the option we are looking for
67       * @return is the given option contained in the command line arguments?
68       */
69  
70      public boolean contains( String option )
71      {
72          return( this.find(option) >= 0 ? true : false );
73      }
74  
75      /**
76       * @return the number of command line arguments
77       */
78      public int length()
79      {
80          return this.args.length;
81      }
82  
83      /**
84       * Returns the string value for the given option.
85       *
86       * @param option the option
87       * @return the associated value
88       */
89      public String getStringValue( String option )
90      {
91          return this.getValue(option);
92      }
93  
94      /**
95       * Returns the string value for the given option.
96       *
97       * @param option the option
98       * @param defaultValue the default value if the option is not defined
99       * @return the associated value
100      */
101     public String getStringValue( String option, String defaultValue )
102     {
103         return this.getValue(option,defaultValue);
104     }
105 
106     /**
107      * Returns the boolean value for the given option.
108      *
109      * @param option the option
110      * @return the associated value
111      */
112 
113     public boolean getBooleanValue( String option )
114     {
115         return Boolean.valueOf(this.getValue(option)).booleanValue();
116     }
117 
118     /**
119      * Returns the boolean value for the given option.
120      *
121      * @param option the option
122      * @param defaultValue the default value if the option is not defined
123      * @return the associated value
124      */
125     public boolean getBooleanValue( String option, boolean defaultValue )
126     {
127         String temp = Boolean.toString(defaultValue);
128         return Boolean.valueOf(this.getValue(option,temp)).booleanValue();
129     }
130 
131     /**
132      * Get the given argument.
133      *
134      * @param index the index of the command line argument
135      * @return the commandl ine argument
136      */
137     private String getArg( int index )
138     {
139         return this.args[index];
140     }
141 
142     /**
143      * Find a command-line option.
144      *
145      * @param option the option
146      * @return the index of the give option or -1 otherwise
147      */
148     private int find( String option )
149     {
150         String strOption = this.prefix + option;
151 
152         // Iterate through all command line arguments and look for "-[chOption]"
153 
154         for( int i = 0; i < args.length; i++)
155         {
156             if ( args[i].equals( strOption ) )
157             {
158                 return i;
159             }
160         }
161 
162         return -1;
163     }
164 
165     /**
166      * Determines if a value is defined for the given option.
167      *
168      * @return true if a value is defined
169      */
170     private boolean hasValue( int index )
171     {
172         String value;
173 
174         if( (index+1) < this.length() )
175         {
176             value = this.getArg(index+1);
177 
178             if( value.startsWith(this.prefix) )
179             {
180                 return false;
181             }
182             else
183             {
184                 return true;
185             }
186         }
187         else
188         {
189             return false;
190         }
191     }
192 
193     /**
194      * Get the value of a command line option.
195      *
196      * @param option the option
197      * @return the value of the option
198      */
199     private String getValue( String option )
200     {
201         String value = this.getValue(option,null);
202 
203         if( value == null )
204         {
205             // the options is there but no value was defined by the caller
206             String msg = "No value supplied for " + this.prefix + option;
207             throw new IllegalArgumentException( msg );
208         }
209         else
210         {
211             return value;
212         }
213     }
214 
215     /**
216      * Get the value of a command line option
217      * @param option the option
218      * @param defaultValue the default value if the option was not found
219      * @return the value of the option
220      */
221     private String getValue( String option, String defaultValue )
222     {
223         int index = this.find(option);
224 
225         if( index < 0 )
226         {
227             // the option is not found
228             return defaultValue;
229         }
230 
231         if( this.hasValue(index) )
232         {
233             // a value is available for this option
234             return this.getArg(index+1);
235         }
236         else
237         {
238             // the options is there but no value was defined by the caller
239             String msg = "No value supplied for " + this.prefix + option;
240             throw new IllegalArgumentException( msg );
241         }
242     }
243 }
244