1- /*
2- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/GnuParser.java,v 1.6 2002/08/15 22:05:18 jkeyes Exp $
3- * $Revision: 1.6 $
4- * $Date: 2002/08/15 22:05:18 $
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- */
611package org .apache .commons .cli ;
622
633import java .util .Arrays ;
4+ import java .util .ArrayList ;
645import java .util .Collection ;
656import java .util .Iterator ;
66- import java .util .ListIterator ;
677import java .util .Map ;
8+ import java .util .Iterator ;
689
6910/**
70- * GnuParser parses the command line arguments using the GNU style.
71- * For example, -buildfile can only be interpreted as the option
72- * 'buildfile'.
73- *
7411 * @author John Keyes (jbjk at mac.com)
7512 */
76- public class GnuParser implements CommandLineParser {
77-
78- /** current options instance */
79- private Options options ;
80-
81- /** convience member for the command line */
82- private CommandLine cmd ;
13+ public class GnuParser extends Parser {
8314
84- /** required options subset of options */
85- private Map requiredOptions ;
15+ private ArrayList tokens = new ArrayList ();
8616
87- /**
88- * Parse the arguments according to the specified options.
89- *
90- * @param options the specified Options
91- * @param arguments the command line arguments
92- * @return the list of atomic option and value tokens
93- * @throws ParseException if there are any problems encountered
94- * while parsing the command line tokens.
95- */
96- public CommandLine parse ( Options options , String [] arguments )
97- throws ParseException
98- {
99- return parse ( options , arguments , false );
17+ private void init () {
18+ tokens .clear ();
10019 }
10120
102- /**
103- * Parse the arguments according to the specified options.
104- *
105- * @param opts the specified Options
106- * @param arguments the command line arguments
107- * @param stopAtNonOption specifies whether to continue parsing the
108- * arguments if a non option is encountered.
109- * @return the CommandLine
110- * @throws ParseException if there are any problems encountered
111- * while parsing the command line tokens.
112- */
113- public CommandLine parse ( Options opts , String [] arguments , boolean stopAtNonOption )
114- throws ParseException
21+ protected String [] flatten ( Options options ,
22+ String [] arguments ,
23+ boolean stopAtNonOption )
11524 {
116- // set the member instances
117- options = opts ;
118- cmd = new CommandLine ();
119- requiredOptions = options .getRequiredOptions ();
120-
121- ListIterator iter = Arrays .asList ( arguments ).listIterator ();
122- String token = null ;
123-
124- // flag to indicate whether the remainder of the tokens should
125- // be added to the other arguments list
126- boolean eatTheRest = false ;
127-
128- while ( iter .hasNext () ) {
129- token = (String ) iter .next ();
130- if ( token .equals ("--" ) ) {
131- eatTheRest = true ;
132- }
133- else if ( token .startsWith ("--" ) ) {
134- //process the long-option
135- processOption ( token , iter );
136- }
137- else if ( token .startsWith ("-" ) ) {
138- if ( token .length () == 1 ) {
139- // It's not really an option, so
140- // just drop it on the list
141- if ( stopAtNonOption ) {
142- eatTheRest = true ;
143- }
144- else {
145- cmd .addArg ( token );
146- }
25+ init ();
26+ for ( int i = 0 ; i < arguments .length ; i ++ ) {
27+ Option option = options .getOption ( arguments [i ] );
28+ try {
29+ Option specialOption = options .getOption ( arguments [i ].substring (0 ,2 ) );
30+ if ( specialOption != null && option == null ) {
31+ tokens .add ( arguments [i ].substring (0 ,2 ) );
32+ tokens .add ( arguments [i ].substring (2 ) );
14733 }
14834 else {
149- processOption ( token , iter );
35+ tokens . add ( arguments [ i ] );
15036 }
15137 }
152- else {
153- // It's just a normal non-option arg,
154- // so dump it into the list of returned
155- // values.
156-
157- cmd .addArg ( token );
158-
159- if ( stopAtNonOption ) {
160- eatTheRest = true ;
161- }
162- }
163-
164- if ( eatTheRest ) {
165- while ( iter .hasNext () ) {
166- cmd .addArg ( (String )iter .next () );
167- }
38+ catch ( IndexOutOfBoundsException exp ) {
39+ tokens .add ( arguments [i ] );
16840 }
16941 }
170-
171- // see if all required options have been processed
172- checkRequiredOptions ( );
173-
174- return cmd ;
42+ return (String [])tokens .toArray ( new String [] {} );
17543 }
176-
177- /**
178- * It the option can accept multiple argument values then
179- * keep adding values until the next option token is encountered.
180- *
181- * @param opt the specified option
182- * @param iter the iterator over the command line tokens
183- */
184- public void processArgs ( Option opt , ListIterator iter )
185- throws ParseException
186- {
187- if ( !iter .hasNext () && !opt .hasOptionalArg () ) {
188- throw new MissingArgumentException ( "no argument for:" + opt .getOpt () );
189- }
190- // loop until an option is found
191- while ( iter .hasNext () ) {
192- String var = (String )iter .next ();
193-
194- // its an option
195- if ( !var .equals ( "-" ) && var .startsWith ( "-" ) ) {
196- // set the iterator pointer back a position
197- iter .previous ();
198- break ;
199- }
200- // its a value
201- else if ( !opt .addValue ( var ) ) {
202- iter .previous ();
203- return ;
204- }
205- }
206- }
207-
208- /**
209- * Process the option represented by <code>arg</code>.
210- *
211- * @param arg the string representation of an option
212- * @param iter the command line token iterator
213- */
214- private void processOption ( String arg , ListIterator iter )
215- throws ParseException
216- {
217- String value = null ;
218-
219- // see if it is a single character special option
220- Option opt = (Option ) options .getOption ( arg );
221- Option specialOption = (Option ) options .getOption ( arg .substring (0 ,2 ) );
222- if ( specialOption != null && opt == null ) {
223- opt = specialOption ;
224- value = arg .substring ( 2 );
225- char sep = opt .getValueSeparator ();
226-
227- if ( sep > 0 ) {
228- int findex ;
229- while ( ( findex = value .indexOf ( sep ) ) != -1 ) {
230- String val = value .substring ( 0 , findex );
231- value = value .substring ( findex + 1 );
232- if ( !opt .addValue ( val ) ) {
233- cmd .addArg ( val );
234- }
235- }
236- if ( !opt .addValue ( value ) ) {
237- cmd .addArg ( value );
238- }
239- }
240- else {
241- // add the argument value
242- opt .addValue ( value );
243- }
244- }
245-
246- // if there is no option throw an UnrecognisedOptionException
247- if ( opt == null ) {
248- throw new UnrecognizedOptionException ("Unrecognized option: " + arg );
249- }
250-
251- // if the option is a required option remove the option from
252- // the requiredOptions list
253- if ( opt .isRequired () ) {
254- requiredOptions .remove ( opt );
255- }
256-
257- // if the option is in an OptionGroup make that option the selected
258- // option of the group
259- if ( options .getOptionGroup ( opt ) != null ) {
260- ( (OptionGroup )( options .getOptionGroup ( opt ) ) ).setSelected ( opt );
261- }
262-
263- // if the option takes an argument value
264- if ( opt .hasArg () ) {
265- processArgs ( opt , iter );
266- }
267-
268- // set the option on the command line
269- cmd .setOpt ( opt );
270- }
271-
272- /**
273- * Ensures that all required options are present.
274- *
275- * @throws ParseException if all of the required options
276- * are not present.
277- */
278- private void checkRequiredOptions ( )
279- throws ParseException {
280-
281- // if there are required options that have not been
282- // processsed
283- if ( requiredOptions .size () > 0 ) {
284- Iterator iter = requiredOptions .values ().iterator ();
285- StringBuffer buff = new StringBuffer ();
286-
287- // loop through the required options
288- while ( iter .hasNext () ) {
289- Option missing = (Option )iter .next ();
290- buff .append ( "-" );
291- buff .append ( missing .getOpt () );
292- buff .append ( " " );
293- buff .append ( missing .getDescription () );
294- }
295-
296- // throw the MissingOptionException
297- throw new MissingOptionException ( buff .toString () );
298- }
299- }
300-
30144}
0 commit comments