| 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.Comparator; |
| 21 | |
import java.util.Iterator; |
| 22 | |
import java.util.List; |
| 23 | |
import java.util.Set; |
| 24 | |
|
| 25 | |
import org.apache.commons.cli2.Argument; |
| 26 | |
import org.apache.commons.cli2.Option; |
| 27 | |
import org.apache.commons.cli2.OptionException; |
| 28 | |
import org.apache.commons.cli2.WriteableCommandLine; |
| 29 | |
import org.apache.commons.cli2.resource.ResourceConstants; |
| 30 | |
import org.apache.commons.cli2.resource.ResourceHelper; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public class SourceDestArgument |
| 39 | |
extends ArgumentImpl { |
| 40 | |
private final Argument source; |
| 41 | |
private final Argument dest; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public SourceDestArgument(final Argument source, |
| 50 | |
final Argument dest) { |
| 51 | 7 | this(source, dest, DEFAULT_INITIAL_SEPARATOR, DEFAULT_SUBSEQUENT_SEPARATOR, |
| 52 | |
DEFAULT_CONSUME_REMAINING, null); |
| 53 | 6 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public SourceDestArgument(final Argument source, |
| 66 | |
final Argument dest, |
| 67 | |
final char initialSeparator, |
| 68 | |
final char subsequentSeparator, |
| 69 | |
final String consumeRemaining, |
| 70 | |
final List defaultValues) { |
| 71 | 7 | super("SourceDestArgument", null, sum(source.getMinimum(), dest.getMinimum()), |
| 72 | |
sum(source.getMaximum(), dest.getMaximum()), initialSeparator, subsequentSeparator, |
| 73 | |
null, consumeRemaining, defaultValues, 0); |
| 74 | |
|
| 75 | 7 | this.source = source; |
| 76 | 7 | this.dest = dest; |
| 77 | |
|
| 78 | 7 | if (dest.getMinimum() != dest.getMaximum()) { |
| 79 | 1 | throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SOURCE_DEST_MUST_ENFORCE_VALUES)); |
| 80 | |
} |
| 81 | 6 | } |
| 82 | |
|
| 83 | |
private static int sum(final int a, |
| 84 | |
final int b) { |
| 85 | 14 | return Math.max(a, Math.max(b, a + b)); |
| 86 | |
} |
| 87 | |
|
| 88 | |
public void appendUsage(final StringBuffer buffer, |
| 89 | |
final Set helpSettings, |
| 90 | |
final Comparator comp) { |
| 91 | 2 | final int length = buffer.length(); |
| 92 | |
|
| 93 | 2 | source.appendUsage(buffer, helpSettings, comp); |
| 94 | |
|
| 95 | 2 | if (buffer.length() != length) { |
| 96 | 1 | buffer.append(' '); |
| 97 | |
} |
| 98 | |
|
| 99 | 2 | dest.appendUsage(buffer, helpSettings, comp); |
| 100 | 2 | } |
| 101 | |
|
| 102 | |
public List helpLines(int depth, |
| 103 | |
Set helpSettings, |
| 104 | |
Comparator comp) { |
| 105 | 1 | final List helpLines = new ArrayList(); |
| 106 | 1 | helpLines.addAll(source.helpLines(depth, helpSettings, comp)); |
| 107 | 1 | helpLines.addAll(dest.helpLines(depth, helpSettings, comp)); |
| 108 | |
|
| 109 | 1 | return helpLines; |
| 110 | |
} |
| 111 | |
|
| 112 | |
public void validate(WriteableCommandLine commandLine, |
| 113 | |
Option option) |
| 114 | |
throws OptionException { |
| 115 | 3 | final List values = commandLine.getValues(option); |
| 116 | |
|
| 117 | 3 | final int limit = values.size() - dest.getMinimum(); |
| 118 | 3 | int count = 0; |
| 119 | |
|
| 120 | 3 | final Iterator i = values.iterator(); |
| 121 | |
|
| 122 | 7 | while (count++ < limit) { |
| 123 | 4 | commandLine.addValue(source, i.next()); |
| 124 | |
} |
| 125 | |
|
| 126 | 5 | while (i.hasNext()) { |
| 127 | 2 | commandLine.addValue(dest, i.next()); |
| 128 | |
} |
| 129 | |
|
| 130 | 3 | source.validate(commandLine, source); |
| 131 | 2 | dest.validate(commandLine, dest); |
| 132 | 2 | } |
| 133 | |
|
| 134 | |
public boolean canProcess(final WriteableCommandLine commandLine, |
| 135 | |
final String arg) { |
| 136 | 5 | return source.canProcess(commandLine, arg) || dest.canProcess(commandLine, arg); |
| 137 | |
} |
| 138 | |
} |