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.Collections; |
20 | |
import java.util.Comparator; |
21 | |
import java.util.List; |
22 | |
import java.util.ListIterator; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
import org.apache.commons.cli2.DisplaySetting; |
26 | |
import org.apache.commons.cli2.HelpLine; |
27 | |
import org.apache.commons.cli2.OptionException; |
28 | |
import org.apache.commons.cli2.WriteableCommandLine; |
29 | |
import org.apache.commons.cli2.resource.ResourceConstants; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class PropertyOption |
35 | |
extends OptionImpl { |
36 | |
public static final String DEFAULT_OPTION_STRING = "-D"; |
37 | |
public static final String DEFAULT_DESCRIPTION = |
38 | |
"Passes properties and values to the application"; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 1 | public static final PropertyOption INSTANCE = new PropertyOption(); |
44 | |
private final String optionString; |
45 | |
private final String description; |
46 | |
private final Set prefixes; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public PropertyOption() { |
53 | 288 | this(DEFAULT_OPTION_STRING, DEFAULT_DESCRIPTION, 'D'); |
54 | 288 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public PropertyOption(final String optionString, |
63 | |
final String description, |
64 | |
final int id) { |
65 | 290 | super(id, false); |
66 | 290 | this.optionString = optionString; |
67 | 290 | this.description = description; |
68 | 290 | this.prefixes = Collections.singleton(optionString); |
69 | 290 | } |
70 | |
|
71 | |
public boolean canProcess(final WriteableCommandLine commandLine, |
72 | |
final String argument) { |
73 | 29 | return (argument != null) && argument.startsWith(optionString) && |
74 | |
(argument.length() > optionString.length()); |
75 | |
} |
76 | |
|
77 | |
public Set getPrefixes() { |
78 | 667 | return prefixes; |
79 | |
} |
80 | |
|
81 | |
public void process(final WriteableCommandLine commandLine, |
82 | |
final ListIterator arguments) |
83 | |
throws OptionException { |
84 | 11 | final String arg = (String) arguments.next(); |
85 | |
|
86 | 11 | if (!canProcess(commandLine, arg)) { |
87 | 1 | throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg); |
88 | |
} |
89 | |
|
90 | 10 | final int propertyStart = optionString.length(); |
91 | 10 | final int equalsIndex = arg.indexOf('=', propertyStart); |
92 | |
final String property; |
93 | |
final String value; |
94 | |
|
95 | 10 | if (equalsIndex < 0) { |
96 | 1 | property = arg.substring(propertyStart); |
97 | 1 | value = "true"; |
98 | |
} else { |
99 | 9 | property = arg.substring(propertyStart, equalsIndex); |
100 | 9 | value = arg.substring(equalsIndex + 1); |
101 | |
} |
102 | |
|
103 | 10 | commandLine.addProperty(this, property, value); |
104 | 10 | } |
105 | |
|
106 | |
public Set getTriggers() { |
107 | 634 | return Collections.singleton(optionString); |
108 | |
} |
109 | |
|
110 | |
public void validate(WriteableCommandLine commandLine) { |
111 | |
|
112 | 1 | } |
113 | |
|
114 | |
public void appendUsage(final StringBuffer buffer, |
115 | |
final Set helpSettings, |
116 | |
final Comparator comp) { |
117 | 4 | final boolean display = helpSettings.contains(DisplaySetting.DISPLAY_PROPERTY_OPTION); |
118 | |
|
119 | 4 | final boolean bracketed = helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED); |
120 | |
|
121 | 4 | if (display) { |
122 | 3 | buffer.append(optionString); |
123 | |
|
124 | 3 | if (bracketed) { |
125 | 2 | buffer.append('<'); |
126 | |
} |
127 | |
|
128 | 3 | buffer.append("property"); |
129 | |
|
130 | 3 | if (bracketed) { |
131 | 2 | buffer.append('>'); |
132 | |
} |
133 | |
|
134 | 3 | buffer.append("="); |
135 | |
|
136 | 3 | if (bracketed) { |
137 | 2 | buffer.append('<'); |
138 | |
} |
139 | |
|
140 | 3 | buffer.append("value"); |
141 | |
|
142 | 3 | if (bracketed) { |
143 | 2 | buffer.append('>'); |
144 | |
} |
145 | |
} |
146 | 4 | } |
147 | |
|
148 | |
public String getPreferredName() { |
149 | 1081 | return optionString; |
150 | |
} |
151 | |
|
152 | |
public String getDescription() { |
153 | 1082 | return description; |
154 | |
} |
155 | |
|
156 | |
public List helpLines(final int depth, |
157 | |
final Set helpSettings, |
158 | |
final Comparator comp) { |
159 | 3 | if (helpSettings.contains(DisplaySetting.DISPLAY_PROPERTY_OPTION)) { |
160 | 2 | final HelpLine helpLine = new HelpLineImpl(this, depth); |
161 | |
|
162 | 2 | return Collections.singletonList(helpLine); |
163 | |
} else { |
164 | 1 | return Collections.EMPTY_LIST; |
165 | |
} |
166 | |
} |
167 | |
} |