Author: rfscholte
Date: Sat Feb 25 11:09:03 2017
New Revision: 1784363
URL: http://svn.apache.org/viewvc?rev=1784363&view=rev
Log:
CLI-269: Introduce CommandLine.Builder
Modified:
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
Modified:
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java?rev=1784363&r1=1784362&r2=1784363&view=diff
==============================================================================
---
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
(original)
+++
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
Sat Feb 25 11:09:03 2017
@@ -377,4 +377,42 @@ public class CommandLine implements Seri
// return the array
return processed.toArray(optionsArray);
}
+
+ /**
+ * A nested builder class to create <code>CommandLine</code> instance
+ * using descriptive methods.
+ *
+ * @since 1.4
+ */
+ public static final class Builder
+ {
+ private final CommandLine commandLine = new CommandLine();
+
+ /**
+ * Add an option to the command line. The values of the option are
stored.
+ *
+ * @param opt the processed option
+ */
+ public Builder addOption( Option opt )
+ {
+ commandLine.addOption( opt );
+ return this;
+ }
+
+ /**
+ * Add left-over unrecognized option/argument.
+ *
+ * @param arg the unrecognized option/argument.
+ */
+ public Builder addArg( String arg )
+ {
+ commandLine.addArg( arg );
+ return this;
+ }
+
+ public CommandLine build()
+ {
+ return commandLine;
+ }
+ }
}
Modified:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java?rev=1784363&r1=1784362&r2=1784363&view=diff
==============================================================================
---
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
(original)
+++
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
Sat Feb 25 11:09:03 2017
@@ -76,4 +76,18 @@ public class CommandLineTest
assertEquals(123, ((Number) cmd.getParsedOptionValue("i")).intValue());
assertEquals("foo", cmd.getParsedOptionValue("f"));
}
+
+ @Test
+ public void testBuilder()
+ throws Exception
+ {
+ CommandLine.Builder builder = new CommandLine.Builder();
+ builder.addArg( "foo" ).addArg( "bar" );
+ builder.addOption( Option.builder( "T" ).build() );
+ CommandLine cmd = builder.build();
+
+ assertEquals( "foo", cmd.getArgs()[0] );
+ assertEquals( "bar", cmd.getArgList().get( 1 ) );
+ assertEquals( "T", cmd.getOptions()[0].getOpt() );
+ }
}