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 private static final long serialVersionUID = 1L;
32
33
34 private final Map<String, Option> optionMap = new LinkedHashMap<>();
35
36
37 private String selected;
38
39
40 private boolean required;
41
42
43
44
45
46
47
48 public OptionGroup addOption(final Option option) {
49
50
51 optionMap.put(option.getKey(), option);
52
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
111 if (selected != null && !selected.equals(option.getKey())) {
112 throw new AlreadySelectedException(this, option);
113 }
114 selected = option.getKey();
115 }
116
117
118
119
120
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 }