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
020/**
021 * Thrown when more than one option in an option group
022 * has been provided.
023 *
024 * @version $Id: AlreadySelectedException.java 1443102 2013-02-06 18:12:16Z tn $
025 */
026public class AlreadySelectedException extends ParseException
027{
028    /**
029     * This exception {@code serialVersionUID}.
030     */
031    private static final long serialVersionUID = 3674381532418544760L;
032
033    /** The option group selected. */
034    private OptionGroup group;
035
036    /** The option that triggered the exception. */
037    private Option option;
038
039    /**
040     * Construct a new <code>AlreadySelectedException</code>
041     * with the specified detail message.
042     *
043     * @param message the detail message
044     */
045    public AlreadySelectedException(String message)
046    {
047        super(message);
048    }
049
050    /**
051     * Construct a new <code>AlreadySelectedException</code>
052     * for the specified option group.
053     *
054     * @param group  the option group already selected
055     * @param option the option that triggered the exception
056     * @since 1.2
057     */
058    public AlreadySelectedException(OptionGroup group, Option option)
059    {
060        this("The option '" + option.getKey() + "' was specified but an option from this group "
061                + "has already been selected: '" + group.getSelected() + "'");
062        this.group = group;
063        this.option = option;
064    }
065
066    /**
067     * Returns the option group where another option has been selected.
068     *
069     * @return the related option group
070     * @since 1.2
071     */
072    public OptionGroup getOptionGroup()
073    {
074        return group;
075    }
076
077    /**
078     * Returns the option that was added to the group and triggered the exception.
079     *
080     * @return the related option
081     * @since 1.2
082     */
083    public Option getOption()
084    {
085        return option;
086    }
087}