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 ArgumentTest.java ConsumeRemainingOptionTest.java
Date Sat, 25 Oct 2003 15:02:05 GMT
roxspring    2003/10/25 08:02:05

  Modified:    cli/src/java/org/apache/commons/cli2 ArgumentBuilder.java
                        ArgumentImpl.java GroupImpl.java
                        CommandLineParser.java
               cli/src/test/org/apache/commons/cli2 ArgumentTest.java
  Removed:     cli/src/java/org/apache/commons/cli2
                        ConsumeRemainingOption.java
               cli/src/test/org/apache/commons/cli2
                        ConsumeRemainingOptionTest.java
  Log:
  Moving ConsumeRemaining logic to ArgumentImpl
  PR: 24038
  
  ArgumentImpl / ArgumentBuilder:
  Added consumeRemaining attribute defaulting to "--".  Changed processValues(..)  so that
when a value appears matching the consumeRemaining value, remaining values are accepted without
further processing.
  
  ArgumentTest:
  A few minor tweaks to and new tests to accommodate the changes above.
  
  CommandLineParser:
  Removed unused imports
  
  GroupImpl:
  Removed debug messages
  
  ConsumeRemainingOption + Test:
  Removed as now useless
  
  Revision  Changes    Path
  1.6       +5 -4      jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentBuilder.java
  
  Index: ArgumentBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentBuilder.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ArgumentBuilder.java	24 Oct 2003 18:49:48 -0000	1.5
  +++ ArgumentBuilder.java	25 Oct 2003 15:02:05 -0000	1.6
  @@ -132,7 +132,8 @@
                   maximum,
                   initialSeparator,
                   subsequentSeparator,
  -                validator);
  +                validator,
  +                consumeRemaining);
   
           reset();
   
  
  
  
  1.7       +42 -77    jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentImpl.java
  
  Index: ArgumentImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/ArgumentImpl.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ArgumentImpl.java	24 Oct 2003 18:49:48 -0000	1.6
  +++ ArgumentImpl.java	25 Oct 2003 15:02:05 -0000	1.7
  @@ -86,10 +86,12 @@
       private final boolean initialSplit;
       private final boolean subsequentSplit;
       private final Validator validator;
  +    private final String consumeRemaining;
   
       private static final char NUL = '\0';
       public static final char DEFAULT_INITIAL_SEPARATOR = NUL;
       public static final char DEFAULT_SUBSEQUENT_SEPARATOR = NUL;
  +    public static final String DEFAULT_CONSUME_REMAINING = "--";
   
       /**
        * Creates a new Argument instance.
  @@ -107,7 +109,8 @@
           final int maximum,
           final char initialSeparator,
           final char subsequentSeparator,
  -        final Validator validator)
  +        final Validator validator,
  +        final String consumeRemaining)
       {
   
           if (name == null)
  @@ -126,6 +129,7 @@
           this.initialSplit = initialSeparator != NUL;
           this.subsequentSplit = subsequentSeparator != NUL;
           this.validator = validator;
  +        this.consumeRemaining = consumeRemaining;
   
           if (minimum > maximum)
           {
  @@ -142,35 +146,7 @@
           final ListIterator arguments)
           throws OptionException {
   
  -        int argumentCount = commandLine.getValues(this).size();
  -        final int initialCount = argumentCount;
  -        while (arguments.hasNext() && argumentCount < maximum) {
  -
  -            final String allValues = (String) arguments.next();
  -
  -            if (commandLine.looksLikeOption(allValues)) {
  -                arguments.previous();
  -                break;
  -            }
  -
  -            final StringTokenizer values =
  -                new StringTokenizer(
  -                    allValues,
  -                    String.valueOf(subsequentSepatator));
  -
  -            while (values.hasMoreTokens() && argumentCount < maximum) {
  -                ++argumentCount;
  -                commandLine.addValue(this, values.nextToken());
  -            }
  -            
  -            if (values.hasMoreTokens()) {
  -                throw new UnexpectedValueException(this, values.nextToken());
  -            }
  -        }
  -        
  -        if (argumentCount < minimum || initialCount == argumentCount) {
  -            throw new MissingValueException(this);
  -        }
  +        processValues(commandLine, arguments, this);
       }
   
       /* (non-Javadoc)
  @@ -207,65 +183,54 @@
        */
       public void processValues(
           final CommandLine commandLine,
  -        final ListIterator args,
  +        final ListIterator arguments,
           final Option option)
           throws OptionException
       {
  +        int argumentCount = commandLine.getValues(option).size();
  +        final int initialCount = argumentCount;
  +        while (arguments.hasNext() && argumentCount < maximum) {
   
  -        //commandLine.addOption(option);
  -        int argumentCount;
  -        final List argumentValues = commandLine.getValues(option);
  -        if (argumentValues == null)
  -        {
  -            argumentCount = 0;
  -        } else
  -        {
  -            argumentCount = argumentValues.size();
  -        }
  -
  -        String argument = "";
  -        while (args.hasNext() && argumentCount < maximum)
  -        {
  -            argument = (String)args.next();
  -
  -            if (commandLine.looksLikeOption(argument))
  +            final String allValues = (String) arguments.next();
  +            
  +            if(allValues.equals(consumeRemaining))
               {
  -                args.previous();
  +                while (arguments.hasNext() && argumentCount < maximum) {
  +                    ++argumentCount;
  +                    commandLine.addValue(option, arguments.next());
  +                }
  +            }
  +            else if (commandLine.looksLikeOption(allValues)) {
  +                arguments.previous();
                   break;
               }
  +            else if(subsequentSplit)
  +            {   
  +                final StringTokenizer values =
  +                    new StringTokenizer(
  +                        allValues,
  +                        String.valueOf(subsequentSepatator));
  +                
  +                arguments.remove();
   
  -            if (subsequentSplit)
  -            {
  -                args.remove();
  -                String remaining = argument;
  -                int subsequentIndex = remaining.indexOf(subsequentSepatator);
  -
  -                while (subsequentIndex >= 0 && argumentCount < maximum)
  -                {
  +                while (values.hasMoreTokens() && argumentCount < maximum) {
                       ++argumentCount;
  -                    final String head = remaining.substring(0, subsequentIndex);
  -                    args.add(head);
  -                    commandLine.addValue(option, head);
  -                    remaining = remaining.substring(subsequentIndex + 1);
  -                    subsequentIndex = remaining.indexOf(subsequentSepatator);
  +                    final String token = values.nextToken();
  +                    commandLine.addValue(option, token);
  +                    arguments.add(token);
                   }
  -                ++argumentCount;
  -                args.add(remaining);
  -                commandLine.addValue(option, remaining);
   
  -                if (argumentCount > maximum)
  -                {
  -                    throw new UnexpectedValueException(option, remaining);
  +                if (values.hasMoreTokens()) {
  +                    throw new UnexpectedValueException(option, values.nextToken());
                   }
  -            } else
  -            {
  +            }
  +            else{
                   ++argumentCount;
  -                commandLine.addValue(option, argument);
  +                commandLine.addValue(option, allValues);
               }
           }
  -
  -        if (argumentCount < minimum)
  -        {
  +        
  +        if (argumentCount < minimum || initialCount == argumentCount) {
               throw new MissingValueException(option);
           }
       }
  
  
  
  1.4       +3 -4      jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/GroupImpl.java
  
  Index: GroupImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/GroupImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- GroupImpl.java	24 Oct 2003 18:49:48 -0000	1.3
  +++ GroupImpl.java	25 Oct 2003 15:02:05 -0000	1.4
  @@ -137,7 +137,6 @@
           while(j.hasNext())
           {
               final Option option = (Option)j.next();
  -            System.out.println("   "+option);
               if(option.canProcess(arg)){
                   return true;
               }
  
  
  
  1.4       +3 -5      jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/CommandLineParser.java
  
  Index: CommandLineParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/CommandLineParser.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CommandLineParser.java	24 Oct 2003 18:49:48 -0000	1.3
  +++ CommandLineParser.java	25 Oct 2003 15:02:05 -0000	1.4
  @@ -61,8 +61,6 @@
   package org.apache.commons.cli2;
   
   import java.io.IOException;
  -import java.io.OutputStreamWriter;
  -import java.io.PrintWriter;
   import java.util.ArrayList;
   import java.util.Arrays;
   import java.util.LinkedList;
  
  
  
  1.3       +82 -10    jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/ArgumentTest.java
  
  Index: ArgumentTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/ArgumentTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ArgumentTest.java	22 Oct 2003 16:32:14 -0000	1.2
  +++ ArgumentTest.java	25 Oct 2003 15:02:05 -0000	1.3
  @@ -84,11 +84,12 @@
   			1,
   			'\0',
   			'\0',
  -			null);
  +			null,
  +            ArgumentImpl.DEFAULT_CONSUME_REMAINING);
   	}
   
   	public static Argument buildHostArgument() {
  -		return new ArgumentImpl("host", "The host name", 2, 3, '\0', ',', null);
  +		return new ArgumentImpl("host", "The host name", 2, 3, '\0', ',', null, null);
   	}
   
   	public static Argument buildPathArgument() {
  @@ -99,7 +100,8 @@
   			Integer.MAX_VALUE,
   			'=',
   			';',
  -			null);
  +			null, 
  +		    ArgumentImpl.DEFAULT_CONSUME_REMAINING);
   	}
   
   	public static Argument buildDateLimitArgument() {
  @@ -110,7 +112,8 @@
   			1,
   			'=',
   			'\0',
  -			new DateValidator(DateValidatorTest.YYYY_MM_YY));
  +			new DateValidator(DateValidatorTest.YYYY_MM_YY), 
  +            null);
   	}
   
   	public static Argument buildTargetsArgument() {
  @@ -121,8 +124,27 @@
   			Integer.MAX_VALUE,
   			'\0',
   			',',
  -			null);
  +			null, 
  +            null);
   	}
  +    
  +    public void testNew(){
  +        try
  +        {
  +            new ArgumentImpl(
  +                "limit",
  +                "the last acceptable date",
  +                10,
  +                5,
  +                '=',
  +                '\0',
  +                new DateValidator(DateValidatorTest.YYYY_MM_YY), 
  +                null);
  +        }
  +        catch(IllegalArgumentException e){
  +            assertEquals("minimum must not exceed maximum",e.getMessage());
  +        }
  +    }
   
   	/* (non-Javadoc)
   	 * @see org.apache.commons.cli2.ArgumentTestCase#testPreProcess()
  @@ -188,7 +210,15 @@
   		final List args = list();
   		final CommandLine commandLine = commandLine(option, args);
   		final ListIterator iterator = args.listIterator();
  -		option.processValues(commandLine, iterator, option);
  +        
  +        try
  +        {
  +            option.processValues(commandLine, iterator, option);
  +        }
  +        catch(final MissingValueException mve){
  +            assertEquals(option,mve.getOption());
  +            assertEquals("Missing value(s) target [target ...]",mve.getMessage());
  +        }
   
   		assertFalse(iterator.hasNext());
   		assertFalse(commandLine.hasOption(option));
  @@ -268,7 +298,7 @@
   		final CommandLine commandLine = commandLine(option, args);
   		final ListIterator iterator = args.listIterator();
   		option.process(commandLine, iterator);
  -
  +        
   		assertFalse(iterator.hasNext());
   		assertTrue(commandLine.hasOption(option));
   		assertTrue(commandLine.hasOption("path"));
  @@ -420,4 +450,46 @@
   
   		assertFalse(i.hasNext());
   	}
  +
  +    public void testCanProcess_ConsumeRemaining() {
  +        final Option option = buildUsernameArgument();
  +
  +        assertTrue(option.canProcess("--"));
  +    }
  +
  +    public void testProcess_ConsumeRemaining() throws OptionException {
  +        final Option option = buildPathArgument();
  +        final List args = list("options", "--", "--ignored","-Dprop=val");
  +        final CommandLine commandLine = commandLine(option, args);
  +        final ListIterator iterator = args.listIterator();
  +
  +        option.process(commandLine, iterator);
  +        final List values = commandLine.getValues(option);
  +        assertTrue(values.contains("options"));
  +        assertTrue(values.contains("--ignored"));
  +        assertTrue(values.contains("-Dprop=val"));
  +        assertEquals(3, values.size());
  +        assertFalse(iterator.hasNext());
  +    }
  +
  +    public void testProcess_ConsumeNothing() throws OptionException {
  +        final Option option = buildPathArgument();
  +        final List args = list("--");
  +        final CommandLine commandLine = commandLine(option, args);
  +        final ListIterator iterator = args.listIterator();
  +        
  +        try
  +        {
  +            option.process(commandLine, iterator);
  +            fail("Missing Value!");
  +        }
  +        catch(MissingValueException mve)
  +        {
  +            assertEquals(option,mve.getOption());
  +            assertEquals("Missing value(s) path [path ...]",mve.getMessage());
  +        }
  +
  +        assertTrue(commandLine.getValues(option).isEmpty());
  +        assertFalse(iterator.hasNext());
  +    }
   }
  
  
  

---------------------------------------------------------------------
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