commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r667565 - in /commons/proper/cli/branches/cli-1.x/src: java/org/apache/commons/cli/CommandLine.java java/org/apache/commons/cli/Parser.java test/org/apache/commons/cli/BugsTest.java
Date Fri, 13 Jun 2008 15:08:57 GMT
Author: ebourg
Date: Fri Jun 13 08:08:56 2008
New Revision: 667565

URL: http://svn.apache.org/viewvc?rev=667565&view=rev
Log:
Restored the CLI 1.0 behavior regarding repeated options and the number of arguments per option
(CLI-137)

Modified:
    commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java
    commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java

Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java?rev=667565&r1=667564&r2=667565&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java Fri
Jun 13 08:08:56 2008
@@ -17,12 +17,11 @@
 package org.apache.commons.cli;
 
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Set;
-import java.util.HashSet;
 
 /** 
  * <p>Represents list of arguments parsed against
@@ -47,7 +46,7 @@
     private List args = new LinkedList();
 
     /** the processed options */
-    private Set options = new HashSet();
+    private List options = new ArrayList();
 
     /** Map of unique options for ease to get complete list of options */
 //    private Set allOptions = new HashSet();
@@ -149,15 +148,19 @@
      */
     public String[] getOptionValues(String opt)
     {
-        Option key = resolveOption( opt );
+        List values = new ArrayList();
 
-        if (options.contains(key))
+        for ( Iterator it = options.iterator(); it.hasNext(); )
         {
-            return key.getValues();
+            Option option = (Option) it.next();
+            if (opt.equals(option.getOpt()) || opt.equals( option.getLongOpt()))
+            {
+                values.addAll(option.getValuesList());
+            }
         }
 
-        return null;
-        }
+        return values.isEmpty() ? null : (String[]) values.toArray(new String[values.size()]);
+    }
 
     /**
      * <p>Retrieves the option object given the long or short option as a String</p>
@@ -318,4 +321,4 @@
         // return the array
         return (Option[]) processed.toArray(optionsArray);
     }
-}
\ No newline at end of file
+}

Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java?rev=667565&r1=667564&r2=667565&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java Fri Jun
13 08:08:56 2008
@@ -396,7 +396,7 @@
         }
         
         // get the option represented by arg
-        final Option opt = getOptions().getOption(arg);
+        Option opt = (Option) getOptions().getOption(arg).clone();
 
         // if the option is a required option remove the option from
         // the requiredOptions list

Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java?rev=667565&r1=667564&r2=667565&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java Fri
Jun 13 08:08:56 2008
@@ -22,6 +22,7 @@
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Properties;
 
 import junit.framework.TestCase;
@@ -31,8 +32,7 @@
     public void test11457() throws Exception
     {
         Options options = new Options();
-        options.addOption(OptionBuilder.withLongOpt("verbose")
-                .create());
+        options.addOption(OptionBuilder.withLongOpt("verbose").create());
         String[] args = new String[]{"--verbose"};
 
         CommandLineParser parser = new PosixParser();
@@ -44,12 +44,8 @@
     public void test11458() throws Exception
     {
         Options options = new Options();
-        options.addOption( OptionBuilder.withValueSeparator( '=' )
-                           .hasArgs()
-                           .create( 'D' ) );
-        options.addOption( OptionBuilder.withValueSeparator( ':' )
-                           .hasArgs()
-                           .create( 'p' ) );
+        options.addOption( OptionBuilder.withValueSeparator( '=' ).hasArgs().create( 'D'
) );
+        options.addOption( OptionBuilder.withValueSeparator( ':' ).hasArgs().create( 'p'
) );
         String[] args = new String[] { "-DJAVA_HOME=/opt/java" , "-pfile1:file2:file3" };
 
         CommandLineParser parser = new PosixParser();
@@ -107,10 +103,8 @@
     {
         // Posix 
         Options options = new Options();
-        options.addOption( OptionBuilder.hasOptionalArg()
-                           .create( 'a' ) );
-        options.addOption( OptionBuilder.hasArg()
-                           .create( 'b' ) );
+        options.addOption( OptionBuilder.hasOptionalArg().create( 'a' ) );
+        options.addOption( OptionBuilder.hasArg().create( 'b' ) );
         String[] args = new String[] { "-a", "-bvalue" };
 
         CommandLineParser parser = new PosixParser();
@@ -120,10 +114,8 @@
 
         // GNU
         options = new Options();
-        options.addOption( OptionBuilder.hasOptionalArg()
-                           .create( 'a' ) );
-        options.addOption( OptionBuilder.hasArg()
-                           .create( 'b' ) );
+        options.addOption( OptionBuilder.hasOptionalArg().create( 'a' ) );
+        options.addOption( OptionBuilder.hasArg().create( 'b' ) );
         args = new String[] { "-a", "-b", "value" };
 
         parser = new GnuParser();
@@ -227,12 +219,9 @@
     public void test13666() throws Exception
     {
         Options options = new Options();
-        Option dir = OptionBuilder.withDescription( "dir" )
-                                       .hasArg()
-                                       .create( 'd' );
+        Option dir = OptionBuilder.withDescription( "dir" ).hasArg().create( 'd' );
         options.addOption( dir );
         
-        
         final PrintStream oldSystemOut = System.out;
         try
         {
@@ -279,29 +268,35 @@
         boolean exception = false;
 
         String[] args = new String[] {  };
-        try {
-            CommandLine line = parser.parse( opts, args );
+        try
+        {
+            CommandLine line = parser.parse(opts, args);
         }
-        catch( ParseException exp ) {
+        catch (ParseException exp)
+        {
             exception = true;
         }
 
-        if( !exception ) {
-            fail( "Expected exception not caught.");
+        if (!exception)
+        {
+            fail("Expected exception not caught.");
         }
 
         exception = false;
 
         args = new String[] { "-s" };
-        try {
-            CommandLine line = parser.parse( opts, args );
+        try
+        {
+            CommandLine line = parser.parse(opts, args);
         }
-        catch( ParseException exp ) {
+        catch (ParseException exp)
+        {
             exception = true;
         }
 
-        if( !exception ) {
-            fail( "Expected exception not caught.");
+        if (!exception)
+        {
+            fail("Expected exception not caught.");
         }
 
         exception = false;
@@ -328,7 +323,8 @@
         }
     }
 
-    public void test14786() throws Exception {
+    public void test14786() throws Exception
+    {
         Option o = OptionBuilder.isRequired().withDescription("test").create("test");
         Options opts = new Options();
         opts.addOption(o);
@@ -342,23 +338,25 @@
         assertTrue( line.hasOption( "test" ) );
     }
 
-    public void test15046() throws Exception {
+    public void test15046() throws Exception
+    {
         CommandLineParser parser = new PosixParser();
-        final String[] CLI_ARGS = new String[] {"-z", "c"};
-        Option option = new Option("z", "timezone", true, 
-                                   "affected option");
-        Options cliOptions = new Options();
-        cliOptions.addOption(option);
-        parser.parse(cliOptions, CLI_ARGS);
-		
+        String[] CLI_ARGS = new String[] {"-z", "c"};
+
+        Options options = new Options();
+        options.addOption(new Option("z", "timezone", true, "affected option"));
+
+        parser.parse(options, CLI_ARGS);
+        
         //now add conflicting option
-        cliOptions.addOption("c", "conflict", true, "conflict option");
-        CommandLine line = parser.parse(cliOptions, CLI_ARGS);
-        assertEquals( option.getValue(), "c" );
+        options.addOption("c", "conflict", true, "conflict option");
+        CommandLine line = parser.parse(options, CLI_ARGS);
+        assertEquals( line.getOptionValue('z'), "c" );
         assertTrue( !line.hasOption("c") );
     }
 
-    public void test15648() throws Exception {
+    public void test15648() throws Exception
+    {
         CommandLineParser parser = new PosixParser();
         final String[] args = new String[] { "-m", "\"Two Words\"" };
         Option m = OptionBuilder.hasArgs().create("m");
@@ -368,7 +366,8 @@
         assertEquals( "Two Words", line.getOptionValue( "m" ) );
     }
 
-    public void test27635() {
+    public void test27635()
+    {
         Option help = new Option("h", "help", false, "print this message");
         Option version = new Option("v", "version", false, "print version information");
         Option newRun = new Option("n", "new", false, "Create NLT cache entries only for
new items");
@@ -436,7 +435,8 @@
                 ,out.toString());
     }
     
-    public void test31148() throws ParseException {
+    public void test31148() throws ParseException
+    {
         Option multiArgOption = new Option("o","option with multiple args");
         multiArgOption.setArgs(1);
         
@@ -453,7 +453,8 @@
         assertEquals("ovalue",cl.getOptionValue('o'));
     }
     
-    public void test21215() {
+    public void test21215()
+    {
         Options options = new Options();
         HelpFormatter formatter = new HelpFormatter();
         String SEP = System.getProperty("line.separator");
@@ -470,7 +471,8 @@
                 ,out.toString());
     }
     
-    public void test19383() {
+    public void test19383()
+    {
         Options options = new Options();
         options.addOption(new Option("a","aaa",false,"aaaaaaa"));
         options.addOption(new Option(null,"bbb",false,"bbbbbbb"));



Mime
View raw message