View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.cli;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.net.URL;
23  import java.util.Date;
24  
25  /**
26   * <p>Allows Options to be created from a single String.
27   * The pattern contains various single character flags and via
28   * an optional punctuation character, their expected type.
29   * </p>
30   * 
31   * <table border="1">
32   *   <caption>Overview of PatternOptionBuilder patterns</caption>
33   *   <tr><td>a</td><td>-a flag</td></tr>
34   *   <tr><td>b@</td><td>-b [classname]</td></tr>
35   *   <tr><td>c&gt;</td><td>-c [filename]</td></tr>
36   *   <tr><td>d+</td><td>-d [classname] (creates object via empty constructor)</td></tr>
37   *   <tr><td>e%</td><td>-e [number] (creates Double/Long instance depending on existing of a '.')</td></tr>
38   *   <tr><td>f/</td><td>-f [url]</td></tr>
39   *   <tr><td>g:</td><td>-g [string]</td></tr>
40   * </table>
41   * 
42   * <p>
43   * For example, the following allows command line flags of '-v -p string-value -f /dir/file'.
44   * The exclamation mark precede a mandatory option.
45   * </p>
46   *
47   * <pre>
48   *     Options options = PatternOptionBuilder.parsePattern("vp:!f/");
49   * </pre>
50   *
51   * <p>
52   * TODO: These need to break out to OptionType and also to be pluggable.
53   * </p>
54   */
55  public class PatternOptionBuilder
56  {
57      /** String class */
58      public static final Class<String> STRING_VALUE = String.class;
59  
60      /** Object class */
61      public static final Class<Object> OBJECT_VALUE = Object.class;
62  
63      /** Number class */
64      public static final Class<Number> NUMBER_VALUE = Number.class;
65  
66      /** Date class */
67      public static final Class<Date> DATE_VALUE = Date.class;
68  
69      /** Class class */
70      public static final Class<?> CLASS_VALUE = Class.class;
71  
72      /// can we do this one??
73      // is meant to check that the file exists, else it errors.
74      // ie) it's for reading not writing.
75  
76      /** FileInputStream class */
77      public static final Class<FileInputStream> EXISTING_FILE_VALUE = FileInputStream.class;
78  
79      /** File class */
80      public static final Class<File> FILE_VALUE = File.class;
81  
82      /** File array class */
83      public static final Class<File[]> FILES_VALUE = File[].class;
84  
85      /** URL class */
86      public static final Class<URL> URL_VALUE = URL.class;
87  
88      /**
89       * Retrieve the class that <code>ch</code> represents.
90       *
91       * @param ch the specified character
92       * @return The class that <code>ch</code> represents
93       */
94      public static Object getValueClass(final char ch)
95      {
96          switch (ch)
97          {
98              case '@':
99                  return PatternOptionBuilder.OBJECT_VALUE;
100             case ':':
101                 return PatternOptionBuilder.STRING_VALUE;
102             case '%':
103                 return PatternOptionBuilder.NUMBER_VALUE;
104             case '+':
105                 return PatternOptionBuilder.CLASS_VALUE;
106             case '#':
107                 return PatternOptionBuilder.DATE_VALUE;
108             case '<':
109                 return PatternOptionBuilder.EXISTING_FILE_VALUE;
110             case '>':
111                 return PatternOptionBuilder.FILE_VALUE;
112             case '*':
113                 return PatternOptionBuilder.FILES_VALUE;
114             case '/':
115                 return PatternOptionBuilder.URL_VALUE;
116         }
117 
118         return null;
119     }
120 
121     /**
122      * Returns whether <code>ch</code> is a value code, i.e.
123      * whether it represents a class in a pattern.
124      *
125      * @param ch the specified character
126      * @return true if <code>ch</code> is a value code, otherwise false.
127      */
128     public static boolean isValueCode(final char ch)
129     {
130         return ch == '@'
131                 || ch == ':'
132                 || ch == '%'
133                 || ch == '+'
134                 || ch == '#'
135                 || ch == '<'
136                 || ch == '>'
137                 || ch == '*'
138                 || ch == '/'
139                 || ch == '!';
140     }
141 
142     /**
143      * Returns the {@link Options} instance represented by <code>pattern</code>.
144      *
145      * @param pattern the pattern string
146      * @return The {@link Options} instance
147      */
148     public static Options parsePattern(final String pattern)
149     {
150         char opt = ' ';
151         boolean required = false;
152         Class<?> type = null;
153 
154         final Options options = new Options();
155 
156         for (int i = 0; i < pattern.length(); i++)
157         {
158             final char ch = pattern.charAt(i);
159 
160             // a value code comes after an option and specifies
161             // details about it
162             if (!isValueCode(ch))
163             {
164                 if (opt != ' ')
165                 {
166                     final Option option = Option.builder(String.valueOf(opt))
167                         .hasArg(type != null)
168                         .required(required)
169                         .type(type)
170                         .build();
171                     
172                     // we have a previous one to deal with
173                     options.addOption(option);
174                     required = false;
175                     type = null;
176                     opt = ' ';
177                 }
178 
179                 opt = ch;
180             }
181             else if (ch == '!')
182             {
183                 required = true;
184             }
185             else
186             {
187                 type = (Class<?>) getValueClass(ch);
188             }
189         }
190 
191         if (opt != ' ')
192         {
193             final Option option = Option.builder(String.valueOf(opt))
194                 .hasArg(type != null)
195                 .required(required)
196                 .type(type)
197                 .build();
198             
199             // we have a final one to deal with
200             options.addOption(option);
201         }
202 
203         return options;
204     }
205 }