2020
2121import org .apache .commons .cli2 .Argument ;
2222import org .apache .commons .cli2 .option .ArgumentImpl ;
23+ import org .apache .commons .cli2 .resource .ResourceConstants ;
24+ import org .apache .commons .cli2 .resource .ResourceHelper ;
2325import org .apache .commons .cli2 .validation .Validator ;
2426
2527/**
2628 * Builds Argument instances.
2729 */
2830public class ArgumentBuilder {
2931
32+ /** i18n */
33+ private final static ResourceHelper resources = ResourceHelper .getResourceHelper ();
34+
3035 /** name of the argument. Used for display and lookups in CommandLine */
3136 private String name ;
3237
@@ -118,6 +123,12 @@ public final ArgumentBuilder reset() {
118123 * @return this ArgumentBuilder
119124 */
120125 public final ArgumentBuilder withName (final String newName ) {
126+ if (newName == null ) {
127+ throw new IllegalArgumentException (resources .getMessage (ResourceConstants .ARGUMENT_BUILDER_NULL_NAME ));
128+ }
129+ if ("" .equals (newName )) {
130+ throw new IllegalArgumentException (resources .getMessage (ResourceConstants .ARGUMENT_BUILDER_EMPTY_NAME ));
131+ }
121132 this .name = newName ;
122133 return this ;
123134 }
@@ -142,6 +153,9 @@ public final ArgumentBuilder withDescription(final String newDescription) {
142153 * @return this ArgumentBuilder
143154 */
144155 public final ArgumentBuilder withMinimum (final int newMinimum ) {
156+ if (newMinimum < 0 ) {
157+ throw new IllegalArgumentException (resources .getMessage (ResourceConstants .ARGUMENT_BUILDER_NEGATIVE_MINIMUM ));
158+ }
145159 this .minimum = newMinimum ;
146160 return this ;
147161 }
@@ -153,6 +167,9 @@ public final ArgumentBuilder withMinimum(final int newMinimum) {
153167 * @return this ArgumentBuilder
154168 */
155169 public final ArgumentBuilder withMaximum (final int newMaximum ) {
170+ if (newMaximum < 0 ) {
171+ throw new IllegalArgumentException (resources .getMessage (ResourceConstants .ARGUMENT_BUILDER_NEGATIVE_MAXIMUM ));
172+ }
156173 this .maximum = newMaximum ;
157174 return this ;
158175 }
@@ -197,6 +214,9 @@ public final ArgumentBuilder withSubsequentSeparator(
197214 * @return this ArgumentBuilder
198215 */
199216 public final ArgumentBuilder withValidator (final Validator newValidator ) {
217+ if (newValidator == null ) {
218+ throw new IllegalArgumentException (resources .getMessage (ResourceConstants .ARGUMENT_BUILDER_NULL_VALIDATOR ));
219+ }
200220 this .validator = newValidator ;
201221 return this ;
202222 }
@@ -210,7 +230,12 @@ public final ArgumentBuilder withValidator(final Validator newValidator) {
210230 * @return this ArgumentBuilder
211231 */
212232 public final ArgumentBuilder withConsumeRemaining (final String newConsumeRemaining ) {
213-
233+ if (newConsumeRemaining == null ) {
234+ throw new IllegalArgumentException (resources .getMessage (ResourceConstants .ARGUMENT_BUILDER_NULL_CONSUME_REMAINING ));
235+ }
236+ if ( "" .equals (newConsumeRemaining )) {
237+ throw new IllegalArgumentException (resources .getMessage (ResourceConstants .ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING ));
238+ }
214239 this .consumeRemaining = newConsumeRemaining ;
215240 return this ;
216241 }
@@ -222,6 +247,10 @@ public final ArgumentBuilder withConsumeRemaining(final String newConsumeRemaini
222247 * @return this ArgumentBuilder
223248 */
224249 public final ArgumentBuilder withDefault (final Object defaultValue ) {
250+ if (defaultValue == null ) {
251+ throw new IllegalArgumentException (resources .getMessage (ResourceConstants .ARGUMENT_BUILDER_NULL_DEFAULT ));
252+ }
253+
225254 if (this .defaultValues == null ) {
226255 this .defaultValues = new ArrayList (1 );
227256 }
@@ -236,6 +265,9 @@ public final ArgumentBuilder withDefault(final Object defaultValue) {
236265 * @return this ArgumentBuilder
237266 */
238267 public final ArgumentBuilder withDefaults (final List newDefaultValues ) {
268+ if (newDefaultValues == null ) {
269+ throw new IllegalArgumentException (resources .getMessage (ResourceConstants .ARGUMENT_BUILDER_NULL_DEFAULTS ));
270+ }
239271 this .defaultValues = newDefaultValues ;
240272 return this ;
241273 }
0 commit comments