Skip to content

Commit 38ab386

Browse files
committed
Add more descriptive methods to Option.Builder, adapt unit tests.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1447094 13f79535-47bb-0310-9956-ffa450edef68
1 parent 639e070 commit 38ab386

5 files changed

Lines changed: 80 additions & 42 deletions

File tree

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,16 @@ public Builder optionalArg(final boolean isOptional)
894894
return this;
895895
}
896896

897+
/**
898+
* Marks this Option as required.
899+
*
900+
* @return this builder, to allow method chaining
901+
*/
902+
public Builder required()
903+
{
904+
return required(true);
905+
}
906+
897907
/**
898908
* Sets whether the Option is mandatory.
899909
*
@@ -917,10 +927,31 @@ public Builder type(final Class<?> type)
917927
this.type = type;
918928
return this;
919929
}
920-
930+
921931
/**
922-
* Sets the value separator. For example if the argument value
923-
* was a Java property, the value separator would be '='.
932+
* The Option will use '=' as a means to separate argument value.
933+
*
934+
* @return this builder, to allow method chaining
935+
*/
936+
public Builder valueSeparator()
937+
{
938+
return valueSeparator('=');
939+
}
940+
941+
/**
942+
* The Option will use <code>sep</code> as a means to
943+
* separate argument values.
944+
* <p>
945+
* <b>Example:</b>
946+
* <pre>
947+
* Option opt = Option.builder("D").valueSeparator('=')
948+
* .build();
949+
*
950+
* String args = "-Dkey=value";
951+
* CommandLine line = parser.parse(args);
952+
* String propertyName = opt.getValue(0); // will be "key"
953+
* String propertyValue = opt.getValue(1); // will be "value"
954+
* </pre>
924955
*
925956
* @param sep The value separator.
926957
* @return this builder, to allow method chaining

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,16 @@ public static OptionBuilder isRequired()
155155
/**
156156
* The next Option created uses <code>sep</code> as a means to
157157
* separate argument values.
158-
*
158+
* <p>
159159
* <b>Example:</b>
160160
* <pre>
161-
* Option opt = OptionBuilder.withValueSeparator(':')
161+
* Option opt = OptionBuilder.withValueSeparator('=')
162162
* .create('D');
163163
*
164+
* String args = "-Dkey=value";
164165
* CommandLine line = parser.parse(args);
165-
* String propertyName = opt.getValue(0);
166-
* String propertyValue = opt.getValue(1);
166+
* String propertyName = opt.getValue(0); // will be "key"
167+
* String propertyValue = opt.getValue(1); // will be "value"
167168
* </pre>
168169
*
169170
* @param sep The value separator to be used for the argument values.

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

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ public void testPrintSortedUsageWithNullComparator()
322322
public void testPrintOptionGroupUsage()
323323
{
324324
OptionGroup group = new OptionGroup();
325-
group.addOption(OptionBuilder.create("a"));
326-
group.addOption(OptionBuilder.create("b"));
327-
group.addOption(OptionBuilder.create("c"));
325+
group.addOption(Option.builder("a").build());
326+
group.addOption(Option.builder("b").build());
327+
group.addOption(Option.builder("c").build());
328328

329329
Options options = new Options();
330330
options.addOptionGroup(group);
@@ -341,9 +341,9 @@ public void testPrintOptionGroupUsage()
341341
public void testPrintRequiredOptionGroupUsage()
342342
{
343343
OptionGroup group = new OptionGroup();
344-
group.addOption(OptionBuilder.create("a"));
345-
group.addOption(OptionBuilder.create("b"));
346-
group.addOption(OptionBuilder.create("c"));
344+
group.addOption(Option.builder("a").build());
345+
group.addOption(Option.builder("b").build());
346+
group.addOption(Option.builder("c").build());
347347
group.setRequired(true);
348348

349349
Options options = new Options();
@@ -378,7 +378,7 @@ public void testPrintOptionWithEmptyArgNameUsage()
378378
@Test
379379
public void testDefaultArgName()
380380
{
381-
Option option = OptionBuilder.hasArg().isRequired().create("f");
381+
Option option = Option.builder("f").hasArg().required(true).build();
382382

383383
Options options = new Options();
384384
options.addOption(option);
@@ -501,35 +501,36 @@ public void testOptionWithoutShortFormat2()
501501
Option newRun = new Option("n", "new", false, "Create NLT cache entries only for new items");
502502
Option trackerRun = new Option("t", "tracker", false, "Create NLT cache entries only for tracker items");
503503

504-
Option timeLimit = OptionBuilder.withLongOpt("limit")
505-
.hasArg()
506-
.withValueSeparator()
507-
.withDescription("Set time limit for execution, in mintues")
508-
.create("l");
504+
Option timeLimit = Option.builder("l")
505+
.longOpt("limit")
506+
.hasArg()
507+
.valueSeparator()
508+
.desc("Set time limit for execution, in mintues")
509+
.build();
509510

510-
Option age = OptionBuilder.withLongOpt("age")
511+
Option age = Option.builder("a").longOpt("age")
511512
.hasArg()
512-
.withValueSeparator()
513-
.withDescription("Age (in days) of cache item before being recomputed")
514-
.create("a");
513+
.valueSeparator()
514+
.desc("Age (in days) of cache item before being recomputed")
515+
.build();
515516

516-
Option server = OptionBuilder.withLongOpt("server")
517-
.hasArg()
518-
.withValueSeparator()
519-
.withDescription("The NLT server address")
520-
.create("s");
517+
Option server = Option.builder("s").longOpt("server")
518+
.hasArg()
519+
.valueSeparator()
520+
.desc("The NLT server address")
521+
.build();
521522

522-
Option numResults = OptionBuilder.withLongOpt("results")
523-
.hasArg()
524-
.withValueSeparator()
525-
.withDescription("Number of results per item")
526-
.create("r");
523+
Option numResults = Option.builder("r").longOpt("results")
524+
.hasArg()
525+
.valueSeparator()
526+
.desc("Number of results per item")
527+
.build();
527528

528-
Option configFile = OptionBuilder.withLongOpt("config")
529-
.hasArg()
530-
.withValueSeparator()
531-
.withDescription("Use the specified configuration file")
532-
.create();
529+
Option configFile = Option.builder().longOpt("config")
530+
.hasArg()
531+
.valueSeparator()
532+
.desc("Use the specified configuration file")
533+
.build();
533534

534535
Options mOptions = new Options();
535536
mOptions.addOption(help);
@@ -568,8 +569,8 @@ public void testHelpWithLongOptSeparator() throws Exception
568569
{
569570
Options options = new Options();
570571
options.addOption( "f", true, "the file" );
571-
options.addOption(OptionBuilder.withLongOpt("size").withDescription("the size").hasArg().withArgName("SIZE").create('s'));
572-
options.addOption(OptionBuilder.withLongOpt("age").withDescription("the age").hasArg().create());
572+
options.addOption(Option.builder("s").longOpt("size").desc("the size").hasArg().argName("SIZE").build());
573+
options.addOption(Option.builder().longOpt("age").desc("the age").hasArg().build());
573574

574575
HelpFormatter formatter = new HelpFormatter();
575576
assertEquals(HelpFormatter.DEFAULT_LONG_OPT_SEPARATOR, formatter.getLongOptSeparator());
@@ -595,8 +596,8 @@ public void testUsageWithLongOptSeparator() throws Exception
595596
{
596597
Options options = new Options();
597598
options.addOption( "f", true, "the file" );
598-
options.addOption(OptionBuilder.withLongOpt("size").withDescription("the size").hasArg().withArgName("SIZE").create('s'));
599-
options.addOption(OptionBuilder.withLongOpt("age").withDescription("the age").hasArg().create());
599+
options.addOption(Option.builder("s").longOpt("size").desc("the size").hasArg().argName("SIZE").build());
600+
options.addOption(Option.builder().longOpt("age").desc("the age").hasArg().build());
600601

601602
HelpFormatter formatter = new HelpFormatter();
602603
formatter.setLongOptSeparator("=");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.junit.Test;
2626

27+
@SuppressWarnings("deprecation") // OptionBuilder is marked deprecated
2728
public class OptionBuilderTest
2829
{
2930
@Test

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class OptionTest
2828
{
2929
private static class TestOption extends Option
3030
{
31+
private static final long serialVersionUID = 1L;
32+
3133
public TestOption(String opt, boolean hasArg, String description) throws IllegalArgumentException
3234
{
3335
super(opt, hasArg, description);
@@ -72,6 +74,8 @@ public void testClone()
7274

7375
private static class DefaultOption extends Option
7476
{
77+
private static final long serialVersionUID = 1L;
78+
7579
private final String defaultValue;
7680

7781
public DefaultOption(String opt, String description, String defaultValue) throws IllegalArgumentException

0 commit comments

Comments
 (0)