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: */package org.apache.commons.cli;
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.Iterator;
021:
022: /**
023: * A group of mutually exclusive options.
024: * @author John Keyes ( john at integralsource.com )
025: * @version $Revision: 542144 $
026: */
027: public class OptionGroup {
028:
029: /** hold the options */
030: private HashMap optionMap = new HashMap();
031:
032: /** the name of the selected option */
033: private String selected;
034:
035: /** specified whether this group is required */
036: private boolean required;
037:
038: /**
039: * add <code>opt</code> to this group
040: *
041: * @param opt the option to add to this group
042: * @return this option group with opt added
043: */
044: public OptionGroup addOption(Option opt) {
045: // key - option name
046: // value - the option
047: optionMap.put(opt.getKey(), opt);
048:
049: return this ;
050: }
051:
052: /**
053: * @return the names of the options in this group as a
054: * <code>Collection</code>
055: */
056: public Collection getNames() {
057: // the key set is the collection of names
058: return optionMap.keySet();
059: }
060:
061: /**
062: * @return the options in this group as a <code>Collection</code>
063: */
064: public Collection getOptions() {
065: // the values are the collection of options
066: return optionMap.values();
067: }
068:
069: /**
070: * set the selected option of this group to <code>name</code>.
071: * @param opt the option that is selected
072: * @throws AlreadySelectedException if an option from this group has
073: * already been selected.
074: */
075: public void setSelected(Option opt) throws AlreadySelectedException {
076: // if no option has already been selected or the
077: // same option is being reselected then set the
078: // selected member variable
079: if ((this .selected == null)
080: || this .selected.equals(opt.getOpt())) {
081: this .selected = opt.getOpt();
082: } else {
083: throw new AlreadySelectedException(
084: "an option from this group has "
085: + "already been selected: '" + selected
086: + "'");
087: }
088: }
089:
090: /**
091: * @return the selected option name
092: */
093: public String getSelected() {
094: return selected;
095: }
096:
097: /**
098: * @param required specifies if this group is required
099: */
100: public void setRequired(boolean required) {
101: this .required = required;
102: }
103:
104: /**
105: * Returns whether this option group is required.
106: *
107: * @return whether this option group is required
108: */
109: public boolean isRequired() {
110: return this .required;
111: }
112:
113: /**
114: * <p>Returns the stringified version of this OptionGroup.</p>
115: * @return the stringified representation of this group
116: */
117: public String toString() {
118: StringBuffer buff = new StringBuffer();
119:
120: Iterator iter = getOptions().iterator();
121:
122: buff.append("[");
123:
124: while (iter.hasNext()) {
125: Option option = (Option) iter.next();
126:
127: if (option.getOpt() != null) {
128: buff.append("-");
129: buff.append(option.getOpt());
130: } else {
131: buff.append("--");
132: buff.append(option.getLongOpt());
133: }
134:
135: buff.append(" ");
136: buff.append(option.getDescription());
137:
138: if (iter.hasNext()) {
139: buff.append(", ");
140: }
141: }
142:
143: buff.append("]");
144:
145: return buff.toString();
146: }
147: }
|