AlreadySelectedException.java

  1. /*
  2.   Licensed to the Apache Software Foundation (ASF) under one or more
  3.   contributor license agreements.  See the NOTICE file distributed with
  4.   this work for additional information regarding copyright ownership.
  5.   The ASF licenses this file to You under the Apache License, Version 2.0
  6.   (the "License"); you may not use this file except in compliance with
  7.   the License.  You may obtain a copy of the License at

  8.       http://www.apache.org/licenses/LICENSE-2.0

  9.   Unless required by applicable law or agreed to in writing, software
  10.   distributed under the License is distributed on an "AS IS" BASIS,
  11.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12.   See the License for the specific language governing permissions and
  13.   limitations under the License.
  14.  */

  15. package org.apache.commons.cli;

  16. /**
  17.  * Thrown when more than one option in an option group has been provided.
  18.  */
  19. public class AlreadySelectedException extends ParseException {

  20.     /**
  21.      * This exception {@code serialVersionUID}.
  22.      */
  23.     private static final long serialVersionUID = 3674381532418544760L;

  24.     /** The option group selected. */
  25.     private final OptionGroup group;

  26.     /** The option that triggered the exception. */
  27.     private final Option option;

  28.     /**
  29.      * Constructs a new {@code AlreadySelectedException} for the specified option group.
  30.      *
  31.      * @param group the option group already selected
  32.      * @param option the option that triggered the exception
  33.      * @since 1.2
  34.      */
  35.     public AlreadySelectedException(final OptionGroup group, final Option option) {
  36.         this(String.format("The option '%s' was specified but an option from this group has already been selected: '%s'", option.getKey(), group.getSelected()),
  37.                 group, option);
  38.     }

  39.     /**
  40.      * Constructs a new {@code AlreadySelectedException} with the specified detail message.
  41.      *
  42.      * @param message the detail message
  43.      */
  44.     public AlreadySelectedException(final String message) {
  45.         this(message, null, null);
  46.     }

  47.     private AlreadySelectedException(final String message, final OptionGroup group, final Option option) {
  48.         super(message);
  49.         this.group = group;
  50.         this.option = option;
  51.     }

  52.     /**
  53.      * Gets the option that was added to the group and triggered the exception.
  54.      *
  55.      * @return the related option
  56.      * @since 1.2
  57.      */
  58.     public Option getOption() {
  59.         return option;
  60.     }

  61.     /**
  62.      * Gets the option group where another option has been selected.
  63.      *
  64.      * @return the related option group
  65.      * @since 1.2
  66.      */
  67.     public OptionGroup getOptionGroup() {
  68.         return group;
  69.     }
  70. }