Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 74779 invoked from network); 19 Nov 2002 00:16:28 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 19 Nov 2002 00:16:28 -0000 Received: (qmail 24980 invoked by uid 97); 19 Nov 2002 00:17:31 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 24931 invoked by uid 97); 19 Nov 2002 00:17:29 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 24897 invoked by uid 97); 19 Nov 2002 00:17:28 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Date: 19 Nov 2002 00:16:18 -0000 Message-ID: <20021119001618.7970.qmail@icarus.apache.org> From: jkeyes@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli OptionsTest.java OptionGroupTest.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N jkeyes 2002/11/18 16:16:18 Modified: cli/src/java/org/apache/commons/cli CommandLine.java OptionGroup.java cli/src/test/org/apache/commons/cli OptionGroupTest.java Added: cli/src/test/org/apache/commons/cli OptionsTest.java Log: some bug fixes submitted by Rob, removed duff println, add new OptionsTest submitted by Rob Revision Changes Path 1.15 +1 -5 jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLine.java Index: CommandLine.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLine.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- CommandLine.java 18 Nov 2002 08:41:26 -0000 1.14 +++ CommandLine.java 19 Nov 2002 00:16:18 -0000 1.15 @@ -298,10 +298,6 @@ names.put( opt.getLongOpt(), key ); } - if( opt.getValues() != null ) { - System.out.println( opt.getKey() + "=" + opt.getValues().length ); - } - options.put( key, opt ); } @@ -321,7 +317,7 @@ * @return an array of the processed {@link Option}s. */ public Option[] getOptions( ) { - Collection processed = hashcodeMap.values(); + Collection processed = options.values(); // reinitialise array optionsArray = new Option[ processed.size() ]; 1.7 +9 -3 jakarta-commons/cli/src/java/org/apache/commons/cli/OptionGroup.java Index: OptionGroup.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionGroup.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- OptionGroup.java 24 Oct 2002 23:17:49 -0000 1.6 +++ OptionGroup.java 19 Nov 2002 00:16:18 -0000 1.7 @@ -90,7 +90,7 @@ public OptionGroup addOption(Option opt) { // key - option name // value - the option - optionMap.put( "-" + opt.getOpt(), opt ); + optionMap.put( opt.getKey(), opt ); return this; } @@ -168,8 +168,14 @@ while( iter.hasNext() ) { Option option = (Option)iter.next(); - buff.append( "-" ); - buff.append( option.getOpt() ); + if( option.getOpt() != null ) { + buff.append( "-" ); + buff.append( option.getOpt() ); + } + else { + buff.append( "--" ); + buff.append( option.getLongOpt() ); + } buff.append( " " ); buff.append( option.getDescription( ) ); 1.5 +31 -0 jakarta-commons/cli/src/test/org/apache/commons/cli/OptionGroupTest.java Index: OptionGroupTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/OptionGroupTest.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- OptionGroupTest.java 19 Sep 2002 22:59:44 -0000 1.4 +++ OptionGroupTest.java 19 Nov 2002 00:16:18 -0000 1.5 @@ -51,6 +51,14 @@ group2.addOption( chapter ); _options.addOptionGroup( group2 ); + + Option importOpt = new Option( null, "import", false, "section to process" ); + Option exportOpt = new Option( null, "export", false, "chapter to process" ); + OptionGroup group3 = new OptionGroup(); + group3.addOption( importOpt ); + group3.addOption( exportOpt ); + _options.addOptionGroup( group3 ); + _options.addOption( "r", "revision", false, "revision number" ); } @@ -233,6 +241,29 @@ assertTrue( "Confirm -s is set", cl.hasOption("s") ); assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") ); assertTrue( "Confirm NO extra args", cl.getArgList().size() == 0); + } + catch (ParseException e) + { + fail( e.toString() ); + } + } + + public void testValidLongOnlyOptions() + { + try + { + CommandLine cl = parser.parse( _options, new String[]{"--export"}); + assertTrue( "Confirm --export is set", cl.hasOption("export") ); + } + catch (ParseException e) + { + fail( e.toString() ); + } + + try + { + CommandLine cl = parser.parse( _options, new String[]{"--import"}); + assertTrue( "Confirm --import is set", cl.hasOption("import") ); } catch (ParseException e) { 1.1 jakarta-commons/cli/src/test/org/apache/commons/cli/OptionsTest.java Index: OptionsTest.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/OptionsTest.java,v 1.1 2002/11/19 00:16:18 jkeyes Exp $ * $Revision: 1.1 $ * $Date: 2002/11/19 00:16:18 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * */ package org.apache.commons.cli; import java.util.ArrayList; import java.util.Collection; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * @author Rob Oxspring roxspring@apache.org * @version $Revision: 1.1 $ */ public class OptionsTest extends TestCase { public static Test suite() { return new TestSuite ( OptionsTest.class ); } public OptionsTest( String name ) { super( name ); } public void setUp() { } public void tearDown() { } public void testHelpOptions(){ Option longOnly1 = OptionBuilder .withLongOpt("long-only1") .create(); Option longOnly2 = OptionBuilder .withLongOpt("long-only2") .create(); Option shortOnly1 = OptionBuilder .create("1"); Option shortOnly2 = OptionBuilder .create("2"); Option bothA = OptionBuilder .withLongOpt("bothA") .create("a"); Option bothB = OptionBuilder .withLongOpt("bothB") .create("b"); Options options = new Options(); options.addOption(longOnly1); options.addOption(longOnly2); options.addOption(shortOnly1); options.addOption(shortOnly2); options.addOption(bothA); options.addOption(bothB); Collection allOptions = new ArrayList(); allOptions.add(longOnly1); allOptions.add(longOnly2); allOptions.add(shortOnly1); allOptions.add(shortOnly2); allOptions.add(bothA); allOptions.add(bothB); Collection helpOptions = options.helpOptions(); assertTrue("Everything in all should be in help",helpOptions.containsAll(allOptions)); assertTrue("Everything in help should be in all",allOptions.containsAll(helpOptions)); } } -- To unsubscribe, e-mail: For additional commands, e-mail: