From 38185db716f592471cff748fdbe9a34d4e65e4bb Mon Sep 17 00:00:00 2001
From: Henri Yandell Thrown when more than one option in an option group
- * has been provided. Construct a new A simple implementation of {@link Parser}'s abstract
- * {@link Parser#flatten(Options,String[],boolean) flatten} method. Note: Represents list of arguments parsed against
- * a {@link Options} descriptor.
- *
- * It allows querying of a boolean {@link #hasOption(String opt)},
- * in addition to retrieving the {@link #getOptionValue(String opt)}
- * for options requiring arguments. Additionally, any left-over or unrecognized arguments,
- * are available for further processing. Dump state, suitable for debugging. Resets the members to their original state i.e. remove
- * all of This flatten method does so using the following rules:
- * AlreadySelectedException
- * with the specified detail message.options and stopAtNonOption
- * are not used in this flatten method.arguments String array.
- */
- protected String[] flatten(Options options, String[] arguments,
- boolean stopAtNonOption)
- {
- // just echo the arguments
- return arguments;
- }
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/CLI2Converter.java b/src/java/org/apache/commons/cli/CLI2Converter.java
deleted file mode 100644
index 517b48e2b..000000000
--- a/src/java/org/apache/commons/cli/CLI2Converter.java
+++ /dev/null
@@ -1,190 +0,0 @@
-/**
- * Copyright 2004 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ListIterator;
-import java.util.Set;
-
-import org.apache.commons.cli2.Group;
-import org.apache.commons.cli2.Option;
-import org.apache.commons.cli2.builder.ArgumentBuilder;
-import org.apache.commons.cli2.builder.DefaultOptionBuilder;
-import org.apache.commons.cli2.builder.GroupBuilder;
-import org.apache.commons.cli2.validation.InvalidArgumentException;
-import org.apache.commons.cli2.validation.Validator;
-
-/**
- * A utility class for converting data structures version 1 to
- * version 2 Option instances.
- */
-public class CLI2Converter {
-
- private CLI2Converter(){
- // prevent creation of static utility class
- }
-
- /**
- * Creates a version 2 Option instance from a version 1 Option instance.
- *
- * @param option1 the version 1 Option to convert
- * @return a version 2 Option
- */
- public static Option option(final org.apache.commons.cli.Option option1){
-
- final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
- obuilder.withRequired(option1.isRequired());
-
- final String shortName = option1.getOpt();
- if(shortName!=null && !" ".equals(shortName)){
- obuilder.withShortName(shortName);
- }
-
- final String longName = option1.getLongOpt();
- if(longName!=null){
- obuilder.withLongName(longName);
- }
- obuilder.withId(option1.getId());
-
- final String description = option1.getDescription();
- if(description!=null){
- obuilder.withDescription(description);
- }
-
- if(option1.hasArg()){
- final ArgumentBuilder abuilder = new ArgumentBuilder();
- final String argName = option1.getArgName();
- abuilder.withName(argName);
- abuilder.withMaximum(option1.getArgs());
- if(option1.hasValueSeparator()){
- abuilder.withSubsequentSeparator(option1.getValueSeparator());
- }
- if(option1.hasOptionalArg()){
- abuilder.withMinimum(0);
- }
- else{
- //TODO check what non-optional arg means
- abuilder.withMinimum(option1.getArgs());
- }
-
- final Object type = option1.getType();
- if(type!=null){
- abuilder.withValidator(new TypeHandlerValidator(type));
- }
-
- obuilder.withArgument(abuilder.create());
- }
-
- return obuilder.create();
- }
-
- /**
- * Creates a version 2 Group instance from a version 1 OptionGroup instance.
- *
- * @param optionGroup1 the version 1 OptionGroup to convert
- * @return a version 2 Group
- */
- public static Group group(final OptionGroup optionGroup1){
-
- final GroupBuilder gbuilder = new GroupBuilder();
-
- for(final Iterator i = optionGroup1.getOptions().iterator();i.hasNext();){
- final org.apache.commons.cli.Option option1 = (org.apache.commons.cli.Option)i.next();
- final Option option2 = option(option1);
- gbuilder.withOption(option2);
- }
-
- gbuilder.withMaximum(1);
-
- if(optionGroup1.isRequired()){
- gbuilder.withMinimum(1);
- }
-
- return gbuilder.create();
- }
-
- /**
- * Creates a version 2 Group instance from a version 1 Options instance.
- *
- * @param options1 the version 1 Options to convert
- * @return a version 2 Group
- */
- public static Group group(final Options options1){
-
- final GroupBuilder gbuilder = new GroupBuilder();
-
- final Set optionGroups = new HashSet();
-
- for(final Iterator i = options1.getOptionGroups().iterator();i.hasNext();){
- final OptionGroup optionGroup1 = (OptionGroup)i.next();
- Group group = group(optionGroup1);
- gbuilder.withOption(group);
- optionGroups.add(optionGroup1);
- }
-
- for(final Iterator i = options1.getOptions().iterator();i.hasNext();){
- final org.apache.commons.cli.Option option1 = (org.apache.commons.cli.Option)i.next();
- if(!optionInAGroup(option1,optionGroups)){
- final Option option2 = option(option1);
- gbuilder.withOption(option2);
- }
- }
-
- return gbuilder.create();
- }
-
- private static boolean optionInAGroup(final org.apache.commons.cli.Option option1, final Set optionGroups) {
- for (Iterator i = optionGroups.iterator(); i.hasNext();) {
- OptionGroup group = (OptionGroup) i.next();
- if(group.getOptions().contains(option1)){
- return true;
- }
- }
- return false;
- }
-}
-
-class TypeHandlerValidator implements Validator{
-
- private final Object type;
-
- /**
- * Creates a new Validator using the TypeHandler class.
- *
- * @see TypeHandler
- * @param type The required type for valid elements
- */
- public TypeHandlerValidator(final Object type){
- this.type = type;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
- */
- public void validate(final List values) throws InvalidArgumentException {
- final ListIterator i = values.listIterator();
- while(i.hasNext()){
- final String value = (String)i.next();
- final Object converted = TypeHandler.createValue(value,type);
- if(converted==null){
- throw new InvalidArgumentException("Unable to understand value: " + value);
- }
- i.set(converted);
- }
- }
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/CommandLine.java b/src/java/org/apache/commons/cli/CommandLine.java
deleted file mode 100644
index 6c1c278a4..000000000
--- a/src/java/org/apache/commons/cli/CommandLine.java
+++ /dev/null
@@ -1,319 +0,0 @@
-/**
- * Copyright 1999-2001,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Object type of this Option.
- *
- * @param opt the name of the option
- * @return the type of this Option
- */
- public Object getOptionObject(String opt)
- {
- String res = getOptionValue(opt);
-
- if (!options.containsKey(opt))
- {
- return null;
- }
-
- Object type = ((Option) options.get(opt)).getType();
-
- return (res == null) ? null : TypeHandler.createValue(res, type);
- }
-
- /**
- * Return the Object type of this Option.
- *
- * @param opt the name of the option
- * @return the type of opt
- */
- public Object getOptionObject(char opt)
- {
- return getOptionObject(String.valueOf(opt));
- }
-
- /**
- * Retrieve the argument, if any, of this option.
- *
- * @param opt the name of the option
- * @return Value of the argument if option is set, and has an argument,
- * otherwise null.
- */
- public String getOptionValue(String opt)
- {
- String[] values = getOptionValues(opt);
-
- return (values == null) ? null : values[0];
- }
-
- /**
- * Retrieve the argument, if any, of this option.
- *
- * @param opt the character name of the option
- * @return Value of the argument if option is set, and has an argument,
- * otherwise null.
- */
- public String getOptionValue(char opt)
- {
- return getOptionValue(String.valueOf(opt));
- }
-
- /**
- * Retrieves the array of values, if any, of an option.
- *
- * @param opt string name of the option
- * @return Values of the argument if option is set, and has an argument,
- * otherwise null.
- */
- public String[] getOptionValues(String opt)
- {
- opt = Util.stripLeadingHyphens(opt);
-
- String key = opt;
-
- if (names.containsKey(opt))
- {
- key = (String) names.get(opt);
- }
-
- if (options.containsKey(key))
- {
- return ((Option) options.get(key)).getValues();
- }
-
- return null;
- }
-
- /**
- * Retrieves the array of values, if any, of an option.
- *
- * @param opt character name of the option
- * @return Values of the argument if option is set, and has an argument,
- * otherwise null.
- */
- public String[] getOptionValues(char opt)
- {
- return getOptionValues(String.valueOf(opt));
- }
-
- /**
- * Retrieve the argument, if any, of an option.
- *
- * @param opt name of the option
- * @param defaultValue is the default value to be returned if the option
- * is not specified
- * @return Value of the argument if option is set, and has an argument,
- * otherwise defaultValue.
- */
- public String getOptionValue(String opt, String defaultValue)
- {
- String answer = getOptionValue(opt);
-
- return (answer != null) ? answer : defaultValue;
- }
-
- /**
- * Retrieve the argument, if any, of an option.
- *
- * @param opt character name of the option
- * @param defaultValue is the default value to be returned if the option
- * is not specified
- * @return Value of the argument if option is set, and has an argument,
- * otherwise defaultValue.
- */
- public String getOptionValue(char opt, String defaultValue)
- {
- return getOptionValue(String.valueOf(opt), defaultValue);
- }
-
- /**
- * Retrieve any left-over non-recognized options and arguments
- *
- * @return remaining items passed in but not parsed as an array
- */
- public String[] getArgs()
- {
- String[] answer = new String[args.size()];
-
- args.toArray(answer);
-
- return answer;
- }
-
- /**
- * Retrieve any left-over non-recognized options and arguments
- *
- * @return remaining items passed in but not parsed as a List.
- */
- public List getArgList()
- {
- return args;
- }
-
- /**
- * jkeyes
- * - commented out until it is implemented properly
- * Iterator over the processed {@link Option}
- * members of this {@link CommandLine}
- */
- public Iterator iterator()
- {
- return hashcodeMap.values().iterator();
- }
-
- /**
- * Returns an array of the processed {@link Option}s.
- *
- * @return an array of the processed {@link Option}s.
- */
- public Option[] getOptions()
- {
- Collection processed = options.values();
-
-
- // reinitialise array
- optionsArray = new Option[processed.size()];
-
- // return the array
- return (Option[]) processed.toArray(optionsArray);
- }
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/CommandLineParser.java b/src/java/org/apache/commons/cli/CommandLineParser.java
deleted file mode 100644
index ae59f6a15..000000000
--- a/src/java/org/apache/commons/cli/CommandLineParser.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/**
- * Copyright 1999-2001,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-import java.util.Properties;
-
-/**
- * A class that implements the CommandLineParser interface
- * can parse a String array according to the {@link Options} specified
- * and return a {@link CommandLine}.
- *
- * @author John Keyes (john at integralsource.com)
- */
-public interface CommandLineParser {
-
- /**
- * Parse the arguments according to the specified options.
- *
- * @param options the specified Options
- * @param arguments the command line arguments
- * @return the list of atomic option and value tokens
- *
- * @throws ParseException if there are any problems encountered
- * while parsing the command line tokens.
- */
- CommandLine parse(Options options, String[] arguments)
- throws ParseException;
-
- /**
- * Parse the arguments according to the specified options and
- * properties.
- *
- * @param options the specified Options
- * @param arguments the command line arguments
- * @param properties command line option name-value pairs
- * @return the list of atomic option and value tokens
- *
- * @throws ParseException if there are any problems encountered
- * while parsing the command line tokens.
- */
- CommandLine parse(Options options, String[] arguments,
- Properties properties)
- throws ParseException;
-
- /**
- * Parse the arguments according to the specified options.
- *
- * @param options the specified Options
- * @param arguments the command line arguments
- * @param stopAtNonOption specifies whether to continue parsing the
- * arguments if a non option is encountered.
- *
- * @return the list of atomic option and value tokens
- * @throws ParseException if there are any problems encountered
- * while parsing the command line tokens.
- */
- CommandLine parse(Options options, String[] arguments,
- boolean stopAtNonOption)
- throws ParseException;
-
- /**
- * Parse the arguments according to the specified options and
- * properties.
- *
- * @param options the specified Options
- * @param arguments the command line arguments
- * @param properties command line option name-value pairs
- * @param stopAtNonOption specifies whether to continue parsing the
- *
- * @return the list of atomic option and value tokens
- * @throws ParseException if there are any problems encountered
- * while parsing the command line tokens.
- */
- CommandLine parse(Options options, String[] arguments,
- Properties properties, boolean stopAtNonOption)
- throws ParseException;
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/GnuParser.java b/src/java/org/apache/commons/cli/GnuParser.java
deleted file mode 100644
index b510037b3..000000000
--- a/src/java/org/apache/commons/cli/GnuParser.java
+++ /dev/null
@@ -1,173 +0,0 @@
-/**
- * Copyright 1999-2001,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-import java.util.ArrayList;
-
-/**
- * The class GnuParser provides an implementation of the
- * {@link Parser#flatten(Options,String[],boolean) flatten} method.
- *
- * @author John Keyes (john at integralsource.com)
- * @see Parser
- * @version $Revision$
- */
-public class GnuParser extends Parser {
-
- /** holder for flattened tokens */
- private ArrayList tokens = new ArrayList();
-
- /**
- * tokens entries.
- */
- private void init()
- {
- tokens.clear();
- }
-
- /**
- *
- *
- * arguments entry AND an {@link Option}
- * does not exist for the whole argument then
- * add the first character as an option to the processed tokens
- * list e.g. "-D" and add the rest of the entry to the also.
Print the help for options with the specified
- * command line syntax. This method prints help information to
- * System.out.
Print the help for options with the specified
- * command line syntax. This method prints help information to
- * System.out.
Print the help for options with the specified
- * command line syntax. This method prints help information to
- * System.out.
Print the help for options with the specified
- * command line syntax. This method prints help information to
- * System.out.
Print the help for options with the specified
- * command line syntax. This method prints help information to
- * System.out.
Print the help for options with the specified
- * command line syntax. This method prints help information to
- * System.out.
Print the help for options with the specified
- * command line syntax.
Print the help for options with the specified
- * command line syntax.
Prints the usage statement for the specified application.
- * - * @param pw The PrintWriter to print the usage statement - * @param width The number of characters to display per line - * @param app The application name - * @param options The command line Options - * - */ - public void printUsage(PrintWriter pw, int width, String app, - Options options) - { - // initialise the string buffer - StringBuffer buff = new StringBuffer(defaultSyntaxPrefix).append(app) - .append(" "); - - // create a list for processed option groups - final Collection processedGroups = new ArrayList(); - - // temp variable - Option option; - - // iterate over the options - for (Iterator i = options.getOptions().iterator(); i.hasNext();) - { - // get the next Option - option = (Option) i.next(); - - // check if the option is part of an OptionGroup - OptionGroup group = options.getOptionGroup(option); - - // if the option is part of a group - if (group != null) - { - // and if the group has not already been processed - if (!processedGroups.contains(group)) - { - // add the group to the processed list - processedGroups.add(group); - - - // add the usage clause - appendOptionGroup(buff, group); - } - - // otherwise the option was displayed in the group - // previously so ignore it. - } - - // if the Option is not part of an OptionGroup - else - { - appendOption(buff, option, option.isRequired()); - } - - if (i.hasNext()) - { - buff.append(" "); - } - } - - - // call printWrapped - printWrapped(pw, width, buff.toString().indexOf(' ') + 1, - buff.toString()); - } - - /** - * Appends the usage clause for an OptionGroup to a StringBuffer. - * The clause is wrapped in square brackets if the group is required. - * The display of the options is handled by appendOption - * @param buff the StringBuffer to append to - * @param group the group to append - * @see #appendOption(StringBuffer,Option,boolean) - */ - private static void appendOptionGroup(final StringBuffer buff, - final OptionGroup group) - { - if (!group.isRequired()) - { - buff.append("["); - } - - // for each option in the OptionGroup - for (Iterator i = group.getOptions().iterator(); i.hasNext();) - { - // whether the option is required or not is handled at group level - appendOption(buff, (Option) i.next(), true); - - if (i.hasNext()) - { - buff.append(" | "); - } - } - - if (!group.isRequired()) - { - buff.append("]"); - } - } - - /** - * Appends the usage clause for an Option to a StringBuffer. - * - * @param buff the StringBuffer to append to - * @param option the Option to append - * @param required whether the Option is required or not - */ - private static void appendOption(final StringBuffer buff, - final Option option, - final boolean required) - { - if (!required) - { - buff.append("["); - } - - if (option.getOpt() != null) - { - buff.append("-").append(option.getOpt()); - } - else - { - buff.append("--").append(option.getLongOpt()); - } - - // if the Option has a value - if (option.hasArg() && (option.getArgName() != null)) - { - buff.append(" <").append(option.getArgName()).append(">"); - } - - // if the Option is not a required option - if (!required) - { - buff.append("]"); - } - } - - /** - *Print the cmdLineSyntax to the specified writer, using the - * specified width.
- * - * @param pw The printWriter to write the help to - * @param width The number of characters per line for the usage statement. - * @param cmdLineSyntax The usage statement. - */ - public void printUsage(PrintWriter pw, int width, String cmdLineSyntax) - { - int argPos = cmdLineSyntax.indexOf(' ') + 1; - - printWrapped(pw, width, defaultSyntaxPrefix.length() + argPos, - defaultSyntaxPrefix + cmdLineSyntax); - } - - /** - *Print the help for the specified Options to the specified writer, - * using the specified width, left padding and description padding.
- * - * @param pw The printWriter to write the help to - * @param width The number of characters to display per line - * @param options The command line Options - * @param leftPad the number of characters of padding to be prefixed - * to each line - * @param descPad the number of characters of padding to be prefixed - * to each description line - */ - public void printOptions(PrintWriter pw, int width, Options options, - int leftPad, int descPad) - { - StringBuffer sb = new StringBuffer(); - - renderOptions(sb, width, options, leftPad, descPad); - pw.println(sb.toString()); - } - - /** - *Print the specified text to the specified PrintWriter.
- * - * @param pw The printWriter to write the help to - * @param width The number of characters to display per line - * @param text The text to be written to the PrintWriter - */ - public void printWrapped(PrintWriter pw, int width, String text) - { - printWrapped(pw, width, 0, text); - } - - /** - *Print the specified text to the specified PrintWriter.
- * - * @param pw The printWriter to write the help to - * @param width The number of characters to display per line - * @param nextLineTabStop The position on the next line for the first tab. - * @param text The text to be written to the PrintWriter - */ - public void printWrapped(PrintWriter pw, int width, int nextLineTabStop, - String text) - { - StringBuffer sb = new StringBuffer(text.length()); - - renderWrappedText(sb, width, nextLineTabStop, text); - pw.println(sb.toString()); - } - - // --------------------------------------------------------------- Protected - - /** - *Render the specified Options and return the rendered Options - * in a StringBuffer.
- * - * @param sb The StringBuffer to place the rendered Options into. - * @param width The number of characters to display per line - * @param options The command line Options - * @param leftPad the number of characters of padding to be prefixed - * to each line - * @param descPad the number of characters of padding to be prefixed - * to each description line - * - * @return the StringBuffer with the rendered Options contents. - */ - protected StringBuffer renderOptions(StringBuffer sb, int width, - Options options, int leftPad, - int descPad) - { - final String lpad = createPadding(leftPad); - final String dpad = createPadding(descPad); - - // first create list containing onlyRender the specified text and return the rendered Options - * in a StringBuffer.
- * - * @param sb The StringBuffer to place the rendered text into. - * @param width The number of characters to display per line - * @param nextLineTabStop The position on the next line for the first tab. - * @param text The text to be rendered. - * - * @return the StringBuffer with the rendered Options contents. - */ - protected StringBuffer renderWrappedText(StringBuffer sb, int width, - int nextLineTabStop, String text) - { - int pos = findWrapPos(text, width, 0); - - if (pos == -1) - { - sb.append(rtrim(text)); - - return sb; - } - sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine); - - // all following lines must be padded with nextLineTabStop space - // characters - final String padding = createPadding(nextLineTabStop); - - while (true) - { - text = padding + text.substring(pos).trim(); - pos = findWrapPos(text, width, nextLineTabStop); - - if (pos == -1) - { - sb.append(text); - - return sb; - } - - sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine); - } - } - - /** - * Finds the next text wrap position afterstartPos for the
- * text in text with the column width width.
- * The wrap point is the last postion before startPos+width having a
- * whitespace character (space, \n, \r).
- *
- * @param text The text being searched for the wrap position
- * @param width width of the wrapped text
- * @param startPos position from which to start the lookup whitespace
- * character
- * @return postion on which the text must be wrapped or -1 if the wrap
- * position is at the end of the text
- */
- protected int findWrapPos(String text, int width, int startPos)
- {
- int pos = -1;
-
- // the line ends before the max wrap pos or a new line char found
- if (((pos = text.indexOf('\n', startPos)) != -1 && pos <= width)
- || ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width))
- {
- return pos+1;
- }
- else if ((startPos + width) >= text.length())
- {
- return -1;
- }
-
-
- // look for the last whitespace character before startPos+width
- pos = startPos + width;
-
- char c;
-
- while ((pos >= startPos) && ((c = text.charAt(pos)) != ' ')
- && (c != '\n') && (c != '\r'))
- {
- --pos;
- }
-
- // if we found it - just return
- if (pos > startPos)
- {
- return pos;
- }
-
- // must look for the first whitespace chearacter after startPos
- // + width
- pos = startPos + width;
-
- while ((pos <= text.length()) && ((c = text.charAt(pos)) != ' ')
- && (c != '\n') && (c != '\r'))
- {
- ++pos;
- }
-
- return (pos == text.length()) ? (-1) : pos;
- }
-
- /**
- * Return a String of padding of length len.
Remove the trailing whitespace from the specified String.
- * - * @param s The String to remove the trailing padding from. - * - * @return The String of without the trailing padding - */ - protected String rtrim(String s) - { - if ((s == null) || (s.length() == 0)) - { - return s; - } - - int pos = s.length(); - - while ((pos > 0) && Character.isWhitespace(s.charAt(pos - 1))) - { - --pos; - } - - return s.substring(0, pos); - } - - // ------------------------------------------------------ Package protected - // ---------------------------------------------------------------- Private - // ---------------------------------------------------------- Inner classes - /** - *This class implements the Comparator interface
- * for comparing Options.
Compares its two arguments for order. Returns a negative - * integer, zero, or a positive integer as the first argument - * is less than, equal to, or greater than the second.
- * - * @param o1 The first Option to be compared. - * @param o2 The second Option to be compared. - * - * @return a negative integer, zero, or a positive integer as - * the first argument is less than, equal to, or greater than the - * second. - */ - public int compare(Object o1, Object o2) - { - Option opt1 = (Option)o1; - Option opt2 = (Option)o2; - - return opt1.getKey().compareToIgnoreCase(opt2.getKey()); - } - } -} diff --git a/src/java/org/apache/commons/cli/MissingArgumentException.java b/src/java/org/apache/commons/cli/MissingArgumentException.java deleted file mode 100644 index ccdea497c..000000000 --- a/src/java/org/apache/commons/cli/MissingArgumentException.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright 1999-2001,2004 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -/** - *Thrown when an option requiring an argument - * is not provided with an argument.
- * - * @author John Keyes (john at integralsource.com) - * @see ParseException - */ -public class MissingArgumentException - extends ParseException { - - /** - *Construct a new MissingArgumentException
- * with the specified detail message.
Thrown when a required option has not been provided.
- * - * @author John Keyes ( john at integralsource.com ) - * @see ParseException - */ -public class MissingOptionException - extends ParseException { - - /** - *Construct a new MissingSelectedException
- * with the specified detail message.
Describes a single command-line option. It maintains - * information regarding the short-name of the option, the long-name, - * if any exists, a flag indicating if an argument is required for - * this option, and a self-documenting description of the option.
- * - *An Option is not created independantly, but is create through - * an instance of {@link Options}.
- *
- * @see org.apache.commons.cli.Options
- * @see org.apache.commons.cli.CommandLine
- *
- * @author bob mcwhirter (bob @ werken.com)
- * @author James Strachan
- * @version $Revision$
- */
-public class Option {
-
- /** constant that specifies the number of argument values has
- not been specified */
- public static final int UNINITIALIZED = -1;
-
- /** constant that specifies the number of argument values is infinite */
- public static final int UNLIMITED_VALUES = -2;
-
- /** opt the name of the option */
- private String opt;
-
- /** longOpt is the long representation of the option */
- private String longOpt;
-
- /** hasArg specifies whether this option has an associated argument */
- private boolean hasArg;
-
- /** argName specifies the name of the argument for this option */
- private String argName = "arg";
-
- /** description of the option */
- private String description;
-
- /** required specifies whether this option is required to be present */
- private boolean required;
-
- /** specifies whether the argument value of this Option is optional */
- private boolean optionalArg;
-
- /**
- * numberOfArgs specifies the number of argument values this option
- * can have
- */
- private int numberOfArgs = UNINITIALIZED;
-
- /** the type of this Option */
- private Object type;
-
- /** the list of argument values **/
- private ArrayList values = new ArrayList();
-
- /** the character that is the value separator */
- private char valuesep;
-
- /**
- * Creates an Option using the specified parameters.
- *
- * @param opt short representation of the option
- * @param description describes the function of the option
- *
- * @throws IllegalArgumentException if there are any non valid
- * Option characters in opt.
- */
- public Option(String opt, String description)
- throws IllegalArgumentException
- {
- this(opt, null, false, description);
- }
-
- /**
- * Creates an Option using the specified parameters.
- *
- * @param opt short representation of the option
- * @param hasArg specifies whether the Option takes an argument or not
- * @param description describes the function of the option
- *
- * @throws IllegalArgumentException if there are any non valid
- * Option characters in opt.
- */
- public Option(String opt, boolean hasArg, String description)
- throws IllegalArgumentException
- {
- this(opt, null, hasArg, description);
- }
-
- /**
- * Creates an Option using the specified parameters.
- *
- * @param opt short representation of the option
- * @param longOpt the long representation of the option
- * @param hasArg specifies whether the Option takes an argument or not
- * @param description describes the function of the option
- *
- * @throws IllegalArgumentException if there are any non valid
- * Option characters in opt.
- */
- public Option(String opt, String longOpt, boolean hasArg,
- String description)
- throws IllegalArgumentException
- {
- // ensure that the option is valid
- OptionValidator.validateOption(opt);
-
- this.opt = opt;
- this.longOpt = longOpt;
-
- // if hasArg is set then the number of arguments is 1
- if (hasArg)
- {
- this.numberOfArgs = 1;
- }
-
- this.hasArg = hasArg;
- this.description = description;
- }
-
- /**
- * Returns the id of this Option. This is only set when the
- * Option shortOpt is a single character. This is used for switch
- * statements.
- *
- * @return the id of this Option
- */
- public int getId()
- {
- return getKey().charAt(0);
- }
-
- /**
- * Returns the 'unique' Option identifier.
- *
- * @return the 'unique' Option identifier
- */
- String getKey()
- {
- // if 'opt' is null, then it is a 'long' option
- if (opt == null)
- {
- return this.longOpt;
- }
-
- return this.opt;
- }
-
- /**
- * Retrieve the name of this Option.
- *
- * It is this String which can be used with
- * {@link CommandLine#hasOption(String opt)} and
- * {@link CommandLine#getOptionValue(String opt)} to check
- * for existence and argument.
- *
- * @return The name of this option
- */
- public String getOpt()
- {
- return this.opt;
- }
-
- /**
- * Retrieve the type of this Option.
- *
- * @return The type of this option
- */
- public Object getType()
- {
- return this.type;
- }
-
- /**
- * Sets the type of this Option.
- *
- * @param type the type of this Option
- */
- public void setType(Object type)
- {
- this.type = type;
- }
-
- /**
- * Retrieve the long name of this Option.
- *
- * @return Long name of this option, or null, if there is no long name
- */
- public String getLongOpt()
- {
- return this.longOpt;
- }
-
- /**
- * Sets the long name of this Option.
- *
- * @param longOpt the long name of this Option
- */
- public void setLongOpt(String longOpt)
- {
- this.longOpt = longOpt;
- }
-
- /**
- * Sets whether this Option can have an optional argument.
- *
- * @param optionalArg specifies whether the Option can have
- * an optional argument.
- */
- public void setOptionalArg(boolean optionalArg)
- {
- this.optionalArg = optionalArg;
- }
-
- /**
- * @return whether this Option can have an optional argument
- */
- public boolean hasOptionalArg()
- {
- return this.optionalArg;
- }
-
- /**
- * Query to see if this Option has a long name
- *
- * @return boolean flag indicating existence of a long name
- */
- public boolean hasLongOpt()
- {
- return (this.longOpt != null);
- }
-
- /**
- * Query to see if this Option requires an argument
- *
- * @return boolean flag indicating if an argument is required
- */
- public boolean hasArg()
- {
- return (this.numberOfArgs > 0) || (numberOfArgs == UNLIMITED_VALUES);
- }
-
- /**
- * Retrieve the self-documenting description of this Option
- *
- * @return The string description of this option
- */
- public String getDescription()
- {
- return this.description;
- }
-
- /**
- * Sets the self-documenting description of this Option
- *
- * @param description The description of this option
- */
- public void setDescription(String description)
- {
- this.description = description;
- }
-
- /**
- * Query to see if this Option requires an argument
- *
- * @return boolean flag indicating if an argument is required
- */
- public boolean isRequired()
- {
- return this.required;
- }
-
- /**
- * Sets whether this Option is mandatory.
- *
- * @param required specifies whether this Option is mandatory
- */
- public void setRequired(boolean required)
- {
- this.required = required;
- }
-
- /**
- * Sets the display name for the argument value.
- *
- * @param argName the display name for the argument value.
- */
- public void setArgName(String argName)
- {
- this.argName = argName;
- }
-
- /**
- * Gets the display name for the argument value.
- *
- * @return the display name for the argument value.
- */
- public String getArgName()
- {
- return this.argName;
- }
-
- /**
- * Returns whether the display name for the argument value
- * has been set.
- *
- * @return if the display name for the argument value has been
- * set.
- */
- public boolean hasArgName()
- {
- return (this.argName != null && this.argName.length() > 0);
- }
-
- /**
- * Query to see if this Option can take many values.
- *
- * @return boolean flag indicating if multiple values are allowed
- */
- public boolean hasArgs()
- {
- return (this.numberOfArgs > 1)
- || (this.numberOfArgs == UNLIMITED_VALUES);
- }
-
- /**
- * Sets the number of argument values this Option can take.
- *
- * @param num the number of argument values
- */
- public void setArgs(int num)
- {
- this.numberOfArgs = num;
- }
-
- /**
- * Sets the value separator. For example if the argument value
- * was a Java property, the value separator would be '='.
- *
- * @param sep The value separator.
- */
- public void setValueSeparator(char sep)
- {
- this.valuesep = sep;
- }
-
- /**
- * Returns the value separator character.
- *
- * @return the value separator character.
- */
- public char getValueSeparator()
- {
- return this.valuesep;
- }
-
- /**
- * Return whether this Option has specified a value separator.
- *
- * @return whether this Option has specified a value separator.
- */
- public boolean hasValueSeparator()
- {
- return (this.valuesep > 0);
- }
-
- /**
- * Returns the number of argument values this Option can take.
- *
- * @return num the number of argument values
- */
- public int getArgs()
- {
- return this.numberOfArgs;
- }
-
- /**
- * Adds the specified value to this Option.
- *
- * @param value is a/the value of this Option
- */
- void addValue(String value)
- {
- switch (numberOfArgs)
- {
- case UNINITIALIZED:
- throw new RuntimeException("NO_ARGS_ALLOWED");
-
- default:
- processValue(value);
- }
- }
-
- /**
- * Processes the value. If this Option has a value separator
- * the value will have to be parsed into individual tokens. When
- * n-1 tokens have been processed and there are more value separators
- * in the value, parsing is ceased and the remaining characters are
- * added as a single token.
- *
- * @param value The String to be processed.
- *
- * @since 1.0.1
- */
- private void processValue(String value)
- {
- // this Option has a separator character
- if (hasValueSeparator())
- {
- // get the separator character
- char sep = getValueSeparator();
-
- // store the index for the value separator
- int index = value.indexOf(sep);
-
- // while there are more value separators
- while (index != -1)
- {
- // next value to be added
- if (values.size() == (numberOfArgs - 1))
- {
- break;
- }
-
-
- // store
- add(value.substring(0, index));
-
-
- // parse
- value = value.substring(index + 1);
-
-
- // get new index
- index = value.indexOf(sep);
- }
- }
-
-
- // store the actual value or the last value that has been parsed
- add(value);
- }
-
- /**
- * Add the value to this Option. If the number of arguments
- * is greater than zero and there is enough space in the list then
- * add the value. Otherwise, throw a runtime exception.
- *
- * @param value The value to be added to this Option
- *
- * @since 1.0.1
- */
- private void add(String value)
- {
- if ((numberOfArgs > 0) && (values.size() > (numberOfArgs - 1)))
- {
- throw new RuntimeException("Cannot add value, list full.");
- }
-
-
- // store value
- this.values.add(value);
- }
-
- /**
- * Returns the specified value of this Option or
- * null if there is no value.
- *
- * @return the value/first value of this Option or
- * null if there is no value.
- */
- public String getValue()
- {
- return hasNoValues() ? null : (String) this.values.get(0);
- }
-
- /**
- * Returns the specified value of this Option or
- * null if there is no value.
- *
- * @param index The index of the value to be returned.
- *
- * @return the specified value of this Option or
- * null if there is no value.
- *
- * @throws IndexOutOfBoundsException if index is less than 1
- * or greater than the number of the values for this Option.
- */
- public String getValue(int index)
- throws IndexOutOfBoundsException
- {
- return hasNoValues() ? null : (String) this.values.get(index);
- }
-
- /**
- * Returns the value/first value of this Option or the
- * defaultValue if there is no value.
- *
- * @param defaultValue The value to be returned if ther
- * is no value.
- *
- * @return the value/first value of this Option or the
- * defaultValue if there are no values.
- */
- public String getValue(String defaultValue)
- {
- String value = getValue();
-
- return (value != null) ? value : defaultValue;
- }
-
- /**
- * Return the values of this Option as a String array
- * or null if there are no values
- *
- * @return the values of this Option as a String array
- * or null if there are no values
- */
- public String[] getValues()
- {
- return hasNoValues()
- ? null : (String[]) this.values.toArray(new String[] { });
- }
-
- /**
- * @return the values of this Option as a List
- * or null if there are no values
- */
- public java.util.List getValuesList()
- {
- return this.values;
- }
-
- /**
- * Dump state, suitable for debugging.
- *
- * @return Stringified form of this object
- */
- public String toString()
- {
- StringBuffer buf = new StringBuffer().append("[ option: ");
-
- buf.append(this.opt);
-
- if (this.longOpt != null)
- {
- buf.append(" ").append(this.longOpt);
- }
-
- buf.append(" ");
-
- if (hasArg)
- {
- buf.append("+ARG");
- }
-
- buf.append(" :: ").append(this.description);
-
- if (this.type != null)
- {
- buf.append(" :: ").append(this.type);
- }
-
- buf.append(" ]");
-
- return buf.toString();
- }
-
- /**
- * Returns whether this Option has any values.
- *
- * @return whether this Option has any values.
- */
- private boolean hasNoValues()
- {
- return this.values.size() == 0;
- }
-}
diff --git a/src/java/org/apache/commons/cli/OptionBuilder.java b/src/java/org/apache/commons/cli/OptionBuilder.java
deleted file mode 100644
index e1ac1afc6..000000000
--- a/src/java/org/apache/commons/cli/OptionBuilder.java
+++ /dev/null
@@ -1,372 +0,0 @@
-/**
- * Copyright 1999-2001,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-/**
- *
OptionBuilder allows the user to create Options using descriptive - * methods.
- *Details on the Builder pattern can be found at - * - * http://c2.com/cgi-bin/wiki?BuilderPattern.
- * - * @author John Keyes (john at integralsource.com) - * @since 1.0 - */ -public class OptionBuilder { - - /** long option */ - private static String longopt; - - /** option description */ - private static String description; - - /** argument name */ - private static String argName; - - /** is required? */ - private static boolean required; - - /** the number of arguments */ - private static int numberOfArgs = Option.UNINITIALIZED; - - /** option type */ - private static Object type; - - /** option can have an optional argument value */ - private static boolean optionalArg; - - /** value separator for argument value */ - private static char valuesep; - - /** option builder instance */ - private static OptionBuilder instance = new OptionBuilder(); - - /** - * private constructor to prevent instances being created - */ - private OptionBuilder() - { - // hide the constructor - } - - /** - * Resets the member variables to their default values. - */ - private static void reset() - { - description = null; - argName = "arg"; - longopt = null; - type = null; - required = false; - numberOfArgs = Option.UNINITIALIZED; - - - // PMM 9/6/02 - these were missing - optionalArg = false; - valuesep = (char) 0; - } - - /** - * The next Option created will have the following long option value. - * - * @param newLongopt the long option value - * @return the OptionBuilder instance - */ - public static OptionBuilder withLongOpt(String newLongopt) - { - OptionBuilder.longopt = newLongopt; - - return instance; - } - - /** - * The next Option created will require an argument value. - * - * @return the OptionBuilder instance - */ - public static OptionBuilder hasArg() - { - OptionBuilder.numberOfArgs = 1; - - return instance; - } - - /** - * The next Option created will require an argument value if - *hasArg is true.
- *
- * @param hasArg if true then the Option has an argument value
- * @return the OptionBuilder instance
- */
- public static OptionBuilder hasArg(boolean hasArg)
- {
- OptionBuilder.numberOfArgs = (hasArg == true) ? 1 : Option.UNINITIALIZED;
-
- return instance;
- }
-
- /**
- * The next Option created will have the specified argument value
- * name.
- *
- * @param name the name for the argument value
- * @return the OptionBuilder instance
- */
- public static OptionBuilder withArgName(String name)
- {
- OptionBuilder.argName = name;
-
- return instance;
- }
-
- /**
- * The next Option created will be required.
- *
- * @return the OptionBuilder instance
- */
- public static OptionBuilder isRequired()
- {
- OptionBuilder.required = true;
-
- return instance;
- }
-
- /**
- * The next Option created uses sep as a means to
- * separate argument values.
- *
- * Example:
- *
- * Option opt = OptionBuilder.withValueSeparator(':')
- * .create('D');
- *
- * CommandLine line = parser.parse(args);
- * String propertyName = opt.getValue(0);
- * String propertyValue = opt.getValue(1);
- *
- *
- * @param sep The value separator to be used for the argument values.
- *
- * @return the OptionBuilder instance
- */
- public static OptionBuilder withValueSeparator(char sep)
- {
- OptionBuilder.valuesep = sep;
-
- return instance;
- }
-
- /**
- * The next Option created uses '=' as a means to
- * separate argument values.
- *
- * Example:
- *
- * Option opt = OptionBuilder.withValueSeparator()
- * .create('D');
- *
- * CommandLine line = parser.parse(args);
- * String propertyName = opt.getValue(0);
- * String propertyValue = opt.getValue(1);
- *
- *
- * @return the OptionBuilder instance
- */
- public static OptionBuilder withValueSeparator()
- {
- OptionBuilder.valuesep = '=';
-
- return instance;
- }
-
- /**
- * The next Option created will be required if required
- * is true.
- *
- * @param newRequired if true then the Option is required
- * @return the OptionBuilder instance
- */
- public static OptionBuilder isRequired(boolean newRequired)
- {
- OptionBuilder.required = newRequired;
-
- return instance;
- }
-
- /**
- * The next Option created can have unlimited argument values.
- *
- * @return the OptionBuilder instance
- */
- public static OptionBuilder hasArgs()
- {
- OptionBuilder.numberOfArgs = Option.UNLIMITED_VALUES;
-
- return instance;
- }
-
- /**
- * The next Option created can have num
- * argument values.
- *
- * @param num the number of args that the option can have
- * @return the OptionBuilder instance
- */
- public static OptionBuilder hasArgs(int num)
- {
- OptionBuilder.numberOfArgs = num;
-
- return instance;
- }
-
- /**
- * The next Option can have an optional argument.
- *
- * @return the OptionBuilder instance
- */
- public static OptionBuilder hasOptionalArg()
- {
- OptionBuilder.numberOfArgs = 1;
- OptionBuilder.optionalArg = true;
-
- return instance;
- }
-
- /**
- * The next Option can have an unlimited number of
- * optional arguments.
- *
- * @return the OptionBuilder instance
- */
- public static OptionBuilder hasOptionalArgs()
- {
- OptionBuilder.numberOfArgs = Option.UNLIMITED_VALUES;
- OptionBuilder.optionalArg = true;
-
- return instance;
- }
-
- /**
- * The next Option can have the specified number of
- * optional arguments.
- *
- * @param numArgs - the maximum number of optional arguments
- * the next Option created can have.
- * @return the OptionBuilder instance
- */
- public static OptionBuilder hasOptionalArgs(int numArgs)
- {
- OptionBuilder.numberOfArgs = numArgs;
- OptionBuilder.optionalArg = true;
-
- return instance;
- }
-
- /**
- * The next Option created will have a value that will be an instance
- * of type.
- *
- * @param newType the type of the Options argument value
- * @return the OptionBuilder instance
- */
- public static OptionBuilder withType(Object newType)
- {
- OptionBuilder.type = newType;
-
- return instance;
- }
-
- /**
- * The next Option created will have the specified description
- *
- * @param newDescription a description of the Option's purpose
- * @return the OptionBuilder instance
- */
- public static OptionBuilder withDescription(String newDescription)
- {
- OptionBuilder.description = newDescription;
-
- return instance;
- }
-
- /**
- * Create an Option using the current settings and with
- * the specified Option char.
- *
- * @param opt the character representation of the Option
- * @return the Option instance
- * @throws IllegalArgumentException if opt is not
- * a valid character. See Option.
- */
- public static Option create(char opt)
- throws IllegalArgumentException
- {
- return create(String.valueOf(opt));
- }
-
- /**
- * Create an Option using the current settings
- *
- * @return the Option instance
- * @throws IllegalArgumentException if longOpt has
- * not been set.
- */
- public static Option create()
- throws IllegalArgumentException
- {
- if (longopt == null)
- {
- throw new IllegalArgumentException("must specify longopt");
- }
-
- return create(null);
- }
-
- /**
- * Create an Option using the current settings and with
- * the specified Option char.
- *
- * @param opt the java.lang.String representation
- * of the Option
- * @return the Option instance
- * @throws IllegalArgumentException if opt is not
- * a valid character. See Option.
- */
- public static Option create(String opt)
- throws IllegalArgumentException
- {
- // create the option
- Option option = new Option(opt, description);
-
-
- // set the option properties
- option.setLongOpt(longopt);
- option.setRequired(required);
- option.setOptionalArg(optionalArg);
- option.setArgs(numberOfArgs);
- option.setType(type);
- option.setValueSeparator(valuesep);
- option.setArgName(argName);
-
-
- // reset the OptionBuilder properties
- OptionBuilder.reset();
-
- // return the Option instance
- return option;
- }
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/OptionGroup.java b/src/java/org/apache/commons/cli/OptionGroup.java
deleted file mode 100644
index 905a10f74..000000000
--- a/src/java/org/apache/commons/cli/OptionGroup.java
+++ /dev/null
@@ -1,162 +0,0 @@
-/**
- * Copyright 1999-2001,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-
-/**
- * A group of mutually exclusive options.
- * @author John Keyes ( john at integralsource.com )
- * @version $Revision$
- */
-public class OptionGroup {
-
- /** hold the options */
- private HashMap optionMap = new HashMap();
-
- /** the name of the selected option */
- private String selected;
-
- /** specified whether this group is required */
- private boolean required;
-
- /**
- * add opt to this group
- *
- * @param opt the option to add to this group
- * @return this option group with opt added
- */
- public OptionGroup addOption(Option opt)
- {
- // key - option name
- // value - the option
- optionMap.put(opt.getKey(), opt);
-
- return this;
- }
-
- /**
- * @return the names of the options in this group as a
- * Collection
- */
- public Collection getNames()
- {
- // the key set is the collection of names
- return optionMap.keySet();
- }
-
- /**
- * @return the options in this group as a Collection
- */
- public Collection getOptions()
- {
- // the values are the collection of options
- return optionMap.values();
- }
-
- /**
- * set the selected option of this group to name.
- * @param opt the option that is selected
- * @throws AlreadySelectedException if an option from this group has
- * already been selected.
- */
- public void setSelected(Option opt)
- throws AlreadySelectedException
- {
- // if no option has already been selected or the
- // same option is being reselected then set the
- // selected member variable
- if ((this.selected == null) || this.selected.equals(opt.getOpt()))
- {
- this.selected = opt.getOpt();
- }
- else
- {
- throw new AlreadySelectedException("an option from this group has "
- + "already been selected: '"
- + selected + "'");
- }
- }
-
- /**
- * @return the selected option name
- */
- public String getSelected()
- {
- return selected;
- }
-
- /**
- * @param required specifies if this group is required
- */
- public void setRequired(boolean required)
- {
- this.required = required;
- }
-
- /**
- * Returns whether this option group is required.
- *
- * @return whether this option group is required
- */
- public boolean isRequired()
- {
- return this.required;
- }
-
- /**
- * Returns the stringified version of this OptionGroup.
- * @return the stringified representation of this group - */ - public String toString() - { - StringBuffer buff = new StringBuffer(); - - Iterator iter = getOptions().iterator(); - - buff.append("["); - - while (iter.hasNext()) - { - Option option = (Option) iter.next(); - - if (option.getOpt() != null) - { - buff.append("-"); - buff.append(option.getOpt()); - } - else - { - buff.append("--"); - buff.append(option.getLongOpt()); - } - - buff.append(" "); - buff.append(option.getDescription()); - - if (iter.hasNext()) - { - buff.append(", "); - } - } - - buff.append("]"); - - return buff.toString(); - } -} \ No newline at end of file diff --git a/src/java/org/apache/commons/cli/OptionValidator.java b/src/java/org/apache/commons/cli/OptionValidator.java deleted file mode 100644 index 5096182a7..000000000 --- a/src/java/org/apache/commons/cli/OptionValidator.java +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Copyright 1999-2001,2004 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -/** - * Validates an Option string. - * - * @author John Keyes ( john at integralsource.com ) - */ -public class OptionValidator { - - /** - *Validates whether opt is a permissable Option
- * shortOpt. The rules that specify if the opt
- * is valid are:
opt is not NULLopt that is either
- * ' '(special case), '?', '@' or a letteropt that only contains
- * letters.Returns whether the specified character is a valid Option.
- * - * @param c the option to validate - * @return true ifc is a letter, ' ', '?' or '@',
- * otherwise false.
- */
- private static boolean isValidOpt(char c)
- {
- return (isValidChar(c) || (c == ' ') || (c == '?') || c == '@');
- }
-
- /**
- * Returns whether the specified character is a valid character.
- * - * @param c the character to validate - * @return true ifc is a letter.
- */
- private static boolean isValidChar(char c)
- {
- return Character.isJavaIdentifierPart(c);
- }
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/Options.java b/src/java/org/apache/commons/cli/Options.java
deleted file mode 100644
index 815c6a3c0..000000000
--- a/src/java/org/apache/commons/cli/Options.java
+++ /dev/null
@@ -1,277 +0,0 @@
-/**
- * Copyright 1999-2001,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-/** Main entry-point into the library.
- * - *Options represents a collection of {@link Option} objects, which - * describe the possible options for a command-line.
- * - *
It may flexibly parse long and short options, with or without - * values. Additionally, it may parse only a portion of a commandline, - * allowing for flexible multi-stage parsing.
- *
- * @see org.apache.commons.cli.CommandLine
- *
- * @author bob mcwhirter (bob @ werken.com)
- * @author James Strachan
- * @version $Revision$
- */
-public class Options {
-
- /** a map of the options with the character key */
- private Map shortOpts = new HashMap();
-
- /** a map of the options with the long key */
- private Map longOpts = new HashMap();
-
- /** a map of the required options */
- private List requiredOpts = new ArrayList();
-
- /** a map of the option groups */
- private Map optionGroups = new HashMap();
-
- /** Construct a new Options descriptor
- */
- public Options()
- {
- // nothing to do
- }
-
- /**
- * Add the specified option group.
- *
- * @param group the OptionGroup that is to be added
- * @return the resulting Options instance
- */
- public Options addOptionGroup(OptionGroup group)
- {
- Iterator options = group.getOptions().iterator();
-
- if (group.isRequired())
- {
- requiredOpts.add(group);
- }
-
- while (options.hasNext())
- {
- Option option = (Option) options.next();
-
-
- // an Option cannot be required if it is in an
- // OptionGroup, either the group is required or
- // nothing is required
- option.setRequired(false);
- addOption(option);
-
- optionGroups.put(option.getKey(), group);
- }
-
- return this;
- }
-
- /**
- * Lists the OptionGroups that are members of this Options instance.
- * @return a Collection of OptionGroup instances.
- */
- Collection getOptionGroups(){
- return new HashSet(optionGroups.values());
- }
-
- /**
- * Add an option that only contains a short-name.
- * It may be specified as requiring an argument.
- *
- * @param opt Short single-character name of the option.
- * @param hasArg flag signally if an argument is required after this option
- * @param description Self-documenting description
- * @return the resulting Options instance
- */
- public Options addOption(String opt, boolean hasArg, String description)
- {
- addOption(opt, null, hasArg, description);
-
- return this;
- }
-
- /**
- * Add an option that contains a short-name and a long-name.
- * It may be specified as requiring an argument.
- *
- * @param opt Short single-character name of the option.
- * @param longOpt Long multi-character name of the option.
- * @param hasArg flag signally if an argument is required after this option
- * @param description Self-documenting description
- * @return the resulting Options instance
- */
- public Options addOption(String opt, String longOpt, boolean hasArg,
- String description)
- {
- addOption(new Option(opt, longOpt, hasArg, description));
-
- return this;
- }
-
- /**
- * Adds an option instance
- *
- * @param opt the option that is to be added
- * @return the resulting Options instance
- */
- public Options addOption(Option opt)
- {
- String key = opt.getKey();
-
- // add it to the long option list
- if (opt.hasLongOpt())
- {
- longOpts.put(opt.getLongOpt(), opt);
- }
-
- // if the option is required add it to the required list
- if (opt.isRequired() )
- {
- if( requiredOpts.contains(key) ) {
- requiredOpts.remove( requiredOpts.indexOf(key) );
- }
- requiredOpts.add(key);
- }
-
- shortOpts.put(key, opt);
-
- return this;
- }
-
- /**
- * Retrieve a read-only list of options in this set
- *
- * @return read-only Collection of {@link Option} objects in this descriptor
- */
- public Collection getOptions()
- {
- return Collections.unmodifiableCollection(helpOptions());
- }
-
- /**
- * Returns the Options for use by the HelpFormatter.
- *
- * @return the List of Options
- */
- List helpOptions()
- {
- List opts = new ArrayList(shortOpts.values());
-
- // now look through the long opts to see if there are any Long-opt
- // only options
- Iterator iter = longOpts.values().iterator();
-
- while (iter.hasNext())
- {
- Object item = iter.next();
-
- if (!opts.contains(item))
- {
- opts.add(item);
- }
- }
-
- return new ArrayList(opts);
- }
-
- /**
- * Returns the required options as a
- * java.util.Collection.
- *
- * @return Collection of required options
- */
- public List getRequiredOptions()
- {
- return requiredOpts;
- }
-
- /**
- * Retrieve the named {@link Option}
- *
- * @param opt short or long name of the {@link Option}
- * @return the option represented by opt
- */
- public Option getOption(String opt)
- {
- opt = Util.stripLeadingHyphens(opt);
-
- if (shortOpts.containsKey(opt))
- {
- return (Option) shortOpts.get(opt);
- }
-
- return (Option) longOpts.get(opt);
- }
-
- /**
- * Returns whether the named {@link Option} is a member
- * of this {@link Options}.
- *
- * @param opt short or long name of the {@link Option}
- * @return true if the named {@link Option} is a member
- * of this {@link Options}
- */
- public boolean hasOption(String opt)
- {
- opt = Util.stripLeadingHyphens(opt);
-
- return shortOpts.containsKey(opt) || longOpts.containsKey(opt);
- }
-
- /**
- * Returns the OptionGroup the opt
- * belongs to.
- * @param opt the option whose OptionGroup is being queried.
- *
- * @return the OptionGroup if opt is part
- * of an OptionGroup, otherwise return null
- */
- public OptionGroup getOptionGroup(Option opt)
- {
- return (OptionGroup) optionGroups.get(opt.getKey());
- }
-
- /**
- * Dump state, suitable for debugging.
- *
- * @return Stringified form of this object
- */
- public String toString()
- {
- StringBuffer buf = new StringBuffer();
-
- buf.append("[ Options: [ short ");
- buf.append(shortOpts.toString());
- buf.append(" ] [ long ");
- buf.append(longOpts);
- buf.append(" ]");
-
- return buf.toString();
- }
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/ParseException.java b/src/java/org/apache/commons/cli/ParseException.java
deleted file mode 100644
index 9b0653679..000000000
--- a/src/java/org/apache/commons/cli/ParseException.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/**
- * Copyright 1999-2001,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-/**
- *
Base for Exceptions thrown during parsing of a command-line.
- * - * @author bob mcwhirter (bob @ werken.com) - * @version $Revision$ - */ -public class ParseException extends Exception { - - /** - *Construct a new ParseException
- * with the specified detail message.
Parser creates {@link CommandLine}s.
Subclasses must implement this method to reduce
- * the arguments that have been passed to the parse
- * method.
Parses the specified arguments
- * based on the specifed {@link Options}.
Options
- * @param arguments the arguments
- * @return the CommandLine
- * @throws ParseException if an error occurs when parsing the
- * arguments.
- */
- public CommandLine parse(Options options, String[] arguments)
- throws ParseException
- {
- return parse(options, arguments, null, false);
- }
-
- /**
- * Parse the arguments according to the specified options and
- * properties.
- *
- * @param options the specified Options
- * @param arguments the command line arguments
- * @param properties command line option name-value pairs
- * @return the list of atomic option and value tokens
- *
- * @throws ParseException if there are any problems encountered
- * while parsing the command line tokens.
- */
- public CommandLine parse(Options options, String[] arguments,
- Properties properties)
- throws ParseException
- {
- return parse(options, arguments, properties, false);
- }
-
- /**
- * Parses the specified arguments
- * based on the specifed {@link Options}.
Options
- * @param arguments the arguments
- * @param stopAtNonOption specifies whether to stop
- * interpreting the arguments when a non option has
- * been encountered and to add them to the CommandLines
- * args list.
- *
- * @return the CommandLine
- * @throws ParseException if an error occurs when parsing the
- * arguments.
- */
- public CommandLine parse(Options options, String[] arguments,
- boolean stopAtNonOption)
- throws ParseException
- {
- return parse(options, arguments, null, stopAtNonOption);
- }
-
- /**
- * Parse the arguments according to the specified options and
- * properties.
- *
- * @param options the specified Options
- * @param arguments the command line arguments
- * @param properties command line option name-value pairs
- * @param stopAtNonOption stop parsing the arguments when the first
- * non option is encountered.
- *
- * @return the list of atomic option and value tokens
- *
- * @throws ParseException if there are any problems encountered
- * while parsing the command line tokens.
- */
- public CommandLine parse(Options options, String[] arguments,
- Properties properties, boolean stopAtNonOption)
- throws ParseException
- {
- // initialise members
- this.options = options;
- requiredOptions = options.getRequiredOptions();
- cmd = new CommandLine();
-
- boolean eatTheRest = false;
-
- if (arguments == null)
- {
- arguments = new String[0];
- }
-
- List tokenList = Arrays.asList(flatten(this.options,
- arguments,
- stopAtNonOption));
-
- ListIterator iterator = tokenList.listIterator();
-
- // process each flattened token
- while (iterator.hasNext())
- {
- String t = (String) iterator.next();
-
- // the value is the double-dash
- if ("--".equals(t))
- {
- eatTheRest = true;
- }
-
- // the value is a single dash
- else if ("-".equals(t))
- {
- if (stopAtNonOption)
- {
- eatTheRest = true;
- }
- else
- {
- cmd.addArg(t);
- }
- }
-
- // the value is an option
- else if (t.startsWith("-"))
- {
- if (stopAtNonOption && !options.hasOption(t))
- {
- eatTheRest = true;
- cmd.addArg(t);
- }
- else
- {
- processOption(t, iterator);
- }
- }
-
- // the value is an argument
- else
- {
- cmd.addArg(t);
-
- if (stopAtNonOption)
- {
- eatTheRest = true;
- }
- }
-
- // eat the remaining tokens
- if (eatTheRest)
- {
- while (iterator.hasNext())
- {
- String str = (String) iterator.next();
-
- // ensure only one double-dash is added
- if (!"--".equals(str))
- {
- cmd.addArg(str);
- }
- }
- }
- }
-
- processProperties(properties);
- checkRequiredOptions();
-
- return cmd;
- }
-
- /**
- * Sets the values of Options using the values in
- * properties.
Throws a {@link MissingOptionException} if all of the - * required options are no present.
- * - * @throws MissingOptionException if any of the required Options - * are not present. - */ - private void checkRequiredOptions() - throws MissingOptionException - { - // if there are required options that have not been - // processsed - if (requiredOptions.size() > 0) - { - Iterator iter = requiredOptions.iterator(); - StringBuffer buff = new StringBuffer(); - - // loop through the required options - while (iter.hasNext()) - { - buff.append(iter.next()); - } - - throw new MissingOptionException(buff.toString()); - } - } - - /** - *Process the argument values for the specified Option
- * opt using the values retrieved from the
- * specified iterator iter.
- *
- * @param opt The current Option
- * @param iter The iterator over the flattened command line
- * Options.
- *
- * @throws ParseException if an argument value is required
- * and it is has not been found.
- */
- public void processArgs(Option opt, ListIterator iter)
- throws ParseException
- {
- // loop until an option is found
- while (iter.hasNext())
- {
- String str = (String) iter.next();
-
- // found an Option, not an argument
- if (options.hasOption(str) && str.startsWith("-"))
- {
- iter.previous();
- break;
- }
-
- // found a value
- try
- {
- opt.addValue( Util.stripLeadingAndTrailingQuotes(str) );
- }
- catch (RuntimeException exp)
- {
- iter.previous();
- break;
- }
- }
-
- if ((opt.getValues() == null) && !opt.hasOptionalArg())
- {
- throw new MissingArgumentException("Missing argument for option:"
- + opt.getKey());
- }
- }
-
- /**
- *
Process the Option specified by arg
- * using the values retrieved from the specfied iterator
- * iter.
- *
- * @param arg The String value representing an Option
- * @param iter The iterator over the flattened command
- * line arguments.
- *
- * @throws ParseException if arg does not
- * represent an Option
- */
- private void processOption(String arg, ListIterator iter)
- throws ParseException
- {
- boolean hasOption = options.hasOption(arg);
-
- // if there is no option throw an UnrecognisedOptionException
- if (!hasOption)
- {
- throw new UnrecognizedOptionException("Unrecognized option: "
- + arg);
- }
-
- // get the option represented by arg
- final Option opt = options.getOption(arg);
-
- // if the option is a required option remove the option from
- // the requiredOptions list
- if (opt.isRequired())
- {
- requiredOptions.remove(opt.getKey());
- }
-
- // if the option is in an OptionGroup make that option the selected
- // option of the group
- if (options.getOptionGroup(opt) != null)
- {
- OptionGroup group = options.getOptionGroup(opt);
-
- if (group.isRequired())
- {
- requiredOptions.remove(group);
- }
-
- group.setSelected(opt);
- }
-
- // if the option takes an argument value
- if (opt.hasArg())
- {
- processArgs(opt, iter);
- }
-
-
- // set the option on the command line
- cmd.addOption(opt);
- }
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/PatternOptionBuilder.java b/src/java/org/apache/commons/cli/PatternOptionBuilder.java
deleted file mode 100644
index a359d6096..000000000
--- a/src/java/org/apache/commons/cli/PatternOptionBuilder.java
+++ /dev/null
@@ -1,211 +0,0 @@
-/**
- * Copyright 1999-2001,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-/**
- *
- * Allows Options to be created from a single String. - * The pattern contains various single character flags and via - * an optional punctuation character, their expected type. - *
- * - *| a | -a flag |
| b@ | -b [classname] |
| c> | -c [filename] |
| d+ | -d [classname] (creates object via empty contructor) |
| e% | -e [number] (creates Number instance) |
| f/ | -f [url] |
- * For example, the following allows command line flags of '-v -p string-value -f /dir/file'. - *
- *Options options = PatternOptionBuilder.parsePattern("vp:f/");
- *
- * - * TODO These need to break out to OptionType and also - * to be pluggable. - *
- * - * @author Henri Yandell (bayard @ generationjava.com) - * @version $Revision$ - */ -public class PatternOptionBuilder { - - /** String class */ - public static final Class STRING_VALUE = java.lang.String.class; - - /** Object class */ - public static final Class OBJECT_VALUE = java.lang.Object.class; - - /** Number class */ - public static final Class NUMBER_VALUE = java.lang.Number.class; - - /** Date class */ - public static final Class DATE_VALUE = java.util.Date.class; - - /** Class class */ - public static final Class CLASS_VALUE = java.lang.Class.class; - - /// can we do this one?? - // is meant to check that the file exists, else it errors. - // ie) it's for reading not writing. - - /** FileInputStream class */ - public static final Class EXISTING_FILE_VALUE = - java.io.FileInputStream.class; - - /** File class */ - public static final Class FILE_VALUE = java.io.File.class; - - /** File array class */ - public static final Class FILES_VALUE = java.io.File[].class; - - /** URL class */ - public static final Class URL_VALUE = java.net.URL.class; - - /** - *Retrieve the class that ch represents.
ch represents
- */
- public static Object getValueClass(char ch)
- {
- if (ch == '@')
- {
- return PatternOptionBuilder.OBJECT_VALUE;
- }
- else if (ch == ':')
- {
- return PatternOptionBuilder.STRING_VALUE;
- }
- else if (ch == '%')
- {
- return PatternOptionBuilder.NUMBER_VALUE;
- }
- else if (ch == '+')
- {
- return PatternOptionBuilder.CLASS_VALUE;
- }
- else if (ch == '#')
- {
- return PatternOptionBuilder.DATE_VALUE;
- }
- else if (ch == '<')
- {
- return PatternOptionBuilder.EXISTING_FILE_VALUE;
- }
- else if (ch == '>')
- {
- return PatternOptionBuilder.FILE_VALUE;
- }
- else if (ch == '*')
- {
- return PatternOptionBuilder.FILES_VALUE;
- }
- else if (ch == '/')
- {
- return PatternOptionBuilder.URL_VALUE;
- }
-
- return null;
- }
-
- /**
- * Returns whether ch is a value code, i.e.
- * whether it represents a class in a pattern.
ch is a value code, otherwise false.
- */
- public static boolean isValueCode(char ch)
- {
- if ((ch != '@') && (ch != ':') && (ch != '%') && (ch != '+')
- && (ch != '#') && (ch != '<') && (ch != '>') && (ch != '*')
- && (ch != '/') && (ch != '!'))
- {
- return false;
- }
-
- return true;
- }
-
- /**
- * Returns the {@link Options} instance represented by
- * pattern.
Resets the members to their original state i.e. remove
- * all of tokens entries, set eatTheRest
- * to false and set currentOption to null.
An implementation of {@link Parser}'s abstract - * {@link Parser#flatten(Options,String[],boolean) flatten} method.
- * - *The following are the rules used by this flatten method. - *
stopAtNonOption is true then do not
- * burst anymore of arguments entries, just add each
- * successive entry without further processing. Otherwise, ignore
- * stopAtNonOption.arguments entry is "--"
- * just add the entry to the list of processed tokensarguments entry is "-"
- * just add the entry to the list of processed tokensarguments entry is two characters
- * in length and the first character is "-" then check if this
- * is a valid {@link Option} id. If it is a valid id, then add the
- * entry to the list of processed tokens and set the current {@link Option}
- * member. If it is not a valid id and stopAtNonOption
- * is true, then the remaining entries are copied to the list of
- * processed tokens. Otherwise, the current entry is ignored.arguments entry is more than two
- * characters in length and the first character is "-" then
- * we need to burst the entry to determine its constituents. For more
- * information on the bursting algorithm see
- * {@link PosixParser#burstToken(String, boolean) burstToken}.arguments entry is not handled
- * by any of the previous rules, then the entry is added to the list
- * of processed tokens.arguments String array.
- */
- protected String[] flatten(Options options, String[] arguments,
- boolean stopAtNonOption)
- {
- init();
- this.options = options;
-
- // an iterator for the command line tokens
- Iterator iter = Arrays.asList(arguments).iterator();
- String token = null;
-
- // process each command line token
- while (iter.hasNext())
- {
- // get the next command line token
- token = (String) iter.next();
-
- // handle SPECIAL TOKEN
- if (token.startsWith("--"))
- {
- if (token.indexOf('=') != -1)
- {
- tokens.add(token.substring(0, token.indexOf('=')));
- tokens.add(token.substring(token.indexOf('=') + 1,
- token.length()));
- }
- else
- {
- tokens.add(token);
- }
- }
-
- // single hyphen
- else if ("-".equals(token))
- {
- processSingleHyphen(token);
- }
- else if (token.startsWith("-"))
- {
- int tokenLength = token.length();
-
- if (tokenLength == 2)
- {
- processOptionToken(token, stopAtNonOption);
- }
- else if (options.hasOption(token)) {
- tokens.add(token);
- }
- // requires bursting
- else
- {
- burstToken(token, stopAtNonOption);
- }
- }
- else
- {
- if (stopAtNonOption)
- {
- process(token);
- }
- else
- {
- tokens.add(token);
- }
- }
-
- gobble(iter);
- }
-
- return (String[]) tokens.toArray(new String[] { });
- }
-
- /**
- * Adds the remaining tokens to the processed tokens list.
- * - * @param iter An iterator over the remaining tokens - */ - private void gobble(Iterator iter) - { - if (eatTheRest) - { - while (iter.hasNext()) - { - tokens.add(iter.next()); - } - } - } - - /** - *If there is a current option and it can have an argument - * value then add the token to the processed tokens list and - * set the current option to null.
- *If there is a current option and it can have argument - * values then add the token to the processed tokens list.
- *If there is not a current option add the special token
- * "--" and the current value to the processed
- * tokens list. The add all the remaining argument
- * values to the processed tokens list.
If it is a hyphen then add the hyphen directly to - * the processed tokens list.
- * - * @param hyphen The hyphen token - */ - private void processSingleHyphen(String hyphen) - { - tokens.add(hyphen); - } - - /** - *If an {@link Option} exists for token then
- * set the current option and add the token to the processed
- * list.
If an {@link Option} does not exist and stopAtNonOption
- * is set then ignore the current token and add the remaining tokens
- * to the processed tokens list directly.
Breaks token into its constituent parts
- * using the following algorithm.
- *
stopAtNonOption IS set then add the special token
- * "--" followed by the remaining characters and also
- * the remaining tokens directly to the processed tokens list.stopAtNonOption IS NOT set then add that
- * character prepended with "-".Returns the Object of type obj
- * with the value of str.
obj initialised with
- * the value of str.
- */
- public static Object createValue(String str, Object obj)
- {
- return createValue(str, (Class) obj);
- }
-
- /**
- * Returns the Object of type clazz
- * with the value of str.
clazz initialised with
- * the value of str.
- */
- public static Object createValue(String str, Class clazz)
- {
- if (PatternOptionBuilder.STRING_VALUE == clazz)
- {
- return str;
- }
- else if (PatternOptionBuilder.OBJECT_VALUE == clazz)
- {
- return createObject(str);
- }
- else if (PatternOptionBuilder.NUMBER_VALUE == clazz)
- {
- return createNumber(str);
- }
- else if (PatternOptionBuilder.DATE_VALUE == clazz)
- {
- return createDate(str);
- }
- else if (PatternOptionBuilder.CLASS_VALUE == clazz)
- {
- return createClass(str);
- }
- else if (PatternOptionBuilder.FILE_VALUE == clazz)
- {
- return createFile(str);
- }
- else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)
- {
- return createFile(str);
- }
- else if (PatternOptionBuilder.FILES_VALUE == clazz)
- {
- return createFiles(str);
- }
- else if (PatternOptionBuilder.URL_VALUE == clazz)
- {
- return createURL(str);
- }
- else
- {
- return null;
- }
- }
-
- /**
- * Create an Object from the classname and empty constructor.
- * - * @param str the argument value - * @return the initialised object, or null if it couldn't create - * the Object. - */ - public static Object createObject(String str) - { - Class cl = null; - - try - { - cl = Class.forName(str); - } - catch (ClassNotFoundException cnfe) - { - System.err.println("Unable to find: " + str); - - return null; - } - - Object instance = null; - - try - { - instance = cl.newInstance(); - } - catch (InstantiationException cnfe) - { - System.err.println("InstantiationException; Unable to create: " - + str); - - return null; - } - catch (IllegalAccessException cnfe) - { - System.err.println("IllegalAccessException; Unable to create: " - + str); - - return null; - } - - return instance; - } - - /** - *Create a number from a String.
- * - * @param str the value - * @return the number represented bystr, if str
- * is not a number, null is returned.
- */
- public static Number createNumber(String str)
- {
- // Needs to be able to create
- try
- {
- // do searching for decimal point etc, but atm just make an Integer
- return NumberUtils.createNumber(str);
- }
- catch (NumberFormatException nfe)
- {
- System.err.println(nfe.getMessage());
-
- return null;
- }
- }
-
- /**
- * Returns the class whose name is str.
Returns the date represented by str.
str is a valid date string,
- * otherwise return null.
- */
- public static Date createDate(String str)
- {
- Date date = null;
-
- if (date == null)
- {
- System.err.println("Unable to parse: " + str);
- }
-
- return date;
- }
-
- /**
- * Returns the URL represented by str.
str is well-formed, otherwise
- * return null.
- */
- public static URL createURL(String str)
- {
- try
- {
- return new URL(str);
- }
- catch (MalformedURLException mue)
- {
- System.err.println("Unable to parse: " + str);
-
- return null;
- }
- }
-
- /**
- * Returns the File represented by str.
str.
- */
- public static File createFile(String str)
- {
- return new File(str);
- }
-
- /**
- * Returns the File[] represented by str.
str.
- */
- public static File[] createFiles(String str)
- {
- // to implement/port:
- // return FileW.findFiles(str);
- return null;
- }
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/UnrecognizedOptionException.java b/src/java/org/apache/commons/cli/UnrecognizedOptionException.java
deleted file mode 100644
index b417c1953..000000000
--- a/src/java/org/apache/commons/cli/UnrecognizedOptionException.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/**
- * Copyright 1999-2001,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.cli;
-
-/**
- * Exception thrown during parsing signalling an unrecognized - * option was seen.
- * - * @author bob mcwhiter (bob @ werken.com) - * @version $Revision$ - */ -public class UnrecognizedOptionException - extends ParseException { - - /** - *
Construct a new UnrecognizedArgumentException
- * with the specified detail message.
Remove the hyphens from the begining of str and
- * return the new String.
str.
- * E.g. if str is '"one two"', then 'one two' is returned.
- *
- * @param str The string from which the leading and trailing quotes
- * should be removed.
- *
- * @return The string without the leading and trailing quotes.
- */
- static String stripLeadingAndTrailingQuotes(String str)
- {
- if (str.startsWith("\"")) {
- str = str.substring(1, str.length());
- }
- if (str.endsWith("\"")) {
- str = str.substring(0, str.length()-1);
- }
- return str;
- }
-}
\ No newline at end of file
diff --git a/src/java/org/apache/commons/cli/overview.html b/src/java/org/apache/commons/cli/overview.html
deleted file mode 100644
index eadba1e49..000000000
--- a/src/java/org/apache/commons/cli/overview.html
+++ /dev/null
@@ -1,28 +0,0 @@
-
-
-
- Commons CLI -- version ##VERSION## (##QUALITY##)
- -The commons-cli package aides in parsing command-line arguments.
- -Allow command-line arguments to be parsed against a descriptor of - valid options (long and short), potentially with arguments.
- -command-line arguments may be of the typical String[]
- form, but also may be a java.util.List. Indexes allow
- for parsing only a portion of the command-line. Also, functionality
- for parsing the command-line in phases is built in, allowing for
- 'cvs-style' command-lines, where some global options are specified
- before a 'command' argument, and command-specific options are
- specified after the command argument:
-
-
-
-
-
-
- myApp -p <port> command -p <printer>
-
-
The homepage for the project is - jakarta commons/ - diff --git a/src/java/org/apache/commons/cli/package.html b/src/java/org/apache/commons/cli/package.html deleted file mode 100644 index 5bf206675..000000000 --- a/src/java/org/apache/commons/cli/package.html +++ /dev/null @@ -1,6 +0,0 @@ - -
- - Commons CLI 1.0 - - diff --git a/src/test/org/apache/commons/cli/ApplicationTest.java b/src/test/org/apache/commons/cli/ApplicationTest.java deleted file mode 100644 index 99013b21d..000000000 --- a/src/test/org/apache/commons/cli/ApplicationTest.java +++ /dev/null @@ -1,130 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - *- * This is a collection of tests that test real world - * applications command lines. - *
- * - *- * The following are the applications that are tested: - *
- * This is a collection of tests that test real world - * applications command lines focusing on options with - * long and short names. - *
- */ -public class LongOptionWithShort extends TestCase { - public LongOptionWithShort(String name) { - super(name); - } - - public static Test suite() { - return new TestSuite(LongOptionWithShort.class); - } - - /** - * - */ - public void testLongOptionWithShort() { - Option help = new Option("h", "help", false, "print this message"); - Option version = new Option("v", "version", false, - "print version information"); - Option newRun = new Option("n", "new", false, - "Create NLT cache entries only for new items"); - Option trackerRun = new Option("t", "tracker", false, - "Create NLT cache entries only for tracker items"); - - Option timeLimit = OptionBuilder.withLongOpt("limit").hasArg() - .withValueSeparator() - .withDescription("Set time limit for execution, in mintues") - .create("l"); - - Option age = OptionBuilder.withLongOpt("age").hasArg() - .withValueSeparator() - .withDescription("Age (in days) of cache item before being recomputed") - .create("a"); - - Option server = OptionBuilder.withLongOpt("server").hasArg() - .withValueSeparator() - .withDescription("The NLT server address") - .create("s"); - - Option numResults = OptionBuilder.withLongOpt("results").hasArg() - .withValueSeparator() - .withDescription("Number of results per item") - .create("r"); - - Option configFile = OptionBuilder.withLongOpt("file").hasArg() - .withValueSeparator() - .withDescription("Use the specified configuration file") - .create(); - - Options options = new Options(); - options.addOption(help); - options.addOption(version); - options.addOption(newRun); - options.addOption(trackerRun); - options.addOption(timeLimit); - options.addOption(age); - options.addOption(server); - options.addOption(numResults); - options.addOption(configFile); - - // create the command line parser - CommandLineParser parser = new PosixParser(); - - String[] args = new String[] { - "-v", - "-l", - "10", - "-age", - "5", - "-file", - "filename" - }; - - try { - CommandLine line = parser.parse(options, args); - assertTrue(line.hasOption("v")); - assertEquals(line.getOptionValue("l"), "10"); - assertEquals(line.getOptionValue("limit"), "10"); - assertEquals(line.getOptionValue("a"), "5"); - assertEquals(line.getOptionValue("age"), "5"); - assertEquals(line.getOptionValue("file"), "filename"); - } - catch (ParseException exp) { - fail("Unexpected exception:" + exp.getMessage()); - } - } -} diff --git a/src/test/org/apache/commons/cli/OptionBuilderTest.java b/src/test/org/apache/commons/cli/OptionBuilderTest.java deleted file mode 100644 index e1f3083f9..000000000 --- a/src/test/org/apache/commons/cli/OptionBuilderTest.java +++ /dev/null @@ -1,164 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import junit.textui.TestRunner; - -public class OptionBuilderTest extends TestCase { - - public OptionBuilderTest( String name ) { - super( name ); - } - - public static Test suite() { - return new TestSuite( OptionBuilderTest.class ); - } - - public static void main( String args[] ) { - TestRunner.run( suite() ); - } - - public void testCompleteOption( ) { - Option simple = OptionBuilder.withLongOpt( "simple option") - .hasArg( ) - .isRequired( ) - .hasArgs( ) - .withType( new Float( 10 ) ) - .withDescription( "this is a simple option" ) - .create( 's' ); - - assertEquals( "s", simple.getOpt() ); - assertEquals( "simple option", simple.getLongOpt() ); - assertEquals( "this is a simple option", simple.getDescription() ); - assertEquals( simple.getType().getClass(), Float.class ); - assertTrue( simple.hasArg() ); - assertTrue( simple.isRequired() ); - assertTrue( simple.hasArgs() ); - } - - public void testTwoCompleteOptions( ) { - Option simple = OptionBuilder.withLongOpt( "simple option") - .hasArg( ) - .isRequired( ) - .hasArgs( ) - .withType( new Float( 10 ) ) - .withDescription( "this is a simple option" ) - .create( 's' ); - - assertEquals( "s", simple.getOpt() ); - assertEquals( "simple option", simple.getLongOpt() ); - assertEquals( "this is a simple option", simple.getDescription() ); - assertEquals( simple.getType().getClass(), Float.class ); - assertTrue( simple.hasArg() ); - assertTrue( simple.isRequired() ); - assertTrue( simple.hasArgs() ); - - simple = OptionBuilder.withLongOpt( "dimple option") - .hasArg( ) - .withDescription( "this is a dimple option" ) - .create( 'd' ); - - assertEquals( "d", simple.getOpt() ); - assertEquals( "dimple option", simple.getLongOpt() ); - assertEquals( "this is a dimple option", simple.getDescription() ); - assertNull( simple.getType() ); - assertTrue( simple.hasArg() ); - assertTrue( !simple.isRequired() ); - assertTrue( !simple.hasArgs() ); - } - - public void testBaseOptionCharOpt() { - Option base = OptionBuilder.withDescription( "option description") - .create( 'o' ); - - assertEquals( "o", base.getOpt() ); - assertEquals( "option description", base.getDescription() ); - assertTrue( !base.hasArg() ); - } - - public void testBaseOptionStringOpt() { - Option base = OptionBuilder.withDescription( "option description") - .create( "o" ); - - assertEquals( "o", base.getOpt() ); - assertEquals( "option description", base.getDescription() ); - assertTrue( !base.hasArg() ); - } - - public void testSpecialOptChars() { - - // '?' - try { - Option opt = OptionBuilder.withDescription( "help options" ) - .create( '?' ); - assertEquals( "?", opt.getOpt() ); - } - catch( IllegalArgumentException arg ) { - fail( "IllegalArgumentException caught" ); - } - - // '@' - try { - Option opt = OptionBuilder.withDescription( "read from stdin" ) - .create( '@' ); - assertEquals( "@", opt.getOpt() ); - } - catch( IllegalArgumentException arg ) { - fail( "IllegalArgumentException caught" ); - } - } - - public void testOptionArgNumbers() { - Option opt = OptionBuilder.withDescription( "option description" ) - .hasArgs( 2 ) - .create( 'o' ); - assertEquals( 2, opt.getArgs() ); - } - - public void testIllegalOptions() { - // bad single character option - try { - Option opt = OptionBuilder.withDescription( "option description" ) - .create( '"' ); - fail( "IllegalArgumentException not caught" ); - } - catch( IllegalArgumentException exp ) { - // success - } - - // bad character in option string - try { - Option opt = OptionBuilder.create( "opt`" ); - fail( "IllegalArgumentException not caught" ); - } - catch( IllegalArgumentException exp ) { - // success - } - - // valid option - try { - Option opt = OptionBuilder.create( "opt" ); - // success - } - catch( IllegalArgumentException exp ) { - fail( "IllegalArgumentException caught" ); - } - } -} \ No newline at end of file diff --git a/src/test/org/apache/commons/cli/OptionGroupTest.java b/src/test/org/apache/commons/cli/OptionGroupTest.java deleted file mode 100644 index 8d66b1d70..000000000 --- a/src/test/org/apache/commons/cli/OptionGroupTest.java +++ /dev/null @@ -1,280 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * @author John Keyes (john at integralsource.com) - * @version $Revision$ - */ -public class OptionGroupTest extends TestCase -{ - - private Options _options = null; - private CommandLineParser parser = new PosixParser(); - - - public static Test suite() - { - return new TestSuite ( OptionGroupTest.class ); - } - - public OptionGroupTest( String name ) - { - super( name ); - } - - public void setUp() - { - Option file = new Option( "f", "file", false, "file to process" ); - Option dir = new Option( "d", "directory", false, "directory to process" ); - OptionGroup group = new OptionGroup(); - group.addOption( file ); - group.addOption( dir ); - _options = new Options().addOptionGroup( group ); - - Option section = new Option( "s", "section", false, "section to process" ); - Option chapter = new Option( "c", "chapter", false, "chapter to process" ); - OptionGroup group2 = new OptionGroup(); - group2.addOption( section ); - 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" ); - } - - public void tearDown() - { - } - - public void testSingleOptionFromGroup() - { - String[] args = new String[] { "-f" }; - - try - { - CommandLine cl = parser.parse( _options, args); - - assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") ); - assertTrue( "Confirm -f is set", cl.hasOption("f") ); - assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); - assertTrue( "Confirm -s is NOT 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 testSingleOption() - { - String[] args = new String[] { "-r" }; - - try - { - CommandLine cl = parser.parse( _options, args); - - assertTrue( "Confirm -r is set", cl.hasOption("r") ); - assertTrue( "Confirm -f is NOT set", !cl.hasOption("f") ); - assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); - assertTrue( "Confirm -s is NOT 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 testTwoValidOptions() - { - String[] args = new String[] { "-r", "-f" }; - - try - { - CommandLine cl = parser.parse( _options, args); - - assertTrue( "Confirm -r is set", cl.hasOption("r") ); - assertTrue( "Confirm -f is set", cl.hasOption("f") ); - assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); - assertTrue( "Confirm -s is NOT 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 testSingleLongOption() - { - String[] args = new String[] { "--file" }; - - try - { - CommandLine cl = parser.parse( _options, args); - - assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") ); - assertTrue( "Confirm -f is set", cl.hasOption("f") ); - assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); - assertTrue( "Confirm -s is NOT 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 testTwoValidLongOptions() - { - String[] args = new String[] { "--revision", "--file" }; - - try - { - CommandLine cl = parser.parse( _options, args); - - assertTrue( "Confirm -r is set", cl.hasOption("r") ); - assertTrue( "Confirm -f is set", cl.hasOption("f") ); - assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); - assertTrue( "Confirm -s is NOT 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 testNoOptionsExtraArgs() - { - String[] args = new String[] { "arg1", "arg2" }; - - try - { - CommandLine cl = parser.parse( _options, args); - - assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") ); - assertTrue( "Confirm -f is NOT set", !cl.hasOption("f") ); - assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); - assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") ); - assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") ); - assertTrue( "Confirm TWO extra args", cl.getArgList().size() == 2); - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testTwoOptionsFromGroup() - { - String[] args = new String[] { "-f", "-d" }; - - try - { - CommandLine cl = parser.parse( _options, args); - fail( "two arguments from group not allowed" ); - } - catch (ParseException e) - { - if( !( e instanceof AlreadySelectedException ) ) - { - fail( "incorrect exception caught:" + e.getMessage() ); - } - } - } - - public void testTwoLongOptionsFromGroup() - { - String[] args = new String[] { "--file", "--directory" }; - - try - { - CommandLine cl = parser.parse( _options, args); - fail( "two arguments from group not allowed" ); - } - catch (ParseException e) - { - if( !( e instanceof AlreadySelectedException ) ) - { - fail( "incorrect exception caught:" + e.getMessage() ); - } - } - } - - public void testTwoOptionsFromDifferentGroup() - { - String[] args = new String[] { "-f", "-s" }; - - try - { - CommandLine cl = parser.parse( _options, args); - assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") ); - assertTrue( "Confirm -f is set", cl.hasOption("f") ); - assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); - 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) - { - fail( e.toString() ); - } - } - - -} diff --git a/src/test/org/apache/commons/cli/OptionsTest.java b/src/test/org/apache/commons/cli/OptionsTest.java deleted file mode 100644 index 1cdae6864..000000000 --- a/src/test/org/apache/commons/cli/OptionsTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -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$ - */ -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)); - } - - - -} - diff --git a/src/test/org/apache/commons/cli/ParseRequiredTest.java b/src/test/org/apache/commons/cli/ParseRequiredTest.java deleted file mode 100644 index 104da9332..000000000 --- a/src/test/org/apache/commons/cli/ParseRequiredTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * @author John Keyes (john at integralsource.com) - * @version $Revision$ - */ -public class ParseRequiredTest extends TestCase -{ - - private Options _options = null; - private CommandLineParser parser = new PosixParser(); - - public static Test suite() { - return new TestSuite(ParseRequiredTest.class); - } - - public ParseRequiredTest(String name) - { - super(name); - } - - public void setUp() - { - _options = new Options() - .addOption("a", - "enable-a", - false, - "turn [a] on or off") - .addOption( OptionBuilder.withLongOpt( "bfile" ) - .hasArg() - .isRequired() - .withDescription( "set the value of [b]" ) - .create( 'b' ) ); - } - - public void tearDown() - { - - } - - public void testWithRequiredOption() - { - String[] args = new String[] { "-b", "file" }; - - try - { - CommandLine cl = parser.parse(_options,args); - - assertTrue( "Confirm -a is NOT set", !cl.hasOption("a") ); - assertTrue( "Confirm -b is set", cl.hasOption("b") ); - assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("file") ); - assertTrue( "Confirm NO of extra args", cl.getArgList().size() == 0); - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testOptionAndRequiredOption() - { - String[] args = new String[] { "-a", "-b", "file" }; - - try - { - 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.getOptionValue("b").equals("file") ); - assertTrue( "Confirm NO of extra args", cl.getArgList().size() == 0); - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testMissingRequiredOption() - { - String[] args = new String[] { "-a" }; - - try - { - CommandLine cl = parser.parse(_options,args); - fail( "exception should have been thrown" ); - } - catch (ParseException e) - { - if( !( e instanceof MissingOptionException ) ) - { - fail( "expected to catch MissingOptionException" ); - } - } - } - -} diff --git a/src/test/org/apache/commons/cli/ParseTest.java b/src/test/org/apache/commons/cli/ParseTest.java deleted file mode 100644 index 96ce866e5..000000000 --- a/src/test/org/apache/commons/cli/ParseTest.java +++ /dev/null @@ -1,290 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class ParseTest extends TestCase -{ - - private Options _options = null; - private CommandLineParser _parser = null; - - public static Test suite() { - return new TestSuite(ParseTest.class); - } - - public ParseTest(String name) - { - super(name); - } - - public void setUp() - { - _options = new Options() - .addOption("a", - "enable-a", - false, - "turn [a] on or off") - .addOption("b", - "bfile", - true, - "set the value of [b]") - .addOption("c", - "copt", - false, - "turn [c] on or off"); - - _parser = new PosixParser(); - } - - public void tearDown() - { - - } - - public void testSimpleShort() - { - String[] args = new String[] { "-a", - "-b", "toast", - "foo", "bar" }; - - try - { - 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.getOptionValue("b").equals("toast") ); - assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testSimpleLong() - { - String[] args = new String[] { "--enable-a", - "--bfile", "toast", - "foo", "bar" }; - - try - { - 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.getOptionValue("b").equals("toast") ); - assertTrue( "Confirm arg of --bfile", cl.getOptionValue( "bfile" ).equals( "toast" ) ); - assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testComplexShort() - { - String[] args = new String[] { "-acbtoast", - "foo", "bar" }; - - try - { - CommandLine cl = _parser.parse(_options, args); - - assertTrue( "Confirm -a is set", cl.hasOption("a") ); - assertTrue( "Confirm -b is set", cl.hasOption("b") ); - assertTrue( "Confirm -c is set", cl.hasOption("c") ); - assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); - assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testExtraOption() - { - String[] args = new String[] { "-adbtoast", - "foo", "bar" }; - - boolean caught = false; - - try - { - 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.getOptionValue("b").equals("toast") ); - assertTrue( "Confirm size of extra args", cl.getArgList().size() == 3); - } - catch (UnrecognizedOptionException e) - { - caught = true; - } - catch (ParseException e) - { - fail( e.toString() ); - } - assertTrue( "Confirm UnrecognizedOptionException caught", caught ); - } - - public void testMissingArg() - { - - String[] args = new String[] { "-acb" }; - - boolean caught = false; - - try - { - CommandLine cl = _parser.parse(_options, args); - } - catch (MissingArgumentException e) - { - caught = true; - } - catch (ParseException e) - { - fail( e.toString() ); - } - - assertTrue( "Confirm MissingArgumentException caught", caught ); - } - - public void testStop() - { - String[] args = new String[] { "-c", - "foober", - "-btoast" }; - - try - { - CommandLine cl = _parser.parse(_options, args, true); - assertTrue( "Confirm -c is set", cl.hasOption("c") ); - assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testMultiple() - { - String[] args = new String[] { "-c", - "foobar", - "-btoast" }; - - try - { - CommandLine cl = _parser.parse(_options, args, true); - assertTrue( "Confirm -c is set", cl.hasOption("c") ); - assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); - - cl = _parser.parse(_options, cl.getArgs() ); - - assertTrue( "Confirm -c is not set", ! cl.hasOption("c") ); - assertTrue( "Confirm -b is set", cl.hasOption("b") ); - assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); - assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); - assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") ); - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testMultipleWithLong() - { - String[] args = new String[] { "--copt", - "foobar", - "--bfile", "toast" }; - - try - { - CommandLine cl = _parser.parse(_options,args, - true); - assertTrue( "Confirm -c is set", cl.hasOption("c") ); - assertTrue( "Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3); - - cl = _parser.parse(_options, cl.getArgs() ); - - assertTrue( "Confirm -c is not set", ! cl.hasOption("c") ); - assertTrue( "Confirm -b is set", cl.hasOption("b") ); - assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); - assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); - assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") ); - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testDoubleDash() - { - String[] args = new String[] { "--copt", - "--", - "-b", "toast" }; - - try - { - CommandLine cl = _parser.parse(_options, args); - - assertTrue( "Confirm -c is set", cl.hasOption("c") ); - assertTrue( "Confirm -b is not set", ! cl.hasOption("b") ); - assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); - - } - catch (ParseException e) - { - fail( e.toString() ); - } - } - - public void testSingleDash() - { - String[] args = new String[] { "--copt", - "-b", "-", - "-a", - "-" }; - - try - { - 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.getOptionValue("b").equals("-") ); - assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); - assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("-") ); - } - catch (ParseException e) - { - fail( e.toString() ); - } - - } -} diff --git a/src/test/org/apache/commons/cli/PatternOptionBuilderTest.java b/src/test/org/apache/commons/cli/PatternOptionBuilderTest.java deleted file mode 100644 index 805ff6b72..000000000 --- a/src/test/org/apache/commons/cli/PatternOptionBuilderTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Test case for the PatternOptionBuilder class - * - * @author Henri Yandell - **/ -public class PatternOptionBuilderTest -extends TestCase -{ - public static void main( String[] args ) - { - String[] testName = { PatternOptionBuilderTest.class.getName() }; - junit.textui.TestRunner.main(testName); - } - - public static TestSuite suite() - { - return new TestSuite(PatternOptionBuilderTest.class); - } - - public PatternOptionBuilderTest( String s ) - { - super( s ); - } - - public void testSimplePattern() - { - try { - Options options = PatternOptionBuilder.parsePattern("a:b@cde>f+n%t/"); - String[] args = new String[] { "-c", "-a", "foo", "-b", "java.util.Vector", "-e", "build.xml", "-f", "java.util.Calendar", "-n", "4.5", "-t", "http://jakarta.apache.org/" }; - - CommandLineParser parser = new PosixParser(); - CommandLine line = parser.parse(options,args); - - // tests the char methods of CommandLine that delegate to - // the String methods - assertEquals("flag a", "foo", line.getOptionValue("a")); - assertEquals("flag a", "foo", line.getOptionValue('a')); - assertEquals("string flag a", "foo", line.getOptionObject("a")); - assertEquals("string flag a", "foo", line.getOptionObject('a')); - assertEquals("object flag b", new java.util.Vector(), line.getOptionObject("b")); - assertEquals("object flag b", new java.util.Vector(), line.getOptionObject('b')); - assertEquals("boolean true flag c", true, line.hasOption("c")); - assertEquals("boolean true flag c", true, line.hasOption('c')); - assertEquals("boolean false flag d", false, line.hasOption("d")); - assertEquals("boolean false flag d", false, line.hasOption('d')); - assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject("e")); - assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject('e')); - assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject("f")); - assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject('f')); - assertEquals("number flag n", new Float(4.5), line.getOptionObject("n")); - assertEquals("number flag n", new Float(4.5), line.getOptionObject('n')); - assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject("t")); - assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject('t')); - /// DATES NOT SUPPORTED YET. - // assertEquals("number flag t", new java.util.Date(1023400137276L), line.getOptionObject('z')); - // input is: "Thu Jun 06 17:48:57 EDT 2002" - } - catch( ParseException exp ) { - fail( exp.getMessage() ); - } - catch( java.net.MalformedURLException exp ) { - fail( exp.getMessage() ); - } - } - -} diff --git a/src/test/org/apache/commons/cli/TestHelpFormatter.java b/src/test/org/apache/commons/cli/TestHelpFormatter.java deleted file mode 100644 index 039efd919..000000000 --- a/src/test/org/apache/commons/cli/TestHelpFormatter.java +++ /dev/null @@ -1,176 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -import java.io.ByteArrayOutputStream; -import java.io.PrintWriter; - -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Test case for the HelpFormatter class - * - * @author Slawek Zachcial - * @author John Keyes ( john at integralsource.com ) - **/ -public class TestHelpFormatter extends TestCase -{ - public static void main( String[] args ) - { - String[] testName = { TestHelpFormatter.class.getName() }; - junit.textui.TestRunner.main(testName); - } - - public static TestSuite suite() - { - return new TestSuite(TestHelpFormatter.class); - } - - public TestHelpFormatter( String s ) - { - super( s ); - } - - public void testFindWrapPos() - throws Exception - { - HelpFormatter hf = new HelpFormatter(); - - String text = "This is a test."; - //text width should be max 8; the wrap postition is 7 - assertEquals("wrap position", 7, hf.findWrapPos(text, 8, 0)); - //starting from 8 must give -1 - the wrap pos is after end - assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8)); - //if there is no a good position before width to make a wrapping look for the next one - text = "aaaa aa"; - assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0)); - } - - public void testPrintWrapped() - throws Exception - { - StringBuffer sb = new StringBuffer(); - HelpFormatter hf = new HelpFormatter(); - - String text = "This is a test."; - String expected; - - expected = "This is a" + hf.getNewLine() + "test."; - hf.renderWrappedText(sb, 12, 0, text); - assertEquals("single line text", expected, sb.toString()); - - sb.setLength(0); - expected = "This is a" + hf.getNewLine() + " test."; - hf.renderWrappedText(sb, 12, 4, text); - assertEquals("single line padded text", expected, sb.toString()); - - text = - "aaaa aaaa aaaa" + hf.getNewLine() + - "aaaaaa" + hf.getNewLine() + - "aaaaa"; - - expected = text; - sb.setLength(0); - hf.renderWrappedText(sb, 16, 0, text); - assertEquals("multi line text", expected, sb.toString()); - - expected = - "aaaa aaaa aaaa" + hf.getNewLine() + - " aaaaaa" + hf.getNewLine() + - " aaaaa"; - sb.setLength(0); - hf.renderWrappedText(sb, 16, 4, text); - assertEquals("multi-line padded text", expected, sb.toString()); - } - - public void testPrintOptions() - throws Exception - { - StringBuffer sb = new StringBuffer(); - HelpFormatter hf = new HelpFormatter(); - final int leftPad = 1; - final int descPad = 3; - final String lpad = hf.createPadding(leftPad); - final String dpad = hf.createPadding(descPad); - Options options = null; - String expected = null; - - options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa"); - expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa"; - hf.renderOptions(sb, 60, options, leftPad, descPad); - assertEquals("simple non-wrapped option", expected, sb.toString()); - - int nextLineTabStop = leftPad+descPad+"-a".length(); - expected = - lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.getNewLine() + - hf.createPadding(nextLineTabStop) + "aaaa aaaa"; - sb.setLength(0); - hf.renderOptions(sb, nextLineTabStop+17, options, leftPad, descPad); - assertEquals("simple wrapped option", expected, sb.toString()); - - - options = new Options().addOption("a", "aaa", false, "dddd dddd dddd dddd"); - expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd"; - sb.setLength(0); - hf.renderOptions(sb, 60, options, leftPad, descPad); - assertEquals("long non-wrapped option", expected, sb.toString()); - - nextLineTabStop = leftPad+descPad+"-a,--aaa".length(); - expected = - lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() + - hf.createPadding(nextLineTabStop) + "dddd dddd"; - sb.setLength(0); - hf.renderOptions(sb, 25, options, leftPad, descPad); - assertEquals("long wrapped option", expected, sb.toString()); - - options = new Options(). - addOption("a", "aaa", false, "dddd dddd dddd dddd"). - addOption("b", false, "feeee eeee eeee eeee"); - expected = - lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() + - hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.getNewLine() + - lpad + "-b " + dpad + "feeee eeee" + hf.getNewLine() + - hf.createPadding(nextLineTabStop) + "eeee eeee"; - sb.setLength(0); - hf.renderOptions(sb, 25, options, leftPad, descPad); - assertEquals("multiple wrapped options", expected, sb.toString()); - } - - public void testAutomaticUsage() - throws Exception - { - HelpFormatter hf = new HelpFormatter(); - Options options = null; - String expected = "usage: app [-a]"; - ByteArrayOutputStream out = new ByteArrayOutputStream( ); - PrintWriter pw = new PrintWriter( out ); - - options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa"); - hf.printUsage( pw, 60, "app", options ); - pw.flush(); - assertEquals("simple auto usage", expected, out.toString().trim()); - out.reset(); - - expected = "usage: app [-a] [-b]"; - options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa") - .addOption("b", false, "bbb" ); - hf.printUsage( pw, 60, "app", options ); - pw.flush(); - assertEquals("simple auto usage", expected, out.toString().trim()); - out.reset(); - } -} diff --git a/src/test/org/apache/commons/cli/ValueTest.java b/src/test/org/apache/commons/cli/ValueTest.java deleted file mode 100644 index 98bf2bf09..000000000 --- a/src/test/org/apache/commons/cli/ValueTest.java +++ /dev/null @@ -1,426 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import java.util.Arrays; -import java.util.Properties; - -public class ValueTest extends TestCase -{ - - public static Test suite() { - return new TestSuite(ValueTest.class); - } - - private CommandLine _cl = null; - private CommandLine _clOptional = null; - private Options opts = new Options(); - - public ValueTest(String name) - { - super(name); - } - - public void setUp() - { - opts.addOption("a", - false, - "toggle -a"); - - opts.addOption("b", - true, - "set -b"); - - opts.addOption("c", - "c", - false, - "toggle -c"); - - opts.addOption("d", - "d", - true, - "set -d"); - - opts.addOption( OptionBuilder.hasOptionalArg() - .create( 'e') ); - - opts.addOption( OptionBuilder.hasOptionalArg() - .withLongOpt( "fish" ) - .create( ) ); - - opts.addOption( OptionBuilder.hasOptionalArgs() - .withLongOpt( "gravy" ) - .create( ) ); - - opts.addOption( OptionBuilder.hasOptionalArgs( 2 ) - .withLongOpt( "hide" ) - .create( ) ); - - opts.addOption( OptionBuilder.hasOptionalArgs( 2 ) - .create( 'i' ) ); - - opts.addOption( OptionBuilder.hasOptionalArgs( ) - .create( 'j' ) ); - - opts.addOption( OptionBuilder.hasArgs( ).withValueSeparator( ',' ) - .create( 'k' ) ); - - String[] args = new String[] { "-a", - "-b", "foo", - "--c", - "--d", "bar" - }; - - try - { - CommandLineParser parser = new PosixParser(); - _cl = parser.parse(opts,args); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void tearDown() - { - - } - - public void testShortNoArg() - { - assertTrue( _cl.hasOption("a") ); - assertNull( _cl.getOptionValue("a") ); - } - - public void testShortWithArg() - { - assertTrue( _cl.hasOption("b") ); - assertNotNull( _cl.getOptionValue("b") ); - assertEquals( _cl.getOptionValue("b"), "foo"); - } - - public void testLongNoArg() - { - assertTrue( _cl.hasOption("c") ); - assertNull( _cl.getOptionValue("c") ); - } - - public void testLongWithArg() - { - assertTrue( _cl.hasOption("d") ); - assertNotNull( _cl.getOptionValue("d") ); - assertEquals( _cl.getOptionValue("d"), "bar"); - } - - public void testShortOptionalArgNoValue() - { - String[] args = new String[] { "-e" - }; - try - { - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(opts,args); - assertTrue( cmd.hasOption("e") ); - assertNull( cmd.getOptionValue("e") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testShortOptionalArgValue() - { - String[] args = new String[] { "-e", "everything" - }; - try - { - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(opts,args); - assertTrue( cmd.hasOption("e") ); - assertEquals( "everything", cmd.getOptionValue("e") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testLongOptionalNoValue() - { - String[] args = new String[] { "--fish" - }; - try - { - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(opts,args); - assertTrue( cmd.hasOption("fish") ); - assertNull( cmd.getOptionValue("fish") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testLongOptionalArgValue() - { - String[] args = new String[] { "--fish", "face" - }; - try - { - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(opts,args); - assertTrue( cmd.hasOption("fish") ); - assertEquals( "face", cmd.getOptionValue("fish") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testShortOptionalArgValues() - { - String[] args = new String[] { "-j", "ink", "idea" - }; - try - { - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(opts,args); - assertTrue( cmd.hasOption("j") ); - assertEquals( "ink", cmd.getOptionValue("j") ); - assertEquals( "ink", cmd.getOptionValues("j")[0] ); - assertEquals( "idea", cmd.getOptionValues("j")[1] ); - assertEquals( cmd.getArgs().length, 0 ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testLongOptionalArgValues() - { - String[] args = new String[] { "--gravy", "gold", "garden" - }; - try - { - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(opts,args); - assertTrue( cmd.hasOption("gravy") ); - assertEquals( "gold", cmd.getOptionValue("gravy") ); - assertEquals( "gold", cmd.getOptionValues("gravy")[0] ); - assertEquals( "garden", cmd.getOptionValues("gravy")[1] ); - assertEquals( cmd.getArgs().length, 0 ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testShortOptionalNArgValues() - { - String[] args = new String[] { "-i", "ink", "idea", "isotope", "ice" - }; - try - { - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(opts,args); - assertTrue( cmd.hasOption("i") ); - assertEquals( "ink", cmd.getOptionValue("i") ); - assertEquals( "ink", cmd.getOptionValues("i")[0] ); - assertEquals( "idea", cmd.getOptionValues("i")[1] ); - assertEquals( cmd.getArgs().length, 2 ); - assertEquals( "isotope", cmd.getArgs()[0] ); - assertEquals( "ice", cmd.getArgs()[1] ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testLongOptionalNArgValues() - { - String[] args = new String[] { - "--hide", "house", "hair", "head" - }; - - CommandLineParser parser = new PosixParser(); - - try - { - CommandLine cmd = parser.parse(opts,args); - assertTrue( cmd.hasOption("hide") ); - assertEquals( "house", cmd.getOptionValue("hide") ); - assertEquals( "house", cmd.getOptionValues("hide")[0] ); - assertEquals( "hair", cmd.getOptionValues("hide")[1] ); - assertEquals( cmd.getArgs().length, 1 ); - assertEquals( "head", cmd.getArgs()[0] ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testPropertyOptionSingularValue() - { - Properties properties = new Properties(); - properties.setProperty( "hide", "seek" ); - - CommandLineParser parser = new PosixParser(); - - try - { - CommandLine cmd = parser.parse(opts, null, properties); - assertTrue( cmd.hasOption("hide") ); - assertEquals( "seek", cmd.getOptionValue("hide") ); - assertTrue( !cmd.hasOption("fake") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testPropertyOptionFlags() - { - Properties properties = new Properties(); - properties.setProperty( "a", "true" ); - properties.setProperty( "c", "yes" ); - properties.setProperty( "e", "1" ); - - CommandLineParser parser = new PosixParser(); - - try - { - CommandLine cmd = parser.parse(opts, null, properties); - assertTrue( cmd.hasOption("a") ); - assertTrue( cmd.hasOption("c") ); - assertTrue( cmd.hasOption("e") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - - properties = new Properties(); - properties.setProperty( "a", "false" ); - properties.setProperty( "c", "no" ); - properties.setProperty( "e", "0" ); - try - { - CommandLine cmd = parser.parse(opts, null, properties); - assertTrue( !cmd.hasOption("a") ); - assertTrue( !cmd.hasOption("c") ); - assertTrue( !cmd.hasOption("e") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - - properties = new Properties(); - properties.setProperty( "a", "TRUE" ); - properties.setProperty( "c", "nO" ); - properties.setProperty( "e", "TrUe" ); - try - { - CommandLine cmd = parser.parse(opts, null, properties); - assertTrue( cmd.hasOption("a") ); - assertTrue( !cmd.hasOption("c") ); - assertTrue( cmd.hasOption("e") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - - properties = new Properties(); - properties.setProperty( "a", "just a string" ); - properties.setProperty( "e", "" ); - try - { - CommandLine cmd = parser.parse(opts, null, properties); - assertTrue( !cmd.hasOption("a") ); - assertTrue( !cmd.hasOption("c") ); - assertTrue( !cmd.hasOption("e") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - - } - - public void testPropertyOptionMultipleValues() - { - Properties properties = new Properties(); - properties.setProperty( "k", "one,two" ); - - CommandLineParser parser = new PosixParser(); - - String[] values = new String[] { - "one", "two" - }; - try - { - CommandLine cmd = parser.parse(opts, null, properties); - assertTrue( cmd.hasOption("k") ); - assertTrue( Arrays.equals( values, cmd.getOptionValues('k') ) ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void testPropertyOverrideValues() - { - String[] args = new String[] { - "-j", - "found", - "-i", - "ink" - }; - - Properties properties = new Properties(); - properties.setProperty( "j", "seek" ); - try - { - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(opts, args, properties); - assertTrue( cmd.hasOption("j") ); - assertEquals( "found", cmd.getOptionValue("j") ); - assertTrue( cmd.hasOption("i") ); - assertEquals( "ink", cmd.getOptionValue("i") ); - assertTrue( !cmd.hasOption("fake") ); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - -} diff --git a/src/test/org/apache/commons/cli/ValuesTest.java b/src/test/org/apache/commons/cli/ValuesTest.java deleted file mode 100644 index c49318659..000000000 --- a/src/test/org/apache/commons/cli/ValuesTest.java +++ /dev/null @@ -1,253 +0,0 @@ -/** - * Copyright 2001-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli; - -import java.util.Arrays; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class ValuesTest extends TestCase -{ - /** CommandLine instance */ - private CommandLine _cmdline = null; - private Option _option = null; - - public static Test suite() { - return new TestSuite( ValuesTest.class ); - } - - public ValuesTest( String name ) - { - super( name ); - } - - public void setUp() - { - Options opts = new Options(); - - opts.addOption("a", - false, - "toggle -a"); - - opts.addOption("b", - true, - "set -b"); - - opts.addOption("c", - "c", - false, - "toggle -c"); - - opts.addOption("d", - "d", - true, - "set -d"); - - opts.addOption( OptionBuilder.withLongOpt( "e" ) - .hasArgs() - .withDescription( "set -e ") - .create( 'e' ) ); - - opts.addOption("f", - "f", - false, - "jk"); - - opts.addOption( OptionBuilder.withLongOpt( "g" ) - .hasArgs( 2 ) - .withDescription( "set -g") - .create( 'g' ) ); - - opts.addOption( OptionBuilder.withLongOpt( "h" ) - .hasArgs( 2 ) - .withDescription( "set -h") - .create( 'h' ) ); - - opts.addOption( OptionBuilder.withLongOpt( "i" ) - .withDescription( "set -i") - .create( 'i' ) ); - - opts.addOption( OptionBuilder.withLongOpt( "j" ) - .hasArgs( ) - .withDescription( "set -j") - .withValueSeparator( '=' ) - .create( 'j' ) ); - - opts.addOption( OptionBuilder.withLongOpt( "k" ) - .hasArgs( ) - .withDescription( "set -k") - .withValueSeparator( '=' ) - .create( 'k' ) ); - - _option = OptionBuilder.withLongOpt( "m" ) - .hasArgs( ) - .withDescription( "set -m") - .withValueSeparator( ) - .create( 'm' ); - - opts.addOption( _option ); - - String[] args = new String[] { "-a", - "-b", "foo", - "--c", - "--d", "bar", - "-e", "one", "two", - "-f", - "arg1", "arg2", - "-g", "val1", "val2" , "arg3", - "-h", "val1", "-i", - "-h", "val2", - "-jkey=value", - "-j", "key=value", - "-kkey1=value1", - "-kkey2=value2", - "-mkey=value"}; - - CommandLineParser parser = new PosixParser(); - - try - { - _cmdline = parser.parse(opts,args); - } - catch (ParseException e) - { - fail("Cannot setUp() CommandLine: " + e.toString()); - } - } - - public void tearDown() - { - - } - - public void testShortArgs() - { - assertTrue( _cmdline.hasOption("a") ); - assertTrue( _cmdline.hasOption("c") ); - - assertNull( _cmdline.getOptionValues("a") ); - assertNull( _cmdline.getOptionValues("c") ); - } - - public void testShortArgsWithValue() - { - assertTrue( _cmdline.hasOption("b") ); - assertTrue( _cmdline.getOptionValue("b").equals("foo")); - assertTrue( _cmdline.getOptionValues("b").length == 1); - - assertTrue( _cmdline.hasOption("d") ); - assertTrue( _cmdline.getOptionValue("d").equals("bar")); - assertTrue( _cmdline.getOptionValues("d").length == 1); - } - - public void testMultipleArgValues() - { - String[] result = _cmdline.getOptionValues("e"); - String[] values = new String[] { "one", "two" }; - assertTrue( _cmdline.hasOption("e") ); - assertTrue( _cmdline.getOptionValues("e").length == 2); - assertTrue( Arrays.equals( values, _cmdline.getOptionValues("e") ) ); - } - - public void testTwoArgValues() - { - String[] result = _cmdline.getOptionValues("g"); - String[] values = new String[] { "val1", "val2" }; - assertTrue( _cmdline.hasOption("g") ); - assertTrue( _cmdline.getOptionValues("g").length == 2); - assertTrue( Arrays.equals( values, _cmdline.getOptionValues("g") ) ); - } - - public void testComplexValues() - { - String[] result = _cmdline.getOptionValues("h"); - String[] values = new String[] { "val1", "val2" }; - assertTrue( _cmdline.hasOption("i") ); - assertTrue( _cmdline.hasOption("h") ); - assertTrue( _cmdline.getOptionValues("h").length == 2); - assertTrue( Arrays.equals( values, _cmdline.getOptionValues("h") ) ); - } - - public void testExtraArgs() - { - String[] args = new String[] { "arg1", "arg2", "arg3" }; - assertTrue( _cmdline.getArgs().length == 3 ); - assertTrue( Arrays.equals( args, _cmdline.getArgs() ) ); - } - - public void testCharSeparator() - { - // tests the char methods of CommandLine that delegate to - // the String methods - String[] values = new String[] { "key", "value", "key", "value" }; - assertTrue( _cmdline.hasOption( "j" ) ); - assertTrue( _cmdline.hasOption( 'j' ) ); - assertEquals( 4, _cmdline.getOptionValues( "j" ).length ); - assertEquals( 4, _cmdline.getOptionValues( 'j' ).length ); - assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "j" ) ) ); - assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'j' ) ) ); - - values = new String[] { "key1", "value1", "key2", "value2" }; - assertTrue( _cmdline.hasOption( "k" ) ); - assertTrue( _cmdline.hasOption( 'k' ) ); - assertTrue( _cmdline.getOptionValues( "k" ).length == 4 ); - assertTrue( _cmdline.getOptionValues( 'k' ).length == 4 ); - assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "k" ) ) ); - assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'k' ) ) ); - - values = new String[] { "key", "value" }; - assertTrue( _cmdline.hasOption( "m" ) ); - assertTrue( _cmdline.hasOption( 'm' ) ); - assertTrue( _cmdline.getOptionValues( "m" ).length == 2); - assertTrue( _cmdline.getOptionValues( 'm' ).length == 2); - assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "m" ) ) ); - assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'm' ) ) ); - } - - /** - * jkeyes - commented out this test as the new architecture - * breaks this type of functionality. I have left the test - * here in case I get a brainwave on how to resolve this. - */ - /* - public void testGetValue() - { - // the 'm' option - assertTrue( _option.getValues().length == 2 ); - assertEquals( _option.getValue(), "key" ); - assertEquals( _option.getValue( 0 ), "key" ); - assertEquals( _option.getValue( 1 ), "value" ); - - try { - assertEquals( _option.getValue( 2 ), "key" ); - fail( "IndexOutOfBounds not caught" ); - } - catch( IndexOutOfBoundsException exp ) { - - } - - try { - assertEquals( _option.getValue( -1 ), "key" ); - fail( "IndexOutOfBounds not caught" ); - } - catch( IndexOutOfBoundsException exp ) { - - } - } - */ -} diff --git a/src/test/org/apache/commons/cli/bug/BugCLI18Test.java b/src/test/org/apache/commons/cli/bug/BugCLI18Test.java deleted file mode 100644 index a17d74508..000000000 --- a/src/test/org/apache/commons/cli/bug/BugCLI18Test.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.cli.bug; - -import org.apache.commons.cli.*; - -import java.io.PrintWriter; -import java.io.StringWriter; - -import junit.framework.TestCase; - -/** - * http://issues.apache.org/jira/browse/CLI-18 - */ -public class BugCLI18Test extends TestCase { - - public void testCLI18() { - Options options = new Options(); - options.addOption(new Option("a","aaa",false,"aaaaaaa")); - options.addOption(new Option(null,"bbb",false,"bbbbbbb dksh fkshd fkhs dkfhsdk fhskd hksdks dhfowehfsdhfkjshf skfhkshf sf jkshfk sfh skfh skf f")); - options.addOption(new Option("c",null,false,"ccccccc")); - - HelpFormatter formatter = new HelpFormatter(); - StringWriter out = new StringWriter(); - - formatter.printHelp(new PrintWriter(out),80, "foobar", "dsfkfsh kdh hsd hsdh fkshdf ksdh fskdh fsdh fkshfk sfdkjhskjh fkjh fkjsh khsdkj hfskdhf skjdfh ksf khf s", options, 2, 2, "blort j jgj j jg jhghjghjgjhgjhg jgjhgj jhg jhg hjg jgjhghjg jhg hjg jhgjg jgjhghjg jg jgjhgjgjg jhg jhgjh" + '\r' + '\n' + "rarrr", true); - } -} - From 7e7e4d85577cd94e159a99160c357d1337494b44 Mon Sep 17 00:00:00 2001 From: Henri Yandell-Issues may be reported via ASF Bugzilla. -Please remember that Bugzilla is shared between all commons components, -so prefix your issue by [cli]. +Issues may be reported via ASF JIRA.
diff --git a/xdocs/issue-tracking.xml b/xdocs/issue-tracking.xml index fc4acd835..23193cb27 100644 --- a/xdocs/issue-tracking.xml +++ b/xdocs/issue-tracking.xml @@ -23,19 +23,18 @@ limitations under the License.- Commons CLI uses ASF Bugzilla for tracking issues. - To use Bugzilla you may need to create an account. + Commons CLI uses ASF JIRA for tracking issues. + To use JIRA you may need to create an account.
If you would like to report a bug, or raise an enhancement request with Commons CLI please do the following:
- You may also find these links useful: -
-