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