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