Skip to content

Commit f9df518

Browse files
author
John Keyes
committed
iterator for all Option instances parsed
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129799 13f79535-47bb-0310-9956-ffa450edef68
1 parent c7bde89 commit f9df518

8 files changed

Lines changed: 159 additions & 111 deletions

File tree

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ public class CommandLine {
8989
/** the unrecognised options/arguments */
9090
private List args = new LinkedList();
9191

92-
/** the recognised options */
93-
private Map options = new HashMap();
92+
/** the processed options */
93+
private Map options = new HashMap();
94+
95+
/** Map of unique options for ease to get complete list of options */
96+
private Map hashcodeMap = new HashMap();
9497

9598
/** the processed options */
9699
private Option[] optionsArray;
@@ -125,7 +128,8 @@ public boolean hasOption( char opt ) {
125128
*/
126129
public Object getOptionObject( String opt ) {
127130
String res = getOptionValue( opt );
128-
Object type = ((Option)options.get( opt )).getType();
131+
132+
Object type = ((Option)((List)options.get(opt)).iterator().next()).getType();
129133
return res == null ? null : TypeHandler.createValue(res, type);
130134
}
131135

@@ -143,7 +147,8 @@ public Object getOptionObject( char opt ) {
143147
* @return Value of the argument if option is set, and has an argument, else null.
144148
*/
145149
public String getOptionValue( String opt ) {
146-
return (String)((Option)options.get( opt )).getValue();
150+
String[] values = getOptionValues(opt);
151+
return (values == null) ? null : values[0];
147152
}
148153

149154
/** <p>Retrieve the argument, if any, of an option.</p>
@@ -161,7 +166,16 @@ public String getOptionValue( char opt ) {
161166
* @return An array of values if the option is set, and has an argument, else null.
162167
*/
163168
public String[] getOptionValues( String opt ) {
164-
return (String[])((Option)options.get( opt )).getValues();
169+
List values = new java.util.ArrayList();
170+
171+
List opts = (List)options.get( opt );
172+
Iterator iter = opts.iterator();
173+
174+
while( iter.hasNext() ) {
175+
Option optt = (Option)iter.next();
176+
values.addAll( optt.getValuesList() );
177+
}
178+
return (values.size() == 0) ? null : (String[])values.toArray(new String[]{});
165179
}
166180

167181
/** <p>Retrieves the array of values, if any, of an option.</p>
@@ -212,10 +226,14 @@ public List getArgList() {
212226
return args;
213227
}
214228

215-
/** <p>Dump state, suitable for debugging.</p>
229+
/**
230+
* jkeyes
231+
* - commented out until it is implemented properly
232+
* <p>Dump state, suitable for debugging.</p>
216233
*
217234
* @return Stringified form of this object
218235
*/
236+
/*
219237
public String toString() {
220238
StringBuffer buf = new StringBuffer();
221239
@@ -227,7 +245,8 @@ public String toString() {
227245
228246
return buf.toString();
229247
}
230-
248+
*/
249+
231250
/**
232251
* <p>Add left-over unrecognized option/argument.</p>
233252
*
@@ -244,7 +263,15 @@ void addArg(String arg) {
244263
* @param opt the processed option
245264
*/
246265
void setOpt( Option opt ) {
247-
options.put( opt.getOpt(), opt );
266+
hashcodeMap.put( new Integer( opt.hashCode() ), opt );
267+
268+
if( options.get( opt.getOpt() ) != null ) {
269+
((java.util.List)options.get( opt.getOpt() )).add( opt );
270+
}
271+
else {
272+
options.put( opt.getOpt(), new java.util.ArrayList() );
273+
((java.util.List)options.get( opt.getOpt() ) ).add( opt );
274+
}
248275
}
249276

250277
/**
@@ -254,7 +281,7 @@ void setOpt( Option opt ) {
254281
* members of this {@link CommandLine}
255282
*/
256283
public Iterator iterator( ) {
257-
return options.values().iterator();
284+
return hashcodeMap.values().iterator();
258285
}
259286

260287
/**
@@ -263,7 +290,7 @@ public Iterator iterator( ) {
263290
* @return an array of the processed {@link Option}s.
264291
*/
265292
public Option[] getOptions( ) {
266-
Collection processed = options.values();
293+
Collection processed = hashcodeMap.values();
267294

268295
// reinitialise array
269296
optionsArray = new Option[ processed.size() ];

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

Lines changed: 9 additions & 27 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/GnuParser.java,v 1.4 2002/08/04 23:04:52 jkeyes Exp $
3-
* $Revision: 1.4 $
4-
* $Date: 2002/08/04 23:04:52 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/GnuParser.java,v 1.5 2002/08/14 22:27:39 jkeyes Exp $
3+
* $Revision: 1.5 $
4+
* $Date: 2002/08/14 22:27:39 $
55
*
66
* ====================================================================
77
*
@@ -64,6 +64,7 @@
6464
import java.util.Collection;
6565
import java.util.Iterator;
6666
import java.util.ListIterator;
67+
import java.util.Map;
6768

6869
/**
6970
* GnuParser parses the command line arguments using the GNU style.
@@ -81,7 +82,7 @@ public class GnuParser implements CommandLineParser {
8182
private CommandLine cmd;
8283

8384
/** required options subset of options */
84-
private Collection requiredOptions;
85+
private Map requiredOptions;
8586

8687
/**
8788
* Parse the arguments according to the specified options.
@@ -197,28 +198,9 @@ public void processArgs( Option opt, ListIterator iter )
197198
break;
198199
}
199200
// its a value
200-
else {
201-
char sep = opt.getValueSeparator();
202-
203-
if( sep > 0 ) {
204-
int findex;
205-
while( ( findex = var.indexOf( sep ) ) != -1 ) {
206-
String val = var.substring( 0, findex );
207-
var = var.substring( findex + 1);
208-
if( !opt.addValue( val ) ) {
209-
iter.previous();
210-
return;
211-
}
212-
}
213-
if( !opt.addValue( var ) ) {
214-
iter.previous();
215-
return;
216-
};
217-
}
218-
else if( !opt.addValue( var ) ) {
219-
iter.previous();
220-
return;
221-
}
201+
else if( !opt.addValue( var ) ) {
202+
iter.previous();
203+
return;
222204
}
223205
}
224206
}
@@ -299,7 +281,7 @@ private void checkRequiredOptions( )
299281
// if there are required options that have not been
300282
// processsed
301283
if( requiredOptions.size() > 0 ) {
302-
Iterator iter = requiredOptions.iterator();
284+
Iterator iter = requiredOptions.values().iterator();
303285
StringBuffer buff = new StringBuffer();
304286

305287
// loop through the required options

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
* @version $Revision: 1.6 $
9090
*/
9191

92-
public class Option {
92+
public class Option implements Cloneable {
9393

9494
/** opt the single character representation of the option */
9595
private String opt = null;
@@ -368,9 +368,26 @@ public boolean addValue( String value ) {
368368
case UNINITIALIZED:
369369
return false;
370370
case UNLIMITED_VALUES:
371+
if( getValueSeparator() > 0 ) {
372+
int index = 0;
373+
while( (index = value.indexOf( getValueSeparator() ) ) != -1 ) {
374+
this.values.add( value.substring( 0, index ) );
375+
value = value.substring( index+1 );
376+
}
377+
}
371378
this.values.add( value );
372379
return true;
373380
default:
381+
if( getValueSeparator() > 0 ) {
382+
int index = 0;
383+
while( (index = value.indexOf( getValueSeparator() ) ) != -1 ) {
384+
if( values.size() > numberOfArgs-1 ) {
385+
return false;
386+
}
387+
this.values.add( value.substring( 0, index ) );
388+
value = value.substring( index+1 );
389+
}
390+
}
374391
if( values.size() > numberOfArgs-1 ) {
375392
return false;
376393
}
@@ -413,4 +430,18 @@ public String getValue( String defaultValue ) {
413430
public String[] getValues() {
414431
return this.values.size()==0 ? null : (String[])this.values.toArray(new String[]{});
415432
}
433+
434+
public java.util.List getValuesList() {
435+
return this.values;
436+
}
437+
438+
public Object clone() {
439+
Option option = new Option( getOpt(), getDescription() );
440+
option.setArgs( getArgs() );
441+
option.setRequired( isRequired() );
442+
option.setLongOpt( getLongOpt() );
443+
option.setType( getType() );
444+
option.setValueSeparator( getValueSeparator() );
445+
return option;
446+
}
416447
}

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

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class OptionGroup {
7575
private HashMap optionMap = new HashMap();
7676

7777
/** the name of the selected option */
78-
private Option selected;
78+
private String selected;
7979

8080
/**
8181
* add <code>opt</code> to this group
@@ -117,8 +117,9 @@ public void setSelected(Option opt) throws AlreadySelectedException {
117117
// if no option has already been selected or the
118118
// same option is being reselected then set the
119119
// selected member variable
120-
if ( this.selected == null || this.selected.equals( opt ) ) {
121-
this.selected = opt;
120+
121+
if ( this.selected == null || this.selected.equals( opt.getOpt() ) ) {
122+
this.selected = opt.getOpt();
122123
}
123124
else {
124125
throw new AlreadySelectedException( "an option from this group has " +
@@ -130,51 +131,10 @@ public void setSelected(Option opt) throws AlreadySelectedException {
130131
/**
131132
* @return the selected option name
132133
*/
133-
public Option getSelected() {
134+
public String getSelected() {
134135
return selected;
135136
}
136137

137-
/**
138-
* @return the usage string for this option group
139-
*/
140-
/*
141-
public String usageString()
142-
{
143-
StringBuffer buff = new StringBuffer();
144-
145-
buff.append( "<\n");
146-
147-
Iterator oiter = getOptions().iterator();
148-
149-
while( oiter.hasNext() )
150-
{
151-
Option option = (Option)oiter.next();
152-
Collection names = option.getNames();
153-
154-
Iterator iter = names.iterator();
155-
156-
while( iter.hasNext() )
157-
{
158-
buff.append( option.getPrefix() );
159-
buff.append( iter.next() );
160-
if( iter.hasNext() )
161-
{
162-
buff.append( " | " );
163-
}
164-
}
165-
buff.append( " " );
166-
buff.append( option.getDescription( ) );
167-
if ( oiter.hasNext() )
168-
{
169-
buff.append( "\n or\n" );
170-
}
171-
}
172-
buff.append( "\n>");
173-
buff.append( "\n" );
174-
return buff.toString();
175-
}
176-
*/
177-
178138
/**
179139
* <p>Returns the stringified version of this OptionGroup.</p>
180140
* @return the stringified representation of this group

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public Options addOptionGroup( OptionGroup group ) {
113113
while( options.hasNext() ) {
114114
Option option = (Option)options.next();
115115
addOption( option );
116-
optionGroups.put( option, group );
116+
117+
optionGroups.put( option.getOpt(), group );
117118
}
118119

119120
return this;
@@ -183,28 +184,48 @@ public Collection getOptions() {
183184
*
184185
* @return Collection of required options
185186
*/
186-
public Collection getRequiredOptions() {
187-
return requiredOpts.values();
187+
public Map getRequiredOptions() {
188+
return requiredOpts;
188189
}
189190

190191
/** <p>Retrieve the named {@link Option}</p>
191192
*
192193
* @param opt short single-character name of the {@link Option}
193194
* @return the option represented by opt
194195
*/
195-
public Option getOption(String opt) {
196+
public Option getOption( String opt ) {
197+
198+
Option option = null;
199+
200+
// short option
201+
if( opt.length() == 1 ) {
202+
option = (Option)shortOpts.get( "-" + opt );
203+
}
204+
// long option
205+
else if( opt.startsWith( "--" ) ) {
206+
option = (Option)longOpts.get( opt );
207+
}
208+
// a just-in-case
209+
else {
210+
option = (Option)shortOpts.get( opt );
211+
}
212+
213+
return (option == null) ? null : (Option)option.clone();
214+
}
215+
216+
boolean hasOption(String opt) {
196217

197218
// short option
198219
if( opt.length() == 1 ) {
199-
return (Option) shortOpts.get( "-" + opt );
220+
return shortOpts.containsKey( "-" + opt );
200221
}
201222
// long option
202223
else if( opt.startsWith( "--" ) ) {
203-
return (Option) longOpts.get( opt );
224+
return longOpts.containsKey( opt );
204225
}
205226
// a just-in-case
206227
else {
207-
return (Option) shortOpts.get( opt );
228+
return shortOpts.containsKey( opt );
208229
}
209230
}
210231

@@ -216,7 +237,7 @@ else if( opt.startsWith( "--" ) ) {
216237
* of an OptionGroup, otherwise return null
217238
*/
218239
public OptionGroup getOptionGroup( Option opt ) {
219-
return (OptionGroup)optionGroups.get( opt );
240+
return (OptionGroup)optionGroups.get( opt.getOpt() );
220241
}
221242

222243
/** <p>Dump state, suitable for debugging.</p>

0 commit comments

Comments
 (0)