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 */
017
018package org.apache.commons.cli;
019
020import java.util.Collection;
021import java.util.Iterator;
022
023/**
024 * Exception thrown when an option can't be identified from a partial name.
025 * 
026 * @version $Id: AmbiguousOptionException.java 1669814 2015-03-28 18:09:26Z britter $
027 * @since 1.3
028 */
029public class AmbiguousOptionException extends UnrecognizedOptionException
030{
031    /**
032     * This exception {@code serialVersionUID}.
033     */
034    private static final long serialVersionUID = 5829816121277947229L;
035
036    /** The list of options matching the partial name specified */
037    private final Collection<String> matchingOptions;
038
039    /**
040     * Constructs a new AmbiguousOptionException.
041     *
042     * @param option          the partial option name
043     * @param matchingOptions the options matching the name
044     */
045    public AmbiguousOptionException(String option, Collection<String> matchingOptions)
046    {
047        super(createMessage(option, matchingOptions), option);
048        this.matchingOptions = matchingOptions;
049    }
050
051    /**
052     * Returns the options matching the partial name.
053     * @return a collection of options matching the name
054     */
055    public Collection<String> getMatchingOptions()
056    {
057        return matchingOptions;
058    }
059
060    /**
061     * Build the exception message from the specified list of options.
062     * 
063     * @param option
064     * @param matchingOptions
065     * @return
066     */
067    private static String createMessage(String option, Collection<String> matchingOptions)
068    {
069        StringBuilder buf = new StringBuilder("Ambiguous option: '");
070        buf.append(option);
071        buf.append("'  (could be: ");
072
073        Iterator<String> it = matchingOptions.iterator();
074        while (it.hasNext())
075        {
076            buf.append("'");
077            buf.append(it.next());
078            buf.append("'");
079            if (it.hasNext())
080            {
081                buf.append(", ");
082            }
083        }
084        buf.append(")");
085
086        return buf.toString();
087    }
088}