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/cli CommandLineParser.java
Date Tue, 17 Jun 2003 22:39:03 GMT
jkeyes      2003/06/17 15:39:03

  Modified:    cli/src/test/org/apache/commons/cli Tag: cli_1_x
                        BugzillaTest.java GnuParseTest.java
               cli/src/java/org/apache/commons/cli Tag: cli_1_x
                        CommandLineParser.java
  Log:
  
- CommandLineParser WIP
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.7   +6 -6      jakarta-commons/cli/src/test/org/apache/commons/cli/Attic/BugzillaTest.java
  
  Index: BugzillaTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/Attic/BugzillaTest.java,v
  retrieving revision 1.1.2.6
  retrieving revision 1.1.2.7
  diff -u -r1.1.2.6 -r1.1.2.7
  --- BugzillaTest.java	12 Jun 2003 21:22:49 -0000	1.1.2.6
  +++ BugzillaTest.java	17 Jun 2003 22:39:03 -0000	1.1.2.7
  @@ -114,9 +114,9 @@
   
           java.util.List values = cmd.getValues("-p");
   
  -        assertEquals(values.get(0), "file1");
  -        assertEquals(values.get(1), "file2");
  -        assertEquals(values.get(2), "file3");
  +        assertEquals("file1",values.get(0));
  +        assertEquals("file2",values.get(1));
  +        assertEquals("file3",values.get(2));
   
           java.util.Iterator iter = cmd.iterator();
           while (iter.hasNext()) {
  
  
  
  1.3.2.4   +11 -16    jakarta-commons/cli/src/test/org/apache/commons/cli/GnuParseTest.java
  
  Index: GnuParseTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/GnuParseTest.java,v
  retrieving revision 1.3.2.3
  retrieving revision 1.3.2.4
  diff -u -r1.3.2.3 -r1.3.2.4
  --- GnuParseTest.java	4 Jun 2003 01:21:54 -0000	1.3.2.3
  +++ GnuParseTest.java	17 Jun 2003 22:39:03 -0000	1.3.2.4
  @@ -142,26 +142,21 @@
       }
   
       public void testExtraOption() throws Exception {
  -        String[] args =
  +        final String[] args =
               new String[] { "-a", "-d", "-b", "toast", "foo", "bar" };
   
  -        boolean caught = false;
  -
           try {
  -            CommandLine cl = _parser.parse(_options, args);
  +            final CommandLine cl = _parser.parse(_options, args);
   
               assertTrue("Confirm -a is set", cl.hasOption("-a"));
  -            assertTrue("Confirm -b is set", cl.hasOption("-b"));
  -            assertTrue("confirm arg of -b", cl.getValue("-b").equals("toast"));
  -            assertTrue(
  -                "Confirm size of extra args",
  -                cl.getAnonymousValues().size() == 3);
  -        } catch (UnknownOptionException e) {
  -            caught = true;
  -        } catch (ParseException e) {
  +            assertEquals(
  +                "wrong number of args",
  +                5,
  +                cl.getAnonymousValues().size());
  +        }
  +        catch (final ParseException e) {
               fail(e.toString());
           }
  -        assertTrue("Confirm UnrecognizedOptionException caught", caught);
       }
   
       public void testMissingArg() {
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.6.2.10  +228 -165  jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLineParser.java
  
  Index: CommandLineParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLineParser.java,v
  retrieving revision 1.6.2.9
  retrieving revision 1.6.2.10
  diff -u -r1.6.2.9 -r1.6.2.10
  --- CommandLineParser.java	12 Jun 2003 21:22:49 -0000	1.6.2.9
  +++ CommandLineParser.java	17 Jun 2003 22:39:03 -0000	1.6.2.10
  @@ -80,95 +80,172 @@
       public static final int GNU = 2;
       private int style = POSIX;
       private Properties props = new Properties();
  -        
  +
  +    private static final String LONG_PREFIX = "--";
  +    private static final String JAVA_PROPERTY = "-D";
  +    private static final String PREFIX = "-";
  +    
       public void setStyle(final int style) {
  -        this.style = style;    
  +        this.style = style;
       }
  -    
  -    public CommandLine parse(final Options options, final String[] args) 
  -    throws UnknownOptionException, MissingValueException, AlreadySelectedException, ParseException
{
  -        CommandLineCreator creator = new CommandLineImpl();
  +
  +    public CommandLine parse(final Options options, final String[] args)
  +    throws UnknownOptionException,
  +           MissingValueException,
  +           AlreadySelectedException,
  +           ParseException {
  +                
  +        final CommandLineCreator creator = new CommandLineImpl();
           final List tokens = Arrays.asList(args);
   
  -        CommandLine line = parse(options, tokens.listIterator(), creator);
  +        final CommandLine line = parse(options, tokens.listIterator(), creator);
  +
  +        // validate the command line
  +        options.isValid((CommandLine) creator);
           
  -        options.isValid((CommandLine)creator);
           return line;
       }
  -    
  -    private CommandLine parse(final Options options, final ListIterator iter, final CommandLineCreator
creator) 
  +
  +    private CommandLine parse(
  +        final Options options,
  +        final ListIterator iter,
  +        final CommandLineCreator creator)
       throws UnknownOptionException, MissingValueException {
  +
           while (iter.hasNext()) {
  -            String arg = iter.next().toString();
  -            
  -            if ("--".equals(arg)) {
  +
  +            final String cmdlineToken = iter.next().toString();
  +
  +            /*
  +             * CONSUME ALL REMAINING TOKENS
  +             */
  +            if (CommandLineParser.LONG_PREFIX.equals(cmdlineToken)) {
                   List trailing = this.processTrailingValues(iter);
  -                creator.setUnprocessed(trailing); 
  -            }
  -            else if (arg.startsWith("-D") && arg.length() > 2) {
  -                processJavaProperty(arg);
  -            }
  -            else if (options.hasArgument(arg)) {
  -                processArgument(options, iter, creator, arg, null);
  -            }
  -            else if (options.hasOption(arg)) {
  -                processOption(options, iter, creator, arg);
  -            }
  -            else if (options.inOptionGroup(arg)) {
  -                processOptionGroup(options, iter, creator, arg);
  +                creator.setUnprocessed(trailing);
               }
  +            /*
  +             * JAVA PROPERTY
  +             */
  +            else if (cmdlineToken.startsWith(CommandLineParser.JAVA_PROPERTY) 
  +                     && cmdlineToken.length() > 2) {
  +                processJavaProperty(cmdlineToken);
  +            }
  +            /*
  +             * ARGUMENT
  +             */
  +            else if (options.hasArgument(cmdlineToken)) {
  +                processArgument(options, iter, creator, cmdlineToken);
  +            }
  +            /*
  +             * OPTION
  +             */
  +            else if (options.hasOption(cmdlineToken)) {
  +                processOption(options, iter, creator, cmdlineToken);
  +            }
  +            /*
  +             * OPTION GROUP
  +             */
  +            else if (options.inOptionGroup(cmdlineToken)) {
  +                processOptionGroup(options, iter, creator, cmdlineToken);
  +            }
  +            /*
  +             * POSIX TOKEN
  +             */
               else if (
                   this.style == CommandLineParser.POSIX
  -                    && !arg.equals("-")
  -                    && arg.startsWith("-")
  -                    && !arg.startsWith("--")) {
  -                burst(options, arg, creator, iter);
  -            } 
  -            else if (arg.startsWith("-") && !arg.equals("-")) {
  -                char[] chars = arg.toCharArray();
  -                int findex = -1;
  -                for (int i = 0; i < chars.length && findex == -1; i++) {
  -                    if (!Character.isJavaIdentifierPart(chars[i])
  -                        && chars[i] != '-') {
  -                        findex = i;
  -                    }
  -                }
  -
  -                String ntoken =
  -                    (findex != -1) ? arg.substring(0, findex) : "";
  -
  -                Option option = options.getOption(ntoken);
  -                if (option == null) {
  -                    if (options.inOptionGroup(ntoken)) {
  -                        OptionGroup group = options.getOptionGroup(arg);
  -                        option = group.getOption(arg);
  -                    }
  -                }
  -
  -                if (option != null) {
  -                    if (option instanceof Argument) {
  -                        List values =  processArgument((Argument)option, iter, arg.substring(findex
+ 1));
  -                        creator.addArgument((Argument)option, values);
  -                    }
  -                }
  -                else {
  -                    throw new UnknownOptionException("unknown '" + arg + "'");
  -                }
  -            }
  +                    && !cmdlineToken.equals(CommandLineParser.PREFIX)
  +                    && cmdlineToken.startsWith(CommandLineParser.PREFIX)
  +                    && !cmdlineToken.startsWith(CommandLineParser.LONG_PREFIX))
{
  +                burst(options, cmdlineToken, creator, iter);
  +            }
  +            /*
  +             * SPECIAL LONG OPTION 
  +             * The following example is from 'ls': --block-size=<N>
  +             */
  +            else if (
  +                cmdlineToken.startsWith(CommandLineParser.LONG_PREFIX) && 
  +                cmdlineToken.length() > CommandLineParser.LONG_PREFIX.length()) {
  +                
  +                final char[] chars = cmdlineToken.toCharArray();
  +                final int nameValueSeparator = cmdlineToken.indexOf('=');
  +
  +                final String name =
  +                    (nameValueSeparator != -1) ? cmdlineToken.substring(0, nameValueSeparator)
: "";
  +                final String value =
  +                    (nameValueSeparator != -1) ? cmdlineToken.substring(nameValueSeparator
+ 1) : "";
  +                
  +                processToken(
  +                    options,
  +                    iter,
  +                    creator,
  +                    name,
  +                    value);
  +
  +            }
  +            /*
  +             * ANONYMOUS ARGUMENT VALUES
  +             */
               else if (options.hasAnonymousArgument()) {
  -                    iter.previous();
  -                    creator.addArgument(
  -                        options.getAnonymousArgument(),
  -                        processAnonymousValues(iter));
  -            }
  +                iter.previous();
  +                creator.addArgument(
  +                    options.getAnonymousArgument(),
  +                    processAnonymousValues(iter));
  +            }
  +            /*
  +             * UNKNOWN TOKEN
  +             */
               else {
  -                throw new UnknownOptionException(arg);
  +                throw new UnknownOptionException(cmdlineToken);
               }
           }
  -        
  +
  +        // set Java properties
           creator.setProperties(this.props);
   
  -        return (CommandLine)creator;
  +        // return the created CommandLine
  +        return (CommandLine) creator;
  +    }
  +
  +    private boolean processToken(
  +        final Options options,
  +        final ListIterator iter,
  +        final CommandLineCreator creator,
  +        final String optionToken,
  +        final String argumentValue)
  +        throws MissingValueException, UnknownOptionException {
  +
  +        /*
  +         * ARGUMENT
  +         */
  +        if (options.hasArgument(optionToken)) {
  +            processArgument(options, iter, creator, optionToken, argumentValue);
  +            // this is used when bursting the token, if the token
  +            // represents an Argument, the rest of the token is
  +            // the Argument value, so the calling method must break
  +            // from the loop
  +
  +            // TODO: disallow an Argument with max and min == 0
  +            return false;
  +        }
  +        /*
  +         * OPTION
  +         */
  +        else if (options.hasOption(optionToken)) {
  +            processOption(options, iter, creator, optionToken);
  +        }
  +        /*
  +         * OPTION GROUP
  +         */
  +        else if (options.inOptionGroup(optionToken)) {
  +            processOptionGroup(options, iter, creator, optionToken);
  +        }
  +        /*
  +         * UNKNOWN
  +         */
  +        else {
  +            throw new UnknownOptionException("unknown '" + optionToken + "'");
  +        }
  +        return true;
       }
   
       private void processJavaProperty(final String token) {
  @@ -179,7 +256,8 @@
           if (sep != -1) {
               key = token.substring(2, sep);
               value = token.substring(sep + 1, token.length());
  -        } else {
  +        }
  +        else {
               key = token.substring(2, token.length());
               value = "";
           }
  @@ -192,13 +270,14 @@
           final ListIterator iter,
           final CommandLineCreator creator,
           final String arg)
  -    throws MissingValueException, UnknownOptionException {
  -            
  +        throws MissingValueException, UnknownOptionException {
  +
           OptionGroup group = options.getOptionGroup(arg);
           Option option = group.getOption(arg);
           if (option instanceof Argument) {
  -            List values =  processArgument((Argument)option, iter);
  -            creator.addArgument((Argument)option, values);
  +            List values = new ArrayList();
  +            processArgument((Argument) option, iter, values);
  +            creator.addArgument((Argument) option, values);
           }
           else {
               creator.addOption(option);
  @@ -211,33 +290,13 @@
           final ListIterator iter,
           final CommandLineCreator creator,
           final String arg)
  -    throws UnknownOptionException, MissingValueException {
  +        throws UnknownOptionException, MissingValueException {
   
           Option option = options.getOption(arg);
           creator.addOption(option);
           processChildOptions(option, iter, creator);
       }
   
  -    private void processArgument(
  -        final Options options,
  -        final ListIterator iter,
  -        final CommandLineCreator creator,
  -        final String arg,
  -        final String value)
  -    throws MissingValueException, UnknownOptionException {
  -
  -        Argument argument = options.getArgument(arg);
  -        List values = null;
  -        if (value == null || value.length() == 0) {
  -            values = processArgument(argument, iter);
  -        }
  -        else {
  -            values = processArgument(argument, iter, value);
  -        }
  -        creator.addArgument(argument, values);
  -        processChildOptions(argument, iter, creator);
  -    }
  -
       //
       //
       //
  @@ -247,34 +306,28 @@
           final String token,
           final CommandLineCreator creator,
           final ListIterator iter)
  -    throws UnknownOptionException, MissingValueException {
  -               
  +        throws UnknownOptionException, MissingValueException {
  +
           int tokenLength = token.length();
   
           for (int i = 1; i < tokenLength; i++) {
               String arg = "-" + String.valueOf(token.charAt(i));
  -            
  -            if (options.hasArgument(arg)) {
  -                processArgument(options, iter, creator, arg, token.substring(i+1));
  +
  +            if (!processToken(options,
  +                iter,
  +                creator,
  +                arg,
  +                token.substring(i + 1))) {
                   break;
               }
  -            else if (options.hasOption(arg)) {
  -                processOption(options, iter, creator, arg);
  -            }
  -            else if (options.inOptionGroup(arg)) {
  -                processOptionGroup(options, iter, creator, arg);
  -            } 
  -            else {
  -                throw new UnknownOptionException("Unknown option {" + arg + "}");
  -            }
           }
       }
  -    
  +
       private void processChildOptions(
           final Option option,
           final ListIterator iter,
  -        final CommandLineCreator cmdline) 
  -    throws UnknownOptionException, MissingValueException {
  +        final CommandLineCreator cmdline)
  +        throws UnknownOptionException, MissingValueException {
   
           Options opts = option.getOptions();
           if (opts != null) {
  @@ -283,32 +336,6 @@
   
       }
   
  -    private List processArgument(
  -        final Argument argument,
  -        final ListIterator iter,
  -        final String arg)
  -    throws MissingValueException {
  -        
  -        if (arg == null || arg.startsWith("-")) {
  -            throw new MissingValueException(
  -                "Missing value for argument {" + argument.getName() + "}");
  -        }
  -        List cmdline = new ArrayList();
  -
  -        String value = stripLeadingAndTrailingQuotes(arg);
  -        if (argument.getValueSeparator() != (char) 0) {
  -            cmdline.addAll(
  -                getValues(
  -                    stripLeadingAndTrailingQuotes(value),
  -                    argument.getValueSeparator()));
  -        } else {
  -            cmdline.add(value);
  -        }
  -        processArgument(argument, iter, cmdline);
  -
  -        return cmdline;
  -    }
  -
       /**
        * Returns a collection of the individual values from the
        * specified value using the specified separator.
  @@ -333,23 +360,59 @@
           return values;
       }
   
  -    /**
  -     * 
  -     * @param argument
  -     *     the argument that needs values
  -     * 
  -     * @param iter
  -     *     the iterator over the command line arguments
  -     * 
  -     * @return the list of argument values
  -     * 
  -     * @throws MissingValueException
  -     *     if an Argument that requires a value does not have one
  -     *     specified on the command line
  -     */
  -    private List processArgument(final Argument argument, final ListIterator iter)
  +    private void processArgument(
  +        final Options options,
  +        final ListIterator iter,
  +        final CommandLineCreator creator,
  +        final String arg)
  +        throws MissingValueException, UnknownOptionException {
  +
  +        processArgument(options, iter, creator, arg, null);
  +    }
  +
  +    private void processArgument(
  +        final Options options,
  +        final ListIterator iter,
  +        final CommandLineCreator creator,
  +        final String arg,
  +        final String value)
  +        throws MissingValueException, UnknownOptionException {
  +
  +        Argument argument = options.getArgument(arg);
  +        List values = new ArrayList();
  +        if (value != null && value.length() > 0) {
  +            processArgument(argument, iter, values, value);
  +        }
  +        else {
  +            processArgument(argument, iter, values);
  +        }
  +        creator.addArgument(argument, values);
  +        processChildOptions(argument, iter, creator);
  +    }
  +
  +    private void processArgument(
  +        final Argument argument,
  +        final ListIterator iter,
  +        final List values,
  +        final String arg)
           throws MissingValueException {
  -        return processArgument(argument, iter, new ArrayList());
  +
  +        if (arg == null || arg.startsWith("-")) {
  +            throw new MissingValueException(
  +                "Missing value for argument {" + argument.getName() + "}");
  +        }
  +
  +        String value = stripLeadingAndTrailingQuotes(arg);
  +        if (argument.getValueSeparator() != (char) 0) {
  +            values.addAll(
  +                getValues(
  +                    stripLeadingAndTrailingQuotes(value),
  +                    argument.getValueSeparator()));
  +        }
  +        else {
  +            values.add(value);
  +        }
  +        processArgument(argument, iter, values);
       }
   
       /**
  @@ -375,16 +438,16 @@
        *     if an Argument that requires a value does not have one
        *     specified on the command line
        */
  -    private List processArgument(
  +    private void processArgument(
           final Argument argument,
           final ListIterator iter,
           final List argList)
  -    throws MissingValueException {
  -        
  +        throws MissingValueException {
  +
           final int minimumSize = argument.getMinimumSize();
           final int maximumSize = argument.getMaximumSize();
           int processed = argList.size();
  -        
  +
           // grab all of the values
           while (processed < maximumSize && iter.hasNext()) {
               String arg = (String) iter.next();
  @@ -399,11 +462,11 @@
                   break;
               }
           }
  -        
  +
           // check that we have enough values
           if (processed < minimumSize) {
               String name = argument.getLongName();
  -            
  +
               if (name == null) {
                   // use the short version
                   name = argument.getName();
  @@ -411,7 +474,7 @@
               throw new MissingValueException(name);
           }
   
  -        return argList;
  +        //return argList;
       }
   
       /**
  
  
  

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