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 public class DefaultingCommandLine extends CommandLineImpl {
42
43
44
45
46 private final List commandLines = new ArrayList();
47
48
49
50
51
52
53
54
55
56 public void appendCommandLine(final CommandLine commandLine) {
57 commandLines.add(commandLine);
58 }
59
60
61
62
63
64
65
66 public void insertCommandLine(
67 final int index,
68 final CommandLine commandLine) {
69 commandLines.add(index, commandLine);
70 }
71
72
73
74
75
76
77 public Iterator commandLines(){
78 return Collections.unmodifiableList(commandLines).iterator();
79 }
80
81 public Option getOption(String trigger) {
82 for (final Iterator i = commandLines.iterator(); i.hasNext();) {
83 final CommandLine commandLine = (CommandLine)i.next();
84 final Option actual = commandLine.getOption(trigger);
85 if (actual != null) {
86 return actual;
87 }
88 }
89 return null;
90 }
91
92 public List getOptions() {
93 final List options = new ArrayList();
94
95 final List temp = new ArrayList();
96 for (final Iterator i = commandLines.iterator(); i.hasNext();) {
97 final CommandLine commandLine = (CommandLine)i.next();
98 temp.clear();
99 temp.addAll(commandLine.getOptions());
100 temp.removeAll(options);
101 options.addAll(temp);
102 }
103
104 return Collections.unmodifiableList(options);
105 }
106
107 public Set getOptionTriggers() {
108 final Set all = new HashSet();
109 for (final Iterator i = commandLines.iterator(); i.hasNext();) {
110 final CommandLine commandLine = (CommandLine)i.next();
111 all.addAll(commandLine.getOptionTriggers());
112 }
113
114 return Collections.unmodifiableSet(all);
115 }
116
117 public boolean hasOption(Option option) {
118 for (final Iterator i = commandLines.iterator(); i.hasNext();) {
119 final CommandLine commandLine = (CommandLine)i.next();
120 if (commandLine.hasOption(option)) {
121 return true;
122 }
123 }
124 return false;
125 }
126
127 public List getValues(Option option, List defaultValues) {
128 for (final Iterator i = commandLines.iterator(); i.hasNext();) {
129 final CommandLine commandLine = (CommandLine)i.next();
130 final List actual = commandLine.getValues(option);
131 if (actual != null && !actual.isEmpty()) {
132 return actual;
133 }
134 }
135 if(defaultValues==null){
136 return Collections.EMPTY_LIST;
137 }
138 else{
139 return defaultValues;
140 }
141 }
142
143 public Boolean getSwitch(Option option, Boolean defaultValue) {
144 for (final Iterator i = commandLines.iterator(); i.hasNext();) {
145 final CommandLine commandLine = (CommandLine)i.next();
146 final Boolean actual = commandLine.getSwitch(option);
147 if (actual != null) {
148 return actual;
149 }
150 }
151 return defaultValue;
152 }
153
154 public String getProperty(final String property) {
155 return getProperty(new PropertyOption(), property);
156 }
157
158 public String getProperty(final Option option, String property, String defaultValue) {
159 for (final Iterator i = commandLines.iterator(); i.hasNext();) {
160 final CommandLine commandLine = (CommandLine)i.next();
161 final String actual = commandLine.getProperty(option, property);
162 if (actual != null) {
163 return actual;
164 }
165 }
166 return defaultValue;
167 }
168
169 public Set getProperties(final Option option) {
170 final Set all = new HashSet();
171 for (final Iterator i = commandLines.iterator(); i.hasNext();) {
172 final CommandLine commandLine = (CommandLine)i.next();
173 all.addAll(commandLine.getProperties(option));
174 }
175 return Collections.unmodifiableSet(all);
176 }
177
178 public Set getProperties() {
179 return getProperties(new PropertyOption());
180 }
181 }