Skip to content

Commit d30f12f

Browse files
author
John Keyes
committed
allowed characters are now isJavaIdentifierPart, added javadoc to Parser, minor refactoring for required options
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129809 13f79535-47bb-0310-9956-ffa450edef68
1 parent fc0723b commit d30f12f

4 files changed

Lines changed: 157 additions & 38 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private boolean isValidOpt( char c ) {
192192
* @return true if <code>c</code> is a letter.
193193
*/
194194
private boolean isValidChar( char c ) {
195-
return Character.isLetter( c );
195+
return Character.isJavaIdentifierPart( c );
196196
}
197197

198198
/**

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@
6161

6262
package org.apache.commons.cli;
6363

64+
import java.util.ArrayList;
6465
import java.util.Collection;
65-
import java.util.Map;
66+
import java.util.Collections;
6667
import java.util.HashMap;
6768
import java.util.Iterator;
68-
import java.util.Collections;
69+
import java.util.List;
70+
import java.util.Map;
6971

7072
/** <p>Main entry-point into the library.</p>
7173
*
@@ -91,7 +93,7 @@ public class Options {
9193
private Map longOpts = new HashMap();
9294

9395
/** a map of the required options */
94-
private Map requiredOpts = new HashMap();
96+
private List requiredOpts = new ArrayList();
9597

9698
/** a map of the option groups */
9799
private Map optionGroups = new HashMap();
@@ -163,7 +165,7 @@ public Options addOption(Option opt) {
163165

164166
// if the option is required add it to the required list
165167
if ( opt.isRequired() ) {
166-
requiredOpts.put( shortOpt, opt );
168+
requiredOpts.add( shortOpt );
167169
}
168170

169171
shortOpts.put( shortOpt, opt );
@@ -184,7 +186,7 @@ public Collection getOptions() {
184186
*
185187
* @return Collection of required options
186188
*/
187-
public Map getRequiredOptions() {
189+
public List getRequiredOptions() {
188190
return requiredOpts;
189191
}
190192

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

Lines changed: 143 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,64 @@
1+
/*
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/Parser.java,v 1.4 2002/09/01 22:54:56 jkeyes Exp $
3+
* $Revision: 1.4 $
4+
* $Date: 2002/09/01 22:54:56 $
5+
*
6+
* ====================================================================
7+
*
8+
* The Apache Software License, Version 1.1
9+
*
10+
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
11+
* reserved.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
*
17+
* 1. Redistributions of source code must retain the above copyright
18+
* notice, this list of conditions and the following disclaimer.
19+
*
20+
* 2. Redistributions in binary form must reproduce the above copyright
21+
* notice, this list of conditions and the following disclaimer in
22+
* the documentation and/or other materials provided with the
23+
* distribution.
24+
*
25+
* 3. The end-user documentation included with the redistribution, if
26+
* any, must include the following acknowlegement:
27+
* "This product includes software developed by the
28+
* Apache Software Foundation (http://www.apache.org/)."
29+
* Alternately, this acknowlegement may appear in the software itself,
30+
* if and wherever such third-party acknowlegements normally appear.
31+
*
32+
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
33+
* Foundation" must not be used to endorse or promote products derived
34+
* from this software without prior written permission. For written
35+
* permission, please contact apache@apache.org.
36+
*
37+
* 5. Products derived from this software may not be called "Apache"
38+
* nor may "Apache" appear in their names without prior written
39+
* permission of the Apache Group.
40+
*
41+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52+
* SUCH DAMAGE.
53+
* ====================================================================
54+
*
55+
* This software consists of voluntary contributions made by many
56+
* individuals on behalf of the Apache Software Foundation. For more
57+
* information on the Apache Software Foundation, please see
58+
* <http://www.apache.org/>.
59+
*
60+
*/
61+
162
package org.apache.commons.cli;
263

364
import java.util.Arrays;
@@ -6,67 +67,122 @@
667
import java.util.ListIterator;
768
import java.util.Map;
869

70+
/**
71+
* <p><code>Parser</code> creates {@link CommandLine}s.</p>
72+
*
73+
* @author John Keyes (jbjk at mac.com)
74+
* @see Parser
75+
* @version $Revision: 1.4 $
76+
*/
977
public abstract class Parser implements CommandLineParser {
1078

79+
/** commandline instance */
1180
private CommandLine cmd;
81+
/** current Options */
1282
private Options options;
13-
private Map requiredOptions;
83+
/** list of required options strings */
84+
private List requiredOptions;
1485

86+
/**
87+
* <p>Subclasses must implement this method to reduce
88+
* the <code>arguments</code> that have been passed to the parse
89+
* method.</p>
90+
*
91+
* @param opts The Options to parse the arguments by.
92+
* @param args The arguments that have to be flattened.
93+
* @param stopAtNonOption specifies whether to stop
94+
* flattening when a non option has been encountered
95+
* @return a String array of the flattened arguments
96+
*/
1597
abstract protected String[] flatten( Options opts,
16-
String[] args,
98+
String[] arguments,
1799
boolean stopAtNonOption );
18100

19-
public CommandLine parse( Options opts, String[] args )
101+
/**
102+
* <p>Parses the specified <code>arguments</code>
103+
* based on the specifed {@link Options}.</p>
104+
*
105+
* @param options the <code>Options</code>
106+
* @param arguments the <code>arguments</code>
107+
* @return the <code>CommandLine</code>
108+
* @throws ParseException if an error occurs when parsing the
109+
* arguments.
110+
*/
111+
public CommandLine parse( Options options, String[] arguments )
20112
throws ParseException
21113
{
22-
return parse( opts, args, false );
114+
return parse( options, arguments, false );
23115
}
24116

117+
/**
118+
* <p>Parses the specified <code>arguments</code>
119+
* based on the specifed {@link Options}.</p>
120+
*
121+
* @param options the <code>Options</code>
122+
* @param arguments the <code>arguments</code>
123+
* @param stopAtNonOption specifies whether to stop
124+
* interpreting the arguments when a non option has
125+
* been encountered and to add them to the CommandLines
126+
* args list.
127+
* @return the <code>CommandLine</code>
128+
* @throws ParseException if an error occurs when parsing the
129+
* arguments.
130+
*/
25131
public CommandLine parse( Options opts,
26-
String[] args,
132+
String[] arguments,
27133
boolean stopAtNonOption )
28134
throws ParseException
29135
{
136+
// initialise members
30137
options = opts;
31138
requiredOptions = options.getRequiredOptions();
32-
String[] tokens = flatten( opts, args, stopAtNonOption );
33-
List tokenList = Arrays.asList( tokens );
34-
ListIterator iterator = tokenList.listIterator();
35139
cmd = new CommandLine();
140+
36141
boolean eatTheRest = false;
142+
143+
List tokenList = Arrays.asList( flatten( opts, arguments, stopAtNonOption ) );
144+
ListIterator iterator = tokenList.listIterator();
145+
146+
// process each flattened token
37147
while( iterator.hasNext() ) {
38148
String t = (String)iterator.next();
149+
150+
// the value is the double-dash
39151
if( "--".equals( t ) ) {
40152
eatTheRest = true;
41153
}
42-
else if( t.startsWith( "-" ) ) {
43-
if( t.length() == 1 ) {
44-
// not an option, so just drop it on the argument list
45-
if ( stopAtNonOption ) {
46-
eatTheRest = true;
47-
}
48-
else {
49-
cmd.addArg( t );
50-
}
154+
// the value is a single dash
155+
else if( "-".equals( t ) ) {
156+
if( stopAtNonOption ) {
157+
eatTheRest = true;
158+
}
159+
else {
160+
cmd.addArg(t );
51161
}
52-
else if ( stopAtNonOption && !options.hasOption( t ) ) {
162+
}
163+
// the value is an option
164+
else if( t.startsWith( "-" ) ) {
165+
if ( stopAtNonOption && !options.hasOption( t ) ) {
53166
eatTheRest = true;
54167
cmd.addArg( t );
55168
}
56169
else {
57170
processOption( t, iterator );
58171
}
59172
}
173+
// the value is an argument
60174
else {
61175
cmd.addArg( t );
62176
if( stopAtNonOption ) {
63177
eatTheRest = true;
64178
}
65179
}
66180

181+
// eat the remaining tokens
67182
if( eatTheRest ) {
68183
while( iterator.hasNext() ) {
69184
String str = (String)iterator.next();
185+
// ensure only one double-dash is added
70186
if( !"--".equals( str ) ) {
71187
cmd.addArg( str );
72188
}
@@ -77,22 +193,23 @@ else if ( stopAtNonOption && !options.hasOption( t ) ) {
77193
return cmd;
78194
}
79195

80-
private void checkRequiredOptions( )
81-
throws ParseException {
196+
/**
197+
* <p>Throws a {@link MissingOptionException} if all of the
198+
* required options are no present.</p>
199+
*/
200+
private void checkRequiredOptions()
201+
throws MissingOptionException
202+
{
82203

83204
// if there are required options that have not been
84205
// processsed
85206
if( requiredOptions.size() > 0 ) {
86-
Iterator iter = requiredOptions.values().iterator();
207+
Iterator iter = requiredOptions.iterator();
87208
StringBuffer buff = new StringBuffer();
88209

89210
// loop through the required options
90211
while( iter.hasNext() ) {
91-
Option missing = (Option)iter.next();
92-
buff.append( "-" );
93-
buff.append( missing.getOpt() );
94-
buff.append( " " );
95-
buff.append( missing.getDescription() );
212+
buff.append( iter.next() );
96213
}
97214

98215
throw new MissingOptionException( buff.toString() );

src/test/org/apache/commons/cli/BugsTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* version 1.1, a copy of which has been included with this distribution in
66
* the LICENSE file.
77
*
8-
* $Id: BugsTest.java,v 1.5 2002/08/31 17:53:11 jkeyes Exp $
8+
* $Id: BugsTest.java,v 1.6 2002/09/01 22:54:56 jkeyes Exp $
99
*/
1010

1111
package org.apache.commons.cli;
@@ -179,7 +179,7 @@ public void test12210() {
179179

180180
// Therefore, place them in an option group
181181

182-
String[] argv = new String[] { "-exec", "-execopto", "-execoptt" };
182+
String[] argv = new String[] { "-exec", "-exec_opt1", "-exec_opt2" };
183183
OptionGroup grp = new OptionGroup();
184184

185185
grp.addOption(new Option("exec",false,"description for this option"));
@@ -190,8 +190,8 @@ public void test12210() {
190190

191191
// for the exec option, there are 2 options...
192192
Options execOptions = new Options();
193-
execOptions.addOption("execopto",false," desc");
194-
execOptions.addOption("execoptt",false," desc");
193+
execOptions.addOption("exec_opt1",false," desc");
194+
execOptions.addOption("exec_opt2",false," desc");
195195

196196
// similarly, for rep there are 2 options...
197197
Options repOptions = new Options();
@@ -214,8 +214,8 @@ public void test12210() {
214214
if(cmd.hasOption("exec")){
215215
cmd = parser.parse(execOptions,argv,false);
216216
// process the exec_op1 and exec_opt2...
217-
assertTrue( cmd.hasOption("execopto") );
218-
assertTrue( cmd.hasOption("execoptt") );
217+
assertTrue( cmd.hasOption("exec_opt1") );
218+
assertTrue( cmd.hasOption("exec_opt2") );
219219
}
220220
else if(cmd.hasOption("rep")){
221221
cmd = parser.parse(repOptions,argv,false);

0 commit comments

Comments
 (0)