| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.cli2.option; |
| 18 | |
|
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.Collections; |
| 21 | |
import java.util.Comparator; |
| 22 | |
import java.util.HashSet; |
| 23 | |
import java.util.Iterator; |
| 24 | |
import java.util.List; |
| 25 | |
import java.util.ListIterator; |
| 26 | |
import java.util.Set; |
| 27 | |
|
| 28 | |
import org.apache.commons.cli2.Argument; |
| 29 | |
import org.apache.commons.cli2.DisplaySetting; |
| 30 | |
import org.apache.commons.cli2.Group; |
| 31 | |
import org.apache.commons.cli2.OptionException; |
| 32 | |
import org.apache.commons.cli2.WriteableCommandLine; |
| 33 | |
import org.apache.commons.cli2.resource.ResourceConstants; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public class DefaultOption |
| 39 | |
extends ParentImpl { |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public static final String DEFAULT_SHORT_PREFIX = "-"; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public static final String DEFAULT_LONG_PREFIX = "--"; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public static final boolean DEFAULT_BURST_ENABLED = true; |
| 54 | |
private final String preferredName; |
| 55 | |
private final Set aliases; |
| 56 | |
private final Set burstAliases; |
| 57 | |
private final Set triggers; |
| 58 | |
private final Set prefixes; |
| 59 | |
private final String shortPrefix; |
| 60 | |
private final boolean burstEnabled; |
| 61 | |
private final int burstLength; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public DefaultOption(final String shortPrefix, |
| 81 | |
final String longPrefix, |
| 82 | |
final boolean burstEnabled, |
| 83 | |
final String preferredName, |
| 84 | |
final String description, |
| 85 | |
final Set aliases, |
| 86 | |
final Set burstAliases, |
| 87 | |
final boolean required, |
| 88 | |
final Argument argument, |
| 89 | |
final Group children, |
| 90 | |
final int id) { |
| 91 | 1158 | super(argument, children, description, id, required); |
| 92 | |
|
| 93 | 1158 | this.shortPrefix = shortPrefix; |
| 94 | 1158 | this.burstEnabled = burstEnabled; |
| 95 | |
|
| 96 | 1158 | this.burstLength = shortPrefix.length() + 1; |
| 97 | |
|
| 98 | 1158 | this.preferredName = preferredName; |
| 99 | 1158 | this.aliases = |
| 100 | |
(aliases == null) ? Collections.EMPTY_SET |
| 101 | |
: Collections.unmodifiableSet(new HashSet(aliases)); |
| 102 | |
|
| 103 | 1158 | this.burstAliases = |
| 104 | |
(burstAliases == null) ? Collections.EMPTY_SET |
| 105 | |
: Collections.unmodifiableSet(new HashSet(burstAliases)); |
| 106 | |
|
| 107 | 1158 | final Set newTriggers = new HashSet(); |
| 108 | 1158 | newTriggers.add(preferredName); |
| 109 | 1158 | newTriggers.addAll(this.aliases); |
| 110 | 1158 | newTriggers.addAll(this.burstAliases); |
| 111 | 1158 | this.triggers = Collections.unmodifiableSet(newTriggers); |
| 112 | |
|
| 113 | 1158 | final Set newPrefixes = new HashSet(super.getPrefixes()); |
| 114 | 1158 | newPrefixes.add(shortPrefix); |
| 115 | 1158 | newPrefixes.add(longPrefix); |
| 116 | 1158 | this.prefixes = Collections.unmodifiableSet(newPrefixes); |
| 117 | |
|
| 118 | 1158 | checkPrefixes(newPrefixes); |
| 119 | 1158 | } |
| 120 | |
|
| 121 | |
public boolean canProcess(final WriteableCommandLine commandLine, |
| 122 | |
final String argument) { |
| 123 | 165 | return (argument != null) && |
| 124 | |
(super.canProcess(commandLine, argument) || |
| 125 | |
((argument.length() >= burstLength) && |
| 126 | |
burstAliases.contains(argument.substring(0, burstLength)))); |
| 127 | |
} |
| 128 | |
|
| 129 | |
public void processParent(WriteableCommandLine commandLine, |
| 130 | |
ListIterator arguments) |
| 131 | |
throws OptionException { |
| 132 | 117 | final String argument = (String) arguments.next(); |
| 133 | |
|
| 134 | 117 | if (triggers.contains(argument)) { |
| 135 | 97 | commandLine.addOption(this); |
| 136 | 97 | arguments.set(preferredName); |
| 137 | 20 | } else if (burstEnabled && (argument.length() >= burstLength)) { |
| 138 | 20 | final String burst = argument.substring(0, burstLength); |
| 139 | |
|
| 140 | 20 | if (burstAliases.contains(burst)) { |
| 141 | 20 | commandLine.addOption(this); |
| 142 | |
|
| 143 | |
|
| 144 | 20 | arguments.set(preferredName); |
| 145 | |
|
| 146 | 20 | if (getArgument() == null) { |
| 147 | 16 | arguments.add(shortPrefix + argument.substring(burstLength)); |
| 148 | |
} else { |
| 149 | 4 | arguments.add(argument.substring(burstLength)); |
| 150 | |
} |
| 151 | |
|
| 152 | 20 | arguments.previous(); |
| 153 | |
} else { |
| 154 | 0 | throw new OptionException(this, ResourceConstants.CANNOT_BURST, argument); |
| 155 | |
} |
| 156 | 20 | } else { |
| 157 | 0 | throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, argument); |
| 158 | |
} |
| 159 | 117 | } |
| 160 | |
|
| 161 | |
public Set getTriggers() { |
| 162 | 6882 | return triggers; |
| 163 | |
} |
| 164 | |
|
| 165 | |
public Set getPrefixes() { |
| 166 | 3344 | return prefixes; |
| 167 | |
} |
| 168 | |
|
| 169 | |
public void validate(WriteableCommandLine commandLine) |
| 170 | |
throws OptionException { |
| 171 | 87 | if (isRequired() && !commandLine.hasOption(this)) { |
| 172 | 3 | throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED, |
| 173 | |
getPreferredName()); |
| 174 | |
} |
| 175 | |
|
| 176 | 84 | super.validate(commandLine); |
| 177 | 83 | } |
| 178 | |
|
| 179 | |
public void appendUsage(final StringBuffer buffer, |
| 180 | |
final Set helpSettings, |
| 181 | |
final Comparator comp) { |
| 182 | |
|
| 183 | 194 | final boolean optional = |
| 184 | |
!isRequired() && helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL); |
| 185 | 194 | final boolean displayAliases = helpSettings.contains(DisplaySetting.DISPLAY_ALIASES); |
| 186 | |
|
| 187 | 194 | if (optional) { |
| 188 | 3 | buffer.append('['); |
| 189 | |
} |
| 190 | |
|
| 191 | 194 | buffer.append(preferredName); |
| 192 | |
|
| 193 | 194 | if (displayAliases && !aliases.isEmpty()) { |
| 194 | 63 | buffer.append(" ("); |
| 195 | |
|
| 196 | 63 | final List list = new ArrayList(aliases); |
| 197 | 63 | Collections.sort(list); |
| 198 | |
|
| 199 | 63 | for (final Iterator i = list.iterator(); i.hasNext();) { |
| 200 | 73 | final String alias = (String) i.next(); |
| 201 | 73 | buffer.append(alias); |
| 202 | |
|
| 203 | 73 | if (i.hasNext()) { |
| 204 | 10 | buffer.append(','); |
| 205 | |
} |
| 206 | 73 | } |
| 207 | |
|
| 208 | 63 | buffer.append(')'); |
| 209 | |
} |
| 210 | |
|
| 211 | 194 | super.appendUsage(buffer, helpSettings, comp); |
| 212 | |
|
| 213 | 194 | if (optional) { |
| 214 | 3 | buffer.append(']'); |
| 215 | |
} |
| 216 | 194 | } |
| 217 | |
|
| 218 | |
public String getPreferredName() { |
| 219 | 7102 | return preferredName; |
| 220 | |
} |
| 221 | |
} |