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
025 * {@link Parser#flatten(Options, String[], boolean) flatten} method.
026 *
027 * @version $Id: GnuParser.java 1445352 2013-02-12 20:48:19Z tn $
028 * @deprecated since 1.3, use the {@link DefaultParser} instead
029 */
030@Deprecated
031public class GnuParser extends Parser
032{
033    /**
034     * This flatten method does so using the following rules:
035     * <ol>
036     *   <li>If an {@link Option} exists for the first character of
037     *   the <code>arguments</code> entry <b>AND</b> an {@link Option}
038     *   does not exist for the whole <code>argument</code> then
039     *   add the first character as an option to the processed tokens
040     *   list e.g. "-D" and add the rest of the entry to the also.</li>
041     *   <li>Otherwise just add the token to the processed tokens list.</li>
042     * </ol>
043     *
044     * @param options         The Options to parse the arguments by.
045     * @param arguments       The arguments that have to be flattened.
046     * @param stopAtNonOption specifies whether to stop flattening when
047     *                        a non option has been encountered
048     * @return a String array of the flattened arguments
049     */
050    @Override
051    protected String[] flatten(Options options, String[] arguments, boolean stopAtNonOption)
052    {
053        List<String> tokens = new ArrayList<String>();
054
055        boolean eatTheRest = false;
056
057        for (int i = 0; i < arguments.length; i++)
058        {
059            String arg = arguments[i];
060
061            if ("--".equals(arg))
062            {
063                eatTheRest = true;
064                tokens.add("--");
065            }
066            else if ("-".equals(arg))
067            {
068                tokens.add("-");
069            }
070            else if (arg.startsWith("-"))
071            {
072                String opt = Util.stripLeadingHyphens(arg);
073
074                if (options.hasOption(opt))
075                {
076                    tokens.add(arg);
077                }
078                else
079                {
080                    if (opt.indexOf('=') != -1 && options.hasOption(opt.substring(0, opt.indexOf('='))))
081                    {
082                        // the format is --foo=value or -foo=value
083                        tokens.add(arg.substring(0, arg.indexOf('='))); // --foo
084                        tokens.add(arg.substring(arg.indexOf('=') + 1)); // value
085                    }
086                    else if (options.hasOption(arg.substring(0, 2)))
087                    {
088                        // the format is a special properties option (-Dproperty=value)
089                        tokens.add(arg.substring(0, 2)); // -D
090                        tokens.add(arg.substring(2)); // property=value
091                    }
092                    else
093                    {
094                        eatTheRest = stopAtNonOption;
095                        tokens.add(arg);
096                    }
097                }
098            }
099            else
100            {
101                tokens.add(arg);
102            }
103
104            if (eatTheRest)
105            {
106                for (i++; i < arguments.length; i++) //NOPMD
107                {
108                    tokens.add(arguments[i]);
109                }
110            }
111        }
112
113        return tokens.toArray(new String[tokens.size()]);
114    }
115}