View Javadoc

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