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 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 this(DEFAULT_OPTION_STRING, DEFAULT_DESCRIPTION, 'D');
54 }
55
56
57
58
59
60
61
62 public PropertyOption(final String optionString,
63 final String description,
64 final int id) {
65 super(id, false);
66 this.optionString = optionString;
67 this.description = description;
68 this.prefixes = Collections.singleton(optionString);
69 }
70
71 public boolean canProcess(final WriteableCommandLine commandLine,
72 final String argument) {
73 return (argument != null) && argument.startsWith(optionString) &&
74 (argument.length() > optionString.length());
75 }
76
77 public Set getPrefixes() {
78 return prefixes;
79 }
80
81 public void process(final WriteableCommandLine commandLine,
82 final ListIterator arguments)
83 throws OptionException {
84 final String arg = (String) arguments.next();
85
86 if (!canProcess(commandLine, arg)) {
87 throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg);
88 }
89
90 final int propertyStart = optionString.length();
91 final int equalsIndex = arg.indexOf('=', propertyStart);
92 final String property;
93 final String value;
94
95 if (equalsIndex < 0) {
96 property = arg.substring(propertyStart);
97 value = "true";
98 } else {
99 property = arg.substring(propertyStart, equalsIndex);
100 value = arg.substring(equalsIndex + 1);
101 }
102
103 commandLine.addProperty(this, property, value);
104 }
105
106 public Set getTriggers() {
107 return Collections.singleton(optionString);
108 }
109
110 public void validate(WriteableCommandLine commandLine) {
111
112 }
113
114 public void appendUsage(final StringBuffer buffer,
115 final Set helpSettings,
116 final Comparator comp) {
117 final boolean display = helpSettings.contains(DisplaySetting.DISPLAY_PROPERTY_OPTION);
118
119 final boolean bracketed = helpSettings.contains(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);
120
121 if (display) {
122 buffer.append(optionString);
123
124 if (bracketed) {
125 buffer.append('<');
126 }
127
128 buffer.append("property");
129
130 if (bracketed) {
131 buffer.append('>');
132 }
133
134 buffer.append("=");
135
136 if (bracketed) {
137 buffer.append('<');
138 }
139
140 buffer.append("value");
141
142 if (bracketed) {
143 buffer.append('>');
144 }
145 }
146 }
147
148 public String getPreferredName() {
149 return optionString;
150 }
151
152 public String getDescription() {
153 return description;
154 }
155
156 public List helpLines(final int depth,
157 final Set helpSettings,
158 final Comparator comp) {
159 if (helpSettings.contains(DisplaySetting.DISPLAY_PROPERTY_OPTION)) {
160 final HelpLine helpLine = new HelpLineImpl(this, depth);
161
162 return Collections.singletonList(helpLine);
163 } else {
164 return Collections.EMPTY_LIST;
165 }
166 }
167 }