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 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.cli2;
18
19 import java.util.Collections;
20 import java.util.Set;
21
22 import org.apache.commons.cli2.resource.ResourceHelper;
23
24 /**
25 * A problem found while dealing with command line options.
26 */
27 public class OptionException
28 extends Exception {
29 /**
30 * The settings used when displaying the related Option.
31 *
32 * @see DisplaySetting
33 */
34 public static final Set HELP_SETTINGS =
35 Collections.unmodifiableSet(Collections.singleton(DisplaySetting.DISPLAY_PROPERTY_OPTION));
36
37 /** resource helper instance */
38 private static final ResourceHelper helper = ResourceHelper.getResourceHelper();
39
40 /** The Option the exception relates to */
41 private final Option option;
42
43 /** The message explaining the Exception */
44 private final String message;
45
46 /**
47 * Creates a new OptionException.
48 *
49 * @param option
50 * The Option the exception relates to
51 */
52 public OptionException(final Option option) {
53 this(option, null, null);
54 }
55
56 /**
57 * Creates a new OptionException.
58 * @param option the Option the exception relates to
59 * @param messageKey the id of the message to display
60 */
61 public OptionException(final Option option,
62 final String messageKey) {
63 this(option, messageKey, null);
64 }
65
66 /**
67 * Creates a new OptionException.
68 * @param option the Option the exception relates to
69 * @param messageKey the id of the message to display
70 * @param value a value to display with the message
71 */
72 public OptionException(final Option option,
73 final String messageKey,
74 final String value) {
75 this.option = option;
76
77 if (messageKey != null) {
78 final StringBuffer buffer = new StringBuffer();
79
80 if (value != null) {
81 buffer.append(helper.getMessage(messageKey, value));
82 } else {
83 buffer.append(helper.getMessage(messageKey));
84 }
85
86 buffer.append(" ");
87
88 option.appendUsage(buffer, HELP_SETTINGS, null);
89 message = buffer.toString();
90 } else {
91 message = "";
92 }
93 }
94
95 /**
96 * Gets the Option the exception relates to
97 *
98 * @return The related Option
99 */
100 public Option getOption() {
101 return option;
102 }
103
104 public String getMessage() {
105 return message;
106 }
107
108 }