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