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        boolean eatTheRest = false;
049        for (int i = 0; i < arguments.length; i++) {
050            final String arg = arguments[i];
051            if (arg != null) {
052                if ("--".equals(arg)) {
053                    eatTheRest = true;
054                    tokens.add("--");
055                } else if ("-".equals(arg)) {
056                    tokens.add("-");
057                } else if (arg.startsWith("-")) {
058                    final String opt = Util.stripLeadingHyphens(arg);
059                    if (options.hasOption(opt)) {
060                        tokens.add(arg);
061                    } else {
062                        final int equalPos = DefaultParser.indexOfEqual(opt);
063                        if (equalPos != -1 && options.hasOption(opt.substring(0, equalPos))) {
064                            // the format is --foo=value or -foo=value
065                            tokens.add(arg.substring(0, arg.indexOf(Char.EQUAL))); // --foo
066                            tokens.add(arg.substring(arg.indexOf(Char.EQUAL) + 1)); // value
067                        } else if (options.hasOption(arg.substring(0, 2))) {
068                            // the format is a special properties option (-Dproperty=value)
069                            tokens.add(arg.substring(0, 2)); // -D
070                            tokens.add(arg.substring(2)); // property=value
071                        } else {
072                            eatTheRest = stopAtNonOption;
073                            tokens.add(arg);
074                        }
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
088        return tokens.toArray(Util.EMPTY_STRING_ARRAY);
089    }
090}