1 /* 2 Licensed to the Apache Software Foundation (ASF) under one or more 3 contributor license agreements. See the NOTICE file distributed with 4 this work for additional information regarding copyright ownership. 5 The ASF licenses this file to You under the Apache License, Version 2.0 6 (the "License"); you may not use this file except in compliance with 7 the License. You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 */ 17 18 package org.apache.commons.cli; 19 20 import java.util.ArrayList; 21 import java.util.List; 22 23 /** 24 * The class GnuParser provides an implementation of the {@link Parser#flatten(Options, String[], boolean) flatten} 25 * method. 26 * 27 * @deprecated since 1.3, use the {@link DefaultParser} instead 28 */ 29 @Deprecated 30 public class GnuParser extends Parser { 31 /** 32 * This flatten method does so using the following rules: 33 * <ol> 34 * <li>If an {@link Option} exists for the first character of the {@code arguments} entry <b>AND</b> an 35 * {@link Option} does not exist for the whole {@code argument} then add the first character as an option to the 36 * processed tokens list e.g. "-D" and add the rest of the entry to the also.</li> 37 * <li>Otherwise just add the token to the processed tokens list.</li> 38 * </ol> 39 * 40 * @param options The Options to parse the arguments by. 41 * @param arguments The arguments that have to be flattened. 42 * @param stopAtNonOption specifies whether to stop flattening when a non option has been encountered 43 * @return a String array of the flattened arguments 44 */ 45 @Override 46 protected String[] flatten(final Options options, final String[] arguments, final boolean stopAtNonOption) { 47 final List<String> tokens = new ArrayList<>(); 48 boolean eatTheRest = false; 49 for (int i = 0; i < arguments.length; i++) { 50 final String arg = arguments[i]; 51 if ("--".equals(arg)) { 52 eatTheRest = true; 53 tokens.add("--"); 54 } else if ("-".equals(arg)) { 55 tokens.add("-"); 56 } else if (arg.startsWith("-")) { 57 final String opt = Util.stripLeadingHyphens(arg); 58 if (options.hasOption(opt)) { 59 tokens.add(arg); 60 } else { 61 final int equalPos = DefaultParser.indexOfEqual(opt); 62 if (equalPos != -1 && options.hasOption(opt.substring(0, equalPos))) { 63 // the format is --foo=value or -foo=value 64 tokens.add(arg.substring(0, arg.indexOf(Char.EQUAL))); // --foo 65 tokens.add(arg.substring(arg.indexOf(Char.EQUAL) + 1)); // value 66 } else if (options.hasOption(arg.substring(0, 2))) { 67 // the format is a special properties option (-Dproperty=value) 68 tokens.add(arg.substring(0, 2)); // -D 69 tokens.add(arg.substring(2)); // property=value 70 } else { 71 eatTheRest = stopAtNonOption; 72 tokens.add(arg); 73 } 74 } 75 } else { 76 tokens.add(arg); 77 } 78 79 if (eatTheRest) { 80 for (i++; i < arguments.length; i++) { // NOPMD 81 tokens.add(arguments[i]); 82 } 83 } 84 } 85 86 return tokens.toArray(Util.EMPTY_STRING_ARRAY); 87 } 88 }