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
25 * {@link Parser#flatten(Options, String[], boolean) flatten} method.
26 *
27 * @version $Id: GnuParser.java 1445352 2013-02-12 20:48:19Z tn $
28 * @deprecated since 1.3, use the {@link DefaultParser} instead
29 */
30 @Deprecated
31 public class GnuParser extends Parser
32 {
33 /**
34 * This flatten method does so using the following rules:
35 * <ol>
36 * <li>If an {@link Option} exists for the first character of
37 * the <code>arguments</code> entry <b>AND</b> an {@link Option}
38 * does not exist for the whole <code>argument</code> then
39 * add the first character as an option to the processed tokens
40 * list e.g. "-D" and add the rest of the entry to the also.</li>
41 * <li>Otherwise just add the token to the processed tokens list.</li>
42 * </ol>
43 *
44 * @param options The Options to parse the arguments by.
45 * @param arguments The arguments that have to be flattened.
46 * @param stopAtNonOption specifies whether to stop flattening when
47 * a non option has been encountered
48 * @return a String array of the flattened arguments
49 */
50 @Override
51 protected String[] flatten(Options options, String[] arguments, boolean stopAtNonOption)
52 {
53 List<String> tokens = new ArrayList<String>();
54
55 boolean eatTheRest = false;
56
57 for (int i = 0; i < arguments.length; i++)
58 {
59 String arg = arguments[i];
60
61 if ("--".equals(arg))
62 {
63 eatTheRest = true;
64 tokens.add("--");
65 }
66 else if ("-".equals(arg))
67 {
68 tokens.add("-");
69 }
70 else if (arg.startsWith("-"))
71 {
72 String opt = Util.stripLeadingHyphens(arg);
73
74 if (options.hasOption(opt))
75 {
76 tokens.add(arg);
77 }
78 else
79 {
80 if (opt.indexOf('=') != -1 && options.hasOption(opt.substring(0, opt.indexOf('='))))
81 {
82 // the format is --foo=value or -foo=value
83 tokens.add(arg.substring(0, arg.indexOf('='))); // --foo
84 tokens.add(arg.substring(arg.indexOf('=') + 1)); // value
85 }
86 else if (options.hasOption(arg.substring(0, 2)))
87 {
88 // the format is a special properties option (-Dproperty=value)
89 tokens.add(arg.substring(0, 2)); // -D
90 tokens.add(arg.substring(2)); // property=value
91 }
92 else
93 {
94 eatTheRest = stopAtNonOption;
95 tokens.add(arg);
96 }
97 }
98 }
99 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 }