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