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.util.Collection;
21  import java.util.Iterator;
22  
23  /**
24   * Exception thrown when an option can't be identified from a partial name.
25   * 
26   * @version $Id: AmbiguousOptionException.java 1443102 2013-02-06 18:12:16Z tn $
27   * @since 1.3
28   */
29  public class AmbiguousOptionException extends UnrecognizedOptionException
30  {
31      /**
32       * This exception {@code serialVersionUID}.
33       */
34      private static final long serialVersionUID = 5829816121277947229L;
35  
36      /** The list of options matching the partial name specified */
37      private Collection<String> matchingOptions;
38  
39      /**
40       * Constructs a new AmbiguousOptionException.
41       *
42       * @param option          the partial option name
43       * @param matchingOptions the options matching the name
44       */
45      public AmbiguousOptionException(String option, Collection<String> matchingOptions)
46      {
47          super(createMessage(option, matchingOptions), option);
48          this.matchingOptions = matchingOptions;
49      }
50  
51      /**
52       * Returns the options matching the partial name.
53       * @return a collection of options matching the name
54       */
55      public Collection<String> getMatchingOptions()
56      {
57          return matchingOptions;
58      }
59  
60      /**
61       * Build the exception message from the specified list of options.
62       * 
63       * @param option
64       * @param matchingOptions
65       * @return
66       */
67      private static String createMessage(String option, Collection<String> matchingOptions)
68      {
69          StringBuilder buf = new StringBuilder("Ambiguous option: '");
70          buf.append(option);
71          buf.append("'  (could be: ");
72  
73          Iterator<String> it = matchingOptions.iterator();
74          while (it.hasNext())
75          {
76              buf.append("'");
77              buf.append(it.next());
78              buf.append("'");
79              if (it.hasNext())
80              {
81                  buf.append(", ");
82              }
83          }
84          buf.append(")");
85  
86          return buf.toString();
87      }
88  }