2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.cli;
19
import java.util.ArrayList;
22
* The class GnuParser provides an implementation of the
23
* {@link Parser#flatten(Options,String[],boolean) flatten} method.
25
* @author John Keyes (john at integralsource.com)
27
* @version $Revision: 1.2 $
29
public class GnuParser extends Parser {
31
/** holder for flattened tokens */
32
private ArrayList tokens = new ArrayList();
35
* <p>Resets the members to their original state i.e. remove
36
* all of <code>tokens</code> entries.
44
* <p>This flatten method does so using the following rules:
46
* <li>If an {@link Option} exists for the first character of
47
* the <code>arguments</code> entry <b>AND</b> an {@link Option}
48
* does not exist for the whole <code>argument</code> then
49
* add the first character as an option to the processed tokens
50
* list e.g. "-D" and add the rest of the entry to the also.</li>
51
* <li>Otherwise just add the token to the processed tokens list.
56
* @param options The Options to parse the arguments by.
57
* @param arguments The arguments that have to be flattened.
58
* @param stopAtNonOption specifies whether to stop
59
* flattening when a non option has been encountered
60
* @return a String array of the flattened arguments
62
protected String[] flatten(Options options, String[] arguments,
63
boolean stopAtNonOption)
67
boolean eatTheRest = false;
68
Option currentOption = null;
70
for (int i = 0; i < arguments.length; i++)
72
if ("--".equals(arguments[i]))
77
else if ("-".equals(arguments[i]))
81
else if (arguments[i].startsWith("-"))
83
Option option = options.getOption(arguments[i]);
85
// this is not an Option
88
// handle special properties Option
89
Option specialOption =
90
options.getOption(arguments[i].substring(0, 2));
92
if (specialOption != null)
94
tokens.add(arguments[i].substring(0, 2));
95
tokens.add(arguments[i].substring(2));
97
else if (stopAtNonOption)
100
tokens.add(arguments[i]);
104
tokens.add(arguments[i]);
109
// WARNING: Findbugs reports major problems with the following code.
110
// As option cannot be null, currentOption cannot and
111
// much of the code below is never going to be run.
113
currentOption = option;
116
Option specialOption =
117
options.getOption(arguments[i].substring(0, 2));
119
if ((specialOption != null) && (option == null))
121
tokens.add(arguments[i].substring(0, 2));
122
tokens.add(arguments[i].substring(2));
124
else if ((currentOption != null) && currentOption.hasArg())
126
if (currentOption.hasArg())
128
tokens.add(arguments[i]);
129
currentOption = null;
131
else if (currentOption.hasArgs())
133
tokens.add(arguments[i]);
135
else if (stopAtNonOption)
139
tokens.add(arguments[i]);
143
tokens.add(arguments[i]);
146
else if (currentOption != null)
148
tokens.add(arguments[i]);
150
else if (stopAtNonOption)
154
tokens.add(arguments[i]);
158
tokens.add(arguments[i]);
164
tokens.add(arguments[i]);
169
for (i++; i < arguments.length; i++)
171
tokens.add(arguments[i]);
176
return (String[]) tokens.toArray(new String[tokens.size()]);