You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ro...@apache.org on 2003/11/03 22:31:34 UTC
cvs commit: jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2 CommandLine.java
roxspring 2003/11/03 13:31:34
Modified: cli/src/java/org/apache/commons/cli2 CommandLine.java
Added: cli/src/test/org/apache/commons/cli2
CommandLineDefaultsTest.java
Log:
CommandLine can now delegate to other CommandLine instances to get defaults for options, arguments and switches.
Revision Changes Path
1.1 jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/CommandLineDefaultsTest.java
Index: CommandLineDefaultsTest.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/CommandLineDefaultsTest.java,v 1.1 2003/11/03 21:31:33 roxspring Exp $
* $Revision: 1.1 $
* $Date: 2003/11/03 21:31:33 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 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 GroupImpl.
*
* 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.cli2;
import java.util.ArrayList;
import junit.framework.TestCase;
/**
* @author Rob Oxspring
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class CommandLineDefaultsTest extends TestCase
{
private CommandLine commandLine(final Option option)
{
return new CommandLine(option, new ArrayList());
}
private void parsed(
final CommandLine commandLine,
final Option option,
final Boolean bool,
final Object value)
{
if (option != null) {
commandLine.addOption(option);
if (bool != null) {
commandLine.addSwitch(option, bool.booleanValue());
}
if (value != null) {
commandLine.addValue(option, value);
}
}
}
private void alternate(
final CommandLine commandLine,
final Option option,
final Boolean bool,
final Object value)
{
final CommandLine defaults = commandLine(option);
parsed(defaults, option, bool, value);
commandLine.addDefaultCommandLine(defaults);
}
private Option bareOption()
{
return new SwitchBuilder()
.withName("logging")
.withArgument(new ArgumentBuilder().withName("file").create())
.create();
}
private Option defaultedOption()
{
return new SwitchBuilder()
.withName("logging")
.withArgument(new ArgumentBuilder().withName("file").withDefault("myfile").create())
.create();
}
public void testWithParsed()
{
final Option root = bareOption();
final CommandLine cl = commandLine(root);
parsed(cl, root, Boolean.TRUE, "value");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root));
assertEquals("value",cl.getValue(root));
}
public void testWithAlternate()
{
final Option root = bareOption();
final CommandLine cl = commandLine(root);
alternate(cl, root, Boolean.TRUE, "value");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root));
assertEquals("value",cl.getValue(root));
}
public void testWithMethod()
{
final Option root = bareOption();
final CommandLine cl = commandLine(root);
assertFalse(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root,Boolean.TRUE));
assertEquals("value",cl.getValue(root,"value"));
}
// fails for now as the option's default hasn't been "applied"
public void testWithOption()
{
final Option root = defaultedOption();
final CommandLine cl = commandLine(root);
assertFalse(cl.hasOption(root));
assertNull(cl.getSwitch(root));
assertEquals("myfile",cl.getValue(root));
}
public void testWithParsedAlternate()
{
final Option root = bareOption();
final CommandLine cl = commandLine(root);
parsed(cl, root, Boolean.TRUE, "parsed");
alternate(cl, root, Boolean.FALSE, "alternate");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root));
assertEquals("parsed",cl.getValue(root));
}
public void testWithParsedMethod()
{
final Option root = bareOption();
final CommandLine cl = commandLine(root);
parsed(cl, root, Boolean.TRUE, "parsed");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root));
assertEquals("parsed",cl.getValue(root,"method"));
}
public void testWithParsedOption()
{
final Option root = defaultedOption();
final CommandLine cl = commandLine(root);
parsed(cl, root, Boolean.TRUE, "parsed");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root));
assertEquals("parsed",cl.getValue(root));
}
public void testWithAlternateMethod()
{
final Option root = bareOption();
final CommandLine cl = commandLine(root);
alternate(cl, root, Boolean.FALSE, "alternate");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.FALSE,cl.getSwitch(root));
assertEquals("alternate",cl.getValue(root,"method"));
}
public void testWithAlternateOption()
{
final Option root = defaultedOption();
final CommandLine cl = commandLine(root);
alternate(cl, root, Boolean.FALSE, "alternate");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.FALSE,cl.getSwitch(root));
assertEquals("alternate",cl.getValue(root));
}
public void testWithMethodOption()
{
final Option root = defaultedOption();
final CommandLine cl = commandLine(root);
assertFalse(cl.hasOption(root));
assertEquals(null,cl.getSwitch(root));
assertEquals("method",cl.getValue(root,"method"));
}
public void testWithParsedAlternateMethod()
{
final Option root = defaultedOption();
final CommandLine cl = commandLine(root);
parsed(cl, root, Boolean.TRUE, "parsed");
alternate(cl, root, Boolean.FALSE, "alternate");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root));
assertEquals("parsed",cl.getValue(root,"method"));
}
public void testWithParsedAlternateOption()
{
final Option root = defaultedOption();
final CommandLine cl = commandLine(root);
parsed(cl, root, Boolean.TRUE, "parsed");
alternate(cl, root, Boolean.FALSE, "alternate");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root));
assertEquals("parsed",cl.getValue(root));
}
public void testWithParsedMethodOption()
{
final Option root = defaultedOption();
final CommandLine cl = commandLine(root);
parsed(cl, root, Boolean.TRUE, "parsed");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root));
assertEquals("parsed",cl.getValue(root,"method"));
}
public void testWithAlternateMethodOption()
{
final Option root = defaultedOption();
final CommandLine cl = commandLine(root);
alternate(cl, root, Boolean.FALSE, "alternate");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.FALSE,cl.getSwitch(root));
assertEquals("alternate",cl.getValue(root,"method"));
}
public void testWithParsedAlternateMethodOption()
{
final Option root = bareOption();
final CommandLine cl = commandLine(root);
parsed(cl, root, Boolean.TRUE, "parsed");
alternate(cl, root, Boolean.FALSE, "alternate");
assertTrue(cl.hasOption(root));
assertEquals(Boolean.TRUE,cl.getSwitch(root));
assertEquals("parsed",cl.getValue(root,"method"));
}
}
1.8 +143 -60 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.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- CommandLine.java 29 Oct 2003 08:34:56 -0000 1.7
+++ CommandLine.java 3 Nov 2003 21:31:33 -0000 1.8
@@ -63,6 +63,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -87,6 +88,8 @@
private final List normalised;
private final Set prefixes;
+ private CommandLine defaultCommandLine = null;
+
public CommandLine(final Option rootOption, final List arguments) {
this.prefixes = rootOption.prefixes();
this.normalised = arguments;
@@ -122,11 +125,17 @@
}
public boolean hasOption(final String trigger) {
- return nameToOption.containsKey(trigger);
+ return hasOption(getOption(trigger));
}
public boolean hasOption(final Option option) {
- return options.contains(option);
+ final boolean present = options.contains(option);
+ if(!present && defaultCommandLine!=null){
+ return defaultCommandLine.hasOption(option);
+ }
+ else{
+ return present;
+ }
}
public Option getOption(final String trigger) {
@@ -144,25 +153,38 @@
public List getValues(final Option option) {
return getValues(option, null);
}
-
+
+ //TODO Document the order of values and defaults
public List getValues(final Option option, final List defaultValues) {
- final List valueList = (List) values.get(option);
- if (valueList == null) {
- if (defaultValues != null) {
- return defaultValues;
- }
- else if (this.defaults != null &&
- this.defaults.containsKey(option)) {
-
- return (List) this.defaults.get(option);
- }
- else {
- return Collections.EMPTY_LIST;
- }
- }
- else {
- return valueList;
+
+ // First grab the command line values
+ List valueList = (List) values.get(option);
+
+ // Secondly try alternate CommandLines
+ if((valueList==null || valueList.isEmpty()) && defaultCommandLine!=null)
+ {
+ valueList = defaultCommandLine.getValues(option,null);
+ }
+
+ // Thirdly try the defaults supplied to the method
+ if (valueList==null || valueList.isEmpty())
+ {
+ valueList = defaultValues;
+ }
+
+ // Fourthly try the option's default values
+ if (valueList==null || valueList.isEmpty())
+ {
+ valueList = (List)this.defaults.get(option);
+ }
+
+ // Finally use an empty list
+ if (valueList == null)
+ {
+ valueList = Collections.EMPTY_LIST;
}
+
+ return valueList;
}
public Object getValue(final String trigger) {
@@ -178,60 +200,93 @@
}
public Object getValue(final Option option, final Object defaultValue) {
-
- List values = null;
-
- if (defaultValue == null) {
- values = getValues(option);
+
+ final List values;
+ if (defaultValue == null)
+ {
+ values = getValues(option,null);
}
- else {
- final List tmpDefaults = new ArrayList(1);
- tmpDefaults.add(defaultValue);
- values = getValues(option, tmpDefaults);
+ else
+ {
+ values = getValues(option,Collections.singletonList(defaultValue));
}
- if (values == null || values.isEmpty()) {
-
- if (this.defaults != null &&
- this.defaults.containsKey(option)) {
-
- values = (List) this.defaults.get(option);
- return getValue(values);
- }
- else {
- return null;
- }
- }
- else {
- return getValue(values);
- }
- }
-
- private Object getValue(final List values) {
if (values.size() > 1) {
throw new IllegalStateException("More than one value was supplied");
- }
+ }
+
+ if (values.isEmpty())
+ {
+ return null;
+ }
+
return values.get(0);
- }
-
- public Boolean getSwitch(final String trigger) {
- return getSwitch(getOption(trigger));
}
+
+ public Boolean getSwitch(final String trigger) {
+ return getSwitch(getOption(trigger),null);
+ }
+
+ public Boolean getSwitch(final String trigger, final Boolean defaultValue) {
+ return getSwitch(getOption(trigger),defaultValue);
+ }
- public Boolean getSwitch(final Option option) {
- return (Boolean) switches.get(option);
+ public Boolean getSwitch(final Option option) {
+ return getSwitch(option,null);
+ }
+
+ public Boolean getSwitch(final Option option, final Boolean defaultValue) {
+ // First grab the command line values
+ Boolean bool = (Boolean) switches.get(option);
+
+
+ // Secondly try alternate CommandLines
+ if(bool==null && defaultCommandLine!=null)
+ {
+ bool = defaultCommandLine.getSwitch(option);
+ }
+
+ // Thirdly try the defaults supplied to the method
+ if(bool==null){
+ bool = defaultValue;
+ }
+
+ // Fourthly try the option's default values
+ //????
+
+ return bool;
}
public void addProperty(final String property, final String value) {
properties.setProperty(property, value);
}
- public String getProperty(final String property) {
- return properties.getProperty(property);
+ public String getProperty(final String property)
+ {
+ final String value = properties.getProperty(property);
+ if(value==null && defaultCommandLine!=null)
+ {
+ return defaultCommandLine.getProperty(property);
+ }
+ else
+ {
+ return value;
+ }
}
- public Set getProperties() {
- return Collections.unmodifiableSet(properties.keySet());
+ public Set getProperties()
+ {
+ if(defaultCommandLine==null)
+ {
+ return Collections.unmodifiableSet(properties.keySet());
+ }
+ else
+ {
+ final Set props = new HashSet();
+ props.addAll(properties.keySet());
+ props.addAll(defaultCommandLine.getProperties());
+ return Collections.unmodifiableSet(props);
+ }
}
public List getNormalised() {
@@ -298,5 +353,33 @@
public Set getOptionTriggers(){
return Collections.unmodifiableSet(nameToOption.keySet());
+ }
+
+ public CommandLine getDefaultCommandLine()
+ {
+ return defaultCommandLine;
+ }
+
+ public void setDefaultCommandLine(final CommandLine newCommandLine)
+ {
+ this.defaultCommandLine = newCommandLine;
+ }
+
+ public void addDefaultCommandLine(final CommandLine added)
+ {
+ if(defaultCommandLine==null)
+ {
+ defaultCommandLine=added;
+ }
+ else
+ {
+ defaultCommandLine.addDefaultCommandLine(added);
+ }
+ }
+
+ public void pushDefaultCommandLine(final CommandLine pushed)
+ {
+ pushed.addDefaultCommandLine(defaultCommandLine);
+ defaultCommandLine = pushed;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org