| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.cli2.builder; |
| 18 | |
|
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.List; |
| 21 | |
|
| 22 | |
import org.apache.commons.cli2.Argument; |
| 23 | |
import org.apache.commons.cli2.option.ArgumentImpl; |
| 24 | |
import org.apache.commons.cli2.resource.ResourceConstants; |
| 25 | |
import org.apache.commons.cli2.resource.ResourceHelper; |
| 26 | |
import org.apache.commons.cli2.validation.Validator; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class ArgumentBuilder { |
| 32 | |
|
| 33 | |
|
| 34 | 1 | private final static ResourceHelper resources = ResourceHelper.getResourceHelper(); |
| 35 | |
|
| 36 | |
|
| 37 | |
private String name; |
| 38 | |
|
| 39 | |
|
| 40 | |
private String description; |
| 41 | |
|
| 42 | |
|
| 43 | |
private int minimum; |
| 44 | |
|
| 45 | |
|
| 46 | |
private int maximum; |
| 47 | |
|
| 48 | |
|
| 49 | |
private char initialSeparator; |
| 50 | |
|
| 51 | |
|
| 52 | |
private char subsequentSeparator; |
| 53 | |
|
| 54 | |
|
| 55 | |
private Validator validator; |
| 56 | |
|
| 57 | |
|
| 58 | |
private String consumeRemaining; |
| 59 | |
|
| 60 | |
|
| 61 | |
private List defaultValues; |
| 62 | |
|
| 63 | |
|
| 64 | |
private int id; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 68 | public ArgumentBuilder() { |
| 70 | 68 | reset(); |
| 71 | 68 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public final Argument create() { |
| 81 | 117 | final Argument argument = |
| 82 | |
new ArgumentImpl( |
| 83 | |
name, |
| 84 | |
description, |
| 85 | |
minimum, |
| 86 | |
maximum, |
| 87 | |
initialSeparator, |
| 88 | |
subsequentSeparator, |
| 89 | |
validator, |
| 90 | |
consumeRemaining, |
| 91 | |
defaultValues, |
| 92 | |
id); |
| 93 | |
|
| 94 | 117 | reset(); |
| 95 | |
|
| 96 | 117 | return argument; |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public final ArgumentBuilder reset() { |
| 105 | 186 | name = "arg"; |
| 106 | 186 | description = null; |
| 107 | 186 | minimum = 0; |
| 108 | 186 | maximum = Integer.MAX_VALUE; |
| 109 | 186 | initialSeparator = ArgumentImpl.DEFAULT_INITIAL_SEPARATOR; |
| 110 | 186 | subsequentSeparator = ArgumentImpl.DEFAULT_SUBSEQUENT_SEPARATOR; |
| 111 | 186 | validator = null; |
| 112 | 186 | consumeRemaining = "--"; |
| 113 | 186 | defaultValues = null; |
| 114 | 186 | id = 0; |
| 115 | 186 | return this; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public final ArgumentBuilder withName(final String newName) { |
| 128 | 99 | if (newName == null) { |
| 129 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_NAME)); |
| 130 | |
} |
| 131 | 98 | if ("".equals(newName)) { |
| 132 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME)); |
| 133 | |
} |
| 134 | 97 | this.name = newName; |
| 135 | 97 | return this; |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public final ArgumentBuilder withDescription(final String newDescription) { |
| 147 | 1 | this.description = newDescription; |
| 148 | 1 | return this; |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public final ArgumentBuilder withMinimum(final int newMinimum) { |
| 158 | 61 | if (newMinimum < 0) { |
| 159 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM)); |
| 160 | |
} |
| 161 | 60 | this.minimum = newMinimum; |
| 162 | 60 | return this; |
| 163 | |
} |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
public final ArgumentBuilder withMaximum(final int newMaximum) { |
| 172 | 58 | if (newMaximum < 0) { |
| 173 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM)); |
| 174 | |
} |
| 175 | 57 | this.maximum = newMaximum; |
| 176 | 57 | return this; |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
public final ArgumentBuilder withInitialSeparator( |
| 189 | |
final char newInitialSeparator) { |
| 190 | |
|
| 191 | 12 | this.initialSeparator = newInitialSeparator; |
| 192 | 12 | return this; |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
public final ArgumentBuilder withSubsequentSeparator( |
| 205 | |
final char newSubsequentSeparator) { |
| 206 | |
|
| 207 | 1 | this.subsequentSeparator = newSubsequentSeparator; |
| 208 | 1 | return this; |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
public final ArgumentBuilder withValidator(final Validator newValidator) { |
| 219 | 6 | if (newValidator == null) { |
| 220 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR)); |
| 221 | |
} |
| 222 | 5 | this.validator = newValidator; |
| 223 | 5 | return this; |
| 224 | |
} |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
public final ArgumentBuilder withConsumeRemaining(final String newConsumeRemaining) { |
| 235 | 3 | if (newConsumeRemaining == null) { |
| 236 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING)); |
| 237 | |
} |
| 238 | 2 | if ( "".equals(newConsumeRemaining)) { |
| 239 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING)); |
| 240 | |
} |
| 241 | 1 | this.consumeRemaining = newConsumeRemaining; |
| 242 | 1 | return this; |
| 243 | |
} |
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
public final ArgumentBuilder withDefault(final Object defaultValue) { |
| 252 | 16 | if (defaultValue == null) { |
| 253 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT)); |
| 254 | |
} |
| 255 | |
|
| 256 | 15 | if (this.defaultValues == null) { |
| 257 | 7 | this.defaultValues = new ArrayList(1); |
| 258 | |
} |
| 259 | 15 | this.defaultValues.add(defaultValue); |
| 260 | 15 | return this; |
| 261 | |
} |
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
public final ArgumentBuilder withDefaults(final List newDefaultValues) { |
| 270 | 8 | if (newDefaultValues == null) { |
| 271 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS)); |
| 272 | |
} |
| 273 | 7 | this.defaultValues = newDefaultValues; |
| 274 | 7 | return this; |
| 275 | |
} |
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
public final ArgumentBuilder withId(final int newId) { |
| 284 | 1 | this.id = newId; |
| 285 | 1 | return this; |
| 286 | |
} |
| 287 | |
} |