| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 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 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public DefaultOptionBuilder() { |
| 51 | 843 | this(DefaultOption.DEFAULT_SHORT_PREFIX, DefaultOption.DEFAULT_LONG_PREFIX, |
| 52 | |
DefaultOption.DEFAULT_BURST_ENABLED); |
| 53 | 843 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public DefaultOptionBuilder(final String shortPrefix, |
| 64 | |
final String longPrefix, |
| 65 | |
final boolean burstEnabled) |
| 66 | 848 | throws IllegalArgumentException { |
| 67 | 848 | if ((shortPrefix == null) || (shortPrefix.length() == 0)) { |
| 68 | 2 | throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX)); |
| 69 | |
} |
| 70 | |
|
| 71 | 846 | if ((longPrefix == null) || (longPrefix.length() == 0)) { |
| 72 | 2 | throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX)); |
| 73 | |
} |
| 74 | |
|
| 75 | 844 | this.shortPrefix = shortPrefix; |
| 76 | 844 | this.longPrefix = longPrefix; |
| 77 | 844 | this.burstEnabled = burstEnabled; |
| 78 | 844 | reset(); |
| 79 | 844 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public DefaultOption create() |
| 87 | |
throws IllegalStateException { |
| 88 | 1061 | if (preferredName == null) { |
| 89 | 1 | throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME)); |
| 90 | |
} |
| 91 | |
|
| 92 | 1060 | final DefaultOption option = |
| 93 | |
new DefaultOption(shortPrefix, longPrefix, burstEnabled, preferredName, description, |
| 94 | |
aliases, burstAliases, required, argument, children, id); |
| 95 | |
|
| 96 | 1060 | reset(); |
| 97 | |
|
| 98 | 1060 | return option; |
| 99 | |
} |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public DefaultOptionBuilder reset() { |
| 106 | 1906 | preferredName = null; |
| 107 | 1906 | description = null; |
| 108 | 1906 | aliases = new HashSet(); |
| 109 | 1906 | burstAliases = new HashSet(); |
| 110 | 1906 | required = false; |
| 111 | 1906 | argument = null; |
| 112 | 1906 | children = null; |
| 113 | 1906 | id = 0; |
| 114 | |
|
| 115 | 1906 | return this; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public DefaultOptionBuilder withShortName(final String shortName) { |
| 126 | 288 | final String name = shortPrefix + shortName; |
| 127 | |
|
| 128 | 288 | if (preferredName == null) { |
| 129 | 213 | preferredName = name; |
| 130 | |
} else { |
| 131 | 75 | aliases.add(name); |
| 132 | |
} |
| 133 | |
|
| 134 | 288 | if (burstEnabled && (name.length() == (shortPrefix.length() + 1))) { |
| 135 | 245 | burstAliases.add(name); |
| 136 | |
} |
| 137 | |
|
| 138 | 288 | return this; |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public DefaultOptionBuilder withLongName(final String longName) { |
| 149 | 1103 | final String name = longPrefix + longName; |
| 150 | |
|
| 151 | 1103 | if (preferredName == null) { |
| 152 | 849 | preferredName = name; |
| 153 | |
} else { |
| 154 | 254 | aliases.add(name); |
| 155 | |
} |
| 156 | |
|
| 157 | 1103 | return this; |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public DefaultOptionBuilder withDescription(final String newDescription) { |
| 166 | 247 | this.description = newDescription; |
| 167 | |
|
| 168 | 247 | return this; |
| 169 | |
} |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public DefaultOptionBuilder withRequired(final boolean newRequired) { |
| 177 | 12 | this.required = newRequired; |
| 178 | |
|
| 179 | 12 | return this; |
| 180 | |
} |
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
public DefaultOptionBuilder withChildren(final Group newChildren) { |
| 188 | 6 | this.children = newChildren; |
| 189 | |
|
| 190 | 6 | return this; |
| 191 | |
} |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
public DefaultOptionBuilder withArgument(final Argument newArgument) { |
| 199 | 80 | this.argument = newArgument; |
| 200 | |
|
| 201 | 80 | return this; |
| 202 | |
} |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public final DefaultOptionBuilder withId(final int newId) { |
| 212 | 22 | this.id = newId; |
| 213 | |
|
| 214 | 22 | return this; |
| 215 | |
} |
| 216 | |
} |