001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.commons.cli;
017:
018: import java.util.ArrayList;
019:
020: /**
021: * The class GnuParser provides an implementation of the
022: * {@link Parser#flatten(Options,String[],boolean) flatten} method.
023: *
024: * @author John Keyes (john at integralsource.com)
025: * @see Parser
026: * @version $Revision: 542151 $
027: */
028: public class GnuParser extends Parser {
029:
030: /** holder for flattened tokens */
031: private ArrayList tokens = new ArrayList();
032:
033: /**
034: * <p>Resets the members to their original state i.e. remove
035: * all of <code>tokens</code> entries.
036: */
037: private void init() {
038: tokens.clear();
039: }
040:
041: /**
042: * <p>This flatten method does so using the following rules:
043: * <ol>
044: * <li>If an {@link Option} exists for the first character of
045: * the <code>arguments</code> entry <b>AND</b> an {@link Option}
046: * does not exist for the whole <code>argument</code> then
047: * add the first character as an option to the processed tokens
048: * list e.g. "-D" and add the rest of the entry to the also.</li>
049: * <li>Otherwise just add the token to the processed tokens list.
050: * </li>
051: * </ol>
052: * </p>
053: *
054: * @param options The Options to parse the arguments by.
055: * @param arguments The arguments that have to be flattened.
056: * @param stopAtNonOption specifies whether to stop
057: * flattening when a non option has been encountered
058: * @return a String array of the flattened arguments
059: */
060: protected String[] flatten(Options options, String[] arguments,
061: boolean stopAtNonOption) {
062: init();
063:
064: boolean eatTheRest = false;
065: Option currentOption = null;
066:
067: for (int i = 0; i < arguments.length; i++) {
068: if ("--".equals(arguments[i])) {
069: eatTheRest = true;
070: tokens.add("--");
071: } else if ("-".equals(arguments[i])) {
072: tokens.add("-");
073: } else if (arguments[i].startsWith("-")) {
074: Option option = options.getOption(arguments[i]);
075:
076: // this is not an Option
077: if (option == null) {
078: // handle special properties Option
079: Option specialOption = options
080: .getOption(arguments[i].substring(0, 2));
081:
082: if (specialOption != null) {
083: tokens.add(arguments[i].substring(0, 2));
084: tokens.add(arguments[i].substring(2));
085: } else if (stopAtNonOption) {
086: eatTheRest = true;
087: tokens.add(arguments[i]);
088: } else {
089: tokens.add(arguments[i]);
090: }
091: } else {
092: // WARNING: Findbugs reports major problems with the following code.
093: // As option cannot be null, currentOption cannot and
094: // much of the code below is never going to be run.
095:
096: currentOption = option;
097:
098: // special option
099: Option specialOption = options
100: .getOption(arguments[i].substring(0, 2));
101:
102: if ((specialOption != null) && (option == null)) {
103: tokens.add(arguments[i].substring(0, 2));
104: tokens.add(arguments[i].substring(2));
105: } else if ((currentOption != null)
106: && currentOption.hasArg()) {
107: if (currentOption.hasArg()) {
108: tokens.add(arguments[i]);
109: currentOption = null;
110: } else if (currentOption.hasArgs()) {
111: tokens.add(arguments[i]);
112: } else if (stopAtNonOption) {
113: eatTheRest = true;
114: tokens.add("--");
115: tokens.add(arguments[i]);
116: } else {
117: tokens.add(arguments[i]);
118: }
119: } else if (currentOption != null) {
120: tokens.add(arguments[i]);
121: } else if (stopAtNonOption) {
122: eatTheRest = true;
123: tokens.add("--");
124: tokens.add(arguments[i]);
125: } else {
126: tokens.add(arguments[i]);
127: }
128: }
129: } else {
130: tokens.add(arguments[i]);
131: }
132:
133: if (eatTheRest) {
134: for (i++; i < arguments.length; i++) {
135: tokens.add(arguments[i]);
136: }
137: }
138: }
139:
140: return (String[]) tokens.toArray(new String[tokens.size()]);
141: }
142: }
|