commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject [29/50] [abbrv] commons-cli git commit: CLI-269: Introduce CommandLine.Builder
Date Thu, 08 Jun 2017 17:36:58 GMT
CLI-269: Introduce CommandLine.Builder


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1784363 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/commons-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-cli/commit/dc45fde7
Tree: http://git-wip-us.apache.org/repos/asf/commons-cli/tree/dc45fde7
Diff: http://git-wip-us.apache.org/repos/asf/commons-cli/diff/dc45fde7

Branch: refs/heads/master
Commit: dc45fde7e5988908371eb4cccfeeb7a078bf714b
Parents: 0f56df9
Author: Robert Scholte <rfscholte@apache.org>
Authored: Sat Feb 25 11:09:03 2017 +0000
Committer: Robert Scholte <rfscholte@apache.org>
Committed: Sat Feb 25 11:09:03 2017 +0000

----------------------------------------------------------------------
 .../org/apache/commons/cli/CommandLine.java     | 38 ++++++++++++++++++++
 .../org/apache/commons/cli/CommandLineTest.java | 14 ++++++++
 2 files changed, 52 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-cli/blob/dc45fde7/src/main/java/org/apache/commons/cli/CommandLine.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/cli/CommandLine.java b/src/main/java/org/apache/commons/cli/CommandLine.java
index 6890e6f..a6dff13 100644
--- a/src/main/java/org/apache/commons/cli/CommandLine.java
+++ b/src/main/java/org/apache/commons/cli/CommandLine.java
@@ -377,4 +377,42 @@ public class CommandLine implements Serializable
         // 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;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-cli/blob/dc45fde7/src/test/java/org/apache/commons/cli/CommandLineTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/cli/CommandLineTest.java b/src/test/java/org/apache/commons/cli/CommandLineTest.java
index 60a0c91..a25fb02 100644
--- a/src/test/java/org/apache/commons/cli/CommandLineTest.java
+++ b/src/test/java/org/apache/commons/cli/CommandLineTest.java
@@ -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() );
+    }
 }


Mime
View raw message