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.argument.impl.bool;
26
27 import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.PIPE_CHAR;
28 import static net.sourceforge.plantumldependency.common.utils.bool.YesNoBoolean.NO;
29 import static net.sourceforge.plantumldependency.common.utils.bool.YesNoBoolean.YES;
30 import static net.sourceforge.plantumldependency.common.utils.bool.YesNoBoolean.valueOf;
31 import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
32 import static net.sourceforge.plantumldependency.common.utils.string.StringUtils.isNotEmpty;
33 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.EMPTY_OPTION_ARGUMENT_ERROR;
34 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.NOT_BOOLEAN_ARGUMENT_ERROR;
35 import net.sourceforge.plantumldependency.common.utils.bool.YesNoBoolean;
36 import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
37 import net.sourceforge.plantumldependency.commoncli.option.argument.AbstractOptionArgument;
38
39
40
41
42
43
44
45
46
47
48 public class BooleanOptionArgumentImpl extends AbstractOptionArgument < YesNoBoolean > {
49
50
51 private static final long serialVersionUID = -4728168833242872815L;
52
53
54 private static final String MAIN_USAGE = YES.toString() + PIPE_CHAR + NO.toString();
55
56
57 private static final String USAGE_DESCRIPTION = MAIN_USAGE
58 + " specifies a valid file path, not a directory. It can be absolute or relative.";
59
60
61
62
63
64
65
66
67 public BooleanOptionArgumentImpl(final boolean optionArgumentIsMandatory) {
68 super(optionArgumentIsMandatory, new StringBuilder(USAGE_DESCRIPTION));
69 }
70
71
72
73
74
75
76 @Override
77 protected String getMainUsageDescription() {
78 return MAIN_USAGE;
79 }
80
81
82
83
84
85
86 @Override
87 public YesNoBoolean parseArgument(final String argument) throws CommandLineException {
88 YesNoBoolean bool = null;
89
90 if (isNotEmpty(argument)) {
91 try {
92 bool = valueOf(argument.toUpperCase());
93 } catch (final IllegalArgumentException e) {
94 throw new CommandLineException(buildLogString(NOT_BOOLEAN_ARGUMENT_ERROR, argument), e);
95 }
96 } else {
97 throw new CommandLineException(EMPTY_OPTION_ARGUMENT_ERROR);
98
99 }
100
101 return bool;
102 }
103 }