You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2008/03/22 04:08:35 UTC
svn commit: r639943 [2/17] - in /commons/proper/cli/trunk/src:
java/org/apache/commons/cli2/ java/org/apache/commons/cli2/builder/
java/org/apache/commons/cli2/commandline/
java/org/apache/commons/cli2/option/ java/org/apache/commons/cli2/resource/
jav...
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java Fri Mar 21 20:08:23 2008
@@ -1 +1,286 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.builder;import java.util.ArrayList;import java.util.List;import org.apache.commons.cli2.Argument;import org.apache.commons.cli2.option.ArgumentImpl;import org.apache.commo
ns.cli2.resource.ResourceConstants;import org.apache.commons.cli2.resource.ResourceHelper;import org.apache.commons.cli2.validation.Validator;/** * Builds Argument instances. */public class ArgumentBuilder { /** i18n */ private final static ResourceHelper resources = ResourceHelper.getResourceHelper(); /** name of the argument. Used for display and lookups in CommandLine */ private String name; /** description of the argument. Used in the automated online help */ private String description; /** minimum number of values required */ private int minimum; /** maximum number of values permitted */ private int maximum; /** character used to separate the values from the option */ private char initialSeparator; /** character used to separate the values from each other */ private char subsequentSeparator; /** object that should be used to ensure the values are valid */ private Validator validator; /** used to identify the consume remaining
option, typically "--" */ private String consumeRemaining; /** default values for argument */ private List defaultValues; /** id of the argument */ private int id; /** * Creates a new ArgumentBuilder instance */ public ArgumentBuilder() { reset(); } /** * Creates a new Argument instance using the options specified in this * ArgumentBuilder. * * @return A new Argument instance using the options specified in this * ArgumentBuilder. */ public final Argument create() { final Argument argument = new ArgumentImpl( name, description, minimum, maximum, initialSeparator, subsequentSeparator, validator, consumeRemaining, defaultValues, id); reset(); return argument; } /** * Resets the ArgumentBuilder to the defaults for a new A
rgument. The * method is called automatically at the end of a create() call. */ public final ArgumentBuilder reset() { name = "arg"; description = null; minimum = 0; maximum = Integer.MAX_VALUE; initialSeparator = ArgumentImpl.DEFAULT_INITIAL_SEPARATOR; subsequentSeparator = ArgumentImpl.DEFAULT_SUBSEQUENT_SEPARATOR; validator = null; consumeRemaining = "--"; defaultValues = null; id = 0; return this; } /** * Sets the name of the argument. The name is used when displaying usage * information and to allow lookups in the CommandLine object. * * @see org.apache.commons.cli2.CommandLine#getValue(String) * * @param newName the name of the argument * @return this ArgumentBuilder */ public final ArgumentBuilder withName(final String newName) { if (newName == null) { throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMEN
T_BUILDER_NULL_NAME)); } if ("".equals(newName)) { throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME)); } this.name = newName; return this; } /** * Sets the description of the argument. * * The description is used when displaying online help. * * @param newDescription a description of the argument * @return this ArgumentBuilder */ public final ArgumentBuilder withDescription(final String newDescription) { this.description = newDescription; return this; } /** * Sets the minimum number of values needed for the argument to be valid. * * @param newMinimum the number of values needed * @return this ArgumentBuilder */ public final ArgumentBuilder withMinimum(final int newMinimum) { if (newMinimum < 0) { throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM));
} this.minimum = newMinimum; return this; } /** * Sets the maximum number of values allowed for the argument to be valid. * * @param newMaximum the number of values allowed * @return this ArgumentBuilder */ public final ArgumentBuilder withMaximum(final int newMaximum) { if (newMaximum < 0) { throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM)); } this.maximum = newMaximum; return this; } /** * Sets the character used to separate the values from the option. When an * argument is of the form -libs:dir1,dir2,dir3 the initialSeparator would * be ':'. * * @param newInitialSeparator the character used to separate the values * from the option * @return this ArgumentBuilder */ public final ArgumentBuilder withInitialSeparator( final char newInitialSeparator) { this.initialSeparator = newInitialSepa
rator; return this; } /** * Sets the character used to separate the values from each other. When an * argument is of the form -libs:dir1,dir2,dir3 the subsequentSeparator * would be ','. * * @param newSubsequentSeparator the character used to separate the values * from each other * @return this ArgumentBuilder */ public final ArgumentBuilder withSubsequentSeparator( final char newSubsequentSeparator) { this.subsequentSeparator = newSubsequentSeparator; return this; } /** * Sets the validator instance used to perform validation on the Argument * values. * * @param newValidator a Validator instance * @return this ArgumentBuilder */ public final ArgumentBuilder withValidator(final Validator newValidator) { if (newValidator == null) { throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR)); } this.validator
= newValidator; return this; } /** * Sets the "consume remaining" option, defaults to "--". Use this if you * want to allow values that might be confused with option strings. * * @param newConsumeRemaining the string to use for the consume * remaining option * @return this ArgumentBuilder */ public final ArgumentBuilder withConsumeRemaining(final String newConsumeRemaining) { if (newConsumeRemaining == null) { throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING)); } if ( "".equals(newConsumeRemaining)) { throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING)); } this.consumeRemaining = newConsumeRemaining; return this; } /** * Sets the default value. * * @param defaultValue the default value for the Argument * @return this ArgumentBuilder */ p
ublic final ArgumentBuilder withDefault(final Object defaultValue) { if (defaultValue == null) { throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT)); } if (this.defaultValues == null) { this.defaultValues = new ArrayList(1); } this.defaultValues.add(defaultValue); return this; } /** * Sets the default values. * * @param newDefaultValues the default values for the Argument * @return this ArgumentBuilder */ public final ArgumentBuilder withDefaults(final List newDefaultValues) { if (newDefaultValues == null) { throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS)); } this.defaultValues = newDefaultValues; return this; } /** * Sets the id * * @param newId the id of the Argument * @return this ArgumentBuilder */ public final ArgumentBuilder
withId(final int newId) { this.id = newId; return this; }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.builder;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.cli2.Argument;
+import org.apache.commons.cli2.option.ArgumentImpl;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
+import org.apache.commons.cli2.validation.Validator;
+
+/**
+ * Builds Argument instances.
+ */
+public class ArgumentBuilder {
+
+ /** i18n */
+ private final static ResourceHelper resources = ResourceHelper.getResourceHelper();
+
+ /** name of the argument. Used for display and lookups in CommandLine */
+ private String name;
+
+ /** description of the argument. Used in the automated online help */
+ private String description;
+
+ /** minimum number of values required */
+ private int minimum;
+
+ /** maximum number of values permitted */
+ private int maximum;
+
+ /** character used to separate the values from the option */
+ private char initialSeparator;
+
+ /** character used to separate the values from each other */
+ private char subsequentSeparator;
+
+ /** object that should be used to ensure the values are valid */
+ private Validator validator;
+
+ /** used to identify the consume remaining option, typically "--" */
+ private String consumeRemaining;
+
+ /** default values for argument */
+ private List defaultValues;
+
+ /** id of the argument */
+ private int id;
+
+ /**
+ * Creates a new ArgumentBuilder instance
+ */
+ public ArgumentBuilder() {
+ reset();
+ }
+
+ /**
+ * Creates a new Argument instance using the options specified in this
+ * ArgumentBuilder.
+ *
+ * @return A new Argument instance using the options specified in this
+ * ArgumentBuilder.
+ */
+ public final Argument create() {
+ final Argument argument =
+ new ArgumentImpl(
+ name,
+ description,
+ minimum,
+ maximum,
+ initialSeparator,
+ subsequentSeparator,
+ validator,
+ consumeRemaining,
+ defaultValues,
+ id);
+
+ reset();
+
+ return argument;
+ }
+
+ /**
+ * Resets the ArgumentBuilder to the defaults for a new Argument. The
+ * method is called automatically at the end of a create() call.
+ */
+ public final ArgumentBuilder reset() {
+ name = "arg";
+ description = null;
+ minimum = 0;
+ maximum = Integer.MAX_VALUE;
+ initialSeparator = ArgumentImpl.DEFAULT_INITIAL_SEPARATOR;
+ subsequentSeparator = ArgumentImpl.DEFAULT_SUBSEQUENT_SEPARATOR;
+ validator = null;
+ consumeRemaining = "--";
+ defaultValues = null;
+ id = 0;
+ return this;
+ }
+
+ /**
+ * Sets the name of the argument. The name is used when displaying usage
+ * information and to allow lookups in the CommandLine object.
+ *
+ * @see org.apache.commons.cli2.CommandLine#getValue(String)
+ *
+ * @param newName the name of the argument
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withName(final String newName) {
+ if (newName == null) {
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_NAME));
+ }
+ if ("".equals(newName)) {
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME));
+ }
+ this.name = newName;
+ return this;
+ }
+
+ /**
+ * Sets the description of the argument.
+ *
+ * The description is used when displaying online help.
+ *
+ * @param newDescription a description of the argument
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withDescription(final String newDescription) {
+ this.description = newDescription;
+ return this;
+ }
+
+ /**
+ * Sets the minimum number of values needed for the argument to be valid.
+ *
+ * @param newMinimum the number of values needed
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withMinimum(final int newMinimum) {
+ if (newMinimum < 0) {
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM));
+ }
+ this.minimum = newMinimum;
+ return this;
+ }
+
+ /**
+ * Sets the maximum number of values allowed for the argument to be valid.
+ *
+ * @param newMaximum the number of values allowed
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withMaximum(final int newMaximum) {
+ if (newMaximum < 0) {
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM));
+ }
+ this.maximum = newMaximum;
+ return this;
+ }
+
+ /**
+ * Sets the character used to separate the values from the option. When an
+ * argument is of the form -libs:dir1,dir2,dir3 the initialSeparator would
+ * be ':'.
+ *
+ * @param newInitialSeparator the character used to separate the values
+ * from the option
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withInitialSeparator(
+ final char newInitialSeparator) {
+
+ this.initialSeparator = newInitialSeparator;
+ return this;
+ }
+
+ /**
+ * Sets the character used to separate the values from each other. When an
+ * argument is of the form -libs:dir1,dir2,dir3 the subsequentSeparator
+ * would be ','.
+ *
+ * @param newSubsequentSeparator the character used to separate the values
+ * from each other
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withSubsequentSeparator(
+ final char newSubsequentSeparator) {
+
+ this.subsequentSeparator = newSubsequentSeparator;
+ return this;
+ }
+
+ /**
+ * Sets the validator instance used to perform validation on the Argument
+ * values.
+ *
+ * @param newValidator a Validator instance
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withValidator(final Validator newValidator) {
+ if (newValidator == null) {
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR));
+ }
+ this.validator = newValidator;
+ return this;
+ }
+
+ /**
+ * Sets the "consume remaining" option, defaults to "--". Use this if you
+ * want to allow values that might be confused with option strings.
+ *
+ * @param newConsumeRemaining the string to use for the consume
+ * remaining option
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withConsumeRemaining(final String newConsumeRemaining) {
+ if (newConsumeRemaining == null) {
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING));
+ }
+ if ( "".equals(newConsumeRemaining)) {
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING));
+ }
+ this.consumeRemaining = newConsumeRemaining;
+ return this;
+ }
+
+ /**
+ * Sets the default value.
+ *
+ * @param defaultValue the default value for the Argument
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withDefault(final Object defaultValue) {
+ if (defaultValue == null) {
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT));
+ }
+
+ if (this.defaultValues == null) {
+ this.defaultValues = new ArrayList(1);
+ }
+ this.defaultValues.add(defaultValue);
+ return this;
+ }
+
+ /**
+ * Sets the default values.
+ *
+ * @param newDefaultValues the default values for the Argument
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withDefaults(final List newDefaultValues) {
+ if (newDefaultValues == null) {
+ throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS));
+ }
+ this.defaultValues = newDefaultValues;
+ return this;
+ }
+
+ /**
+ * Sets the id
+ *
+ * @param newId the id of the Argument
+ * @return this ArgumentBuilder
+ */
+ public final ArgumentBuilder withId(final int newId) {
+ this.id = newId;
+ return this;
+ }
+}
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/CommandBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/CommandBuilder.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/CommandBuilder.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/CommandBuilder.java Fri Mar 21 20:08:23 2008
@@ -1 +1,187 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.builder;import java.util.HashSet;import java.util.Set;import org.apache.commons.cli2.Argument;import org.apache.commons.cli2.Group;import org.apache.commons.cli2.option.Co
mmand;import org.apache.commons.cli2.resource.ResourceConstants;import org.apache.commons.cli2.resource.ResourceHelper;/** * Builds Command instances */public class CommandBuilder { /** the preferred name of the command */ private String preferredName; /** the description of the command */ private String description; /** the aliases of the command */ private Set aliases; /** whether the command is required or not */ private boolean required; /** the argument of the command */ private Argument argument; /** the children of the command */ private Group children; /** the id of the command */ private int id; /** * Creates a new <code>CommandBuilder</code> instance. */ public CommandBuilder() { reset(); } /** * Creates a new <code>Command</code> instance using the properties of the * <code>CommandBuilder</code>. * * @return the new Command instance */ public Command create() { // ch
eck we have a valid name if (preferredName == null) { throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME)); } // build the command final Command option = new Command(preferredName, description, aliases, required, argument, children, id); // reset the builder reset(); return option; } /** * Resets the CommandBuilder to the defaults for a new Command. * * This method is called automatically at the end of the * {@link #create() create} method. */ public CommandBuilder reset() { preferredName = null; description = null; aliases = new HashSet(); required = false; argument = null; children = null; id = 0; return this; } /** * Specifies the name for the next <code>Command</code> * that is created. The first name is used as the preferred * display name
for the <code>Command</code> and then * later names are used as aliases. * * @param name the name for the next <code>Command</code> * that is created. * @return this <code>CommandBuilder</code>. */ public CommandBuilder withName(final String name) { if (preferredName == null) { preferredName = name; } else { aliases.add(name); } return this; } /** * Specifies the description for the next <code>Command</code> * that is created. This description is used to produce * help documentation for the <code>Command</code>. * * @param newDescription the description for the next * <code>Command</code> that is created. * @return this <code>CommandBuilder</code>. */ public CommandBuilder withDescription(final String newDescription) { this.description = newDescription; return this; } /** * Specifies whether the next <code>Command</code> created is
* required or not. * @param newRequired whether the next <code>Command</code> created is * required or not. * @return this <code>CommandBuilder</code>. */ public CommandBuilder withRequired(final boolean newRequired) { this.required = newRequired; return this; } /** * Specifies the children for the next <code>Command</code> * that is created. * * @param newChildren the child options for the next <code>Command</code> * that is created. * @return this <code>CommandBuilder</code>. */ public CommandBuilder withChildren(final Group newChildren) { this.children = newChildren; return this; } /** * Specifies the argument for the next <code>Command</code> * that is created. * * @param newArgument the argument for the next <code>Command</code> * that is created. * @return this <code>CommandBuilder</code>. */ public CommandBuilder withArgument(final Argument newAr
gument) { this.argument = newArgument; return this; } /** * Specifies the id for the next <code>Command</code> that is created. * * @param newId the id for the next <code>Command</code> that is created. * @return this <code>CommandBuilder</code>. */ public final CommandBuilder withId(final int newId) { this.id = newId; return this; }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.builder;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.commons.cli2.Argument;
+import org.apache.commons.cli2.Group;
+import org.apache.commons.cli2.option.Command;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
+
+/**
+ * Builds Command instances
+ */
+public class CommandBuilder {
+ /** the preferred name of the command */
+ private String preferredName;
+
+ /** the description of the command */
+ private String description;
+
+ /** the aliases of the command */
+ private Set aliases;
+
+ /** whether the command is required or not */
+ private boolean required;
+
+ /** the argument of the command */
+ private Argument argument;
+
+ /** the children of the command */
+ private Group children;
+
+ /** the id of the command */
+ private int id;
+
+ /**
+ * Creates a new <code>CommandBuilder</code> instance.
+ */
+ public CommandBuilder() {
+ reset();
+ }
+
+ /**
+ * Creates a new <code>Command</code> instance using the properties of the
+ * <code>CommandBuilder</code>.
+ *
+ * @return the new Command instance
+ */
+ public Command create() {
+ // check we have a valid name
+ if (preferredName == null) {
+ throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME));
+ }
+
+ // build the command
+ final Command option =
+ new Command(preferredName, description, aliases, required, argument, children, id);
+
+ // reset the builder
+ reset();
+
+ return option;
+ }
+
+ /**
+ * Resets the CommandBuilder to the defaults for a new Command.
+ *
+ * This method is called automatically at the end of the
+ * {@link #create() create} method.
+ */
+ public CommandBuilder reset() {
+ preferredName = null;
+ description = null;
+ aliases = new HashSet();
+ required = false;
+ argument = null;
+ children = null;
+ id = 0;
+
+ return this;
+ }
+
+ /**
+ * Specifies the name for the next <code>Command</code>
+ * that is created. The first name is used as the preferred
+ * display name for the <code>Command</code> and then
+ * later names are used as aliases.
+ *
+ * @param name the name for the next <code>Command</code>
+ * that is created.
+ * @return this <code>CommandBuilder</code>.
+ */
+ public CommandBuilder withName(final String name) {
+ if (preferredName == null) {
+ preferredName = name;
+ } else {
+ aliases.add(name);
+ }
+
+ return this;
+ }
+
+ /**
+ * Specifies the description for the next <code>Command</code>
+ * that is created. This description is used to produce
+ * help documentation for the <code>Command</code>.
+ *
+ * @param newDescription the description for the next
+ * <code>Command</code> that is created.
+ * @return this <code>CommandBuilder</code>.
+ */
+ public CommandBuilder withDescription(final String newDescription) {
+ this.description = newDescription;
+
+ return this;
+ }
+
+ /**
+ * Specifies whether the next <code>Command</code> created is
+ * required or not.
+ * @param newRequired whether the next <code>Command</code> created is
+ * required or not.
+ * @return this <code>CommandBuilder</code>.
+ */
+ public CommandBuilder withRequired(final boolean newRequired) {
+ this.required = newRequired;
+
+ return this;
+ }
+
+ /**
+ * Specifies the children for the next <code>Command</code>
+ * that is created.
+ *
+ * @param newChildren the child options for the next <code>Command</code>
+ * that is created.
+ * @return this <code>CommandBuilder</code>.
+ */
+ public CommandBuilder withChildren(final Group newChildren) {
+ this.children = newChildren;
+
+ return this;
+ }
+
+ /**
+ * Specifies the argument for the next <code>Command</code>
+ * that is created.
+ *
+ * @param newArgument the argument for the next <code>Command</code>
+ * that is created.
+ * @return this <code>CommandBuilder</code>.
+ */
+ public CommandBuilder withArgument(final Argument newArgument) {
+ this.argument = newArgument;
+
+ return this;
+ }
+
+ /**
+ * Specifies the id for the next <code>Command</code> that is created.
+ *
+ * @param newId the id for the next <code>Command</code> that is created.
+ * @return this <code>CommandBuilder</code>.
+ */
+ public final CommandBuilder withId(final int newId) {
+ this.id = newId;
+
+ return this;
+ }
+}
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java Fri Mar 21 20:08:23 2008
@@ -1 +1,215 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.builder;import java.util.HashSet;import java.util.Set;import org.apache.commons.cli2.Argument;import org.apache.commons.cli2.Group;import org.apache.commons.cli2.option.De
faultOption;import org.apache.commons.cli2.resource.ResourceConstants;import org.apache.commons.cli2.resource.ResourceHelper;/** * Builds DefaultOption instances. */public class DefaultOptionBuilder { private final String shortPrefix; private final String longPrefix; private final boolean burstEnabled; private String preferredName; private Set aliases; private Set burstAliases; private boolean required; private String description; private Argument argument; private Group children; private int id; /** * Creates a new DefaultOptionBuilder using defaults * @see DefaultOption#DEFAULT_SHORT_PREFIX * @see DefaultOption#DEFAULT_LONG_PREFIX * @see DefaultOption#DEFAULT_BURST_ENABLED */ public DefaultOptionBuilder() { this(DefaultOption.DEFAULT_SHORT_PREFIX, DefaultOption.DEFAULT_LONG_PREFIX, DefaultOption.DEFAULT_BURST_ENABLED); } /** * Creates a new DefaultOptionBuilder * @param shortPrefix
the prefix to use for short options * @param longPrefix the prefix to use for long options * @param burstEnabled whether to allow gnu style bursting * @throws IllegalArgumentException if either prefix is less than on * character long */ public DefaultOptionBuilder(final String shortPrefix, final String longPrefix, final boolean burstEnabled) throws IllegalArgumentException { if ((shortPrefix == null) || (shortPrefix.length() == 0)) { throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX)); } if ((longPrefix == null) || (longPrefix.length() == 0)) { throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX)); } this.shortPrefix = shortPrefix; this.long
Prefix = longPrefix; this.burstEnabled = burstEnabled; reset(); } /** * Creates a DefaultOption instance * @return the new instance * @throws IllegalStateException if no names have been supplied */ public DefaultOption create() throws IllegalStateException { if (preferredName == null) { throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME)); } final DefaultOption option = new DefaultOption(shortPrefix, longPrefix, burstEnabled, preferredName, description, aliases, burstAliases, required, argument, children, id); reset(); return option; } /** * Resets the builder */ public DefaultOptionBuilder reset() { preferredName = null; description = null; aliases = new HashSet(); burstAliases = new HashSet(); required = false; argument = null
; children = null; id = 0; return this; } /** * Use this short option name. The first name is used as the preferred * display name for the Command and then later names are used as aliases. * * @param shortName the name to use * @return this builder */ public DefaultOptionBuilder withShortName(final String shortName) { final String name = shortPrefix + shortName; if (preferredName == null) { preferredName = name; } else { aliases.add(name); } if (burstEnabled && (name.length() == (shortPrefix.length() + 1))) { burstAliases.add(name); } return this; } /** * Use this long option name. The first name is used as the preferred * display name for the Command and then later names are used as aliases. * * @param longName the name to use * @return this builder */ public DefaultOptionBuilder withLongName(final String l
ongName) { final String name = longPrefix + longName; if (preferredName == null) { preferredName = name; } else { aliases.add(name); } return this; } /** * Use this option description * @param newDescription the description to use * @return this builder */ public DefaultOptionBuilder withDescription(final String newDescription) { this.description = newDescription; return this; } /** * Use this optionality * @param newRequired true iff the Option is required * @return this builder */ public DefaultOptionBuilder withRequired(final boolean newRequired) { this.required = newRequired; return this; } /** * Use this child Group * @param newChildren the child Group to use * @return this builder */ public DefaultOptionBuilder withChildren(final Group newChildren) { this.children = newChildren; return this; }
/** * Use this Argument * @param newArgument the argument to use * @return this builder */ public DefaultOptionBuilder withArgument(final Argument newArgument) { this.argument = newArgument; return this; } /** * Sets the id * * @param newId * the id of the DefaultOption * @return this DefaultOptionBuilder */ public final DefaultOptionBuilder withId(final int newId) { this.id = newId; return this; }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.builder;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.commons.cli2.Argument;
+import org.apache.commons.cli2.Group;
+import org.apache.commons.cli2.option.DefaultOption;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
+
+/**
+ * Builds DefaultOption instances.
+ */
+public class DefaultOptionBuilder {
+ private final String shortPrefix;
+ private final String longPrefix;
+ private final boolean burstEnabled;
+ private String preferredName;
+ private Set aliases;
+ private Set burstAliases;
+ private boolean required;
+ private String description;
+ private Argument argument;
+ private Group children;
+ private int id;
+
+ /**
+ * Creates a new DefaultOptionBuilder using defaults
+ * @see DefaultOption#DEFAULT_SHORT_PREFIX
+ * @see DefaultOption#DEFAULT_LONG_PREFIX
+ * @see DefaultOption#DEFAULT_BURST_ENABLED
+ */
+ public DefaultOptionBuilder() {
+ this(DefaultOption.DEFAULT_SHORT_PREFIX, DefaultOption.DEFAULT_LONG_PREFIX,
+ DefaultOption.DEFAULT_BURST_ENABLED);
+ }
+
+ /**
+ * Creates a new DefaultOptionBuilder
+ * @param shortPrefix the prefix to use for short options
+ * @param longPrefix the prefix to use for long options
+ * @param burstEnabled whether to allow gnu style bursting
+ * @throws IllegalArgumentException if either prefix is less than on
+ * character long
+ */
+ public DefaultOptionBuilder(final String shortPrefix,
+ final String longPrefix,
+ final boolean burstEnabled)
+ throws IllegalArgumentException {
+ if ((shortPrefix == null) || (shortPrefix.length() == 0)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX));
+ }
+
+ if ((longPrefix == null) || (longPrefix.length() == 0)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX));
+ }
+
+ this.shortPrefix = shortPrefix;
+ this.longPrefix = longPrefix;
+ this.burstEnabled = burstEnabled;
+ reset();
+ }
+
+ /**
+ * Creates a DefaultOption instance
+ * @return the new instance
+ * @throws IllegalStateException if no names have been supplied
+ */
+ public DefaultOption create()
+ throws IllegalStateException {
+ if (preferredName == null) {
+ throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME));
+ }
+
+ final DefaultOption option =
+ new DefaultOption(shortPrefix, longPrefix, burstEnabled, preferredName, description,
+ aliases, burstAliases, required, argument, children, id);
+
+ reset();
+
+ return option;
+ }
+
+ /**
+ * Resets the builder
+ */
+ public DefaultOptionBuilder reset() {
+ preferredName = null;
+ description = null;
+ aliases = new HashSet();
+ burstAliases = new HashSet();
+ required = false;
+ argument = null;
+ children = null;
+ id = 0;
+
+ return this;
+ }
+
+ /**
+ * Use this short option name. The first name is used as the preferred
+ * display name for the Command and then later names are used as aliases.
+ *
+ * @param shortName the name to use
+ * @return this builder
+ */
+ public DefaultOptionBuilder withShortName(final String shortName) {
+ final String name = shortPrefix + shortName;
+
+ if (preferredName == null) {
+ preferredName = name;
+ } else {
+ aliases.add(name);
+ }
+
+ if (burstEnabled && (name.length() == (shortPrefix.length() + 1))) {
+ burstAliases.add(name);
+ }
+
+ return this;
+ }
+
+ /**
+ * Use this long option name. The first name is used as the preferred
+ * display name for the Command and then later names are used as aliases.
+ *
+ * @param longName the name to use
+ * @return this builder
+ */
+ public DefaultOptionBuilder withLongName(final String longName) {
+ final String name = longPrefix + longName;
+
+ if (preferredName == null) {
+ preferredName = name;
+ } else {
+ aliases.add(name);
+ }
+
+ return this;
+ }
+
+ /**
+ * Use this option description
+ * @param newDescription the description to use
+ * @return this builder
+ */
+ public DefaultOptionBuilder withDescription(final String newDescription) {
+ this.description = newDescription;
+
+ return this;
+ }
+
+ /**
+ * Use this optionality
+ * @param newRequired true iff the Option is required
+ * @return this builder
+ */
+ public DefaultOptionBuilder withRequired(final boolean newRequired) {
+ this.required = newRequired;
+
+ return this;
+ }
+
+ /**
+ * Use this child Group
+ * @param newChildren the child Group to use
+ * @return this builder
+ */
+ public DefaultOptionBuilder withChildren(final Group newChildren) {
+ this.children = newChildren;
+
+ return this;
+ }
+
+ /**
+ * Use this Argument
+ * @param newArgument the argument to use
+ * @return this builder
+ */
+ public DefaultOptionBuilder withArgument(final Argument newArgument) {
+ this.argument = newArgument;
+
+ return this;
+ }
+
+ /**
+ * Sets the id
+ *
+ * @param newId
+ * the id of the DefaultOption
+ * @return this DefaultOptionBuilder
+ */
+ public final DefaultOptionBuilder withId(final int newId) {
+ this.id = newId;
+
+ return this;
+ }
+}
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/GroupBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/GroupBuilder.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/GroupBuilder.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/GroupBuilder.java Fri Mar 21 20:08:23 2008
@@ -1 +1,118 @@
-/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.builder;import java.util.ArrayList;import java.util.List;import org.apache.commons.cli2.Group;import org.apache.commons.cli2.Option;import org.apache.commons.cli2.option.
GroupImpl;/** * Builds Group instances */public class GroupBuilder { private String name; private String description; private List options; private int minimum; private int maximum; /** * Creates a new GroupBuilder */ public GroupBuilder() { reset(); } /** * Creates a new Group instance * @return the new Group instance */ public Group create() { final GroupImpl group = new GroupImpl(options, name, description, minimum, maximum); reset(); return group; } /** * Resets the builder */ public GroupBuilder reset() { name = null; description = null; options = new ArrayList(); minimum = 0; maximum = Integer.MAX_VALUE; return this; } /** * Use this option description * @param newDescription the description to use * @return this builder */ public GroupBuilder withDescription(final String newDescription) { th
is.description = newDescription; return this; } /** * Use this option name * @param newName the name to use * @return this builder */ public GroupBuilder withName(final String newName) { this.name = newName; return this; } /** * A valid group requires at least this many options present * @param newMinimum the minimum Options required * @return this builder */ public GroupBuilder withMinimum(final int newMinimum) { this.minimum = newMinimum; return this; } /** * A valid group requires at most this many options present * @param newMaximum the maximum Options allowed * @return this builder */ public GroupBuilder withMaximum(final int newMaximum) { this.maximum = newMaximum; return this; } /** * Add this option to the group * @param option the Option to add * @return this builder */ public GroupBuilder withOption(final Option option
) { this.options.add(option); return this; }}
\ No newline at end of file
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.builder;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.cli2.Group;
+import org.apache.commons.cli2.Option;
+import org.apache.commons.cli2.option.GroupImpl;
+
+/**
+ * Builds Group instances
+ */
+public class GroupBuilder {
+
+ private String name;
+ private String description;
+ private List options;
+ private int minimum;
+ private int maximum;
+
+ /**
+ * Creates a new GroupBuilder
+ */
+ public GroupBuilder() {
+ reset();
+ }
+
+ /**
+ * Creates a new Group instance
+ * @return the new Group instance
+ */
+ public Group create() {
+ final GroupImpl group =
+ new GroupImpl(options, name, description, minimum, maximum);
+
+ reset();
+
+ return group;
+ }
+
+ /**
+ * Resets the builder
+ */
+ public GroupBuilder reset() {
+ name = null;
+ description = null;
+ options = new ArrayList();
+ minimum = 0;
+ maximum = Integer.MAX_VALUE;
+ return this;
+ }
+
+ /**
+ * Use this option description
+ * @param newDescription the description to use
+ * @return this builder
+ */
+ public GroupBuilder withDescription(final String newDescription) {
+ this.description = newDescription;
+ return this;
+ }
+
+ /**
+ * Use this option name
+ * @param newName the name to use
+ * @return this builder
+ */
+ public GroupBuilder withName(final String newName) {
+ this.name = newName;
+ return this;
+ }
+
+ /**
+ * A valid group requires at least this many options present
+ * @param newMinimum the minimum Options required
+ * @return this builder
+ */
+ public GroupBuilder withMinimum(final int newMinimum) {
+ this.minimum = newMinimum;
+ return this;
+ }
+
+ /**
+ * A valid group requires at most this many options present
+ * @param newMaximum the maximum Options allowed
+ * @return this builder
+ */
+ public GroupBuilder withMaximum(final int newMaximum) {
+ this.maximum = newMaximum;
+ return this;
+ }
+
+ /**
+ * Add this option to the group
+ * @param option the Option to add
+ * @return this builder
+ */
+ public GroupBuilder withOption(final Option option) {
+ this.options.add(option);
+ return this;
+ }
+}
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/PatternBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/PatternBuilder.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/PatternBuilder.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/PatternBuilder.java Fri Mar 21 20:08:23 2008
@@ -1 +1,202 @@
-/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.builder;import java.util.Iterator;import java.util.LinkedHashSet;import java.util.Set;import org.apache.commons.cli2.Argument;import org.apache.commons.cli2.Option;import
org.apache.commons.cli2.validation.ClassValidator;import org.apache.commons.cli2.validation.DateValidator;import org.apache.commons.cli2.validation.FileValidator;import org.apache.commons.cli2.validation.NumberValidator;import org.apache.commons.cli2.validation.UrlValidator;import org.apache.commons.cli2.validation.Validator;/** * Builds Options using a String pattern *///TODO Document and link to the acceptable patternspublic class PatternBuilder { private final GroupBuilder gbuilder; private final DefaultOptionBuilder obuilder; private final ArgumentBuilder abuilder; /** * Creates a new PatternBuilder */ public PatternBuilder() { this( new GroupBuilder(), new DefaultOptionBuilder(), new ArgumentBuilder()); } /** * Creates a new PatternBuilder * @param gbuilder the GroupBuilder to use * @param obuilder the DefaultOptionBuilder to use * @param abuilder the ArgumentBuilder to use */ p
ublic PatternBuilder( final GroupBuilder gbuilder, final DefaultOptionBuilder obuilder, final ArgumentBuilder abuilder) { this.gbuilder = gbuilder; this.obuilder = obuilder; this.abuilder = abuilder; } private final Set options = new LinkedHashSet(); /** * Creates a new Option instance. * @return a new Option instance */ public Option create() { final Option option; if (options.size() == 1) { option = (Option)options.iterator().next(); } else { gbuilder.reset(); for (final Iterator i = options.iterator(); i.hasNext();) { gbuilder.withOption((Option)i.next()); } option = gbuilder.create(); } reset(); return option; } /** * Resets this builder */ public PatternBuilder reset() { options.clear(); return this; } private void createOption( final char type,
final boolean required, final char opt) { final Argument argument; if (type != ' ') { abuilder.reset(); abuilder.withValidator(validator(type)); if (required) { abuilder.withMinimum(1); } if (type != '*') { abuilder.withMaximum(1); } argument = abuilder.create(); } else { argument = null; } obuilder.reset(); obuilder.withArgument(argument); obuilder.withShortName(String.valueOf(opt)); obuilder.withRequired(required); options.add(obuilder.create()); } /** * Builds an Option using a pattern string. * @param pattern the pattern to build from */ public void withPattern(final String pattern) { int sz = pattern.length(); char opt = ' '; char ch = ' '; char type = ' '; boolean required = false; for (int i = 0; i < sz; i+
+) { ch = pattern.charAt(i); switch (ch) { case '!' : required = true; break; case '@' : case ':' : case '%' : case '+' : case '#' : case '<' : case '>' : case '*' : case '/' : type = ch; break; default : if (opt != ' ') { createOption(type, required, opt); required = false; type = ' '; } opt = ch; } } if (opt != ' ') { createOption(type, required, opt); } } private static Validator validator(final char c) { switch (c) { case '@' : final ClassValidator classv = new ClassValidator(); classv.setInsta
nce(true); return classv; case '+' : final ClassValidator instancev = new ClassValidator(); return instancev; //case ':':// no validator needed for a string case '%' : return NumberValidator.getNumberInstance(); case '#' : return DateValidator.getDateInstance(); case '<' : final FileValidator existingv = new FileValidator(); existingv.setExisting(true); existingv.setFile(true); return existingv; case '>' : case '*' : return new FileValidator(); case '/' : return new UrlValidator(); default : return null; } }}
\ No newline at end of file
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.builder;
+
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import org.apache.commons.cli2.Argument;
+import org.apache.commons.cli2.Option;
+import org.apache.commons.cli2.validation.ClassValidator;
+import org.apache.commons.cli2.validation.DateValidator;
+import org.apache.commons.cli2.validation.FileValidator;
+import org.apache.commons.cli2.validation.NumberValidator;
+import org.apache.commons.cli2.validation.UrlValidator;
+import org.apache.commons.cli2.validation.Validator;
+
+/**
+ * Builds Options using a String pattern
+ */
+//TODO Document and link to the acceptable patterns
+public class PatternBuilder {
+
+ private final GroupBuilder gbuilder;
+ private final DefaultOptionBuilder obuilder;
+ private final ArgumentBuilder abuilder;
+
+ /**
+ * Creates a new PatternBuilder
+ */
+ public PatternBuilder() {
+ this(
+ new GroupBuilder(),
+ new DefaultOptionBuilder(),
+ new ArgumentBuilder());
+ }
+
+ /**
+ * Creates a new PatternBuilder
+ * @param gbuilder the GroupBuilder to use
+ * @param obuilder the DefaultOptionBuilder to use
+ * @param abuilder the ArgumentBuilder to use
+ */
+ public PatternBuilder(
+ final GroupBuilder gbuilder,
+ final DefaultOptionBuilder obuilder,
+ final ArgumentBuilder abuilder) {
+ this.gbuilder = gbuilder;
+ this.obuilder = obuilder;
+ this.abuilder = abuilder;
+ }
+
+ private final Set options = new LinkedHashSet();
+
+ /**
+ * Creates a new Option instance.
+ * @return a new Option instance
+ */
+ public Option create() {
+ final Option option;
+
+ if (options.size() == 1) {
+ option = (Option)options.iterator().next();
+ }
+ else {
+ gbuilder.reset();
+ for (final Iterator i = options.iterator(); i.hasNext();) {
+ gbuilder.withOption((Option)i.next());
+ }
+ option = gbuilder.create();
+ }
+
+ reset();
+
+ return option;
+ }
+
+ /**
+ * Resets this builder
+ */
+ public PatternBuilder reset() {
+ options.clear();
+ return this;
+ }
+
+ private void createOption(
+ final char type,
+ final boolean required,
+ final char opt) {
+ final Argument argument;
+ if (type != ' ') {
+ abuilder.reset();
+ abuilder.withValidator(validator(type));
+ if (required) {
+ abuilder.withMinimum(1);
+ }
+ if (type != '*') {
+ abuilder.withMaximum(1);
+ }
+ argument = abuilder.create();
+ }
+ else {
+ argument = null;
+ }
+
+ obuilder.reset();
+ obuilder.withArgument(argument);
+ obuilder.withShortName(String.valueOf(opt));
+ obuilder.withRequired(required);
+
+ options.add(obuilder.create());
+ }
+
+ /**
+ * Builds an Option using a pattern string.
+ * @param pattern the pattern to build from
+ */
+ public void withPattern(final String pattern) {
+ int sz = pattern.length();
+
+ char opt = ' ';
+ char ch = ' ';
+ char type = ' ';
+ boolean required = false;
+
+ for (int i = 0; i < sz; i++) {
+ ch = pattern.charAt(i);
+
+ switch (ch) {
+ case '!' :
+ required = true;
+ break;
+ case '@' :
+ case ':' :
+ case '%' :
+ case '+' :
+ case '#' :
+ case '<' :
+ case '>' :
+ case '*' :
+ case '/' :
+ type = ch;
+ break;
+ default :
+ if (opt != ' ') {
+ createOption(type, required, opt);
+ required = false;
+ type = ' ';
+ }
+
+ opt = ch;
+ }
+ }
+
+ if (opt != ' ') {
+ createOption(type, required, opt);
+ }
+ }
+
+ private static Validator validator(final char c) {
+ switch (c) {
+ case '@' :
+ final ClassValidator classv = new ClassValidator();
+ classv.setInstance(true);
+ return classv;
+ case '+' :
+ final ClassValidator instancev = new ClassValidator();
+ return instancev;
+ //case ':':// no validator needed for a string
+ case '%' :
+ return NumberValidator.getNumberInstance();
+ case '#' :
+ return DateValidator.getDateInstance();
+ case '<' :
+ final FileValidator existingv = new FileValidator();
+ existingv.setExisting(true);
+ existingv.setFile(true);
+ return existingv;
+ case '>' :
+ case '*' :
+ return new FileValidator();
+ case '/' :
+ return new UrlValidator();
+ default :
+ return null;
+ }
+ }
+}
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/builder/SwitchBuilder.java Fri Mar 21 20:08:23 2008
@@ -1 +1,194 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.builder;import java.util.HashSet;import java.util.Set;import org.apache.commons.cli2.Argument;import org.apache.commons.cli2.Group;import org.apache.commons.cli2.option.Sw
itch;import org.apache.commons.cli2.resource.ResourceConstants;import org.apache.commons.cli2.resource.ResourceHelper;/** * Builds Switch instance. */public class SwitchBuilder { private final String enabledPrefix; private final String disabledPrefix; private String description; private String preferredName; private Set aliases; private boolean required; private Argument argument; private Group children; private int id; private Boolean switchDefault; /** * Creates a new SwitchBuilder using defaults. * @see Switch#DEFAULT_ENABLED_PREFIX * @see Switch#DEFAULT_DISABLED_PREFIX */ public SwitchBuilder() { this(Switch.DEFAULT_ENABLED_PREFIX, Switch.DEFAULT_DISABLED_PREFIX); } /** * Creates a new SwitchBuilder * @param enabledPrefix the prefix to use for enabling the option * @param disabledPrefix the prefix to use for disabling the option * @throws IllegalArgumentException if either prefix is less tha
n 1 * character long or the prefixes match */ public SwitchBuilder(final String enabledPrefix, final String disabledPrefix) throws IllegalArgumentException { if ((enabledPrefix == null) || (enabledPrefix.length() < 1)) { throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_ENABLED_PREFIX)); } if ((disabledPrefix == null) || (disabledPrefix.length() < 1)) { throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_DISABLED_PREFIX)); } if (enabledPrefix.equals(disabledPrefix)) { throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_IDENTICAL_PREFIXES)); } this.enabledPrefix = enabledPrefix; this.disabledPrefix = disabledPrefix; reset(); }
/** * Creates a new Switch instance * @return a new Switch instance */ public Switch create() { final Switch option = new Switch(enabledPrefix, disabledPrefix, preferredName, aliases, description, required, argument, children, id, switchDefault); reset(); return option; } /** * Resets the builder */ public SwitchBuilder reset() { description = null; preferredName = null; required = false; aliases = new HashSet(); argument = null; children = null; id = 0; switchDefault = null; return this; } /** * Use this option description * @param newDescription the description to use * @return this builder */ public SwitchBuilder withDescription(final String newDescription) { this.description = newDescription; return this; } /** * Use this option name. The first name is used as the preferred
* display name for the Command and then later names are used as aliases. * * @param name the name to use * @return this builder */ public SwitchBuilder withName(final String name) { if (preferredName == null) { preferredName = name; } else { aliases.add(name); } return this; } /** * Use this optionality * @param newRequired true iff the Option is required * @return this builder */ public SwitchBuilder withRequired(final boolean newRequired) { this.required = newRequired; return this; } /** * Use this Argument * @param newArgument the argument to use * @return this builder */ public SwitchBuilder withArgument(final Argument newArgument) { this.argument = newArgument; return this; } /** * Use this child Group * @param newChildren the child Group to use * @return this builder */ public SwitchBuilder withC
hildren(final Group newChildren) { this.children = newChildren; return this; } /** * Sets the id * * @param newId * the id of the Switch * @return this SwitchBuilder */ public final SwitchBuilder withId(final int newId) { this.id = newId; return this; } /** * Sets the default state for this switch * * @param newSwitchDefault the default state * @return this SwitchBuilder */ public final SwitchBuilder withSwitchDefault(final Boolean newSwitchDefault) { this.switchDefault = newSwitchDefault; return this; }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.builder;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.commons.cli2.Argument;
+import org.apache.commons.cli2.Group;
+import org.apache.commons.cli2.option.Switch;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
+
+/**
+ * Builds Switch instance.
+ */
+public class SwitchBuilder {
+ private final String enabledPrefix;
+ private final String disabledPrefix;
+ private String description;
+ private String preferredName;
+ private Set aliases;
+ private boolean required;
+ private Argument argument;
+ private Group children;
+ private int id;
+ private Boolean switchDefault;
+
+ /**
+ * Creates a new SwitchBuilder using defaults.
+ * @see Switch#DEFAULT_ENABLED_PREFIX
+ * @see Switch#DEFAULT_DISABLED_PREFIX
+ */
+ public SwitchBuilder() {
+ this(Switch.DEFAULT_ENABLED_PREFIX, Switch.DEFAULT_DISABLED_PREFIX);
+ }
+
+ /**
+ * Creates a new SwitchBuilder
+ * @param enabledPrefix the prefix to use for enabling the option
+ * @param disabledPrefix the prefix to use for disabling the option
+ * @throws IllegalArgumentException if either prefix is less than 1
+ * character long or the prefixes match
+ */
+ public SwitchBuilder(final String enabledPrefix,
+ final String disabledPrefix)
+ throws IllegalArgumentException {
+ if ((enabledPrefix == null) || (enabledPrefix.length() < 1)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_ENABLED_PREFIX));
+ }
+
+ if ((disabledPrefix == null) || (disabledPrefix.length() < 1)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_DISABLED_PREFIX));
+ }
+
+ if (enabledPrefix.equals(disabledPrefix)) {
+ throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_IDENTICAL_PREFIXES));
+ }
+
+ this.enabledPrefix = enabledPrefix;
+ this.disabledPrefix = disabledPrefix;
+ reset();
+ }
+
+ /**
+ * Creates a new Switch instance
+ * @return a new Switch instance
+ */
+ public Switch create() {
+ final Switch option =
+ new Switch(enabledPrefix, disabledPrefix, preferredName, aliases, description,
+ required, argument, children, id, switchDefault);
+
+ reset();
+
+ return option;
+ }
+
+ /**
+ * Resets the builder
+ */
+ public SwitchBuilder reset() {
+ description = null;
+ preferredName = null;
+ required = false;
+ aliases = new HashSet();
+ argument = null;
+ children = null;
+ id = 0;
+ switchDefault = null;
+
+ return this;
+ }
+
+ /**
+ * Use this option description
+ * @param newDescription the description to use
+ * @return this builder
+ */
+ public SwitchBuilder withDescription(final String newDescription) {
+ this.description = newDescription;
+
+ return this;
+ }
+
+ /**
+ * Use this option name. The first name is used as the preferred
+ * display name for the Command and then later names are used as aliases.
+ *
+ * @param name the name to use
+ * @return this builder
+ */
+ public SwitchBuilder withName(final String name) {
+ if (preferredName == null) {
+ preferredName = name;
+ } else {
+ aliases.add(name);
+ }
+
+ return this;
+ }
+
+ /**
+ * Use this optionality
+ * @param newRequired true iff the Option is required
+ * @return this builder
+ */
+ public SwitchBuilder withRequired(final boolean newRequired) {
+ this.required = newRequired;
+
+ return this;
+ }
+
+ /**
+ * Use this Argument
+ * @param newArgument the argument to use
+ * @return this builder
+ */
+ public SwitchBuilder withArgument(final Argument newArgument) {
+ this.argument = newArgument;
+
+ return this;
+ }
+
+ /**
+ * Use this child Group
+ * @param newChildren the child Group to use
+ * @return this builder
+ */
+ public SwitchBuilder withChildren(final Group newChildren) {
+ this.children = newChildren;
+
+ return this;
+ }
+
+ /**
+ * Sets the id
+ *
+ * @param newId
+ * the id of the Switch
+ * @return this SwitchBuilder
+ */
+ public final SwitchBuilder withId(final int newId) {
+ this.id = newId;
+
+ return this;
+ }
+
+ /**
+ * Sets the default state for this switch
+ *
+ * @param newSwitchDefault the default state
+ * @return this SwitchBuilder
+ */
+ public final SwitchBuilder withSwitchDefault(final Boolean newSwitchDefault) {
+ this.switchDefault = newSwitchDefault;
+
+ return this;
+ }
+}
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java Fri Mar 21 20:08:23 2008
@@ -1 +1,120 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.Collections;import java.util.Iterator;import java.util.List;import org.apache.commons.cli2.CommandLine;import org.apache.commons.cli2.Option;i
mport org.apache.commons.cli2.resource.ResourceConstants;import org.apache.commons.cli2.resource.ResourceHelper;/** * Instances of CommandLine represent a command line that has been processed * according to the definition supplied to the parser. */public abstract class CommandLineImpl implements CommandLine { public final boolean hasOption(final String trigger) { return hasOption(getOption(trigger)); } public final List getValues(final String trigger) { return getValues(getOption(trigger), Collections.EMPTY_LIST); } public final List getValues(final String trigger, final List defaultValues) { return getValues(getOption(trigger), defaultValues); } public final List getValues(final Option option) { return getValues(option, Collections.EMPTY_LIST); } public final Object getValue(final String trigger) { return getValue(getOption(trigger), null); } public final Object getValue(final
String trigger, final Object defaultValue) { return getValue(getOption(trigger), defaultValue); } public final Object getValue(final Option option) { return getValue(option, null); } public final Object getValue(final Option option, final Object defaultValue) { final List values; if (defaultValue == null) { values = getValues(option); } else { values = getValues(option, Collections.singletonList(defaultValue)); } if (values.size() > 1) { throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES)); } if (values.isEmpty()) { return defaultValue; } return values.get(0); } public final Boolean getSwitch(final String trigger) { return getSwitch(getOption(trigger), null); } public final Boolean getSwitch(f
inal String trigger, final Boolean defaultValue) { return getSwitch(getOption(trigger), defaultValue); } public final Boolean getSwitch(final Option option) { return getSwitch(option, null); } public final String getProperty(final String property) { return getProperty(property, null); } public final int getOptionCount(final String trigger) { return getOptionCount(getOption(trigger)); } public final int getOptionCount(final Option option) { if (option == null) { return 0; } int count = 0; for (Iterator i = getOptions().iterator(); i.hasNext();) { if (option.equals(i.next())) { ++count; } } return count; }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.cli2.CommandLine;
+import org.apache.commons.cli2.Option;
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
+
+/**
+ * Instances of CommandLine represent a command line that has been processed
+ * according to the definition supplied to the parser.
+ */
+public abstract class CommandLineImpl implements CommandLine {
+ public final boolean hasOption(final String trigger) {
+ return hasOption(getOption(trigger));
+ }
+
+ public final List getValues(final String trigger) {
+ return getValues(getOption(trigger), Collections.EMPTY_LIST);
+ }
+
+ public final List getValues(final String trigger,
+ final List defaultValues) {
+ return getValues(getOption(trigger), defaultValues);
+ }
+
+ public final List getValues(final Option option) {
+ return getValues(option, Collections.EMPTY_LIST);
+ }
+
+ public final Object getValue(final String trigger) {
+ return getValue(getOption(trigger), null);
+ }
+
+ public final Object getValue(final String trigger,
+ final Object defaultValue) {
+ return getValue(getOption(trigger), defaultValue);
+ }
+
+ public final Object getValue(final Option option) {
+ return getValue(option, null);
+ }
+
+ public final Object getValue(final Option option,
+ final Object defaultValue) {
+ final List values;
+
+ if (defaultValue == null) {
+ values = getValues(option);
+ } else {
+ values = getValues(option, Collections.singletonList(defaultValue));
+ }
+
+ if (values.size() > 1) {
+ throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES));
+ }
+
+ if (values.isEmpty()) {
+ return defaultValue;
+ }
+
+ return values.get(0);
+ }
+
+ public final Boolean getSwitch(final String trigger) {
+ return getSwitch(getOption(trigger), null);
+ }
+
+ public final Boolean getSwitch(final String trigger,
+ final Boolean defaultValue) {
+ return getSwitch(getOption(trigger), defaultValue);
+ }
+
+ public final Boolean getSwitch(final Option option) {
+ return getSwitch(option, null);
+ }
+
+ public final String getProperty(final String property) {
+ return getProperty(property, null);
+ }
+
+ public final int getOptionCount(final String trigger) {
+ return getOptionCount(getOption(trigger));
+ }
+
+ public final int getOptionCount(final Option option) {
+ if (option == null) {
+ return 0;
+ }
+
+ int count = 0;
+
+ for (Iterator i = getOptions().iterator(); i.hasNext();) {
+ if (option.equals(i.next())) {
+ ++count;
+ }
+ }
+
+ return count;
+ }
+}