Skip to content

Commit 347bbeb

Browse files
author
John Keyes
committed
refactored the option string handling, added property support for options with an argument value
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129846 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3b8e3de commit 347bbeb

11 files changed

Lines changed: 467 additions & 200 deletions

File tree

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public boolean hasOption( char opt ) {
135135
public Object getOptionObject( String opt ) {
136136
String res = getOptionValue( opt );
137137

138-
Object type = ((Option)((List)options.get(opt)).iterator().next()).getType();
138+
Object type = ((Option)options.get(opt)).getType();
139139
return res == null ? null : TypeHandler.createValue(res, type);
140140
}
141141

@@ -182,21 +182,17 @@ public String getOptionValue( char opt ) {
182182
public String[] getOptionValues( String opt ) {
183183
List values = new java.util.ArrayList();
184184

185+
opt = Util.stripLeadingHyphens( opt );
186+
185187
String key = opt;
186188
if( names.containsKey( opt ) ) {
187189
key = (String)names.get( opt );
188190
}
189191

190192
if( options.containsKey( key ) ) {
191-
List opts = (List)options.get( key );
192-
Iterator iter = opts.iterator();
193-
194-
while( iter.hasNext() ) {
195-
Option optt = (Option)iter.next();
196-
values.addAll( optt.getValuesList() );
197-
}
193+
return ((Option)options.get(key)).getValues();
198194
}
199-
return (values.size() == 0) ? null : (String[])values.toArray(new String[]{});
195+
return null;
200196
}
201197

202198
/**
@@ -294,21 +290,19 @@ void addArg(String arg) {
294290
void addOption( Option opt ) {
295291
hashcodeMap.put( new Integer( opt.hashCode() ), opt );
296292

297-
String key = opt.getOpt();
298-
if( " ".equals(key) ) {
293+
String key = opt.getKey();
294+
if( key == null ) {
299295
key = opt.getLongOpt();
300296
}
301297
else {
302298
names.put( opt.getLongOpt(), key );
303299
}
304300

305-
if( options.get( key ) != null ) {
306-
((java.util.List)options.get( key )).add( opt );
307-
}
308-
else {
309-
options.put( key, new java.util.ArrayList() );
310-
((java.util.List)options.get( key ) ).add( opt );
301+
if( opt.getValues() != null ) {
302+
System.out.println( opt.getKey() + "=" + opt.getValues().length );
311303
}
304+
305+
options.put( key, opt );
312306
}
313307

314308
/**

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

Lines changed: 38 additions & 3 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/CommandLineParser.java,v 1.4 2002/09/19 22:59:43 jkeyes Exp $
3-
* $Revision: 1.4 $
4-
* $Date: 2002/09/19 22:59:43 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/CommandLineParser.java,v 1.5 2002/11/18 08:41:26 jkeyes Exp $
3+
* $Revision: 1.5 $
4+
* $Date: 2002/11/18 08:41:26 $
55
*
66
* ====================================================================
77
*
@@ -60,6 +60,8 @@
6060
*/
6161
package org.apache.commons.cli;
6262

63+
import java.util.Properties;
64+
6365
/**
6466
* A class that implements the <code>CommandLineParser</code> interface
6567
* can parse a String array according to the {@link Options} specified
@@ -75,23 +77,56 @@ public interface CommandLineParser {
7577
* @param options the specified Options
7678
* @param arguments the command line arguments
7779
* @return the list of atomic option and value tokens
80+
*
7881
* @throws ParseException if there are any problems encountered
7982
* while parsing the command line tokens.
8083
*/
8184
public CommandLine parse( Options options, String[] arguments )
8285
throws ParseException;
8386

87+
/**
88+
* Parse the arguments according to the specified options and
89+
* properties.
90+
*
91+
* @param options the specified Options
92+
* @param arguments the command line arguments
93+
* @param properties command line option name-value pairs
94+
* @return the list of atomic option and value tokens
95+
*
96+
* @throws ParseException if there are any problems encountered
97+
* while parsing the command line tokens.
98+
*/
99+
public CommandLine parse( Options options, String[] arguments, Properties props )
100+
throws ParseException;
101+
84102
/**
85103
* Parse the arguments according to the specified options.
86104
*
87105
* @param options the specified Options
88106
* @param arguments the command line arguments
89107
* @param stopAtNonOption specifies whether to continue parsing the
90108
* arguments if a non option is encountered.
109+
*
91110
* @return the list of atomic option and value tokens
92111
* @throws ParseException if there are any problems encountered
93112
* while parsing the command line tokens.
94113
*/
95114
public CommandLine parse( Options options, String[] arguments, boolean stopAtNonOption )
96115
throws ParseException;
116+
117+
/**
118+
* Parse the arguments according to the specified options and
119+
* properties.
120+
*
121+
* @param options the specified Options
122+
* @param arguments the command line arguments
123+
* @param properties command line option name-value pairs
124+
* @param stopAtNonOption specifies whether to continue parsing the
125+
*
126+
* @return the list of atomic option and value tokens
127+
* @throws ParseException if there are any problems encountered
128+
* while parsing the command line tokens.
129+
*/
130+
public CommandLine parse( Options options, String[] arguments, Properties properties, boolean stopAtNonOption)
131+
throws ParseException;
97132
}

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

Lines changed: 62 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,6 @@
5858
* <http://www.apache.org/>.
5959
*
6060
*/
61-
62-
/*
63-
* Copyright (C) The Apache Software Foundation. All rights reserved.
64-
*
65-
* This software is published under the terms of the Apache Software License
66-
* version 1.1, a copy of which has been included with this distribution in
67-
* the LICENSE file.
68-
*
69-
* $Id: Option.java,v 1.6 2002/06/06 22:50:14 bayard Exp $
70-
*/
71-
7261
package org.apache.commons.cli;
7362

7463
import java.util.ArrayList;
@@ -88,7 +77,6 @@
8877
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
8978
* @version $Revision: 1.6 $
9079
*/
91-
9280
public class Option implements Cloneable {
9381

9482
/** constant that specifies the number of argument values has not been specified */
@@ -97,7 +85,7 @@ public class Option implements Cloneable {
9785
/** constant that specifies the number of argument values is infinite */
9886
public final static int UNLIMITED_VALUES = -2;
9987

100-
/** opt the single character representation of the option */
88+
/** opt the name of the option */
10189
private String opt;
10290

10391
/** longOpt is the long representation of the option */
@@ -130,85 +118,9 @@ public class Option implements Cloneable {
130118
/** the list of argument values **/
131119
private ArrayList values = new ArrayList();
132120

133-
/** option char (only valid for single character options) */
134-
private char id;
135-
136121
/** the character that is the value separator */
137122
private char valuesep;
138123

139-
/**
140-
* <p>Validates whether <code>opt</code> is a permissable Option
141-
* shortOpt. The rules that specify if the <code>opt</code>
142-
* is valid are:</p>
143-
* <ul>
144-
* <li><code>opt</code> is not NULL</li>
145-
* <li>a single character <code>opt</code> that is either
146-
* ' '(special case), '?', '@' or a letter</li>
147-
* <li>a multi character <code>opt</code> that only contains
148-
* letters.</li>
149-
* </ul>
150-
*
151-
* @param opt The option string to validate
152-
* @throws IllegalArgumentException if the Option is not valid.
153-
*/
154-
private void validateOption( String opt )
155-
throws IllegalArgumentException
156-
{
157-
// check that opt is not NULL
158-
if( opt == null ) {
159-
throw new IllegalArgumentException( "opt is null" );
160-
}
161-
// handle the single character opt
162-
else if( opt.length() == 1 ) {
163-
char ch = opt.charAt( 0 );
164-
if ( !isValidOpt( ch ) ) {
165-
throw new IllegalArgumentException( "illegal option value '"
166-
+ ch + "'" );
167-
}
168-
id = ch;
169-
}
170-
// handle the multi character opt
171-
else {
172-
char[] chars = opt.toCharArray();
173-
for( int i = 0; i < chars.length; i++ ) {
174-
if( !isValidChar( chars[i] ) ) {
175-
throw new IllegalArgumentException( "opt contains illegal character value '" + chars[i] + "'" );
176-
}
177-
}
178-
}
179-
}
180-
181-
/**
182-
* <p>Returns whether the specified character is a valid Option.</p>
183-
*
184-
* @param c the option to validate
185-
* @return true if <code>c</code> is a letter, ' ', '?' or '@', otherwise false.
186-
*/
187-
private boolean isValidOpt( char c ) {
188-
return ( isValidChar( c ) || c == ' ' || c == '?' || c == '@' );
189-
}
190-
191-
/**
192-
* <p>Returns whether the specified character is a valid character.</p>
193-
*
194-
* @param c the character to validate
195-
* @return true if <code>c</code> is a letter.
196-
*/
197-
private boolean isValidChar( char c ) {
198-
return Character.isJavaIdentifierPart( c );
199-
}
200-
201-
/**
202-
* <p>Returns the id of this Option. This is only set when the
203-
* Option shortOpt is a single character. This is used for switch
204-
* statements.</p>
205-
*
206-
* @return the id of this Option
207-
*/
208-
public int getId( ) {
209-
return id;
210-
}
211-
212124
/**
213125
* Creates an Option using the specified parameters.
214126
*
@@ -247,7 +159,7 @@ public Option( String opt, String longOpt, boolean hasArg, String description )
247159
throws IllegalArgumentException
248160
{
249161
// ensure that the option is valid
250-
validateOption( opt );
162+
OptionValidator.validateOption( opt );
251163

252164
this.opt = opt;
253165
this.longOpt = longOpt;
@@ -261,6 +173,30 @@ public Option( String opt, String longOpt, boolean hasArg, String description )
261173
this.description = description;
262174
}
263175

176+
/**
177+
* <p>Returns the id of this Option. This is only set when the
178+
* Option shortOpt is a single character. This is used for switch
179+
* statements.</p>
180+
*
181+
* @return the id of this Option
182+
*/
183+
public int getId( ) {
184+
return getKey().charAt( 0 );
185+
}
186+
187+
/**
188+
* <p>Returns the 'unique' Option identifier.</p>
189+
*
190+
* @return the 'unique' Option identifier
191+
*/
192+
String getKey() {
193+
// if 'opt' is null, then it is a 'long' option
194+
if( opt == null ) {
195+
return this.longOpt;
196+
}
197+
return this.opt;
198+
}
199+
264200
/** <p>Retrieve the name of this Option.</p>
265201
*
266202
* <p>It is this String which can be used with
@@ -444,37 +380,8 @@ public int getArgs( ) {
444380
return this.numberOfArgs;
445381
}
446382

447-
/**
448-
* <p>Dump state, suitable for debugging.</p>
449-
*
450-
* @return Stringified form of this object
451-
*/
452-
public String toString() {
453-
StringBuffer buf = new StringBuffer().append("[ option: ");
454-
455-
buf.append( this.opt );
456-
457-
if ( this.longOpt != null ) {
458-
buf.append(" ")
459-
.append(this.longOpt);
460-
}
461-
462-
buf.append(" ");
463-
464-
if ( hasArg ) {
465-
buf.append( "+ARG" );
466-
}
467-
468-
buf.append(" :: ")
469-
.append( this.description );
470-
471-
if ( this.type != null ) {
472-
buf.append(" :: ")
473-
.append( this.type );
474-
}
475-
476-
buf.append(" ]");
477-
return buf.toString();
383+
public void clearValues() {
384+
this.values.clear();
478385
}
479386

480387
/**
@@ -572,4 +479,38 @@ public Object clone() {
572479
option.setValueSeparator( getValueSeparator() );
573480
return option;
574481
}
482+
483+
/**
484+
* <p>Dump state, suitable for debugging.</p>
485+
*
486+
* @return Stringified form of this object
487+
*/
488+
public String toString() {
489+
StringBuffer buf = new StringBuffer().append("[ option: ");
490+
491+
buf.append( this.opt );
492+
493+
if ( this.longOpt != null ) {
494+
buf.append(" ")
495+
.append(this.longOpt);
496+
}
497+
498+
buf.append(" ");
499+
500+
if ( hasArg ) {
501+
buf.append( "+ARG" );
502+
}
503+
504+
buf.append(" :: ")
505+
.append( this.description );
506+
507+
if ( this.type != null ) {
508+
buf.append(" :: ")
509+
.append( this.type );
510+
}
511+
512+
buf.append(" ]");
513+
return buf.toString();
514+
}
515+
575516
}

0 commit comments

Comments
 (0)