001/* 002 Licensed to the Apache Software Foundation (ASF) under one or more 003 contributor license agreements. See the NOTICE file distributed with 004 this work for additional information regarding copyright ownership. 005 The ASF licenses this file to You under the Apache License, Version 2.0 006 (the "License"); you may not use this file except in compliance with 007 the License. You may obtain a copy of the License at 008 009 http://www.apache.org/licenses/LICENSE-2.0 010 011 Unless required by applicable law or agreed to in writing, software 012 distributed under the License is distributed on an "AS IS" BASIS, 013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 See the License for the specific language governing permissions and 015 limitations under the License. 016 */ 017package Apache.CLI; 018 019import java.util.Collection; 020import java.util.Iterator; 021 022/** Exception thrown when an option can't be identified from a partial name. */ 023@Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="LICENSE") 024public class AmbiguousOptionException extends UnrecognizedOptionException 025{ 026 private static final long serialVersionUID = 1; 027 028 // Build the exception message from the specified list of options. 029 private static String createMessage 030 (final String option, final Collection<String> matchingOptions) 031 { 032 final StringBuilder buf = new StringBuilder("Ambiguous option: '"); 033 034 buf.append(option); 035 buf.append("' (could be: "); 036 037 final Iterator<String> it = matchingOptions.iterator(); 038 039 while (it.hasNext()) 040 { 041 buf.append("'"); 042 buf.append(it.next()); 043 buf.append("'"); 044 if (it.hasNext()) buf.append(", "); 045 } 046 047 buf.append(")"); 048 049 return buf.toString(); 050 } 051 052 // The list of options matching the partial name specified 053 private final Collection<String> matchingOptions; 054 055 /** 056 * Constructs a new AmbiguousOptionException. 057 * @param option the partial option name 058 * @param matchingOptions the options matching the name 059 */ 060 public AmbiguousOptionException 061 (final String option, final Collection<String> matchingOptions) 062 { 063 super(createMessage(option, matchingOptions), option); 064 this.matchingOptions = matchingOptions; 065 } 066 067 /** 068 * Gets the options matching the partial name. 069 * @return a collection of options matching the name 070 */ 071 public Collection<String> getMatchingOptions() 072 { return matchingOptions; } 073}