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.Arrays;
022import java.util.Iterator;
023import java.util.List;
024
025/**
026 * The class PosixParser provides an implementation of the {@link Parser#flatten(Options,String[],boolean) flatten}
027 * method.
028 *
029 * @deprecated since 1.3, use the {@link DefaultParser} instead
030 */
031@Deprecated
032public class PosixParser extends Parser {
033    /** Holder for flattened tokens */
034    private final List<String> tokens = new ArrayList<>();
035
036    /** Specifies if bursting should continue */
037    private boolean eatTheRest;
038
039    /** Holder for the current option */
040    private Option currentOption;
041
042    /** The command line Options */
043    private Options options;
044
045    /**
046     * Breaks {@code token} into its constituent parts using the following algorithm.
047     *
048     * <ul>
049     * <li>ignore the first character ("<b>-</b>")</li>
050     * <li>for each remaining character check if an {@link Option} exists with that id.</li>
051     * <li>if an {@link Option} does exist then add that character prepended with "<b>-</b>" to the list of processed
052     * tokens.</li>
053     * <li>if the {@link Option} can have an argument value and there are remaining characters in the token then add the
054     * remaining characters as a token to the list of processed tokens.</li>
055     * <li>if an {@link Option} does <b>NOT</b> exist <b>AND</b> {@code stopAtNonOption} <b>IS</b> set then add the
056     * special token "<b>--</b>" followed by the remaining characters and also the remaining tokens directly to the
057     * processed tokens list.</li>
058     * <li>if an {@link Option} does <b>NOT</b> exist <b>AND</b> {@code stopAtNonOption} <b>IS NOT</b> set then add
059     * that character prepended with "<b>-</b>".</li>
060     * </ul>
061     *
062     * @param token The current token to be <b>burst</b>
063     * @param stopAtNonOption Specifies whether to stop processing at the first non-Option encountered.
064     */
065    protected void burstToken(final String token, final boolean stopAtNonOption) {
066        for (int i = 1; i < token.length(); i++) {
067            final String ch = String.valueOf(token.charAt(i));
068
069            if (!options.hasOption(ch)) {
070                if (stopAtNonOption) {
071                    processNonOptionToken(token.substring(i), true);
072                } else {
073                    tokens.add(token);
074                }
075                break;
076            }
077            tokens.add("-" + ch);
078            currentOption = options.getOption(ch);
079
080            if (currentOption.hasArg() && token.length() != i + 1) {
081                tokens.add(token.substring(i + 1));
082
083                break;
084            }
085        }
086    }
087
088    /**
089     * <p>
090     * An implementation of {@link Parser}'s abstract {@link Parser#flatten(Options,String[],boolean) flatten} method.
091     * </p>
092     *
093     * <p>
094     * The following are the rules used by this flatten method.
095     * </p>
096     * <ol>
097     * <li>if {@code stopAtNonOption} is <b>true</b> then do not burst anymore of {@code arguments} entries, just
098     * add each successive entry without further processing. Otherwise, ignore {@code stopAtNonOption}.</li>
099     * <li>if the current {@code arguments} entry is "<b>--</b>" just add the entry to the list of processed
100     * tokens</li>
101     * <li>if the current {@code arguments} entry is "<b>-</b>" just add the entry to the list of processed tokens</li>
102     * <li>if the current {@code arguments} entry is two characters in length and the first character is "<b>-</b>"
103     * then check if this is a valid {@link Option} id. If it is a valid id, then add the entry to the list of processed
104     * tokens and set the current {@link Option} member. If it is not a valid id and {@code stopAtNonOption} is true,
105     * then the remaining entries are copied to the list of processed tokens. Otherwise, the current entry is ignored.</li>
106     * <li>if the current {@code arguments} entry is more than two characters in length and the first character is
107     * "<b>-</b>" then we need to burst the entry to determine its constituents. For more information on the bursting
108     * algorithm see {@link PosixParser#burstToken(String, boolean) burstToken}.</li>
109     * <li>if the current {@code arguments} entry is not handled by any of the previous rules, then the entry is added
110     * to the list of processed tokens.</li>
111     * </ol>
112     *
113     * @param options The command line {@link Options}
114     * @param arguments The command line arguments to be parsed
115     * @param stopAtNonOption Specifies whether to stop flattening when an non option is found.
116     * @return The flattened {@code arguments} String array.
117     */
118    @Override
119    protected String[] flatten(final Options options, final String[] arguments, final boolean stopAtNonOption) throws ParseException {
120        init();
121        this.options = options;
122        // an iterator for the command line tokens
123        final Iterator<String> iter = Arrays.asList(arguments).iterator();
124        // process each command line token
125        while (iter.hasNext()) {
126            // get the next command line token
127            final String token = iter.next();
128            if (token != null) {
129                // single or double hyphen
130                if ("-".equals(token) || "--".equals(token)) {
131                    tokens.add(token);
132                } else if (token.startsWith("--")) {
133                    // handle long option --foo or --foo=bar
134                    final int pos = DefaultParser.indexOfEqual(token);
135                    final String opt = pos == -1 ? token : token.substring(0, pos); // --foo
136
137                    final List<String> matchingOpts = options.getMatchingOptions(opt);
138
139                    if (matchingOpts.isEmpty()) {
140                        processNonOptionToken(token, stopAtNonOption);
141                    } else if (matchingOpts.size() > 1) {
142                        throw new AmbiguousOptionException(opt, matchingOpts);
143                    } else {
144                        currentOption = options.getOption(matchingOpts.get(0));
145
146                        tokens.add("--" + currentOption.getLongOpt());
147                        if (pos != -1) {
148                            tokens.add(token.substring(pos + 1));
149                        }
150                    }
151                } else if (token.startsWith("-")) {
152                    if (token.length() == 2 || options.hasOption(token)) {
153                        processOptionToken(token, stopAtNonOption);
154                    } else if (!options.getMatchingOptions(token).isEmpty()) {
155                        final List<String> matchingOpts = options.getMatchingOptions(token);
156                        if (matchingOpts.size() > 1) {
157                            throw new AmbiguousOptionException(token, matchingOpts);
158                        }
159                        final Option opt = options.getOption(matchingOpts.get(0));
160                        processOptionToken("-" + opt.getLongOpt(), stopAtNonOption);
161                    }
162                    // requires bursting
163                    else {
164                        burstToken(token, stopAtNonOption);
165                    }
166                } else {
167                    processNonOptionToken(token, stopAtNonOption);
168                }
169            }
170            gobble(iter);
171        }
172        return tokens.toArray(Util.EMPTY_STRING_ARRAY);
173    }
174
175    /**
176     * Adds the remaining tokens to the processed tokens list.
177     *
178     * @param iter An iterator over the remaining tokens
179     */
180    private void gobble(final Iterator<String> iter) {
181        if (eatTheRest) {
182            while (iter.hasNext()) {
183                tokens.add(iter.next());
184            }
185        }
186    }
187
188    /**
189     * Resets the members to their original state i.e. remove all of {@code tokens} entries and set
190     * {@code eatTheRest} to false.
191     */
192    private void init() {
193        eatTheRest = false;
194        tokens.clear();
195    }
196
197    /**
198     * Add the special token "<b>--</b>" and the current {@code value} to the processed tokens list. Then add all the
199     * remaining {@code argument} values to the processed tokens list.
200     *
201     * @param value The current token
202     */
203    private void processNonOptionToken(final String value, final boolean stopAtNonOption) {
204        if (stopAtNonOption && (currentOption == null || !currentOption.hasArg())) {
205            eatTheRest = true;
206            tokens.add("--");
207        }
208
209        tokens.add(value);
210    }
211
212    /**
213     * <p>
214     * If an {@link Option} exists for {@code token} then add the token to the processed list.
215     * </p>
216     *
217     * <p>
218     * If an {@link Option} does not exist and {@code stopAtNonOption} is set then add the remaining tokens to the
219     * processed tokens list directly.
220     * </p>
221     *
222     * @param token The current option token
223     * @param stopAtNonOption Specifies whether flattening should halt at the first non option.
224     */
225    private void processOptionToken(final String token, final boolean stopAtNonOption) {
226        if (stopAtNonOption && !options.hasOption(token)) {
227            eatTheRest = true;
228        }
229
230        if (options.hasOption(token)) {
231            currentOption = options.getOption(token);
232        }
233
234        tokens.add(token);
235    }
236}