1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.cli2.commandline; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Collections; |
21 | |
import java.util.HashSet; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.List; |
24 | |
import java.util.Set; |
25 | |
|
26 | |
import org.apache.commons.cli2.CommandLine; |
27 | |
import org.apache.commons.cli2.Option; |
28 | |
import org.apache.commons.cli2.option.PropertyOption; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 40 | public class DefaultingCommandLine extends CommandLineImpl { |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 40 | private final List commandLines = new ArrayList(); |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public void appendCommandLine(final CommandLine commandLine) { |
57 | 40 | commandLines.add(commandLine); |
58 | 40 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public void insertCommandLine( |
67 | |
final int index, |
68 | |
final CommandLine commandLine) { |
69 | 2 | commandLines.add(index, commandLine); |
70 | 2 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public Iterator commandLines(){ |
78 | 6 | return Collections.unmodifiableList(commandLines).iterator(); |
79 | |
} |
80 | |
|
81 | |
public Option getOption(String trigger) { |
82 | 29 | for (final Iterator i = commandLines.iterator(); i.hasNext();) { |
83 | 27 | final CommandLine commandLine = (CommandLine)i.next(); |
84 | 27 | final Option actual = commandLine.getOption(trigger); |
85 | 27 | if (actual != null) { |
86 | 18 | return actual; |
87 | |
} |
88 | 9 | } |
89 | 11 | return null; |
90 | |
} |
91 | |
|
92 | |
public List getOptions() { |
93 | 5 | final List options = new ArrayList(); |
94 | |
|
95 | 5 | final List temp = new ArrayList(); |
96 | 5 | for (final Iterator i = commandLines.iterator(); i.hasNext();) { |
97 | 5 | final CommandLine commandLine = (CommandLine)i.next(); |
98 | 5 | temp.clear(); |
99 | 5 | temp.addAll(commandLine.getOptions()); |
100 | 5 | temp.removeAll(options); |
101 | 5 | options.addAll(temp); |
102 | 5 | } |
103 | |
|
104 | 5 | return Collections.unmodifiableList(options); |
105 | |
} |
106 | |
|
107 | |
public Set getOptionTriggers() { |
108 | 1 | final Set all = new HashSet(); |
109 | 1 | for (final Iterator i = commandLines.iterator(); i.hasNext();) { |
110 | 2 | final CommandLine commandLine = (CommandLine)i.next(); |
111 | 2 | all.addAll(commandLine.getOptionTriggers()); |
112 | 2 | } |
113 | |
|
114 | 1 | return Collections.unmodifiableSet(all); |
115 | |
} |
116 | |
|
117 | |
public boolean hasOption(Option option) { |
118 | 5 | for (final Iterator i = commandLines.iterator(); i.hasNext();) { |
119 | 5 | final CommandLine commandLine = (CommandLine)i.next(); |
120 | 5 | if (commandLine.hasOption(option)) { |
121 | 3 | return true; |
122 | |
} |
123 | 2 | } |
124 | 2 | return false; |
125 | |
} |
126 | |
|
127 | |
public List getValues(Option option, List defaultValues) { |
128 | 28 | for (final Iterator i = commandLines.iterator(); i.hasNext();) { |
129 | 26 | final CommandLine commandLine = (CommandLine)i.next(); |
130 | 26 | final List actual = commandLine.getValues(option); |
131 | 26 | if (actual != null && !actual.isEmpty()) { |
132 | 16 | return actual; |
133 | |
} |
134 | 10 | } |
135 | 12 | if(defaultValues==null){ |
136 | 1 | return Collections.EMPTY_LIST; |
137 | |
} |
138 | |
else{ |
139 | 11 | return defaultValues; |
140 | |
} |
141 | |
} |
142 | |
|
143 | |
public Boolean getSwitch(Option option, Boolean defaultValue) { |
144 | 8 | for (final Iterator i = commandLines.iterator(); i.hasNext();) { |
145 | 8 | final CommandLine commandLine = (CommandLine)i.next(); |
146 | 8 | final Boolean actual = commandLine.getSwitch(option); |
147 | 8 | if (actual != null) { |
148 | 4 | return actual; |
149 | |
} |
150 | 4 | } |
151 | 4 | return defaultValue; |
152 | |
} |
153 | |
|
154 | |
public String getProperty(final String property) { |
155 | 2 | return getProperty(new PropertyOption(), property); |
156 | |
} |
157 | |
|
158 | |
public String getProperty(final Option option, String property, String defaultValue) { |
159 | 4 | for (final Iterator i = commandLines.iterator(); i.hasNext();) { |
160 | 4 | final CommandLine commandLine = (CommandLine)i.next(); |
161 | 4 | final String actual = commandLine.getProperty(option, property); |
162 | 4 | if (actual != null) { |
163 | 2 | return actual; |
164 | |
} |
165 | 2 | } |
166 | 2 | return defaultValue; |
167 | |
} |
168 | |
|
169 | |
public Set getProperties(final Option option) { |
170 | 1 | final Set all = new HashSet(); |
171 | 1 | for (final Iterator i = commandLines.iterator(); i.hasNext();) { |
172 | 1 | final CommandLine commandLine = (CommandLine)i.next(); |
173 | 1 | all.addAll(commandLine.getProperties(option)); |
174 | 1 | } |
175 | 1 | return Collections.unmodifiableSet(all); |
176 | |
} |
177 | |
|
178 | |
public Set getProperties() { |
179 | 1 | return getProperties(new PropertyOption()); |
180 | |
} |
181 | |
} |