1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29 public class OptionGroup implements Serializable {
30
31
32 private static final long serialVersionUID = 1L;
33
34
35 private final Map<String, Option> optionMap = new LinkedHashMap<>();
36
37
38 private String selected;
39
40
41 private boolean required;
42
43
44
45
46
47
48
49 public OptionGroup addOption(final Option option) {
50
51
52 optionMap.put(option.getKey(), option);
53 return this;
54 }
55
56
57
58
59 public Collection<String> getNames() {
60
61 return optionMap.keySet();
62 }
63
64
65
66
67 public Collection<Option> getOptions() {
68
69 return optionMap.values();
70 }
71
72
73
74
75 public String getSelected() {
76 return selected;
77 }
78
79
80
81
82
83
84 public boolean isRequired() {
85 return required;
86 }
87
88
89
90
91 public void setRequired(final boolean required) {
92 this.required = required;
93 }
94
95
96
97
98
99
100
101 public void setSelected(final Option option) throws AlreadySelectedException {
102 if (option == null) {
103
104 selected = null;
105 return;
106 }
107
108
109
110 if (selected != null && !selected.equals(option.getKey())) {
111 throw new AlreadySelectedException(this, option);
112 }
113 selected = option.getKey();
114 }
115
116
117
118
119
120
121 @Override
122 public String toString() {
123 final StringBuilder buff = new StringBuilder();
124 final Iterator<Option> iter = getOptions().iterator();
125 buff.append("[");
126 while (iter.hasNext()) {
127 final Option option = iter.next();
128 if (option.getOpt() != null) {
129 buff.append("-");
130 buff.append(option.getOpt());
131 } else {
132 buff.append("--");
133 buff.append(option.getLongOpt());
134 }
135
136 if (option.getDescription() != null) {
137 buff.append(Char.SP);
138 buff.append(option.getDescription());
139 }
140
141 if (iter.hasNext()) {
142 buff.append(", ");
143 }
144 }
145 buff.append("]");
146 return buff.toString();
147 }
148 }