1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sourceforge.plantumldependency.commoncli.option.impl.help;
26
27 import static java.util.Arrays.asList;
28 import static java.util.Collections.unmodifiableSet;
29 import static java.util.logging.Level.FINE;
30 import static java.util.logging.Logger.getLogger;
31 import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
32 import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
33 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.COMMAND_LINE_NULL_ERROR;
34 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.JAVA_PROGRAM_NULL_ERROR;
35 import static net.sourceforge.plantumldependency.commoncli.constants.log.FineConstants.OPTION_NOT_SPECIFIED_FINE;
36 import static net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus.ACTIVE_OPTIONAL_OPTION_STATUS;
37
38 import java.util.Set;
39 import java.util.TreeSet;
40 import java.util.logging.Logger;
41
42 import net.sourceforge.plantumldependency.commoncli.command.CommandLine;
43 import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
44 import net.sourceforge.plantumldependency.commoncli.option.AbstractOption;
45 import net.sourceforge.plantumldependency.commoncli.option.execution.ExecutableOption;
46 import net.sourceforge.plantumldependency.commoncli.option.execution.OptionExecution;
47 import net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus;
48 import net.sourceforge.plantumldependency.commoncli.program.JavaProgram;
49
50
51
52
53
54
55
56
57
58 public class HelpOption extends AbstractOption implements ExecutableOption {
59
60
61 private static final long serialVersionUID = 3929762825343812282L;
62
63
64 private static final transient Logger LOGGER = getLogger(HelpOption.class.getName());
65
66
67 public static final String OPTION_MAIN_SYNOPSIS = "-h";
68
69
70 public static final Set < String > OPTION_SYNOPSIS = unmodifiableSet(new TreeSet < String >(asList(new String[] {
71 "--help", "-?"})));
72
73
74 private JavaProgram javaProgram;
75
76
77 private int priority;
78
79
80
81
82
83
84
85
86
87
88
89 public HelpOption(final JavaProgram program, final int optionPriority) {
90 this(new StringBuilder("To display this help message."), program, optionPriority, ACTIVE_OPTIONAL_OPTION_STATUS);
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public HelpOption(final StringBuilder fullOptionDescription, final JavaProgram program, final int optionPriority,
110 final OptionStatus optionStatus) {
111 super(OPTION_MAIN_SYNOPSIS, OPTION_SYNOPSIS, fullOptionDescription, optionStatus);
112 setJavaProgram(program);
113 setPriority(optionPriority);
114 }
115
116
117
118
119
120
121 @Override
122 public HelpOption deepClone() {
123 final HelpOption a = (HelpOption) super.deepClone();
124 a.javaProgram = getJavaProgram().deepClone();
125 return a;
126 }
127
128
129
130
131
132
133 @Override
134 protected StringBuilder getFullUsageAdditions() {
135 return new StringBuilder();
136 }
137
138
139
140
141
142
143 @Override
144 protected StringBuilder getFullUsageDescriptionAdditions() {
145 return new StringBuilder();
146 }
147
148
149
150
151
152
153
154
155 private JavaProgram getJavaProgram() {
156 return javaProgram;
157 }
158
159
160
161
162
163
164 @Override
165 protected StringBuilder getMainUsageAdditions() {
166 return new StringBuilder();
167 }
168
169
170
171
172
173
174 @Override
175 public int getPriority() {
176 return priority;
177 }
178
179
180
181
182
183
184 @Override
185 public OptionExecution parseCommandLine(final CommandLine commandLine) throws CommandLineException {
186 checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
187
188 OptionExecution optionExecution = null;
189
190 if (commandLine.isOptionActiveAndSpecified(this)) {
191 optionExecution = new HelpOptionExecution(getJavaProgram(), getPriority());
192 } else {
193 LOGGER.log(FINE, buildLogString(OPTION_NOT_SPECIFIED_FINE, getAllNames()));
194 }
195
196 return optionExecution;
197 }
198
199
200
201
202
203
204
205
206
207 private void setJavaProgram(final JavaProgram value) {
208 checkNull(value, JAVA_PROGRAM_NULL_ERROR);
209
210 javaProgram = value;
211 }
212
213
214
215
216
217
218
219
220
221 private void setPriority(final int value) {
222 priority = value;
223 }
224
225
226
227
228
229
230 @Override
231 public String toString() {
232 return getClass().getSimpleName() + " [" + super.toString() + ", javaProgram=" + javaProgram + ", priority="
233 + priority + "]";
234 }
235 }