View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2.builder;
18  
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  import org.apache.commons.cli2.Argument;
23  import org.apache.commons.cli2.Group;
24  import org.apache.commons.cli2.option.DefaultOption;
25  import org.apache.commons.cli2.resource.ResourceConstants;
26  import org.apache.commons.cli2.resource.ResourceHelper;
27  
28  /**
29   * Builds DefaultOption instances.
30   */
31  public class DefaultOptionBuilder {
32      private final String shortPrefix;
33      private final String longPrefix;
34      private final boolean burstEnabled;
35      private String preferredName;
36      private Set aliases;
37      private Set burstAliases;
38      private boolean required;
39      private String description;
40      private Argument argument;
41      private Group children;
42      private int id;
43  
44      /**
45       * Creates a new DefaultOptionBuilder using defaults
46       * @see DefaultOption#DEFAULT_SHORT_PREFIX
47       * @see DefaultOption#DEFAULT_LONG_PREFIX
48       * @see DefaultOption#DEFAULT_BURST_ENABLED
49       */
50      public DefaultOptionBuilder() {
51          this(DefaultOption.DEFAULT_SHORT_PREFIX, DefaultOption.DEFAULT_LONG_PREFIX,
52               DefaultOption.DEFAULT_BURST_ENABLED);
53      }
54  
55      /**
56       * Creates a new DefaultOptionBuilder
57       * @param shortPrefix the prefix to use for short options
58       * @param longPrefix the prefix to use for long options
59       * @param burstEnabled whether to allow gnu style bursting
60       * @throws IllegalArgumentException if either prefix is less than on
61       *                                  character long
62       */
63      public DefaultOptionBuilder(final String shortPrefix,
64                                  final String longPrefix,
65                                  final boolean burstEnabled)
66          throws IllegalArgumentException {
67          if ((shortPrefix == null) || (shortPrefix.length() == 0)) {
68              throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX));
69          }
70  
71          if ((longPrefix == null) || (longPrefix.length() == 0)) {
72              throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX));
73          }
74  
75          this.shortPrefix = shortPrefix;
76          this.longPrefix = longPrefix;
77          this.burstEnabled = burstEnabled;
78          reset();
79      }
80  
81      /**
82       * Creates a DefaultOption instance
83       * @return the new instance
84       * @throws IllegalStateException if no names have been supplied
85       */
86      public DefaultOption create()
87          throws IllegalStateException {
88          if (preferredName == null) {
89              throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME));
90          }
91  
92          final DefaultOption option =
93              new DefaultOption(shortPrefix, longPrefix, burstEnabled, preferredName, description,
94                                aliases, burstAliases, required, argument, children, id);
95  
96          reset();
97  
98          return option;
99      }
100 
101     /**
102      * Resets the builder.
103      * @return this builder
104      */
105     public DefaultOptionBuilder reset() {
106         preferredName = null;
107         description = null;
108         aliases = new HashSet();
109         burstAliases = new HashSet();
110         required = false;
111         argument = null;
112         children = null;
113         id = 0;
114 
115         return this;
116     }
117 
118     /**
119      * Use this short option name. The first name is used as the preferred
120      * display name for the Command and then later names are used as aliases.
121      *
122      * @param shortName the name to use
123      * @return this builder
124      */
125     public DefaultOptionBuilder withShortName(final String shortName) {
126         final String name = shortPrefix + shortName;
127 
128         if (preferredName == null) {
129             preferredName = name;
130         } else {
131             aliases.add(name);
132         }
133 
134         if (burstEnabled && (name.length() == (shortPrefix.length() + 1))) {
135             burstAliases.add(name);
136         }
137 
138         return this;
139     }
140 
141     /**
142      * Use this long option name.  The first name is used as the preferred
143      * display name for the Command and then later names are used as aliases.
144      *
145      * @param longName the name to use
146      * @return this builder
147      */
148     public DefaultOptionBuilder withLongName(final String longName) {
149         final String name = longPrefix + longName;
150 
151         if (preferredName == null) {
152             preferredName = name;
153         } else {
154             aliases.add(name);
155         }
156 
157         return this;
158     }
159 
160     /**
161      * Use this option description
162      * @param newDescription the description to use
163      * @return this builder
164      */
165     public DefaultOptionBuilder withDescription(final String newDescription) {
166         this.description = newDescription;
167 
168         return this;
169     }
170 
171     /**
172      * Use this optionality
173      * @param newRequired true iff the Option is required
174      * @return this builder
175      */
176     public DefaultOptionBuilder withRequired(final boolean newRequired) {
177         this.required = newRequired;
178 
179         return this;
180     }
181 
182     /**
183      * Use this child Group
184      * @param newChildren the child Group to use
185      * @return this builder
186      */
187     public DefaultOptionBuilder withChildren(final Group newChildren) {
188         this.children = newChildren;
189 
190         return this;
191     }
192 
193     /**
194      * Use this Argument
195      * @param newArgument the argument to use
196      * @return this builder
197      */
198     public DefaultOptionBuilder withArgument(final Argument newArgument) {
199         this.argument = newArgument;
200 
201         return this;
202     }
203 
204     /**
205      * Sets the id
206      *
207      * @param newId
208      *            the id of the DefaultOption
209      * @return this DefaultOptionBuilder
210      */
211     public final DefaultOptionBuilder withId(final int newId) {
212         this.id = newId;
213 
214         return this;
215     }
216 }