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+
162package org .apache .commons .cli ;
263
364import java .util .Arrays ;
667import java .util .ListIterator ;
768import 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+ */
977public 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 () );
0 commit comments