@@ -170,29 +170,40 @@ public Object getOptionObject(final String opt) {
170170 }
171171 }
172172
173+ /**
174+ * Parses a list of values as properties. All odd numbered values are property keys
175+ * and even numbered values are property values. If there are an odd number of values
176+ * the last value is assumed to be a boolean with a value of "true".
177+ * @param props the properties to update.
178+ * @param values the list of values to parse.
179+ * @since 1.7.0
180+ */
181+ private void processPropertiesFromValues (final Properties props , final List <String > values ) {
182+ for (int i = 0 ; i < values .size (); i += 2 ) {
183+ if (i + 1 < values .size ()) {
184+ props .put (values .get (i ), values .get (i + 1 ));
185+ } else {
186+ props .put (values .get (i ), "true" );
187+ }
188+ }
189+ }
190+
173191 /**
174192 * Gets the map of values associated to the option. This is convenient for options specifying Java properties like
175193 * <code>-Dparam1=value1
176- * -Dparam2=value2</code>. The first argument of the option is the key, and the 2nd argument is the value. If the option
177- * has only one argument ({@code -Dfoo}) it is considered as a boolean flag and the value is {@code "true"}.
194+ * -Dparam2=value2</code>. All odd numbered values are property keys
195+ * and even numbered values are property values. If there are an odd number of values
196+ * the last value is assumed to be a boolean flag and the value is "true".
178197 *
179198 * @param option name of the option.
180199 * @return The Properties mapped by the option, never {@code null} even if the option doesn't exists.
181200 * @since 1.5.0
182201 */
183202 public Properties getOptionProperties (final Option option ) {
184203 final Properties props = new Properties ();
185-
186204 for (final Option processedOption : options ) {
187205 if (processedOption .equals (option )) {
188- final List <String > values = processedOption .getValuesList ();
189- if (values .size () >= 2 ) {
190- // use the first 2 arguments as the key/value pair
191- props .put (values .get (0 ), values .get (1 ));
192- } else if (values .size () == 1 ) {
193- // no explicit value, handle it as a boolean
194- props .put (values .get (0 ), "true" );
195- }
206+ processPropertiesFromValues (props , processedOption .getValuesList ());
196207 }
197208 }
198209
@@ -211,17 +222,9 @@ public Properties getOptionProperties(final Option option) {
211222 */
212223 public Properties getOptionProperties (final String opt ) {
213224 final Properties props = new Properties ();
214-
215225 for (final Option option : options ) {
216226 if (opt .equals (option .getOpt ()) || opt .equals (option .getLongOpt ())) {
217- final List <String > values = option .getValuesList ();
218- if (values .size () >= 2 ) {
219- // use the first 2 arguments as the key/value pair
220- props .put (values .get (0 ), values .get (1 ));
221- } else if (values .size () == 1 ) {
222- // no explicit value, handle it as a boolean
223- props .put (values .get (0 ), "true" );
224- }
227+ processPropertiesFromValues (props , option .getValuesList ());
225228 }
226229 }
227230
0 commit comments