Skip to content

Commit 5470bca

Browse files
author
John Keyes
committed
fix bug 14786, some refactorings
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129852 13f79535-47bb-0310-9956-ffa450edef68
1 parent beb276c commit 5470bca

5 files changed

Lines changed: 204 additions & 144 deletions

File tree

src/java/org/apache/commons/cli/Option.java

Lines changed: 161 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/java/org/apache/commons/cli/Options.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,19 @@ public Options addOption(String opt, String longOpt, boolean hasArg, String desc
165165
* @return the resulting Options instance
166166
*/
167167
public Options addOption(Option opt) {
168-
String shortOpt = opt.getOpt();
168+
String key = opt.getKey();
169169

170-
// add it to the long option list
171-
if ( opt.hasLongOpt() ) {
172-
longOpts.put( opt.getLongOpt(), opt );
173-
}
170+
// add it to the long option list
171+
if ( opt.hasLongOpt() ) {
172+
longOpts.put( opt.getLongOpt(), opt );
173+
}
174174

175-
// if the option is required add it to the required list
176-
if ( opt.isRequired() ) {
177-
requiredOpts.add( opt.getKey() );
178-
}
175+
// if the option is required add it to the required list
176+
if ( opt.isRequired() && !requiredOpts.contains( key ) ) {
177+
requiredOpts.add( key );
178+
}
179+
shortOpts.put( key, opt );
179180

180-
shortOpts.put( shortOpt, opt );
181-
182181
return this;
183182
}
184183

src/java/org/apache/commons/cli/Parser.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/Parser.java,v 1.8 2002/11/18 08:41:26 jkeyes Exp $
3-
* $Revision: 1.8 $
4-
* $Date: 2002/11/18 08:41:26 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/Parser.java,v 1.9 2002/11/25 23:43:40 jkeyes Exp $
3+
* $Revision: 1.9 $
4+
* $Date: 2002/11/25 23:43:40 $
55
*
66
* ====================================================================
77
*
@@ -74,7 +74,7 @@
7474
*
7575
* @author John Keyes (john at integralsource.com)
7676
* @see Parser
77-
* @version $Revision: 1.8 $
77+
* @version $Revision: 1.9 $
7878
*/
7979
public abstract class Parser implements CommandLineParser {
8080

@@ -299,9 +299,14 @@ public void processArgs( Option opt, ListIterator iter )
299299
break;
300300
}
301301
// found a value
302-
else if( !opt.addValue( str ) ) {
303-
iter.previous();
304-
break;
302+
else {
303+
try {
304+
opt.addValue( str ) ;
305+
}
306+
catch( RuntimeException exp ) {
307+
iter.previous();
308+
break;
309+
}
305310
}
306311
}
307312

0 commit comments

Comments
 (0)