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 https://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 /** 033 * Constructs a new instance. 034 */ 035 public GnuParser() { 036 // empty 037 } 038 039 /** 040 * This flatten method does so using the following rules: 041 * <ol> 042 * <li>If an {@link Option} exists for the first character of the {@code arguments} entry <strong>AND</strong> an 043 * {@link Option} does not exist for the whole {@code argument} then add the first character as an option to the 044 * processed tokens list for example "-D" and add the rest of the entry to the also.</li> 045 * <li>Otherwise just add the token to the processed tokens list.</li> 046 * </ol> 047 * 048 * @param options The Options to parse the arguments by. 049 * @param arguments The arguments that have to be flattened. 050 * @param stopAtNonOption specifies whether to stop flattening when a non option has been encountered 051 * @return a String array of the flattened arguments 052 */ 053 @Override 054 protected String[] flatten(final Options options, final String[] arguments, final boolean stopAtNonOption) { 055 final List<String> tokens = new ArrayList<>(); 056 boolean eatTheRest = false; 057 for (int i = 0; i < arguments.length; i++) { 058 final String arg = arguments[i]; 059 if (arg != null) { 060 if ("--".equals(arg)) { 061 eatTheRest = true; 062 tokens.add("--"); 063 } else if ("-".equals(arg)) { 064 tokens.add("-"); 065 } else if (arg.startsWith("-")) { 066 final String opt = Util.stripLeadingHyphens(arg); 067 if (options.hasOption(opt)) { 068 tokens.add(arg); 069 } else { 070 final int equalPos = DefaultParser.indexOfEqual(opt); 071 if (equalPos != -1 && options.hasOption(opt.substring(0, equalPos))) { 072 // the format is --foo=value or -foo=value 073 tokens.add(arg.substring(0, arg.indexOf(Char.EQUAL))); // --foo 074 tokens.add(arg.substring(arg.indexOf(Char.EQUAL) + 1)); // value 075 } else if (options.hasOption(arg.substring(0, 2))) { 076 // the format is a special properties option (-Dproperty=value) 077 tokens.add(arg.substring(0, 2)); // -D 078 tokens.add(arg.substring(2)); // property=value 079 } else { 080 eatTheRest = stopAtNonOption; 081 tokens.add(arg); 082 } 083 } 084 } else { 085 tokens.add(arg); 086 } 087 088 if (eatTheRest) { 089 for (i++; i < arguments.length; i++) { // NOPMD 090 tokens.add(arguments[i]); 091 } 092 } 093 } 094 } 095 096 return tokens.toArray(Util.EMPTY_STRING_ARRAY); 097 } 098}