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  
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  /**
27   * A group of mutually exclusive options.
28   */
29  public class OptionGroup implements Serializable {
30      /** The serial version UID. */
31      private static final long serialVersionUID = 1L;
32  
33      /** hold the options */
34      private final Map<String, Option> optionMap = new LinkedHashMap<>();
35  
36      /** The name of the selected option */
37      private String selected;
38  
39      /** specified whether this group is required */
40      private boolean required;
41  
42      /**
43       * Add the specified {@code Option} to this group.
44       *
45       * @param option the option to add to this group
46       * @return this option group with the option added
47       */
48      public OptionGroup addOption(final Option option) {
49          // key - option name
50          // value - the option
51          optionMap.put(option.getKey(), option);
52  
53          return this;
54      }
55  
56      /**
57       * @return the names of the options in this group as a {@code Collection}
58       */
59      public Collection<String> getNames() {
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}
66       */
67      public Collection<Option> getOptions() {
68          // the values are the collection of options
69          return optionMap.values();
70      }
71  
72      /**
73       * @return the selected option name
74       */
75      public String getSelected() {
76          return selected;
77      }
78  
79      /**
80       * Tests whether this option group is required.
81       *
82       * @return whether this option group is required
83       */
84      public boolean isRequired() {
85          return required;
86      }
87  
88      /**
89       * @param required specifies if this group is required
90       */
91      public void setRequired(final boolean required) {
92          this.required = required;
93      }
94  
95      /**
96       * Set the selected option of this group to {@code name}.
97       *
98       * @param option the option that is selected
99       * @throws AlreadySelectedException if an option from this group has already been selected.
100      */
101     public void setSelected(final Option option) throws AlreadySelectedException {
102         if (option == null) {
103             // reset the option previously selected
104             selected = null;
105             return;
106         }
107 
108         // if no option has already been selected or the
109         // same option is being reselected then set the
110         // selected member variable
111         if (selected != null && !selected.equals(option.getKey())) {
112             throw new AlreadySelectedException(this, option);
113         }
114         selected = option.getKey();
115     }
116 
117     /**
118      * Returns the stringified version of this OptionGroup.
119      *
120      * @return the stringified representation of this group
121      */
122     @Override
123     public String toString() {
124         final StringBuilder buff = new StringBuilder();
125 
126         final Iterator<Option> iter = getOptions().iterator();
127 
128         buff.append("[");
129 
130         while (iter.hasNext()) {
131             final Option option = iter.next();
132 
133             if (option.getOpt() != null) {
134                 buff.append("-");
135                 buff.append(option.getOpt());
136             } else {
137                 buff.append("--");
138                 buff.append(option.getLongOpt());
139             }
140 
141             if (option.getDescription() != null) {
142                 buff.append(" ");
143                 buff.append(option.getDescription());
144             }
145 
146             if (iter.hasNext()) {
147                 buff.append(", ");
148             }
149         }
150 
151         buff.append("]");
152 
153         return buff.toString();
154     }
155 }