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 https://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
18 package org.apache.commons.cli;
19
20 import java.io.Serializable;
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25
26 import org.apache.commons.cli.help.OptionFormatter;
27
28 /**
29 * A group of mutually exclusive options.
30 */
31 public class OptionGroup implements Serializable {
32
33 /** The serial version UID. */
34 private static final long serialVersionUID = 1L;
35
36 /**
37 * Maps options where keys are option name and values are the options.
38 */
39 private final Map<String, Option> optionMap = new LinkedHashMap<>();
40
41 /** The name of the selected option. */
42 private String selected;
43
44 /** Specified whether this group is required. */
45 private boolean required;
46
47 /**
48 * Constructs a new instance.
49 */
50 public OptionGroup() {
51 // empty
52 }
53
54 /**
55 * Adds the given {@code Option} to this group.
56 *
57 * @param option the option to add to this group.
58 * @return this option group with the option added.
59 */
60 public OptionGroup addOption(final Option option) {
61 optionMap.put(option.getKey(), option);
62 return this;
63 }
64
65 /**
66 * Gets the names of the options in this group as a {@code Collection}.
67 *
68 * @return the names of the options in this group as a {@code Collection}.
69 */
70 public Collection<String> getNames() {
71 // the key set is the collection of names
72 return optionMap.keySet();
73 }
74
75 /**
76 * Gets the options in this group as a {@code Collection}.
77 *
78 * @return the options in this group as a {@code Collection}.
79 */
80 public Collection<Option> getOptions() {
81 // the values are the collection of options
82 return optionMap.values();
83 }
84
85 /**
86 * Gets the selected option name.
87 *
88 * If the selected option is deprecated <em>no warning is logged</em>.
89 * @return the selected option name.
90 */
91 public String getSelected() {
92 return selected;
93 }
94
95 /**
96 * Tests whether this option group is required.
97 *
98 * @return whether this option group is required.
99 */
100 public boolean isRequired() {
101 return required;
102 }
103
104 /**
105 * Tests whether an option is selected.
106 *
107 * If an option is selected and is deprecated <em>no warning is logged</em>.
108 * @return whether whether an option is selected.
109 * @since 1.9.0
110 */
111 public boolean isSelected() {
112 return selected != null;
113 }
114
115 /**
116 * Sets whether this group is required.
117 *
118 * @param required whether this group is required.
119 */
120 public void setRequired(final boolean required) {
121 this.required = required;
122 }
123
124 /**
125 * Sets the selected option of this group to {@code name}.
126 *
127 * If the selected option is deprecated <em>no warning is logged</em>.
128 * @param option the option that is selected.
129 * @throws AlreadySelectedException if an option from this group has already been selected.
130 */
131 public void setSelected(final Option option) throws AlreadySelectedException {
132 if (option == null) {
133 // reset the option previously selected
134 selected = null;
135 return;
136 }
137 // if no option has already been selected or the
138 // same option is being reselected then set the
139 // selected member variable
140 if (selected != null && !selected.equals(option.getKey())) {
141 throw new AlreadySelectedException(this, option);
142 }
143 selected = option.getKey();
144 }
145
146 /**
147 * Returns the stringified version of this OptionGroup.
148 *
149 * @return the stringified representation of this group.
150 */
151 @Override
152 public String toString() {
153 final StringBuilder buff = new StringBuilder();
154 final Iterator<Option> iter = getOptions().iterator();
155 buff.append("[");
156 while (iter.hasNext()) {
157 final Option option = iter.next();
158 if (option.getOpt() != null) {
159 buff.append(OptionFormatter.DEFAULT_OPT_PREFIX);
160 buff.append(option.getOpt());
161 } else {
162 buff.append(OptionFormatter.DEFAULT_LONG_OPT_PREFIX);
163 buff.append(option.getLongOpt());
164 }
165 if (option.getDescription() != null) {
166 buff.append(Char.SP);
167 buff.append(option.getDescription());
168 }
169 if (iter.hasNext()) {
170 buff.append(", ");
171 }
172 }
173 buff.append("]");
174 return buff.toString();
175 }
176 }