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   * Contains useful helper methods for classes within this package.
22   */
23  final class Util {
24  
25      /**
26       * An empty immutable {@code String} array.
27       */
28      static final String[] EMPTY_STRING_ARRAY = {};
29  
30      /**
31       * Removes the leading and trailing quotes from {@code str}. E.g. if str is '"one two"', then 'one two' is returned.
32       *
33       * @param str The string from which the leading and trailing quotes should be removed.
34       * @return The string without the leading and trailing quotes.
35       */
36      static String stripLeadingAndTrailingQuotes(String str) {
37          final int length = str.length();
38          if (length > 1 && str.startsWith("\"") && str.endsWith("\"") && str.substring(1, length - 1).indexOf('"') == -1) {
39              str = str.substring(1, length - 1);
40          }
41  
42          return str;
43      }
44  
45      /**
46       * Removes the hyphens from the beginning of {@code str} and return the new String.
47       *
48       * @param str The string from which the hyphens should be removed.
49       * @return the new String.
50       */
51      static String stripLeadingHyphens(final String str) {
52          if (str == null) {
53              return null;
54          }
55          if (str.startsWith("--")) {
56              return str.substring(2);
57          }
58          if (str.startsWith("-")) {
59              return str.substring(1);
60          }
61  
62          return str;
63      }
64  }