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  * Extremly simply 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 paramters
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       * @param option the option we are looking for
65       * @return is the given option contained in the command line arguments?
66       */
67  
68      public boolean contains( String option )
69      {
70          return( this.find(option) >= 0 ? true : false );
71      }
72  
73      /**
74       * @return the number of command line arguments
75       */
76      public int length()
77      {
78          return this.args.length;
79      }
80  
81      /**
82       * Returns the string value for the given option.
83       * @param option the option
84       * @return the associated value
85       */
86      public String getStringValue( String option )
87      {
88          return this.getValue(option);
89      }
90  
91      /**
92       * Returns the string value for the given option.
93       * @param option the option
94       * @param defaultValue the default value if the option is not defined
95       * @return the associated value
96       */
97      public String getStringValue( String option, String defaultValue )
98      {
99          return this.getValue(option,defaultValue);
100     }
101 
102     /**
103      * Returns the boolean value for the given option.
104      * @param option the option
105      * @return the associated value
106      */
107 
108     public boolean getBooleanValue( String option )
109     {
110         return Boolean.valueOf(this.getValue(option)).booleanValue();
111     }
112 
113     /**
114      * Returns the boolean value for the given option.
115      * @param option the option
116      * @param defaultValue the default value if the option is not defined
117      * @return the associated value
118      */
119     public boolean getBooleanValue( String option, boolean defaultValue )
120     {
121         String temp = Boolean.toString(defaultValue);
122         return Boolean.valueOf(this.getValue(option,temp)).booleanValue();
123     }
124 
125     /**
126      * Get the given argument.
127      * @param index the index of the command line argument
128      * @return the commandl ine argument
129      */
130     private String getArg( int index )
131     {
132         return this.args[index];
133     }
134 
135     /**
136      * @option the option
137      * @return the index of the give option or -1 otherwise
138      */
139     private int find( String option )
140     {
141         String strOption = this.prefix + option;
142 
143         // Iterate through all command line arguments and look for "-[chOption]"
144 
145         for( int i = 0; i < args.length; i++)
146         {
147             if ( args[i].equals( strOption ) )
148             {
149                 return i;
150             }
151         }
152 
153         return -1;
154     }
155 
156     /**
157      * Determines if a value is defined for the given option
158      * @param option the given option
159      * @return true if a value is defined
160      */
161     private boolean hasValue( int index )
162     {
163         String value = null;
164 
165         if( (index+1) < this.length() )
166         {
167             value = this.getArg(index+1);
168 
169             if( value.startsWith(this.prefix) )
170             {
171                 return false;
172             }
173             else
174             {
175                 return true;
176             }
177         }
178         else
179         {
180             return false;
181         }
182     }
183 
184     /**
185      * Get the value of a command line option
186      * @param option the option
187      * @param defaultValue the default value if the option was not found
188      * @return the value of the option
189      */
190     private String getValue( String option )
191     {
192         String value = this.getValue(option,null);
193 
194         if( value == null )
195         {
196             // the options is there but no value was defined by the caller
197             String msg = "No value supplied for " + this.prefix + option;
198             throw new IllegalArgumentException( msg );
199         }
200         else
201         {
202             return value;
203         }
204     }
205 
206     /**
207      * Get the value of a command line option
208      * @param option the option
209      * @param defaultValue the default value if the option was not found
210      * @return the value of the option
211      */
212     private String getValue( String option, String defaultValue )
213     {
214         int index = this.find(option);
215 
216         if( index < 0 )
217         {
218             // the option is not found
219             return defaultValue;
220         }
221 
222         if( this.hasValue(index) )
223         {
224             // a value is available for this option
225             return this.getArg(index+1);
226         }
227         else
228         {
229             // the options is there but no value was defined by the caller
230             String msg = "No value supplied for " + this.prefix + option;
231             throw new IllegalArgumentException( msg );
232         }
233     }
234 }
235