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