Skip to content

Commit b0b1985

Browse files
committed
[cli-221] checkstyle fixes, option attribute renamed, documentation fixes
1 parent a1d4bc5 commit b0b1985

4 files changed

Lines changed: 53 additions & 34 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ private void handleToken(final String token) throws ParseException {
609609
skipParsing = true;
610610
} else if (currentOption != null && currentOption.acceptsArg() && isArgument(token)) {
611611
currentOption.processValue(stripLeadingAndTrailingQuotesDefaultOn(token));
612-
if (currentOption.areValuesAsList()) {
612+
if (currentOption.isValueSeparatorUsedForSingleArgument()) {
613613
currentOption = null;
614614
}
615615
} else if (token.startsWith("--")) {

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private static Class<?> toType(final Class<?> type) {
103103
private char valueSeparator;
104104

105105
/** multiple values are within a single argument separated by valueSeparator char */
106-
private boolean valuesAsList;
106+
private boolean valueSeparatorUsedForSingleArgument;
107107

108108
/**
109109
* Constructs a new {@code Builder} with the minimum required parameters for an {@code Option} instance.
@@ -318,7 +318,7 @@ public Builder valueSeparator() {
318318
}
319319

320320
/**
321-
* The Option will use {@code sep} as a means to separate java-property-style argument values
321+
* The Option will use {@code sep} as a means to separate java-property-style argument values.
322322
*
323323
* Method is mutually exclusive to listValueSeparator() method.
324324
* <p>
@@ -336,7 +336,7 @@ public Builder valueSeparator() {
336336
* String propertyValue = line.getOptionValues("D")[1]; // will be "value"
337337
* </pre>
338338
*
339-
* In the above example (unlimited args), followup arguments are interpreted
339+
* In the above example, followup arguments are interpreted
340340
* to be additional values to this option, needs to be terminated with -- so that
341341
* others options or args can follow.
342342
*
@@ -359,26 +359,28 @@ public Builder listValueSeparator() {
359359
}
360360

361361
/**
362-
* defines the separator used to separate a list of values passed in a single arg
362+
* defines the separator used to split a list of values passed in a single argument.
363+
*
364+
* Method is mutually exclusive to valueSeparator() method. In the resulting option,
365+
* isValueSeparatorUsedForSingleArgument() will return true.
363366
*
364-
* Method is mutually exclusive to valueSeparator() method.
365367
* <p>
366368
* <strong>Example:</strong>
367369
* </p>
368370
*
369371
* <pre>
370-
* final Option colors = Option.builder().option("c").longOpt("colors").hasArgs().listValueSeparator('|').build();
372+
* final Option colors = Option.builder().option("c").hasArgs().listValueSeparator('|').build();
371373
* final Options options = new Options();
372374
* options.addOption(colors);
373375
*
374376
* final String[] args = {"-c", "red|blue|yellow", "b,c"};
375377
* final DefaultParser parser = new DefaultParser();
376378
* final CommandLine commandLine = parser.parse(options, args, null, true);
377-
* String [] colorValues = commandLine.getOptionValues(colors);
379+
* final String [] colorValues = commandLine.getOptionValues(colors);
378380
* // colorValues[0] will be "red"
379381
* // colorValues[1] will be "blue"
380382
* // colorValues[2] will be "yellow"
381-
* String arguments = commandLine.getArgs()[0]; // will be b,c
383+
* final String arguments = commandLine.getArgs()[0]; // will be b,c
382384
*
383385
* </pre>
384386
*
@@ -388,7 +390,7 @@ public Builder listValueSeparator() {
388390
*/
389391
public Builder listValueSeparator(final char listValueSeparator) {
390392
this.valueSeparator = listValueSeparator;
391-
this.valuesAsList = true;
393+
this.valueSeparatorUsedForSingleArgument = true;
392394
return this;
393395
}
394396
}
@@ -472,7 +474,7 @@ public static Builder builder(final String option) {
472474
private char valueSeparator;
473475

474476
/** multiple values are within a single argument separated by valueSeparator char */
475-
private boolean valuesAsList;
477+
private boolean valueSeparatorUsedForSingleArgument;
476478

477479
/**
478480
* Private constructor used by the nested Builder class.
@@ -492,7 +494,7 @@ private Option(final Builder builder) {
492494
this.type = builder.type;
493495
this.valueSeparator = builder.valueSeparator;
494496
this.converter = builder.converter;
495-
this.valuesAsList = builder.valuesAsList;
497+
this.valueSeparatorUsedForSingleArgument = builder.valueSeparatorUsedForSingleArgument;
496498
}
497499

498500
/**
@@ -877,13 +879,24 @@ public boolean isRequired() {
877879
}
878880

879881
/**
880-
* Tests whether multiple values are expected in a single argument separated by a separation character
882+
* Tests whether multiple values are expected in a single argument split by a separation character
883+
*
884+
* @return boolean true when the builder's listValueSeparator() method was used. Multiple values are expected in a single argument and
885+
* are split by a separation character.
886+
* @since 1.10.0
887+
*/
888+
public boolean isValueSeparatorUsedForSingleArgument() {
889+
return valueSeparatorUsedForSingleArgument;
890+
}
891+
892+
/**
893+
* Set this to true to use the valueSeparator only on a single argument. See also builder's listValueSeparator() method.
881894
*
882-
* @return boolean true when multiple values are expected in a single separated by a separation character
895+
* @param valueSeparatorUsedForSingleArgument the new value for this property
883896
* @since 1.10.0
884897
*/
885-
public boolean areValuesAsList() {
886-
return valuesAsList;
898+
public void setValueSeparatorUsedForSingleArgument(final boolean valueSeparatorUsedForSingleArgument) {
899+
this.valueSeparatorUsedForSingleArgument = valueSeparatorUsedForSingleArgument;
887900
}
888901

889902
/**

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ void listValueSeparatorTest() throws ParseException {
339339
final String[] args = {"-c", "red|blue|yellow", "b,c"};
340340
final DefaultParser parser = new DefaultParser();
341341
final CommandLine commandLine = parser.parse(options, args, null, true);
342-
String [] colorValues = commandLine.getOptionValues(colors);
343-
assertEquals(3, colorValues.length );
342+
final String [] colorValues = commandLine.getOptionValues(colors);
343+
assertEquals(3, colorValues.length);
344344
assertEquals("red", colorValues[0]);
345345
assertEquals("blue", colorValues[1]);
346346
assertEquals("yellow", colorValues[2]);
@@ -350,18 +350,18 @@ void listValueSeparatorTest() throws ParseException {
350350
@ParameterizedTest
351351
@ValueSource(strings = {
352352
"--colors=red,blue,yellow b",
353-
"--colors red,blue,yellow b" ,
354-
"-c=red,blue,yellow b" ,
355-
"-c red,blue,yellow b" })
356-
void listValueSeparatorDefaultTest(String args) throws ParseException {
353+
"--colors red,blue,yellow b",
354+
"-c=red,blue,yellow b",
355+
"-c red,blue,yellow b"})
356+
void listValueSeparatorDefaultTest(final String args) throws ParseException {
357357
final Option colors = Option.builder().option("c").longOpt("colors").hasArgs().listValueSeparator().build();
358358
final Options options = new Options();
359359
options.addOption(colors);
360360

361361
final DefaultParser parser = new DefaultParser();
362362
final CommandLine commandLine = parser.parse(options, args.split(" "), null, true);
363-
String [] colorValues = commandLine.getOptionValues(colors);
364-
assertEquals(3, colorValues.length );
363+
final String [] colorValues = commandLine.getOptionValues(colors);
364+
assertEquals(3, colorValues.length);
365365
assertEquals("red", colorValues[0]);
366366
assertEquals("blue", colorValues[1]);
367367
assertEquals("yellow", colorValues[2]);
@@ -386,7 +386,7 @@ void listValueSeparatorSeriesDoesntMatter(final String args) throws ParseExcepti
386386
final CommandLine commandLine = parser.parse(options, args.split(" "), null, false);
387387
final String [] colorValues = commandLine.getOptionValues(colors);
388388
final String fooValue = commandLine.getOptionValue(foo);
389-
assertEquals(3, colorValues.length );
389+
assertEquals(3, colorValues.length);
390390
assertEquals("red", colorValues[0]);
391391
assertEquals("blue", colorValues[1]);
392392
assertEquals("yellow", colorValues[2]);

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,24 +348,30 @@ void testTypeObject() {
348348
@Test
349349
void testDefaultValueSeparator() {
350350
final Option option = Option.builder().option("a").hasArgs().valueSeparator().build();
351-
assertFalse(option.areValuesAsList());
351+
assertFalse(option.isValueSeparatorUsedForSingleArgument());
352352
assertTrue(option.hasValueSeparator());
353-
assertEquals('=',option.getValueSeparator());
353+
assertEquals('=', option.getValueSeparator());
354354
}
355355

356356
@Test
357-
void testDefaultValueAsList() {
357+
void testDefaultListValueSeparator() {
358358
final Option option = Option.builder().option("a").hasArgs().listValueSeparator().build();
359-
assertTrue(option.areValuesAsList());
359+
assertTrue(option.isValueSeparatorUsedForSingleArgument());
360360
assertTrue(option.hasValueSeparator());
361-
assertEquals(',',option.getValueSeparator());
361+
assertEquals(',', option.getValueSeparator());
362362
}
363-
363+
364364
@Test
365-
void testValueAsList() {
365+
void testListValueSeparator() {
366366
final Option option = Option.builder().option("a").hasArgs().listValueSeparator('|').build();
367-
assertTrue(option.areValuesAsList());
367+
assertTrue(option.isValueSeparatorUsedForSingleArgument());
368+
assertTrue(option.hasValueSeparator());
369+
assertEquals('|', option.getValueSeparator());
370+
371+
option.setValueSeparatorUsedForSingleArgument(false);
372+
assertFalse(option.isValueSeparatorUsedForSingleArgument());
368373
assertTrue(option.hasValueSeparator());
369-
assertEquals('|',option.getValueSeparator());
374+
assertEquals('|', option.getValueSeparator());
375+
370376
}
371377
}

0 commit comments

Comments
 (0)