@@ -287,140 +287,178 @@ public String getDescription() {
287287 return this .description ;
288288 }
289289
290- /**
291- * <p>Query to see if this Option requires an argument</p>
292- *
293- * @return boolean flag indicating if an argument is required
294- */
295- public boolean isRequired () {
296- return this .required ;
297- }
298-
299- /**
300- * <p>Sets whether this Option is mandatory.</p>
301- *
302- * @param required specifies whether this Option is mandatory
303- */
304- public void setRequired ( boolean required ) {
305- this .required = required ;
306- }
307-
308- /**
309- * <p>Sets the display name for the argument value.</p>
310- *
311- * @param argName the display name for the argument value.
312- */
313- public void setArgName ( String argName ) {
314- this .argName = argName ;
315- }
316-
317- /**
318- * <p>Gets the display name for the argument value.</p>
319- *
320- * @return the display name for the argument value.
321- */
322- public String getArgName () {
323- return this .argName ;
324- }
325-
326- /**
327- * <p>Returns whether the display name for the argument value
328- * has been set.</p>
329- *
330- * @return if the display name for the argument value has been
331- * set.
332- */
333- public boolean hasArgName () {
334- return (this .argName != null && this .argName .length () > 0 );
335- }
336-
337- /**
338- * <p>Query to see if this Option can take many values</p>
339- *
340- * @return boolean flag indicating if multiple values are allowed
341- */
342- public boolean hasArgs () {
343- return ( this .numberOfArgs > 1 || this .numberOfArgs == UNLIMITED_VALUES );
344- }
345-
346- /**
347- * <p>Sets the number of argument values this Option can take.</p>
348- *
349- * @param num the number of argument values
350- */
351- public void setArgs ( int num ) {
352- this .numberOfArgs = num ;
353- }
354-
355- /**
356- * <p>Sets the value separator. For example if the argument value
357- * was a Java property, the value separator would be '='.</p>
358- *
359- * @param sep The value separator.
360- */
361- public void setValueSeparator ( char sep ) {
362- this .valuesep = sep ;
363- }
364-
365- /**
366- * <p>Returns the value separator character.</p>
367- *
368- * @return the value separator character.
369- */
370- public char getValueSeparator () {
371- return this .valuesep ;
372- }
373-
374- /**
375- * <p>Returns the number of argument values this Option can take.</p>
376- *
377- * @return num the number of argument values
378- */
379- public int getArgs ( ) {
380- return this .numberOfArgs ;
381- }
382-
383- public void clearValues () {
384- this .values .clear ();
290+ /**
291+ * <p>Query to see if this Option requires an argument</p>
292+ *
293+ * @return boolean flag indicating if an argument is required
294+ */
295+ public boolean isRequired () {
296+ return this .required ;
297+ }
298+
299+ /**
300+ * <p>Sets whether this Option is mandatory.</p>
301+ *
302+ * @param required specifies whether this Option is mandatory
303+ */
304+ public void setRequired ( boolean required ) {
305+ this .required = required ;
306+ }
307+
308+ /**
309+ * <p>Sets the display name for the argument value.</p>
310+ *
311+ * @param argName the display name for the argument value.
312+ */
313+ public void setArgName ( String argName ) {
314+ this .argName = argName ;
315+ }
316+
317+ /**
318+ * <p>Gets the display name for the argument value.</p>
319+ *
320+ * @return the display name for the argument value.
321+ */
322+ public String getArgName () {
323+ return this .argName ;
324+ }
325+
326+ /**
327+ * <p>Returns whether the display name for the argument value
328+ * has been set.</p>
329+ *
330+ * @return if the display name for the argument value has been
331+ * set.
332+ */
333+ public boolean hasArgName () {
334+ return (this .argName != null && this .argName .length () > 0 );
335+ }
336+
337+ /**
338+ * <p>Query to see if this Option can take many values</p>
339+ *
340+ * @return boolean flag indicating if multiple values are allowed
341+ */
342+ public boolean hasArgs () {
343+ return this .numberOfArgs > 1 || this .numberOfArgs == UNLIMITED_VALUES ;
344+ }
345+
346+ /**
347+ * <p>Sets the number of argument values this Option can take.</p>
348+ *
349+ * @param num the number of argument values
350+ */
351+ public void setArgs ( int num ) {
352+ this .numberOfArgs = num ;
353+ }
354+
355+ /**
356+ * <p>Sets the value separator. For example if the argument value
357+ * was a Java property, the value separator would be '='.</p>
358+ *
359+ * @param sep The value separator.
360+ */
361+ public void setValueSeparator ( char sep ) {
362+ this .valuesep = sep ;
363+ }
364+
365+ /**
366+ * <p>Returns the value separator character.</p>
367+ *
368+ * @return the value separator character.
369+ */
370+ public char getValueSeparator () {
371+ return this .valuesep ;
372+ }
373+
374+ /**
375+ * ...
376+ */
377+ public boolean hasValueSeparator () {
378+ return ( this .valuesep > 0 );
379+ }
380+
381+ /**
382+ * <p>Returns the number of argument values this Option can take.</p>
383+ *
384+ * @return num the number of argument values
385+ */
386+ public int getArgs ( ) {
387+ return this .numberOfArgs ;
385388 }
386389
387390 /**
388391 * <p>Adds the specified value to this Option.</p>
389392 *
390393 * @param value is a/the value of this Option
391394 */
392- public boolean addValue ( String value ) {
393-
395+ void addValue ( String value )
396+ {
394397 switch ( numberOfArgs ) {
395398 case UNINITIALIZED :
396- return false ;
397- case UNLIMITED_VALUES :
398- if ( getValueSeparator () > 0 ) {
399- int index = 0 ;
400- while ( (index = value .indexOf ( getValueSeparator () ) ) != -1 ) {
401- this .values .add ( value .substring ( 0 , index ) );
402- value = value .substring ( index +1 );
403- }
404- }
405- this .values .add ( value );
406- return true ;
399+ break ;
407400 default :
408- if ( getValueSeparator () > 0 ) {
409- int index = 0 ;
410- while ( (index = value .indexOf ( getValueSeparator () ) ) != -1 ) {
411- if ( values .size () > numberOfArgs -1 ) {
412- return false ;
413- }
414- this .values .add ( value .substring ( 0 , index ) );
415- value = value .substring ( index +1 );
416- }
417- }
418- if ( values .size () > numberOfArgs -1 ) {
419- return false ;
420- }
421- this .values .add ( value );
422- return true ;
401+ processValue ( value );
402+ }
403+ }
404+
405+ /**
406+ * <p>Processes the value. If this Option has a value separator
407+ * the value will have to be parsed into individual tokens. When
408+ * n-1 tokens have been processed and there are more value separators
409+ * in the value, parsing is ceased and the remaining characters are
410+ * added as a single token.</p>
411+ *
412+ * @since 1.0.1
413+ */
414+ private void processValue ( String value ) {
415+
416+ // this Option has a separator character
417+ if ( hasValueSeparator () ) {
418+
419+ // get the separator character
420+ char sep = getValueSeparator ();
421+
422+ // store the index for the value separator
423+ int index = value .indexOf ( sep );
424+
425+ // while there are more value separators
426+ while ( index != -1 ) {
427+
428+ // next value to be added
429+ if ( values .size () == numberOfArgs -1 ) {
430+ break ;
431+ }
432+
433+ // store
434+ add ( value .substring ( 0 , index ) );
435+
436+ // parse
437+ value = value .substring ( index +1 );
438+
439+ // get new index
440+ index = value .indexOf ( sep );
441+ }
442+ }
443+
444+ // store the actual value or the last value that has been parsed
445+ add ( value );
446+ }
447+
448+ /**
449+ * <p>Add the value to this Option. If the number of arguments
450+ * is greater than zero and there is enough space in the list then
451+ * add the value. Otherwise, throw a runtime exception.
452+ * </p>
453+ *
454+ * @since 1.0.1
455+ */
456+ private void add ( String value ) {
457+ if ( numberOfArgs > 0 && values .size () > numberOfArgs -1 ) {
458+ throw new RuntimeException ( "Cannot add value, list full." );
423459 }
460+ // store value
461+ this .values .add ( value );
424462 }
425463
426464 /**
0 commit comments