commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jke...@apache.org
Subject cvs commit: jakarta-commons/cli/src/java/org/apache/commons/cli2/impl GroupImpl.java
Date Sat, 06 Mar 2004 18:12:59 GMT
jkeyes      2004/03/06 10:12:58

  Modified:    cli/src/java/org/apache/commons/cli2/impl Tag:
                        RESEARCH_CLI_2_ROXSPRING GroupImpl.java
  Log:
  - housekeeping: process method
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.9   +55 -35    jakarta-commons/cli/src/java/org/apache/commons/cli2/impl/Attic/GroupImpl.java
  
  Index: GroupImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli2/impl/Attic/GroupImpl.java,v
  retrieving revision 1.1.2.8
  retrieving revision 1.1.2.9
  diff -u -r1.1.2.8 -r1.1.2.9
  --- GroupImpl.java	5 Mar 2004 23:52:36 -0000	1.1.2.8
  +++ GroupImpl.java	6 Mar 2004 18:12:58 -0000	1.1.2.9
  @@ -16,13 +16,13 @@
   package org.apache.commons.cli2.impl;
   
   import java.util.ArrayList;
  +import java.util.Collection;
   import java.util.Collections;
   import java.util.Comparator;
   import java.util.HashSet;
   import java.util.Iterator;
   import java.util.List;
   import java.util.ListIterator;
  -import java.util.Map;
   import java.util.Set;
   import java.util.SortedMap;
   import java.util.TreeMap;
  @@ -162,48 +162,53 @@
           final ListIterator arguments)
           throws OptionException {
   
  -        boolean foundOption = true;
  -        while (arguments.hasNext() && foundOption) {
  -            foundOption = false;
  +        // [START process each command line token
  +        while (arguments.hasNext()) {
  +       
               final String arg = (String)arguments.next();
  -
               final Option opt = (Option)optionMap.get(arg);
  +
  +            // option found
               if (opt != null) {
                   arguments.previous();
                   opt.process(commandLine, arguments);
  -                foundOption = true;
               }
  +            // [START option NOT found
               else {
  -                final SortedMap tailMap = optionMap.tailMap(arg);
  -
  -                final Iterator i = tailMap.entrySet().iterator();
  -                while (i.hasNext()) {
  -                    final Map.Entry entry = (Map.Entry)i.next();
  -                    final Option option = (Option)entry.getValue();
  -                    if (option.canProcess(arg)) {
  -                        arguments.previous();
  -                        option.process(commandLine, arguments);
  -                        foundOption = true;
  +                // it might be an anonymous argument continue search
  +                
  +                // [START argument may be anonymous
  +                if (couldBeAnOption(arg)) {
  +                    
  +                    // narrow the search
  +                    final Collection values = optionMap.tailMap(arg).values();
  +                    
  +                    for (Iterator i = values.iterator(); i.hasNext();) {
  +                        final Option option = (Option) i.next();
  +                        
  +                        if (option.canProcess(arg)) {
  +                            arguments.previous();
  +                            option.process(commandLine, arguments);
  +                        }
                       }
  -                }
  -            }
  -        }
  -
  -        if (!foundOption) {
  -            arguments.previous();
  -        }
  -
  -        for (final Iterator i = anonymous.iterator(); i.hasNext();) {
  -            final Argument argument = (Argument)i.next();
  -            if (argument.canProcess(arguments)) {
  -                argument.process(commandLine, arguments);
  -                foundOption = true;
  -            }
  -        }
  -
  -        if (!foundOption) {
  -            throw new OptionException(this);
  -        }
  +                } // [END argument may be anonymous
  +                
  +                // [START argument is NOT anonymous
  +                else {
  +                    // move iterator back, current value not used
  +                    arguments.previous();
  +
  +                    // TODO: why do we iterate over all anonymous arguments?
  +                    // canProcess will always return true?
  +                    for (final Iterator i = anonymous.iterator(); i.hasNext();) {
  +                        final Argument argument = (Argument)i.next();
  +                        if (argument.canProcess(arguments)) {
  +                            argument.process(commandLine, arguments);
  +                        }
  +                    }
  +                } // [END argument is NOT anonymous
  +            } // [END option NOT found
  +        } // [END process each command line token
       }
   
       /*
  @@ -412,6 +417,21 @@
           return helpLines;
       }
   
  +    /**
  +     * Returns whether the specified argument may be an option
  +     * 
  +     * @param token A command line token
  +     * @return whether the token may be an option
  +     */
  +    private boolean couldBeAnOption(final String token) {
  +        for (Iterator iter = this.prefixes.iterator(); iter.hasNext();) {
  +            final String prefix = (String) iter.next();
  +            if (token.startsWith(prefix)) {
  +                return true;
  +            }
  +        }
  +        return false;
  +    }
   }
   
   class ReverseStringComparator implements Comparator {
  
  
  

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