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