commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From roxspr...@apache.org
Subject cvs commit: jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2 CommandLineTest.java
Date Fri, 24 Oct 2003 00:10:32 GMT
roxspring    2003/10/23 17:10:31

  Modified:    cli/src/java/org/apache/commons/cli2 CommandLine.java
               cli/src/test/org/apache/commons/cli2 CommandLineTest.java
  Log:
  Added CommandLine.getOptionCount(..) methods + tests.
  This turns out to be pretty trivial once the order of Options is recorded.
  
  PR:24037
  
  Revision  Changes    Path
  1.4       +22 -3     jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/CommandLine.java
  
  Index: CommandLine.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/CommandLine.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CommandLine.java	23 Oct 2003 23:34:23 -0000	1.3
  +++ CommandLine.java	24 Oct 2003 00:10:31 -0000	1.4
  @@ -226,6 +226,25 @@
   
   		return buffer.toString();
   	}
  +    
  +    public int getOptionCount(final String trigger){
  +        return getOptionCount(getOption(trigger));
  +    }
  +    
  +    public int getOptionCount(final Option option){
  +        if(option==null){
  +            return 0;
  +        }
  +        
  +        int count = 0;
  +        for (Iterator i = options.iterator(); i.hasNext();) {
  +            if(option.equals(i.next())){
  +                ++count;
  +            }
  +        }
  +        
  +        return count;
  +    }
   
       public List getOptions(){
           return Collections.unmodifiableList(options);
  
  
  
  1.4       +50 -3     jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/CommandLineTest.java
  
  Index: CommandLineTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/CommandLineTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CommandLineTest.java	23 Oct 2003 23:34:23 -0000	1.3
  +++ CommandLineTest.java	24 Oct 2003 00:10:31 -0000	1.4
  @@ -283,4 +283,51 @@
           assertSame(targets,i.next());
           assertFalse(i.hasNext());
       }
  +    
  +    public void testGetOptionCount() throws OptionException{
  +        final Option help = DefaultOptionTest.buildHelpOption();
  +        final Option login = CommandTest.buildLoginCommand();
  +        final Option targets = ArgumentTest.buildTargetsArgument();
  +        final Option display = SwitchTest.buildDisplaySwitch();
  +
  +        final Group group =
  +            new GroupBuilder()
  +                .withOption(help)
  +                .withOption(login)
  +                .withOption(targets)
  +                .withOption(display)
  +                .create();
  +        
  +        final CommandLineParser parser = new CommandLineParser();
  +        parser.setGroup(group);
  +        final CommandLine cl = parser.parse(new String[]{"--help","login","rob","--help","--help","target1","target2"});
  +        
  +        assertEquals(1,cl.getOptionCount(login));
  +        assertEquals(3,cl.getOptionCount(help));
  +        assertEquals(2,cl.getOptionCount(targets));
  +        assertEquals(0,cl.getOptionCount(display));
  +    }
  +    
  +    public void testGetOptionCount_Strings() throws OptionException{
  +        final Option help = DefaultOptionTest.buildHelpOption();
  +        final Option login = CommandTest.buildLoginCommand();
  +        final Option targets = ArgumentTest.buildTargetsArgument();
  +        final Option display = SwitchTest.buildDisplaySwitch();
  +
  +        final Group group =
  +            new GroupBuilder()
  +                .withOption(help)
  +                .withOption(login)
  +                .withOption(targets)
  +                .withOption(display)
  +                .create();
  +        
  +        final CommandLineParser parser = new CommandLineParser();
  +        parser.setGroup(group);
  +        final CommandLine cl = parser.parse(new String[]{"--help","login","rob","--help","--help","target1","target2"});
  +        
  +        assertEquals(1,cl.getOptionCount("login"));
  +        assertEquals(3,cl.getOptionCount("-?"));
  +        assertEquals(0,cl.getOptionCount("+display"));
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message