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 this(source, dest, DEFAULT_INITIAL_SEPARATOR, DEFAULT_SUBSEQUENT_SEPARATOR,
52 DEFAULT_CONSUME_REMAINING, null);
53 }
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 super("SourceDestArgument", null, sum(source.getMinimum(), dest.getMinimum()),
72 sum(source.getMaximum(), dest.getMaximum()), initialSeparator, subsequentSeparator,
73 null, consumeRemaining, defaultValues, 0);
74
75 this.source = source;
76 this.dest = dest;
77
78 if (dest.getMinimum() != dest.getMaximum()) {
79 throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SOURCE_DEST_MUST_ENFORCE_VALUES));
80 }
81 }
82
83 private static int sum(final int a,
84 final int b) {
85 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 final int length = buffer.length();
92
93 source.appendUsage(buffer, helpSettings, comp);
94
95 if (buffer.length() != length) {
96 buffer.append(' ');
97 }
98
99 dest.appendUsage(buffer, helpSettings, comp);
100 }
101
102 public List helpLines(int depth,
103 Set helpSettings,
104 Comparator comp) {
105 final List helpLines = new ArrayList();
106 helpLines.addAll(source.helpLines(depth, helpSettings, comp));
107 helpLines.addAll(dest.helpLines(depth, helpSettings, comp));
108
109 return helpLines;
110 }
111
112 public void validate(WriteableCommandLine commandLine,
113 Option option)
114 throws OptionException {
115 final List values = commandLine.getValues(option);
116
117 final int limit = values.size() - dest.getMinimum();
118 int count = 0;
119
120 final Iterator i = values.iterator();
121
122 while (count++ < limit) {
123 commandLine.addValue(source, i.next());
124 }
125
126 while (i.hasNext()) {
127 commandLine.addValue(dest, i.next());
128 }
129
130 source.validate(commandLine, source);
131 dest.validate(commandLine, dest);
132 }
133
134 public boolean canProcess(final WriteableCommandLine commandLine,
135 final String arg) {
136 return source.canProcess(commandLine, arg) || dest.canProcess(commandLine, arg);
137 }
138 }