[jira] Commented: (CLI-137) Change of behaviour 1.0 -> 1.1

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (CLI-137) Change of behaviour 1.0 -> 1.1

ASF GitHub Bot (Jira)

    [ https://issues.apache.org/jira/browse/CLI-137?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12600852#action_12600852 ]

Steve Ogden commented on CLI-137:
---------------------------------

Source code:
{code}
import org.apache.commons.cli.*;
import java.util.*;

public class TestBed
{
   private String[] args_;

   private TestBed( String[] args ) throws Exception
   {
      args_ = args;
   }

   private void cliTest() throws Exception
   {
      Options options = new Options();
      Option option = null;

      option = new Option( "a", "alpha", true, "comment" );
      option.setRequired( true );
      option.setArgs( Option.UNLIMITED_VALUES );
      options.addOption( option );

      CommandLineParser parser = new GnuParser();
      CommandLine commandLine = parser.parse( options, args_ );
     
      for ( Iterator iter = commandLine.iterator(); iter.hasNext(); )
      {
         Option opt = (Option)iter.next();
         char id = (char)opt.getId();
         String[] values = opt.getValues();
         System.out.println( "values for " + opt );

         for ( int ii = 0; ii < values.length; ii++ )
         {
            System.out.print( values[ ii ] );

            if ( ii < ( values.length - 1 ) )
            {
               System.out.print( ", " );
            }
         }
         System.out.println();
      }
   }


   public static void main( String[] args ) throws Throwable
   {
      TestBed testBed = new TestBed( args );

      testBed.cliTest();
   }
}
{code}
Running with ant:
{code}
   <target name="run" depends="jar">
      <java jar="${jar.file}"
            output="${log.file}"
            fork="true"-->
      <java classname="${main.class}"
            fork="true">
         <jvmarg value="-showversion"/>
         <arg value="-a 1"/>
         <arg value="-a 2"/>
         <arg value="-a 3"/>
         <arg value="-a 4"/>
         <arg value="-b x"/>
         <arg value="output/output.bin"/>
         <classpath>
            <pathelement location="${classes.dir}"/>
            <fileset dir="${cots.dir}">
               <include name="*.jar"/>
            </fileset>
         </classpath>
      </java>
   </target>
{code}
Produces this output:
{code}
===============================================================================
1.0:
===============================================================================
run:
     [java] java version "1.5.0_15"
     [java] Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
     [java] Java HotSpot(TM) Server VM (build 1.5.0_15-b04, mixed mode)

     [java] values for [ option: a alpha  :: comment ]
     [java]  1
     [java] values for [ option: a alpha  :: comment ]
     [java]  4, -b x, output/output.bin
     [java] values for [ option: a alpha  :: comment ]
     [java]  3
     [java] values for [ option: a alpha  :: comment ]
     [java]  2

===============================================================================
1.1
===============================================================================
run:
     [java] java version "1.5.0_15"
     [java] Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
     [java] Java HotSpot(TM) Server VM (build 1.5.0_15-b04, mixed mode)

     [java] values for [ option: a alpha +ARG :: comment ]
     [java]  1,  2,  3,  4, -b x, output/output.bin
{code}
So it seems we can't have multiple instances of equivalent options when iterating.  The opt values get grouped into a single option instance.

> Change of behaviour 1.0 -> 1.1
> ------------------------------
>
>                 Key: CLI-137
>                 URL: https://issues.apache.org/jira/browse/CLI-137
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: CLI-1.x
>    Affects Versions: 1.1
>         Environment: Ubuntu 7.04 Feisty Fawn (JDK 1.6.0) + Commons CLI 1.0 and 1.1
>            Reporter: Russel Winder
>            Priority: Blocker
>             Fix For: 1.2
>
>
> The code:
> {code}
> import org.apache.commons.cli.*;
> public class Trial {
>   private void execute (String[] commandLine) throws ParseException {
>     Options options = new Options();
>     options.addOption ( OptionBuilder.withLongOpt("flob").hasArg().create('F') );
>     CommandLine line = new GnuParser().parse(options, commandLine);
>     String[] results = line.getOptionValues('F');
>     if ( results != null ) {
>       for ( String s : results ) {
>         System.out.println( "-F " + s );
>       }
>     }
>     results = line.getOptionValues("flob") ;
>     if ( results != null ) {
>       for ( String s : results ) {
>         System.out.println( "--blah " + s );
>       }
>     }
>     String[] theRest = line.getArgs() ;
>     for ( String s : theRest ) {
>       System.out.print( s + " " );
>     }
>     System.out.println();
>   }
>   public static void main (String[] args) throws ParseException {
>     Trial trial = new Trial() ;
>     trial.execute ( new String[] { "-F1" , "-F3" , "-Fbla" , "-F 76" , "--flob" , "54" } ) ;
>   }
> }
> {code}
> when compiled and executed under 1.0 produces:
> trial:
>      [java] -F 1
>      [java] -F 3
>      [java] -F bla
>      [java] -F  76
>      [java] -F 54
>      [java]
> However, when compiled and executed under 1.1 produces:
> trial:
>      [java] -F 1
>      [java] --blah 1
>      [java] 3 bla  76 54

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.