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 */
017
018package org.apache.commons.cli;
019
020import java.util.ArrayList;
021import java.util.List;
022
023/**
024 * The class GnuParser provides an implementation of the {@link Parser#flatten(Options, String[], boolean) flatten}
025 * method.
026 *
027 * @deprecated since 1.3, use the {@link DefaultParser} instead
028 */
029@Deprecated
030public class GnuParser extends Parser {
031    /**
032     * This flatten method does so using the following rules:
033     * <ol>
034     * <li>If an {@link Option} exists for the first character of the {@code arguments} entry <b>AND</b> an
035     * {@link Option} does not exist for the whole {@code argument} then add the first character as an option to the
036     * processed tokens list e.g. "-D" and add the rest of the entry to the also.</li>
037     * <li>Otherwise just add the token to the processed tokens list.</li>
038     * </ol>
039     *
040     * @param options The Options to parse the arguments by.
041     * @param arguments The arguments that have to be flattened.
042     * @param stopAtNonOption specifies whether to stop flattening when a non option has been encountered
043     * @return a String array of the flattened arguments
044     */
045    @Override
046    protected String[] flatten(final Options options, final String[] arguments, final boolean stopAtNonOption) {
047        final List<String> tokens = new ArrayList<>();
048
049        boolean eatTheRest = false;
050
051        for (int i = 0; i < arguments.length; i++) {
052            final String arg = arguments[i];
053
054            if ("--".equals(arg)) {
055                eatTheRest = true;
056                tokens.add("--");
057            } else if ("-".equals(arg)) {
058                tokens.add("-");
059            } else if (arg.startsWith("-")) {
060                final String opt = Util.stripLeadingHyphens(arg);
061
062                if (options.hasOption(opt)) {
063                    tokens.add(arg);
064                } else if (opt.indexOf('=') != -1 && options.hasOption(opt.substring(0, opt.indexOf('=')))) {
065                    // the format is --foo=value or -foo=value
066                    tokens.add(arg.substring(0, arg.indexOf('='))); // --foo
067                    tokens.add(arg.substring(arg.indexOf('=') + 1)); // value
068                } else if (options.hasOption(arg.substring(0, 2))) {
069                    // the format is a special properties option (-Dproperty=value)
070                    tokens.add(arg.substring(0, 2)); // -D
071                    tokens.add(arg.substring(2)); // property=value
072                } else {
073                    eatTheRest = stopAtNonOption;
074                    tokens.add(arg);
075                }
076            } else {
077                tokens.add(arg);
078            }
079
080            if (eatTheRest) {
081                for (i++; i < arguments.length; i++) { // NOPMD
082                    tokens.add(arguments[i]);
083                }
084            }
085        }
086
087        return tokens.toArray(Util.EMPTY_STRING_ARRAY);
088    }
089}