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