Skip to content

Commit 509f6e1

Browse files
committed
Simplify internal delegation
1 parent 2d3586f commit 509f6e1

4 files changed

Lines changed: 17 additions & 26 deletions

File tree

src/main/java/org/apache/commons/cli/DefaultParser.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ protected void handleConcatenatedOptions(final String token) throws ParseExcepti
346346

347347
if (currentOption != null && token.length() != i + 1) {
348348
// add the trail as an argument of the option
349-
currentOption.addValueForProcessing(stripLeadingAndTrailingQuotesDefaultOff(token.substring(i + 1)));
349+
currentOption.processValue(stripLeadingAndTrailingQuotesDefaultOff(token.substring(i + 1)));
350350
break;
351351
}
352352
}
@@ -388,7 +388,7 @@ private void handleLongOptionWithEqual(final String token) throws ParseException
388388
final Option option = options.getOption(key);
389389
if (option.acceptsArg()) {
390390
handleOption(option);
391-
currentOption.addValueForProcessing(stripLeadingAndTrailingQuotesDefaultOff(value));
391+
currentOption.processValue(stripLeadingAndTrailingQuotesDefaultOff(value));
392392
currentOption = null;
393393
} else {
394394
handleUnknownToken(currentToken);
@@ -448,7 +448,7 @@ private void handleProperties(final Properties properties) throws ParseException
448448

449449
if (opt.hasArg()) {
450450
if (opt.getValues() == null || opt.getValues().length == 0) {
451-
opt.addValueForProcessing(stripLeadingAndTrailingQuotesDefaultOff(value));
451+
opt.processValue(stripLeadingAndTrailingQuotesDefaultOff(value));
452452
}
453453
} else if (!("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {
454454
// if the value is not yes, true or 1 then don't add the option to the CommandLine
@@ -492,12 +492,12 @@ private void handleShortAndLongOption(final String token) throws ParseException
492492

493493
if (opt != null && options.getOption(opt).acceptsArg()) {
494494
handleOption(options.getOption(opt));
495-
currentOption.addValueForProcessing(stripLeadingAndTrailingQuotesDefaultOff(t.substring(opt.length())));
495+
currentOption.processValue(stripLeadingAndTrailingQuotesDefaultOff(t.substring(opt.length())));
496496
currentOption = null;
497497
} else if (isJavaProperty(t)) {
498498
// -SV1 (-Dflag)
499499
handleOption(options.getOption(t.substring(0, 1)));
500-
currentOption.addValueForProcessing(stripLeadingAndTrailingQuotesDefaultOff(t.substring(1)));
500+
currentOption.processValue(stripLeadingAndTrailingQuotesDefaultOff(t.substring(1)));
501501
currentOption = null;
502502
} else {
503503
// -S1S2S3 or -S1S2V
@@ -514,16 +514,16 @@ private void handleShortAndLongOption(final String token) throws ParseException
514514
final Option option = options.getOption(opt);
515515
if (option != null && option.acceptsArg()) {
516516
handleOption(option);
517-
currentOption.addValueForProcessing(value);
517+
currentOption.processValue(value);
518518
currentOption = null;
519519
} else {
520520
handleUnknownToken(token);
521521
}
522522
} else if (isJavaProperty(opt)) {
523523
// -SV1=V2 (-Dkey=value)
524524
handleOption(options.getOption(opt.substring(0, 1)));
525-
currentOption.addValueForProcessing(opt.substring(1));
526-
currentOption.addValueForProcessing(value);
525+
currentOption.processValue(opt.substring(1));
526+
currentOption.processValue(value);
527527
currentOption = null;
528528
} else {
529529
// -L=V or -l=V
@@ -545,7 +545,7 @@ private void handleToken(final String token) throws ParseException {
545545
} else if ("--".equals(token)) {
546546
skipParsing = true;
547547
} else if (currentOption != null && currentOption.acceptsArg() && isArgument(token)) {
548-
currentOption.addValueForProcessing(stripLeadingAndTrailingQuotesDefaultOn(token));
548+
currentOption.processValue(stripLeadingAndTrailingQuotesDefaultOn(token));
549549
} else if (token.startsWith("--")) {
550550
handleLongOption(token);
551551
} else if (token.startsWith("-") && !"-".equals(token)) {

src/main/java/org/apache/commons/cli/Option.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -507,19 +507,7 @@ private void add(final String value) {
507507
@Deprecated
508508
public boolean addValue(final String value) {
509509
throw new UnsupportedOperationException(
510-
"The addValue method is not intended for client use. Subclasses should use the addValueForProcessing method instead.");
511-
}
512-
513-
/**
514-
* Adds the specified value to this Option.
515-
*
516-
* @param value is a/the value of this Option.
517-
*/
518-
void addValueForProcessing(final String value) {
519-
if (argCount == UNINITIALIZED) {
520-
throw new IllegalArgumentException("NO_ARGS_ALLOWED");
521-
}
522-
processValue(value);
510+
"The addValue method is not intended for client use. Subclasses should use the processValue method instead.");
523511
}
524512

525513
/**
@@ -823,7 +811,10 @@ public boolean isRequired() {
823811
*
824812
* @param value The String to be processed.
825813
*/
826-
private void processValue(final String value) {
814+
void processValue(final String value) {
815+
if (argCount == UNINITIALIZED) {
816+
throw new IllegalArgumentException("NO_ARGS_ALLOWED");
817+
}
827818
String add = value;
828819
// this Option has a separator character
829820
if (hasValueSeparator()) {

src/main/java/org/apache/commons/cli/Parser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void processArgs(final Option opt, final ListIterator<String> iter) throw
224224
}
225225
// found a value
226226
try {
227-
opt.addValueForProcessing(Util.stripLeadingAndTrailingQuotes(str));
227+
opt.processValue(Util.stripLeadingAndTrailingQuotes(str));
228228
} catch (final RuntimeException exp) {
229229
iter.previous();
230230
break;
@@ -287,7 +287,7 @@ protected void processProperties(final Properties properties) throws ParseExcept
287287
if (opt.hasArg()) {
288288
if (opt.getValues() == null || opt.getValues().length == 0) {
289289
try {
290-
opt.addValueForProcessing(value);
290+
opt.processValue(value);
291291
} catch (final RuntimeException exp) { // NOPMD
292292
// if we cannot add the value don't worry about it
293293
}

src/test/java/org/apache/commons/cli/OptionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private Option roundTrip(final Option o) throws IOException, ClassNotFoundExcept
104104
public void testAddValue() {
105105
final Option option = new Option("f", null);
106106
assertThrows(UnsupportedOperationException.class, () -> option.addValue(""));
107-
assertThrows(IllegalArgumentException.class, () -> option.addValueForProcessing(""));
107+
assertThrows(IllegalArgumentException.class, () -> option.processValue(""));
108108
}
109109

110110
@Test

0 commit comments

Comments
 (0)