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 */ 017package org.apache.commons.cli2; 018 019import java.util.Collections; 020import java.util.Set; 021 022import org.apache.commons.cli2.resource.ResourceHelper; 023 024/** 025 * A problem found while dealing with command line options. 026 */ 027public class OptionException 028 extends Exception { 029 /** 030 * The settings used when displaying the related Option. 031 * 032 * @see DisplaySetting 033 */ 034 public static final Set HELP_SETTINGS = 035 Collections.unmodifiableSet(Collections.singleton(DisplaySetting.DISPLAY_PROPERTY_OPTION)); 036 037 /** resource helper instance */ 038 private static final ResourceHelper helper = ResourceHelper.getResourceHelper(); 039 040 /** The Option the exception relates to */ 041 private final Option option; 042 043 /** The message explaining the Exception */ 044 private final String message; 045 046 /** 047 * Creates a new OptionException. 048 * 049 * @param option 050 * The Option the exception relates to 051 */ 052 public OptionException(final Option option) { 053 this(option, null, null); 054 } 055 056 /** 057 * Creates a new OptionException. 058 * @param option the Option the exception relates to 059 * @param messageKey the id of the message to display 060 */ 061 public OptionException(final Option option, 062 final String messageKey) { 063 this(option, messageKey, null); 064 } 065 066 /** 067 * Creates a new OptionException. 068 * @param option the Option the exception relates to 069 * @param messageKey the id of the message to display 070 * @param value a value to display with the message 071 */ 072 public OptionException(final Option option, 073 final String messageKey, 074 final String value) { 075 this.option = option; 076 077 if (messageKey != null) { 078 final StringBuffer buffer = new StringBuffer(); 079 080 if (value != null) { 081 buffer.append(helper.getMessage(messageKey, value)); 082 } else { 083 buffer.append(helper.getMessage(messageKey)); 084 } 085 086 buffer.append(" "); 087 088 option.appendUsage(buffer, HELP_SETTINGS, null); 089 message = buffer.toString(); 090 } else { 091 message = ""; 092 } 093 } 094 095 /** 096 * Gets the Option the exception relates to 097 * 098 * @return The related Option 099 */ 100 public Option getOption() { 101 return option; 102 } 103 104 public String getMessage() { 105 return message; 106 } 107 108}