View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Handles the java style "-Dprop=value" opions
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       * A default PropertyOption instance
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       * Creates a new PropertyOption using the default settings of a "-D" trigger
50       * and an id of 'D'
51       */
52      public PropertyOption() {
53          this(DEFAULT_OPTION_STRING, DEFAULT_DESCRIPTION, 'D');
54      }
55  
56      /**
57       * Creates a new PropertyOption using the specified parameters
58       * @param optionString the trigger for the Option
59       * @param description the description of the Option
60       * @param id the id of the Option
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         // PropertyOption needs no validation
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 }