Skip to content
Permalink
Browse files
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
  • Loading branch information
netomi committed Feb 17, 2013
1 parent 639e070 commit 38ab386d9d86c6cacea817954064bb25fba312aa
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 42 deletions.
@@ -894,6 +894,16 @@ public Builder optionalArg(final boolean isOptional)
return this;
}

/**
* Marks this Option as required.
*
* @return this builder, to allow method chaining
*/
public Builder required()
{
return required(true);
}

/**
* Sets whether the Option is mandatory.
*
@@ -917,10 +927,31 @@ public Builder type(final Class<?> type)
this.type = type;
return this;
}

/**
* Sets the value separator. For example if the argument value
* was a Java property, the value separator would be '='.
* The Option will use '=' as a means to separate argument value.
*
* @return this builder, to allow method chaining
*/
public Builder valueSeparator()
{
return valueSeparator('=');
}

/**
* The Option will use <code>sep</code> as a means to
* separate argument values.
* <p>
* <b>Example:</b>
* <pre>
* Option opt = Option.builder("D").valueSeparator('=')
* .build();
*
* String args = "-Dkey=value";
* CommandLine line = parser.parse(args);
* String propertyName = opt.getValue(0); // will be "key"
* String propertyValue = opt.getValue(1); // will be "value"
* </pre>
*
* @param sep The value separator.
* @return this builder, to allow method chaining
@@ -155,15 +155,16 @@ public static OptionBuilder isRequired()
/**
* The next Option created uses <code>sep</code> as a means to
* separate argument values.
*
* <p>
* <b>Example:</b>
* <pre>
* Option opt = OptionBuilder.withValueSeparator(':')
* Option opt = OptionBuilder.withValueSeparator('=')
* .create('D');
*
* String args = "-Dkey=value";
* CommandLine line = parser.parse(args);
* String propertyName = opt.getValue(0);
* String propertyValue = opt.getValue(1);
* String propertyName = opt.getValue(0); // will be "key"
* String propertyValue = opt.getValue(1); // will be "value"
* </pre>
*
* @param sep The value separator to be used for the argument values.
@@ -322,9 +322,9 @@ public void testPrintSortedUsageWithNullComparator()
public void testPrintOptionGroupUsage()
{
OptionGroup group = new OptionGroup();
group.addOption(OptionBuilder.create("a"));
group.addOption(OptionBuilder.create("b"));
group.addOption(OptionBuilder.create("c"));
group.addOption(Option.builder("a").build());
group.addOption(Option.builder("b").build());
group.addOption(Option.builder("c").build());

Options options = new Options();
options.addOptionGroup(group);
@@ -341,9 +341,9 @@ public void testPrintOptionGroupUsage()
public void testPrintRequiredOptionGroupUsage()
{
OptionGroup group = new OptionGroup();
group.addOption(OptionBuilder.create("a"));
group.addOption(OptionBuilder.create("b"));
group.addOption(OptionBuilder.create("c"));
group.addOption(Option.builder("a").build());
group.addOption(Option.builder("b").build());
group.addOption(Option.builder("c").build());
group.setRequired(true);

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

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

Option timeLimit = OptionBuilder.withLongOpt("limit")
.hasArg()
.withValueSeparator()
.withDescription("Set time limit for execution, in mintues")
.create("l");
Option timeLimit = Option.builder("l")
.longOpt("limit")
.hasArg()
.valueSeparator()
.desc("Set time limit for execution, in mintues")
.build();

Option age = OptionBuilder.withLongOpt("age")
Option age = Option.builder("a").longOpt("age")
.hasArg()
.withValueSeparator()
.withDescription("Age (in days) of cache item before being recomputed")
.create("a");
.valueSeparator()
.desc("Age (in days) of cache item before being recomputed")
.build();

Option server = OptionBuilder.withLongOpt("server")
.hasArg()
.withValueSeparator()
.withDescription("The NLT server address")
.create("s");
Option server = Option.builder("s").longOpt("server")
.hasArg()
.valueSeparator()
.desc("The NLT server address")
.build();

Option numResults = OptionBuilder.withLongOpt("results")
.hasArg()
.withValueSeparator()
.withDescription("Number of results per item")
.create("r");
Option numResults = Option.builder("r").longOpt("results")
.hasArg()
.valueSeparator()
.desc("Number of results per item")
.build();

Option configFile = OptionBuilder.withLongOpt("config")
.hasArg()
.withValueSeparator()
.withDescription("Use the specified configuration file")
.create();
Option configFile = Option.builder().longOpt("config")
.hasArg()
.valueSeparator()
.desc("Use the specified configuration file")
.build();

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

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

HelpFormatter formatter = new HelpFormatter();
formatter.setLongOptSeparator("=");
@@ -24,6 +24,7 @@

import org.junit.Test;

@SuppressWarnings("deprecation") // OptionBuilder is marked deprecated
public class OptionBuilderTest
{
@Test
@@ -28,6 +28,8 @@
{
private static class TestOption extends Option
{
private static final long serialVersionUID = 1L;

public TestOption(String opt, boolean hasArg, String description) throws IllegalArgumentException
{
super(opt, hasArg, description);
@@ -72,6 +74,8 @@ public void testClone()

private static class DefaultOption extends Option
{
private static final long serialVersionUID = 1L;

private final String defaultValue;

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

0 comments on commit 38ab386

Please sign in to comment.