http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/commandline/PropertiesCommandLine.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/commandline/PropertiesCommandLine.java b/src/java/org/apache/commons/cli2/commandline/PropertiesCommandLine.java deleted file mode 100644 index 1ae4f94..0000000 --- a/src/java/org/apache/commons/cli2/commandline/PropertiesCommandLine.java +++ /dev/null @@ -1,154 +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.cli2.commandline; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; -import java.util.Set; -import java.util.StringTokenizer; - -import org.apache.commons.cli2.Option; - -/** - * A CommandLine implementation using a java Properties instance, useful for - * constructing a complex DefaultingCommandLine - * - * Options are keyed from their property name and presence in the Properties - * instance is taken as presence in the CommandLine. Argument values are taken - * from the property value and are optionally separated using the separator - * char, defined at construction time. Switch values can be specified using a - * simple value of <code>true</code> or <code>false</code>; obviously this means - * that Switches with Arguments are not supported by this implementation. - * - * @see java.util.Properties - * @see org.apache.commons.cli2.commandline.DefaultingCommandLine - * @see org.apache.commons.cli2.Option#getPreferredName() - */ -public class PropertiesCommandLine extends CommandLineImpl { - - private static final char NUL = '\0'; - private final Properties properties; - private final Option root; - private final char separator; - - /** - * Creates a new PropertiesCommandLine using the specified root Option, - * Properties instance. The character 0 is used as the value separator. - * - * @param root the CommandLine's root Option - * @param properties the Properties instance to get values from - */ - public PropertiesCommandLine(final Option root, final Properties properties){ - this(root,properties,NUL); - } - - /** - * Creates a new PropertiesCommandLine using the specified root Option, - * Properties instance and value separator. - * - * @param root the CommandLine's root Option - * @param properties the Properties instance to get values from - * @param separator the character to split argument values - */ - public PropertiesCommandLine(final Option root, final Properties properties, final char separator){ - this.root = root; - this.properties = properties; - this.separator = separator; - } - - - public boolean hasOption(Option option) { - if(option==null){ - return false; - } - else{ - return properties.containsKey(option.getPreferredName()); - } - } - - public Option getOption(String trigger) { - return root.findOption(trigger); - } - - public List getValues(final Option option, final List defaultValues) { - final String value = properties.getProperty(option.getPreferredName()); - - if(value==null){ - return defaultValues; - } - else if(separator>NUL){ - final List values = new ArrayList(); - final StringTokenizer tokens = new StringTokenizer(value,String.valueOf(separator)); - - while(tokens.hasMoreTokens()){ - values.add(tokens.nextToken()); - } - - return values; - } - else{ - return Collections.singletonList(value); - } - } - - public Boolean getSwitch(final Option option, final Boolean defaultValue) { - final String value = properties.getProperty(option.getPreferredName()); - if("true".equals(value)){ - return Boolean.TRUE; - } - else if("false".equals(value)){ - return Boolean.FALSE; - } - else{ - return defaultValue; - } - } - - public String getProperty(final String property, final String defaultValue) { - return properties.getProperty(property,defaultValue); - } - - public Set getProperties() { - return properties.keySet(); - } - - public List getOptions() { - final List options = new ArrayList(); - final Iterator keys = properties.keySet().iterator(); - while(keys.hasNext()){ - final String trigger = (String)keys.next(); - final Option option = root.findOption(trigger); - if(option!=null){ - options.add(option); - } - } - return Collections.unmodifiableList(options); - } - - public Set getOptionTriggers() { - final Set triggers = new HashSet(); - final Iterator options = getOptions().iterator(); - while(options.hasNext()){ - final Option option = (Option)options.next(); - triggers.addAll(option.getTriggers()); - } - return Collections.unmodifiableSet(triggers); - } -}
http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java b/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java deleted file mode 100644 index c829907..0000000 --- a/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright 2004-2005 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.cli2.commandline; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - -import org.apache.commons.cli2.Argument; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; - -/** - * A WriteableCommandLine implementation allowing Options to write their - * processed information to a CommandLine. - */ -public class WriteableCommandLineImpl - extends CommandLineImpl implements WriteableCommandLine { - private final Properties properties = new Properties(); - private final List options = new ArrayList(); - private final Map nameToOption = new HashMap(); - private final Map values = new HashMap(); - private final Map switches = new HashMap(); - private final Map defaultValues = new HashMap(); - private final Map defaultSwitches = new HashMap(); - private final List normalised; - private final Set prefixes; - - /** - * Creates a new WriteableCommandLineImpl rooted on the specified Option, to - * hold the parsed arguments. - * - * @param rootOption the CommandLine's root Option - * @param arguments the arguments this CommandLine represents - */ - public WriteableCommandLineImpl(final Option rootOption, - final List arguments) { - this.prefixes = rootOption.getPrefixes(); - this.normalised = arguments; - } - - public void addOption(Option option) { - options.add(option); - nameToOption.put(option.getPreferredName(), option); - - for (Iterator i = option.getTriggers().iterator(); i.hasNext();) { - nameToOption.put(i.next(), option); - } - } - - public void addValue(final Option option, - final Object value) { - if (option instanceof Argument) { - addOption(option); - } - - List valueList = (List) values.get(option); - - if (valueList == null) { - valueList = new ArrayList(); - values.put(option, valueList); - } - - valueList.add(value); - } - - public void addSwitch(final Option option, - final boolean value) { - addOption(option); - - if (switches.containsKey(option)) { - throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ALREADY_SET)); - } else { - switches.put(option, value ? Boolean.TRUE : Boolean.FALSE); - } - } - - public boolean hasOption(final Option option) { - final boolean present = options.contains(option); - - return present; - } - - public Option getOption(final String trigger) { - return (Option) nameToOption.get(trigger); - } - - public List getValues(final Option option, - final List defaultValues) { - // First grab the command line values - List valueList = (List) values.get(option); - - // Secondly try the defaults supplied to the method - if ((valueList == null) || valueList.isEmpty()) { - valueList = defaultValues; - } - - // Thirdly try the option's default values - if ((valueList == null) || valueList.isEmpty()) { - valueList = (List) this.defaultValues.get(option); - } - - // Finally use an empty list - if (valueList == null) { - valueList = Collections.EMPTY_LIST; - } - - return valueList; - } - - public Boolean getSwitch(final Option option, - final Boolean defaultValue) { - // First grab the command line values - Boolean bool = (Boolean) switches.get(option); - - // Secondly try the defaults supplied to the method - if (bool == null) { - bool = defaultValue; - } - - // Thirdly try the option's default values - if (bool == null) { - bool = (Boolean) this.defaultSwitches.get(option); - } - - return bool; - } - - public void addProperty(final String property, - final String value) { - properties.setProperty(property, value); - } - - public String getProperty(final String property, - final String defaultValue) { - return properties.getProperty(property, defaultValue); - } - - public Set getProperties() { - return Collections.unmodifiableSet(properties.keySet()); - } - - public boolean looksLikeOption(final String trigger) { - for (final Iterator i = prefixes.iterator(); i.hasNext();) { - final String prefix = (String) i.next(); - - if (trigger.startsWith(prefix)) { - return true; - } - } - - return false; - } - - public String toString() { - final StringBuffer buffer = new StringBuffer(); - - // need to add group header - for (final Iterator i = normalised.iterator(); i.hasNext();) { - final String arg = (String) i.next(); - - if (arg.indexOf(' ') >= 0) { - buffer.append("\"").append(arg).append("\""); - } else { - buffer.append(arg); - } - - if (i.hasNext()) { - buffer.append(' '); - } - } - - return buffer.toString(); - } - - public List getOptions() { - return Collections.unmodifiableList(options); - } - - public Set getOptionTriggers() { - return Collections.unmodifiableSet(nameToOption.keySet()); - } - - public void setDefaultValues(final Option option, - final List defaults) { - if (defaults == null) { - defaultValues.remove(option); - } else { - defaultValues.put(option, defaults); - } - } - - public void setDefaultSwitch(final Option option, - final Boolean defaultSwitch) { - if (defaultSwitch == null) { - defaultSwitches.remove(defaultSwitch); - } else { - defaultSwitches.put(option, defaultSwitch); - } - } - - public List getNormalised() { - return Collections.unmodifiableList(normalised); - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/option/ArgumentImpl.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/option/ArgumentImpl.java b/src/java/org/apache/commons/cli2/option/ArgumentImpl.java deleted file mode 100644 index d64f2fc..0000000 --- a/src/java/org/apache/commons/cli2/option/ArgumentImpl.java +++ /dev/null @@ -1,374 +0,0 @@ -/* - * Copyright 2003-2005 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.cli2.option; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Set; -import java.util.StringTokenizer; - -import org.apache.commons.cli2.Argument; -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.HelpLine; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; -import org.apache.commons.cli2.validation.InvalidArgumentException; -import org.apache.commons.cli2.validation.Validator; - -/** - * An implementation of an Argument. - */ -public class ArgumentImpl - extends OptionImpl implements Argument { - private static final char NUL = '\0'; - - /** - * The default value for the initial separator char. - */ - public static final char DEFAULT_INITIAL_SEPARATOR = NUL; - - /** - * The default value for the subsequent separator char. - */ - public static final char DEFAULT_SUBSEQUENT_SEPARATOR = NUL; - - /** - * The default token to indicate that remaining arguments should be consumed - * as values. - */ - public static final String DEFAULT_CONSUME_REMAINING = "--"; - private final String name; - private final String description; - private final int minimum; - private final int maximum; - private final char initialSeparator; - private final char subsequentSeparator; - private final boolean subsequentSplit; - private final Validator validator; - private final String consumeRemaining; - private final List defaultValues; - private final ResourceHelper resources = ResourceHelper.getResourceHelper(); - - /** - * Creates a new Argument instance. - * - * @param name - * The name of the argument - * @param description - * A description of the argument - * @param minimum - * The minimum number of values needed to be valid - * @param maximum - * The maximum number of values allowed to be valid - * @param initialSeparator - * The char separating option from value - * @param subsequentSeparator - * The char separating values from each other - * @param validator - * The object responsible for validating the values - * @param consumeRemaining - * The String used for the "consuming option" group - * @param valueDefaults - * The values to be used if none are specified. - * @param id - * The id of the option, 0 implies automatic assignment. - * - * @see OptionImpl#OptionImpl(int,boolean) - */ - public ArgumentImpl(final String name, - final String description, - final int minimum, - final int maximum, - final char initialSeparator, - final char subsequentSeparator, - final Validator validator, - final String consumeRemaining, - final List valueDefaults, - final int id) { - super(id, false); - - this.name = (name == null) ? "arg" : name; - this.description = description; - this.minimum = minimum; - this.maximum = maximum; - this.initialSeparator = initialSeparator; - this.subsequentSeparator = subsequentSeparator; - this.subsequentSplit = subsequentSeparator != NUL; - this.validator = validator; - this.consumeRemaining = consumeRemaining; - this.defaultValues = valueDefaults; - - if (minimum > maximum) { - throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_MIN_EXCEEDS_MAX)); - } - - if ((valueDefaults != null) && (valueDefaults.size() > 0)) { - if (valueDefaults.size() < minimum) { - throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_TOO_FEW_DEFAULTS)); - } - - if (valueDefaults.size() > maximum) { - throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_TOO_MANY_DEFAULTS)); - } - } - } - - public String getPreferredName() { - return name; - } - - public void processValues(final WriteableCommandLine commandLine, - final ListIterator arguments, - final Option option) - throws OptionException { - int argumentCount = commandLine.getValues(option, Collections.EMPTY_LIST).size(); - - while (arguments.hasNext() && (argumentCount < maximum)) { - final String allValues = stripBoundaryQuotes((String) arguments.next()); - - // should we ignore things that look like options? - if (allValues.equals(consumeRemaining)) { - while (arguments.hasNext() && (argumentCount < maximum)) { - ++argumentCount; - commandLine.addValue(option, arguments.next()); - } - } - // does it look like an option? - else if (commandLine.looksLikeOption(allValues)) { - arguments.previous(); - - break; - } - // should we split the string up? - else if (subsequentSplit) { - final StringTokenizer values = - new StringTokenizer(allValues, String.valueOf(subsequentSeparator)); - - arguments.remove(); - - while (values.hasMoreTokens() && (argumentCount < maximum)) { - ++argumentCount; - - final String token = values.nextToken(); - commandLine.addValue(option, token); - arguments.add(token); - } - - if (values.hasMoreTokens()) { - throw new OptionException(option, ResourceConstants.ARGUMENT_UNEXPECTED_VALUE, - values.nextToken()); - } - } - // it must be a value as it is - else { - ++argumentCount; - commandLine.addValue(option, allValues); - } - } - } - - public boolean canProcess(final WriteableCommandLine commandLine, - final String arg) { - return true; - } - - public Set getPrefixes() { - return Collections.EMPTY_SET; - } - - public void process(WriteableCommandLine commandLine, - ListIterator args) - throws OptionException { - processValues(commandLine, args, this); - } - - public char getInitialSeparator() { - return this.initialSeparator; - } - - public char getSubsequentSeparator() { - return this.subsequentSeparator; - } - - public Set getTriggers() { - return Collections.EMPTY_SET; - } - - public String getConsumeRemaining() { - return this.consumeRemaining; - } - - public List getDefaultValues() { - return this.defaultValues; - } - - public Validator getValidator() { - return this.validator; - } - - public void validate(final WriteableCommandLine commandLine) - throws OptionException { - validate(commandLine, this); - } - - public void validate(final WriteableCommandLine commandLine, - final Option option) - throws OptionException { - final List values = commandLine.getValues(option); - - if (values.size() < minimum) { - throw new OptionException(option, ResourceConstants.ARGUMENT_MISSING_VALUES); - } - - if (values.size() > maximum) { - throw new OptionException(option, ResourceConstants.ARGUMENT_UNEXPECTED_VALUE, - (String) values.get(maximum)); - } - - if (validator != null) { - try { - validator.validate(values); - } catch (InvalidArgumentException ive) { - throw new OptionException(option, ResourceConstants.ARGUMENT_UNEXPECTED_VALUE, - ive.getMessage()); - } - } - } - - public void appendUsage(final StringBuffer buffer, - final Set helpSettings, - final Comparator comp) { - // do we display the outer optionality - final boolean optional = helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL); - - // allow numbering if multiple args - final boolean numbered = - (maximum > 1) && helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED); - - final boolean bracketed = helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED); - - // if infinite args are allowed then crop the list - final int max = (maximum == Integer.MAX_VALUE) ? 2 : maximum; - - int i = 0; - - // for each argument - while (i < max) { - // if we're past the first add a space - if (i > 0) { - buffer.append(' '); - } - - // if the next arg is optional - if ((i >= minimum) && (optional || (i > 0))) { - buffer.append('['); - } - - if (bracketed) { - buffer.append('<'); - } - - // add name - buffer.append(name); - ++i; - - // if numbering - if (numbered) { - buffer.append(i); - } - - if (bracketed) { - buffer.append('>'); - } - } - - // if infinite args are allowed - if (maximum == Integer.MAX_VALUE) { - // append elipsis - buffer.append(" ..."); - } - - // for each argument - while (i > 0) { - --i; - - // if the next arg is optional - if ((i >= minimum) && (optional || (i > 0))) { - buffer.append(']'); - } - } - } - - public String getDescription() { - return description; - } - - public List helpLines(final int depth, - final Set helpSettings, - final Comparator comp) { - final HelpLine helpLine = new HelpLineImpl(this, depth); - - return Collections.singletonList(helpLine); - } - - public int getMaximum() { - return maximum; - } - - public int getMinimum() { - return minimum; - } - - /** - * If there are any leading or trailing quotes remove them from the - * specified token. - * - * @param token - * the token to strip leading and trailing quotes - * - * @return String the possibly modified token - */ - public String stripBoundaryQuotes(String token) { - if (!token.startsWith("\"") || !token.endsWith("\"")) { - return token; - } - - token = token.substring(1, token.length() - 1); - - return token; - } - - public boolean isRequired() { - return getMinimum() > 0; - } - - public void defaults(final WriteableCommandLine commandLine) { - super.defaults(commandLine); - defaultValues(commandLine, this); - } - - public void defaultValues(final WriteableCommandLine commandLine, - final Option option) { - commandLine.setDefaultValues(option, defaultValues); - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/option/Command.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/option/Command.java b/src/java/org/apache/commons/cli2/option/Command.java deleted file mode 100644 index c639094..0000000 --- a/src/java/org/apache/commons/cli2/option/Command.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright 2003-2005 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.cli2.option; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Set; - -import org.apache.commons.cli2.Argument; -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.Group; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; - -/** - * Represents a cvs "update" style command line option. - * - * Like all Parents, Commands can have child options and can be part of - * Arguments - */ -public class Command - extends ParentImpl { - /** The display name for the command */ - private final String preferredName; - - /** The aliases for this command */ - private final Set aliases; - - /** All the names for this command */ - private final Set triggers; - - /** - * Creates a new Command instance. - * - * @param preferredName - * The name normally used to refer to the Command - * @param description - * A description of the Command - * @param aliases - * Alternative names for the Command - * @param required - * Whether the Command is required - * @param argument - * An Argument that the command takes - * @param children - * The Group of child options for this Command - * @param id - * A unique id for the Command - * - * @see ParentImpl#ParentImpl(Argument, Group, String, int, boolean) - */ - public Command(final String preferredName, - final String description, - final Set aliases, - final boolean required, - final Argument argument, - final Group children, - final int id) { - super(argument, children, description, id, required); - - // check the preferred name is valid - if ((preferredName == null) || (preferredName.length() < 1)) { - throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT)); - } - - this.preferredName = preferredName; - - // gracefully and defensively handle aliases - this.aliases = - (aliases == null) ? Collections.EMPTY_SET - : Collections.unmodifiableSet(new HashSet(aliases)); - - // populate the triggers Set - final Set newTriggers = new HashSet(); - newTriggers.add(preferredName); - newTriggers.addAll(this.aliases); - this.triggers = Collections.unmodifiableSet(newTriggers); - } - - public void processParent(final WriteableCommandLine commandLine, - final ListIterator arguments) - throws OptionException { - // grab the argument to process - final String arg = (String) arguments.next(); - - // if we can process it - if (canProcess(commandLine, arg)) { - // then note the option - commandLine.addOption(this); - - // normalise the argument list - arguments.set(preferredName); - } else { - throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg); - } - } - - public Set getTriggers() { - return triggers; - } - - public void validate(WriteableCommandLine commandLine) - throws OptionException { - if (isRequired() && !commandLine.hasOption(this)) { - throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED, - getPreferredName()); - } - - super.validate(commandLine); - } - - public void appendUsage(final StringBuffer buffer, - final Set helpSettings, - final Comparator comp) { - // do we display optionality - final boolean optional = - !isRequired() && helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL); - final boolean displayAliases = helpSettings.contains(DisplaySetting.DISPLAY_ALIASES); - - if (optional) { - buffer.append('['); - } - - buffer.append(preferredName); - - if (displayAliases && !aliases.isEmpty()) { - buffer.append(" ("); - - final List list = new ArrayList(aliases); - Collections.sort(list); - - for (final Iterator i = list.iterator(); i.hasNext();) { - final String alias = (String) i.next(); - buffer.append(alias); - - if (i.hasNext()) { - buffer.append(','); - } - } - - buffer.append(')'); - } - - super.appendUsage(buffer, helpSettings, comp); - - if (optional) { - buffer.append(']'); - } - } - - public String getPreferredName() { - return preferredName; - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/option/DefaultOption.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/option/DefaultOption.java b/src/java/org/apache/commons/cli2/option/DefaultOption.java deleted file mode 100644 index b169b64..0000000 --- a/src/java/org/apache/commons/cli2/option/DefaultOption.java +++ /dev/null @@ -1,220 +0,0 @@ -/* - * Copyright 2003-2005 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.cli2.option; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Set; - -import org.apache.commons.cli2.Argument; -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.Group; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.resource.ResourceConstants; - -/** - * A Parent implementation representing normal options. - */ -public class DefaultOption - extends ParentImpl { - /** - * The default token used to prefix a short option - */ - public static final String DEFAULT_SHORT_PREFIX = "-"; - - /** - * The default token used to prefix a long option - */ - public static final String DEFAULT_LONG_PREFIX = "--"; - - /** - * The default value for the burstEnabled constructor parameter - */ - public static final boolean DEFAULT_BURST_ENABLED = true; - private final String preferredName; - private final Set aliases; - private final Set burstAliases; - private final Set triggers; - private final Set prefixes; - private final String shortPrefix; - private final boolean burstEnabled; - private final int burstLength; - - /** - * Creates a new DefaultOption - * - * @param shortPrefix the prefix used for short options - * @param longPrefix the prefix used for long options - * @param burstEnabled should option bursting be enabled - * @param preferredName the preferred name for this Option, this should begin with either shortPrefix or longPrefix - * @param description a description of this Option - * @param aliases the alternative names for this Option - * @param burstAliases the aliases that can be burst - * @param required whether the Option is strictly required - * @param argument the Argument belonging to this Parent, or null - * @param children the Group children belonging to this Parent, ot null - * @param id the unique identifier for this Option - * @throws IllegalArgumentException if the preferredName or an alias isn't - * prefixed with shortPrefix or longPrefix - */ - public DefaultOption(final String shortPrefix, - final String longPrefix, - final boolean burstEnabled, - final String preferredName, - final String description, - final Set aliases, - final Set burstAliases, - final boolean required, - final Argument argument, - final Group children, - final int id) { - super(argument, children, description, id, required); - - this.shortPrefix = shortPrefix; - this.burstEnabled = burstEnabled; - - this.burstLength = shortPrefix.length() + 1; - - this.preferredName = preferredName; - this.aliases = - (aliases == null) ? Collections.EMPTY_SET - : Collections.unmodifiableSet(new HashSet(aliases)); - - this.burstAliases = - (burstAliases == null) ? Collections.EMPTY_SET - : Collections.unmodifiableSet(new HashSet(burstAliases)); - - final Set newTriggers = new HashSet(); - newTriggers.add(preferredName); - newTriggers.addAll(this.aliases); - newTriggers.addAll(this.burstAliases); - this.triggers = Collections.unmodifiableSet(newTriggers); - - final Set newPrefixes = new HashSet(super.getPrefixes()); - newPrefixes.add(shortPrefix); - newPrefixes.add(longPrefix); - this.prefixes = Collections.unmodifiableSet(newPrefixes); - - checkPrefixes(newPrefixes); - } - - public boolean canProcess(final WriteableCommandLine commandLine, - final String argument) { - return (argument != null) && - (super.canProcess(commandLine, argument) || - ((argument.length() >= burstLength) && - burstAliases.contains(argument.substring(0, burstLength)))); - } - - public void processParent(WriteableCommandLine commandLine, - ListIterator arguments) - throws OptionException { - final String argument = (String) arguments.next(); - - if (triggers.contains(argument)) { - commandLine.addOption(this); - arguments.set(preferredName); - } else if (burstEnabled && (argument.length() >= burstLength)) { - final String burst = argument.substring(0, burstLength); - - if (burstAliases.contains(burst)) { - commandLine.addOption(this); - - //HMM test bursting all vs bursting one by one. - arguments.set(preferredName); - - if (getArgument() == null) { - arguments.add(shortPrefix + argument.substring(burstLength)); - } else { - arguments.add(argument.substring(burstLength)); - } - - arguments.previous(); - } else { - throw new OptionException(this, ResourceConstants.CANNOT_BURST, argument); - } - } else { - throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, argument); - } - } - - public Set getTriggers() { - return triggers; - } - - public Set getPrefixes() { - return prefixes; - } - - public void validate(WriteableCommandLine commandLine) - throws OptionException { - if (isRequired() && !commandLine.hasOption(this)) { - throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED, - getPreferredName()); - } - - super.validate(commandLine); - } - - public void appendUsage(final StringBuffer buffer, - final Set helpSettings, - final Comparator comp) { - // do we display optionality - final boolean optional = - !isRequired() && helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL); - final boolean displayAliases = helpSettings.contains(DisplaySetting.DISPLAY_ALIASES); - - if (optional) { - buffer.append('['); - } - - buffer.append(preferredName); - - if (displayAliases && !aliases.isEmpty()) { - buffer.append(" ("); - - final List list = new ArrayList(aliases); - Collections.sort(list); - - for (final Iterator i = list.iterator(); i.hasNext();) { - final String alias = (String) i.next(); - buffer.append(alias); - - if (i.hasNext()) { - buffer.append(','); - } - } - - buffer.append(')'); - } - - super.appendUsage(buffer, helpSettings, comp); - - if (optional) { - buffer.append(']'); - } - } - - public String getPreferredName() { - return preferredName; - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/option/GroupImpl.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/option/GroupImpl.java b/src/java/org/apache/commons/cli2/option/GroupImpl.java deleted file mode 100644 index eac83ad..0000000 --- a/src/java/org/apache/commons/cli2/option/GroupImpl.java +++ /dev/null @@ -1,516 +0,0 @@ -/* - * Copyright 2003-2005 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.cli2.option; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Map; -import java.util.Set; -import java.util.SortedMap; -import java.util.TreeMap; - -import org.apache.commons.cli2.Argument; -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.Group; -import org.apache.commons.cli2.HelpLine; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.resource.ResourceConstants; - -/** - * An implementation of Group - */ -public class GroupImpl - extends OptionImpl implements Group { - private final String name; - private final String description; - private final List options; - private final int minimum; - private final int maximum; - private final List anonymous; - private final SortedMap optionMap; - private final Set prefixes; - - /** - * Creates a new GroupImpl using the specified parameters. - * - * @param options the Options and Arguments that make up the Group - * @param name the name of this Group, or null - * @param description a description of this Group - * @param minimum the minimum number of Options for a valid CommandLine - * @param maximum the maximum number of Options for a valid CommandLine - */ - public GroupImpl(final List options, - final String name, - final String description, - final int minimum, - final int maximum) { - super(0, false); - - this.name = name; - this.description = description; - this.minimum = minimum; - this.maximum = maximum; - - // store a copy of the options to be used by the - // help methods - this.options = Collections.unmodifiableList(options); - - // anonymous Argument temporary storage - final List newAnonymous = new ArrayList(); - - // map (key=trigger & value=Option) temporary storage - final SortedMap newOptionMap = new TreeMap(ReverseStringComparator.getInstance()); - - // prefixes temporary storage - final Set newPrefixes = new HashSet(); - - // process the options - for (final Iterator i = options.iterator(); i.hasNext();) { - final Option option = (Option) i.next(); - - if (option instanceof Argument) { - i.remove(); - newAnonymous.add(option); - } else { - final Set triggers = option.getTriggers(); - - for (Iterator j = triggers.iterator(); j.hasNext();) { - newOptionMap.put(j.next(), option); - } - - // store the prefixes - newPrefixes.addAll(option.getPrefixes()); - } - } - - this.anonymous = Collections.unmodifiableList(newAnonymous); - this.optionMap = Collections.unmodifiableSortedMap(newOptionMap); - this.prefixes = Collections.unmodifiableSet(newPrefixes); - } - - public boolean canProcess(final WriteableCommandLine commandLine, - final String arg) { - if (arg == null) { - return false; - } - - // if arg does not require bursting - if (optionMap.containsKey(arg)) { - return true; - } - - // filter - final Map tailMap = optionMap.tailMap(arg); - - // check if bursting is required - for (final Iterator iter = tailMap.values().iterator(); iter.hasNext();) { - final Option option = (Option) iter.next(); - - if (option.canProcess(commandLine, arg)) { - return true; - } - } - - if (commandLine.looksLikeOption(arg)) { - return false; - } - - // anonymous argument(s) means we can process it - if (anonymous.size() > 0) { - return true; - } - - return false; - } - - public Set getPrefixes() { - return prefixes; - } - - public Set getTriggers() { - return optionMap.keySet(); - } - - public void process(final WriteableCommandLine commandLine, - final ListIterator arguments) - throws OptionException { - String previous = null; - - // [START process each command line token - while (arguments.hasNext()) { - // grab the next argument - final String arg = (String) arguments.next(); - - // if we have just tried to process this instance - if (arg == previous) { - // rollback and abort - arguments.previous(); - - break; - } - - // remember last processed instance - previous = arg; - - final Option opt = (Option) optionMap.get(arg); - - // option found - if (opt != null) { - arguments.previous(); - opt.process(commandLine, arguments); - } - // [START option NOT found - else { - // it might be an anonymous argument continue search - // [START argument may be anonymous - if (commandLine.looksLikeOption(arg)) { - // narrow the search - final Collection values = optionMap.tailMap(arg).values(); - - boolean foundMemberOption = false; - - for (Iterator i = values.iterator(); i.hasNext() && !foundMemberOption;) { - final Option option = (Option) i.next(); - - if (option.canProcess(commandLine, arg)) { - foundMemberOption = true; - arguments.previous(); - option.process(commandLine, arguments); - } - } - - // back track and abort this group if necessary - if (!foundMemberOption) { - arguments.previous(); - - return; - } - } // [END argument may be anonymous - - // [START argument is NOT anonymous - else { - // move iterator back, current value not used - arguments.previous(); - - // if there are no anonymous arguments then this group can't - // process the argument - if (anonymous.isEmpty()) { - break; - } - - // TODO: why do we iterate over all anonymous arguments? - // canProcess will always return true? - for (final Iterator i = anonymous.iterator(); i.hasNext();) { - final Argument argument = (Argument) i.next(); - - if (argument.canProcess(commandLine, arguments)) { - argument.process(commandLine, arguments); - } - } - } // [END argument is NOT anonymous - } // [END option NOT found - } // [END process each command line token - } - - public void validate(final WriteableCommandLine commandLine) - throws OptionException { - // number of options found - int present = 0; - - // reference to first unexpected option - Option unexpected = null; - - for (final Iterator i = options.iterator(); i.hasNext();) { - final Option option = (Option) i.next(); - - // if the child option is required then validate it - if (option.isRequired()) { - option.validate(commandLine); - } - - if (option instanceof Group) { - option.validate(commandLine); - } - - // if the child option is present then validate it - if (commandLine.hasOption(option)) { - if (++present > maximum) { - unexpected = option; - - break; - } - - option.validate(commandLine); - } - } - - // too many options - if (unexpected != null) { - throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, - unexpected.getPreferredName()); - } - - // too few option - if (present < minimum) { - throw new OptionException(this, ResourceConstants.MISSING_OPTION); - } - - // validate each anonymous argument - for (final Iterator i = anonymous.iterator(); i.hasNext();) { - final Option option = (Option) i.next(); - option.validate(commandLine); - } - } - - public String getPreferredName() { - return name; - } - - public String getDescription() { - return description; - } - - public void appendUsage(final StringBuffer buffer, - final Set helpSettings, - final Comparator comp) { - appendUsage(buffer, helpSettings, comp, "|"); - } - - public void appendUsage(final StringBuffer buffer, - final Set helpSettings, - final Comparator comp, - final String separator) { - final Set helpSettingsCopy = new HashSet(helpSettings); - - final boolean optional = - (minimum == 0) && helpSettingsCopy.contains(DisplaySetting.DISPLAY_OPTIONAL); - - final boolean expanded = - (name == null) || helpSettingsCopy.contains(DisplaySetting.DISPLAY_GROUP_EXPANDED); - - final boolean named = - !expanded || - ((name != null) && helpSettingsCopy.contains(DisplaySetting.DISPLAY_GROUP_NAME)); - - final boolean arguments = helpSettingsCopy.contains(DisplaySetting.DISPLAY_GROUP_ARGUMENT); - - final boolean outer = helpSettingsCopy.contains(DisplaySetting.DISPLAY_GROUP_OUTER); - - helpSettingsCopy.remove(DisplaySetting.DISPLAY_GROUP_OUTER); - - final boolean both = named && expanded; - - if (optional) { - buffer.append('['); - } - - if (named) { - buffer.append(name); - } - - if (both) { - buffer.append(" ("); - } - - if (expanded) { - final Set childSettings; - - if (!helpSettingsCopy.contains(DisplaySetting.DISPLAY_GROUP_EXPANDED)) { - childSettings = DisplaySetting.NONE; - } else { - childSettings = new HashSet(helpSettingsCopy); - childSettings.remove(DisplaySetting.DISPLAY_OPTIONAL); - } - - // grab a list of the group's options. - final List list; - - if (comp == null) { - // default to using the initial order - list = options; - } else { - // sort options if comparator is supplied - list = new ArrayList(options); - Collections.sort(list, comp); - } - - // for each option. - for (final Iterator i = list.iterator(); i.hasNext();) { - final Option option = (Option) i.next(); - - // append usage information - option.appendUsage(buffer, childSettings, comp); - - // add separators as needed - if (i.hasNext()) { - buffer.append(separator); - } - } - } - - if (both) { - buffer.append(')'); - } - - if (optional && outer) { - buffer.append(']'); - } - - if (arguments) { - for (final Iterator i = anonymous.iterator(); i.hasNext();) { - buffer.append(' '); - - final Option option = (Option) i.next(); - option.appendUsage(buffer, helpSettingsCopy, comp); - } - } - - if (optional && !outer) { - buffer.append(']'); - } - } - - public List helpLines(final int depth, - final Set helpSettings, - final Comparator comp) { - final List helpLines = new ArrayList(); - - if (helpSettings.contains(DisplaySetting.DISPLAY_GROUP_NAME)) { - final HelpLine helpLine = new HelpLineImpl(this, depth); - helpLines.add(helpLine); - } - - if (helpSettings.contains(DisplaySetting.DISPLAY_GROUP_EXPANDED)) { - // grab a list of the group's options. - final List list; - - if (comp == null) { - // default to using the initial order - list = options; - } else { - // sort options if comparator is supplied - list = new ArrayList(options); - Collections.sort(list, comp); - } - - // for each option - for (final Iterator i = list.iterator(); i.hasNext();) { - final Option option = (Option) i.next(); - helpLines.addAll(option.helpLines(depth + 1, helpSettings, comp)); - } - } - - if (helpSettings.contains(DisplaySetting.DISPLAY_GROUP_ARGUMENT)) { - for (final Iterator i = anonymous.iterator(); i.hasNext();) { - final Option option = (Option) i.next(); - helpLines.addAll(option.helpLines(depth + 1, helpSettings, comp)); - } - } - - return helpLines; - } - - /** - * Gets the member Options of thie Group. - * Note this does not include any Arguments - * @return only the non Argument Options of the Group - */ - public List getOptions() { - return options; - } - - /** - * Gets the anonymous Arguments of this Group. - * @return the Argument options of this Group - */ - public List getAnonymous() { - return anonymous; - } - - public Option findOption(final String trigger) { - final Iterator i = getOptions().iterator(); - - while (i.hasNext()) { - final Option option = (Option) i.next(); - final Option found = option.findOption(trigger); - - if (found != null) { - return found; - } - } - - return null; - } - - public int getMinimum() { - return minimum; - } - - public int getMaximum() { - return maximum; - } - - public boolean isRequired() { - return getMinimum() > 0; - } - - public void defaults(final WriteableCommandLine commandLine) { - super.defaults(commandLine); - - for (final Iterator i = options.iterator(); i.hasNext();) { - final Option option = (Option) i.next(); - option.defaults(commandLine); - } - - for (final Iterator i = anonymous.iterator(); i.hasNext();) { - final Option option = (Option) i.next(); - option.defaults(commandLine); - } - } -} - - -class ReverseStringComparator implements Comparator { - private static final Comparator instance = new ReverseStringComparator(); - - private ReverseStringComparator() { - // just making sure nobody else creates one - } - - /** - * Gets a singleton instance of a ReverseStringComparator - * @return the singleton instance - */ - public static final Comparator getInstance() { - return instance; - } - - public int compare(final Object o1, - final Object o2) { - final String s1 = (String) o1; - final String s2 = (String) o2; - - return -s1.compareTo(s2); - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/option/HelpLineImpl.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/option/HelpLineImpl.java b/src/java/org/apache/commons/cli2/option/HelpLineImpl.java deleted file mode 100644 index 70a1dd5..0000000 --- a/src/java/org/apache/commons/cli2/option/HelpLineImpl.java +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Copyright 2003-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.cli2.option; - -import java.util.Comparator; -import java.util.Set; - -import org.apache.commons.cli2.HelpLine; -import org.apache.commons.cli2.Option; - -/** - * Represents a line in the help screen. - */ -public class HelpLineImpl implements HelpLine { - - /** The option that this HelpLineImpl describes */ - private final Option option; - - /** The level of indenting for this item */ - private final int indent; - - /** The help settings used to obtain the previous usage */ - private transient Set cachedHelpSettings; - - /** The comparator used to obtain the previous usage */ - private transient Comparator cachedComparator; - - /** The previously obtained usage */ - private transient String cachedUsage; - - /** - * Creates a new HelpLineImpl to represent a particular Option in the online - * help. - * - * @param option - * Option that the HelpLineImpl describes - * @param indent - * Level of indentation for this line - */ - public HelpLineImpl(final Option option, final int indent) { - this.option = option; - this.indent = indent; - } - - /** - * @return The description of the option - */ - public String getDescription() { - return option.getDescription(); - } - - /** - * @return The level of indentation for this line - */ - public int getIndent() { - return indent; - } - - /** - * @return The Option that the help line relates to - */ - public Option getOption() { - return option; - } - - /** - * Builds a usage string for the option using the specified settings and - * comparator. - * - * - * @param helpSettings the settings to apply - * @param comparator a comparator to sort options when applicable - * @return the usage string - */ - public String usage(final Set helpSettings, final Comparator comparator) { - if (cachedUsage == null - || cachedHelpSettings != helpSettings - || cachedComparator != comparator) { - - // cache the arguments to avoid redoing work - cachedHelpSettings = helpSettings; - cachedComparator = comparator; - - // build the new buffer - final StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < indent; ++i) { - buffer.append(" "); - } - option.appendUsage(buffer, helpSettings, comparator); - - // cache the usage string - cachedUsage = buffer.toString(); - } - return cachedUsage; - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/option/OptionImpl.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/option/OptionImpl.java b/src/java/org/apache/commons/cli2/option/OptionImpl.java deleted file mode 100644 index 3b6c6be..0000000 --- a/src/java/org/apache/commons/cli2/option/OptionImpl.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright 2003-2005 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.cli2.option; - -import java.util.Iterator; -import java.util.ListIterator; -import java.util.Set; - -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; - -/** - * A base implementation of Option providing limited ground work for further - * Option implementations. - */ -public abstract class OptionImpl implements Option { - private final int id; - private final boolean required; - - /** - * Creates an OptionImpl with the specified id - * @param id the unique id of this Option - * @param required true iff this Option must be present - */ - public OptionImpl(final int id, - final boolean required) { - this.id = id; - this.required = required; - } - - public boolean canProcess(final WriteableCommandLine commandLine, - final ListIterator arguments) { - if (arguments.hasNext()) { - final String argument = (String) arguments.next(); - arguments.previous(); - - return canProcess(commandLine, argument); - } else { - return false; - } - } - - public String toString() { - final StringBuffer buffer = new StringBuffer(); - appendUsage(buffer, DisplaySetting.ALL, null); - - return buffer.toString(); - } - - public int getId() { - return id; - } - - public boolean equals(final Object thatObj) { - if (thatObj instanceof OptionImpl) { - final OptionImpl that = (OptionImpl) thatObj; - - return (getId() == that.getId()) && - equals(getPreferredName(), that.getPreferredName()) && - equals(getDescription(), that.getDescription()) && - equals(getPrefixes(), that.getPrefixes()) && - equals(getTriggers(), that.getTriggers()); - } else { - return false; - } - } - - private boolean equals(Object left, - Object right) { - if ((left == null) && (right == null)) { - return true; - } else if ((left == null) || (right == null)) { - return false; - } else { - return left.equals(right); - } - } - - public int hashCode() { - int hashCode = getId(); - hashCode = (hashCode * 37) + getPreferredName().hashCode(); - - if (getDescription() != null) { - hashCode = (hashCode * 37) + getDescription().hashCode(); - } - - hashCode = (hashCode * 37) + getPrefixes().hashCode(); - hashCode = (hashCode * 37) + getTriggers().hashCode(); - - return hashCode; - } - - public Option findOption(String trigger) { - if (getTriggers().contains(trigger)) { - return this; - } else { - return null; - } - } - - public boolean isRequired() { - return required; - } - - public void defaults(final WriteableCommandLine commandLine) { - // nothing to do normally - } - - protected void checkPrefixes(final Set prefixes) { - // nothing to do if empty prefix list - if (prefixes.isEmpty()) { - return; - } - - // check preferred name - checkPrefix(prefixes, getPreferredName()); - - // check triggers - this.getTriggers(); - - for (final Iterator i = getTriggers().iterator(); i.hasNext();) { - checkPrefix(prefixes, (String) i.next()); - } - } - - private void checkPrefix(final Set prefixes, - final String trigger) { - for (final Iterator i = prefixes.iterator(); i.hasNext();) { - String prefix = (String) i.next(); - - if (trigger.startsWith(prefix)) { - return; - } - } - - final ResourceHelper helper = ResourceHelper.getResourceHelper(); - final String message = - helper.getMessage(ResourceConstants.OPTION_TRIGGER_NEEDS_PREFIX, trigger, - prefixes.toString()); - throw new IllegalArgumentException(message); - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/option/ParentImpl.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/option/ParentImpl.java b/src/java/org/apache/commons/cli2/option/ParentImpl.java deleted file mode 100644 index 7b58a7d..0000000 --- a/src/java/org/apache/commons/cli2/option/ParentImpl.java +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Copyright 2003-2005 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.cli2.option; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.ListIterator; -import java.util.Set; - -import org.apache.commons.cli2.Argument; -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.Group; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.Parent; -import org.apache.commons.cli2.WriteableCommandLine; - -/** - * A base implementation of Parent providing limited ground work for further - * Parent implementations. - */ -public abstract class ParentImpl - extends OptionImpl implements Parent { - private static final char NUL = '\0'; - private final Group children; - private final Argument argument; - private final String description; - - protected ParentImpl(final Argument argument, - final Group children, - final String description, - final int id, - final boolean required) { - super(id, required); - this.children = children; - this.argument = argument; - this.description = description; - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.Option#process(org.apache.commons.cli2.CommandLine, - * java.util.ListIterator) - */ - public void process(final WriteableCommandLine commandLine, - final ListIterator arguments) - throws OptionException { - if (argument != null) { - handleInitialSeparator(arguments, argument.getInitialSeparator()); - } - - processParent(commandLine, arguments); - - if (argument != null) { - argument.processValues(commandLine, arguments, this); - } - - if ((children != null) && children.canProcess(commandLine, arguments)) { - children.process(commandLine, arguments); - } - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.Option#canProcess(java.lang.String) - */ - public boolean canProcess(final WriteableCommandLine commandLine, - final String arg) { - final Set triggers = getTriggers(); - - if (argument != null) { - final char separator = argument.getInitialSeparator(); - - // if there is a valid separator character - if (separator != NUL) { - final int initialIndex = arg.indexOf(separator); - - // if there is a separator present - if (initialIndex > 0) { - return triggers.contains(arg.substring(0, initialIndex)); - } - } - } - - return triggers.contains(arg); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.Option#prefixes() - */ - public Set getPrefixes() { - return (children == null) ? Collections.EMPTY_SET : children.getPrefixes(); - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.Option#validate(org.apache.commons.cli2.CommandLine) - */ - public void validate(WriteableCommandLine commandLine) - throws OptionException { - if (commandLine.hasOption(this)) { - if (argument != null) { - argument.validate(commandLine, this); - } - - if (children != null) { - children.validate(commandLine); - } - } - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.Option#appendUsage(java.lang.StringBuffer, - * java.util.Set, java.util.Comparator) - */ - public void appendUsage(final StringBuffer buffer, - final Set helpSettings, - final Comparator comp) { - final boolean displayArgument = - (this.argument != null) && - helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT); - final boolean displayChildren = - (this.children != null) && - helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN); - - if (displayArgument) { - buffer.append(' '); - argument.appendUsage(buffer, helpSettings, comp); - } - - if (displayChildren) { - buffer.append(' '); - children.appendUsage(buffer, helpSettings, comp); - } - } - - /** - * @return a description of this parent option - */ - public String getDescription() { - return description; - } - - /* - * (non-Javadoc) - * - * @see org.apache.commons.cli2.Option#helpLines(int, java.util.Set, - * java.util.Comparator) - */ - public List helpLines(final int depth, - final Set helpSettings, - final Comparator comp) { - final List helpLines = new ArrayList(); - helpLines.add(new HelpLineImpl(this, depth)); - - if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT) && (argument != null)) { - helpLines.addAll(argument.helpLines(depth + 1, helpSettings, comp)); - } - - if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN) && (children != null)) { - helpLines.addAll(children.helpLines(depth + 1, helpSettings, comp)); - } - - return helpLines; - } - - /** - * @return Returns the argument. - */ - public Argument getArgument() { - return argument; - } - - /** - * @return Returns the children. - */ - public Group getChildren() { - return children; - } - - /** - * Split the token using the specified separator character. - * @param arguments the current position in the arguments iterator - * @param separator the separator char to split on - */ - private void handleInitialSeparator(final ListIterator arguments, - final char separator) { - // next token - final String newArgument = (String) arguments.next(); - - // split the token - final int initialIndex = newArgument.indexOf(separator); - - if (initialIndex > 0) { - arguments.remove(); - arguments.add(newArgument.substring(0, initialIndex)); - arguments.add(newArgument.substring(initialIndex + 1)); - arguments.previous(); - } - - arguments.previous(); - } - - /* - * @see org.apache.commons.cli2.Option#findOption(java.lang.String) - */ - public Option findOption(final String trigger) { - final Option found = super.findOption(trigger); - - if ((found == null) && (children != null)) { - return children.findOption(trigger); - } else { - return found; - } - } - - public void defaults(final WriteableCommandLine commandLine) { - super.defaults(commandLine); - - if (argument != null) { - argument.defaultValues(commandLine, this); - } - - if (children != null) { - children.defaults(commandLine); - } - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/option/PropertyOption.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/option/PropertyOption.java b/src/java/org/apache/commons/cli2/option/PropertyOption.java deleted file mode 100644 index 7575d84..0000000 --- a/src/java/org/apache/commons/cli2/option/PropertyOption.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 2003-2005 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.cli2.option; - -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.ListIterator; -import java.util.Set; - -import org.apache.commons.cli2.DisplaySetting; -import org.apache.commons.cli2.HelpLine; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.resource.ResourceConstants; - -/** - * Handles the java style "-Dprop=value" opions - */ -public class PropertyOption - extends OptionImpl { - public static final String DEFAULT_OPTION_STRING = "-D"; - public static final String DEFAULT_DESCRIPTION = - "Passes properties and values to the application"; - - /** - * A default PropertyOption instance - */ - public static final PropertyOption INSTANCE = new PropertyOption(); - private final String optionString; - private final String description; - private final Set prefixes; - - /** - * Creates a new PropertyOption using the default settings of a "-D" trigger - * and an id of 'D' - */ - public PropertyOption() { - this(DEFAULT_OPTION_STRING, DEFAULT_DESCRIPTION, 'D'); - } - - /** - * Creates a new PropertyOption using the specified parameters - * @param optionString the trigger for the Option - * @param description the description of the Option - * @param id the id of the Option - */ - public PropertyOption(final String optionString, - final String description, - final int id) { - super(id, false); - this.optionString = optionString; - this.description = description; - this.prefixes = Collections.singleton(optionString); - } - - public boolean canProcess(final WriteableCommandLine commandLine, - final String argument) { - return (argument != null) && argument.startsWith(optionString) && - (argument.length() > optionString.length()); - } - - public Set getPrefixes() { - return prefixes; - } - - public void process(final WriteableCommandLine commandLine, - final ListIterator arguments) - throws OptionException { - final String arg = (String) arguments.next(); - - if (!canProcess(commandLine, arg)) { - throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg); - } - - final int propertyStart = optionString.length(); - final int equalsIndex = arg.indexOf('=', propertyStart); - final String property; - final String value; - - if (equalsIndex < 0) { - property = arg.substring(propertyStart); - value = "true"; - } else { - property = arg.substring(propertyStart, equalsIndex); - value = arg.substring(equalsIndex + 1); - } - - commandLine.addProperty(property, value); - } - - public Set getTriggers() { - return Collections.singleton(optionString); - } - - public void validate(WriteableCommandLine commandLine) { - // PropertyOption needs no validation - } - - public void appendUsage(final StringBuffer buffer, - final Set helpSettings, - final Comparator comp) { - final boolean display = helpSettings.contains(DisplaySetting.DISPLAY_PROPERTY_OPTION); - - final boolean bracketed = helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED); - - if (display) { - buffer.append(optionString); - - if (bracketed) { - buffer.append('<'); - } - - buffer.append("property"); - - if (bracketed) { - buffer.append('>'); - } - - buffer.append("="); - - if (bracketed) { - buffer.append('<'); - } - - buffer.append("value"); - - if (bracketed) { - buffer.append('>'); - } - } - } - - public String getPreferredName() { - return optionString; - } - - public String getDescription() { - return description; - } - - public List helpLines(final int depth, - final Set helpSettings, - final Comparator comp) { - if (helpSettings.contains(DisplaySetting.DISPLAY_PROPERTY_OPTION)) { - final HelpLine helpLine = new HelpLineImpl(this, depth); - - return Collections.singletonList(helpLine); - } else { - return Collections.EMPTY_LIST; - } - } -} http://git-wip-us.apache.org/repos/asf/commons-cli/blob/9e65354f/src/java/org/apache/commons/cli2/option/SourceDestArgument.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/cli2/option/SourceDestArgument.java b/src/java/org/apache/commons/cli2/option/SourceDestArgument.java deleted file mode 100644 index d6d440d..0000000 --- a/src/java/org/apache/commons/cli2/option/SourceDestArgument.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2003-2005 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.cli2.option; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import org.apache.commons.cli2.Argument; -import org.apache.commons.cli2.Option; -import org.apache.commons.cli2.OptionException; -import org.apache.commons.cli2.WriteableCommandLine; -import org.apache.commons.cli2.resource.ResourceConstants; -import org.apache.commons.cli2.resource.ResourceHelper; - -/** - * An Argument implementation that allows a variable size Argument to precede a - * fixed size argument. The canonical example of it's use is in the unix - * <code>cp</code> command where a number of source can be specified with - * exactly one destination specfied at the end. - */ -public class SourceDestArgument - extends ArgumentImpl { - private final Argument source; - private final Argument dest; - - /** - * Creates a SourceDestArgument using defaults where possible. - * - * @param source the variable size Argument - * @param dest the fixed size Argument - */ - public SourceDestArgument(final Argument source, - final Argument dest) { - this(source, dest, DEFAULT_INITIAL_SEPARATOR, DEFAULT_SUBSEQUENT_SEPARATOR, - DEFAULT_CONSUME_REMAINING, null); - } - - /** - * Creates a SourceDestArgument using the specified parameters. - * - * @param source the variable size Argument - * @param dest the fixed size Argument - * @param initialSeparator the inistial separator to use - * @param subsequentSeparator the subsequent separator to use - * @param consumeRemaining the token triggering consume remaining behaviour - * @param defaultValues the default values for the SourceDestArgument - */ - public SourceDestArgument(final Argument source, - final Argument dest, - final char initialSeparator, - final char subsequentSeparator, - final String consumeRemaining, - final List defaultValues) { - super("SourceDestArgument", null, sum(source.getMinimum(), dest.getMinimum()), - sum(source.getMaximum(), dest.getMaximum()), initialSeparator, subsequentSeparator, - null, consumeRemaining, defaultValues, 0); - - this.source = source; - this.dest = dest; - - if (dest.getMinimum() != dest.getMaximum()) { - throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SOURCE_DEST_MUST_ENFORCE_VALUES)); - } - } - - private static int sum(final int a, - final int b) { - return Math.max(a, Math.max(b, a + b)); - } - - public void appendUsage(final StringBuffer buffer, - final Set helpSettings, - final Comparator comp) { - final int length = buffer.length(); - - source.appendUsage(buffer, helpSettings, comp); - - if (buffer.length() != length) { - buffer.append(' '); - } - - dest.appendUsage(buffer, helpSettings, comp); - } - - public List helpLines(int depth, - Set helpSettings, - Comparator comp) { - final List helpLines = new ArrayList(); - helpLines.addAll(source.helpLines(depth, helpSettings, comp)); - helpLines.addAll(dest.helpLines(depth, helpSettings, comp)); - - return helpLines; - } - - public void validate(WriteableCommandLine commandLine, - Option option) - throws OptionException { - final List values = commandLine.getValues(option); - - final int limit = values.size() - dest.getMinimum(); - int count = 0; - - final Iterator i = values.iterator(); - - while (count++ < limit) { - commandLine.addValue(source, i.next()); - } - - while (i.hasNext()) { - commandLine.addValue(dest, i.next()); - } - - source.validate(commandLine, source); - dest.validate(commandLine, dest); - } - - public boolean canProcess(final WriteableCommandLine commandLine, - final String arg) { - return source.canProcess(commandLine, arg) || dest.canProcess(commandLine, arg); - } -}
