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  /**
21   * Validates an Option string.
22   *
23   * @since 1.1
24   */
25  final class OptionValidator {
26      /**
27       * Returns whether the specified character is a valid character.
28       *
29       * @param c the character to validate
30       * @return true if {@code c} is a letter.
31       */
32      private static boolean isValidChar(final char c) {
33          return Character.isJavaIdentifierPart(c);
34      }
35  
36      /**
37       * Returns whether the specified character is a valid Option.
38       *
39       * @param c the option to validate
40       * @return true if {@code c} is a letter, '?' or '@', otherwise false.
41       */
42      private static boolean isValidOpt(final char c) {
43          return isValidChar(c) || c == '?' || c == '@';
44      }
45  
46      /**
47       * Validates whether {@code opt} is a permissible Option shortOpt. The rules that specify if the {@code opt}
48       * is valid are:
49       *
50       * <ul>
51       * <li>a single character {@code opt} that is either ' '(special case), '?', '@' or a letter</li>
52       * <li>a multi character {@code opt} that only contains letters.</li>
53       * </ul>
54       * <p>
55       * In case {@code opt} is {@code null} no further validation is performed.
56       *
57       * @param option The option string to validate, may be null
58       * @throws IllegalArgumentException if the Option is not valid.
59       */
60      static String validate(final String option) throws IllegalArgumentException {
61          // if opt is NULL do not check further
62          if (option == null) {
63              return null;
64          }
65  
66          // handle the single character opt
67          if (option.length() == 1) {
68              final char ch = option.charAt(0);
69  
70              if (!isValidOpt(ch)) {
71                  throw new IllegalArgumentException("Illegal option name '" + ch + "'");
72              }
73          } else {
74              // handle the multi character opt
75              for (final char ch : option.toCharArray()) {
76                  if (!isValidChar(ch)) {
77                      throw new IllegalArgumentException("The option '" + option + "' contains an illegal " + "character : '" + ch + "'");
78                  }
79              }
80          }
81          return option;
82      }
83  }