| 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.List; |
| 23 | |
import java.util.ListIterator; |
| 24 | |
import java.util.Set; |
| 25 | |
|
| 26 | |
import org.apache.commons.cli2.Argument; |
| 27 | |
import org.apache.commons.cli2.DisplaySetting; |
| 28 | |
import org.apache.commons.cli2.Group; |
| 29 | |
import org.apache.commons.cli2.Option; |
| 30 | |
import org.apache.commons.cli2.OptionException; |
| 31 | |
import org.apache.commons.cli2.Parent; |
| 32 | |
import org.apache.commons.cli2.WriteableCommandLine; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public abstract class ParentImpl |
| 39 | |
extends OptionImpl implements Parent { |
| 40 | |
private static final char NUL = '\0'; |
| 41 | |
private final Group children; |
| 42 | |
private final Argument argument; |
| 43 | |
private final String description; |
| 44 | |
|
| 45 | |
protected ParentImpl(final Argument argument, |
| 46 | |
final Group children, |
| 47 | |
final String description, |
| 48 | |
final int id, |
| 49 | |
final boolean required) { |
| 50 | 1295 | super(id, required); |
| 51 | 1295 | this.children = children; |
| 52 | 1295 | this.argument = argument; |
| 53 | 1295 | this.description = description; |
| 54 | 1295 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public void process(final WriteableCommandLine commandLine, |
| 63 | |
final ListIterator arguments) |
| 64 | |
throws OptionException { |
| 65 | 141 | if (argument != null) { |
| 66 | 52 | handleInitialSeparator(arguments, argument.getInitialSeparator()); |
| 67 | |
} |
| 68 | |
|
| 69 | 141 | processParent(commandLine, arguments); |
| 70 | |
|
| 71 | 140 | if (argument != null) { |
| 72 | 52 | argument.processValues(commandLine, arguments, this); |
| 73 | |
} |
| 74 | |
|
| 75 | 140 | if ((children != null) && children.canProcess(commandLine, arguments)) { |
| 76 | 5 | children.process(commandLine, arguments); |
| 77 | |
} |
| 78 | 140 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public boolean canProcess(final WriteableCommandLine commandLine, |
| 86 | |
final String arg) { |
| 87 | 200 | final Set triggers = getTriggers(); |
| 88 | |
|
| 89 | 200 | if (argument != null) { |
| 90 | 49 | final char separator = argument.getInitialSeparator(); |
| 91 | |
|
| 92 | |
|
| 93 | 49 | if (separator != NUL) { |
| 94 | 15 | final int initialIndex = arg.indexOf(separator); |
| 95 | |
|
| 96 | |
|
| 97 | 15 | if (initialIndex > 0) { |
| 98 | 9 | return triggers.contains(arg.substring(0, initialIndex)); |
| 99 | |
} |
| 100 | |
} |
| 101 | |
} |
| 102 | |
|
| 103 | 191 | return triggers.contains(arg); |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public Set getPrefixes() { |
| 112 | 1767 | return (children == null) ? Collections.EMPTY_SET : children.getPrefixes(); |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public void validate(WriteableCommandLine commandLine) |
| 121 | |
throws OptionException { |
| 122 | 110 | if (commandLine.hasOption(this)) { |
| 123 | 107 | if (argument != null) { |
| 124 | 47 | argument.validate(commandLine, this); |
| 125 | |
} |
| 126 | |
|
| 127 | 106 | if (children != null) { |
| 128 | 5 | children.validate(commandLine); |
| 129 | |
} |
| 130 | |
} |
| 131 | 108 | } |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
public void appendUsage(final StringBuffer buffer, |
| 140 | |
final Set helpSettings, |
| 141 | |
final Comparator comp) { |
| 142 | 223 | final boolean displayArgument = |
| 143 | |
(this.argument != null) && |
| 144 | |
helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT); |
| 145 | 223 | final boolean displayChildren = |
| 146 | |
(this.children != null) && |
| 147 | |
helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN); |
| 148 | |
|
| 149 | 223 | if (displayArgument) { |
| 150 | 27 | buffer.append(' '); |
| 151 | 27 | argument.appendUsage(buffer, helpSettings, comp); |
| 152 | |
} |
| 153 | |
|
| 154 | 223 | if (displayChildren) { |
| 155 | 2 | buffer.append(' '); |
| 156 | 2 | children.appendUsage(buffer, helpSettings, comp); |
| 157 | |
} |
| 158 | 223 | } |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public String getDescription() { |
| 164 | 3417 | return description; |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public List helpLines(final int depth, |
| 174 | |
final Set helpSettings, |
| 175 | |
final Comparator comp) { |
| 176 | 107 | final List helpLines = new ArrayList(); |
| 177 | 107 | helpLines.add(new HelpLineImpl(this, depth)); |
| 178 | |
|
| 179 | 107 | if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT) && (argument != null)) { |
| 180 | 2 | helpLines.addAll(argument.helpLines(depth + 1, helpSettings, comp)); |
| 181 | |
} |
| 182 | |
|
| 183 | 107 | if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN) && (children != null)) { |
| 184 | 2 | helpLines.addAll(children.helpLines(depth + 1, helpSettings, comp)); |
| 185 | |
} |
| 186 | |
|
| 187 | 107 | return helpLines; |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
public Argument getArgument() { |
| 194 | 21 | return argument; |
| 195 | |
} |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public Group getChildren() { |
| 201 | 1 | return children; |
| 202 | |
} |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
private void handleInitialSeparator(final ListIterator arguments, |
| 210 | |
final char separator) { |
| 211 | |
|
| 212 | 52 | final String newArgument = (String) arguments.next(); |
| 213 | |
|
| 214 | |
|
| 215 | 52 | final int initialIndex = newArgument.indexOf(separator); |
| 216 | |
|
| 217 | 52 | if (initialIndex > 0) { |
| 218 | 4 | arguments.remove(); |
| 219 | 4 | arguments.add(newArgument.substring(0, initialIndex)); |
| 220 | 4 | String value = newArgument.substring(initialIndex + 1); |
| 221 | |
|
| 222 | |
|
| 223 | 4 | if (value.startsWith("-")) { |
| 224 | 1 | value = '"' + value + '"'; |
| 225 | |
} |
| 226 | 4 | arguments.add(value); |
| 227 | 4 | arguments.previous(); |
| 228 | |
} |
| 229 | |
|
| 230 | 52 | arguments.previous(); |
| 231 | 52 | } |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
public Option findOption(final String trigger) { |
| 237 | 430 | final Option found = super.findOption(trigger); |
| 238 | |
|
| 239 | 430 | if ((found == null) && (children != null)) { |
| 240 | 3 | return children.findOption(trigger); |
| 241 | |
} else { |
| 242 | 427 | return found; |
| 243 | |
} |
| 244 | |
} |
| 245 | |
|
| 246 | |
public void defaults(final WriteableCommandLine commandLine) { |
| 247 | 265 | super.defaults(commandLine); |
| 248 | |
|
| 249 | 265 | if (argument != null) { |
| 250 | 82 | argument.defaultValues(commandLine, this); |
| 251 | |
} |
| 252 | |
|
| 253 | 265 | if (children != null) { |
| 254 | 5 | children.defaults(commandLine); |
| 255 | |
} |
| 256 | 265 | } |
| 257 | |
} |