View Javadoc

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   * @author John Keyes (john at integralsource.com)
28   * @version $Revision: 680644 $, $Date: 2008-07-29 09:13:48 +0100 (Tue, 29 Jul 2008) $
29   */
30  public class GnuParser extends Parser
31  {
32      /**
33       * This flatten method does so using the following rules:
34       * <ol>
35       *   <li>If an {@link Option} exists for the first character of
36       *   the <code>arguments</code> entry <b>AND</b> an {@link Option}
37       *   does not exist for the whole <code>argument</code> then
38       *   add the first character as an option to the processed tokens
39       *   list e.g. "-D" and add the rest of the entry to the also.</li>
40       *   <li>Otherwise just add the token to the processed tokens list.</li>
41       * </ol>
42       *
43       * @param options         The Options to parse the arguments by.
44       * @param arguments       The arguments that have to be flattened.
45       * @param stopAtNonOption specifies whether to stop flattening when
46       *                        a non option has been encountered
47       * @return a String array of the flattened arguments
48       */
49      protected String[] flatten(Options options, String[] arguments, boolean stopAtNonOption)
50      {
51          List tokens = new ArrayList();
52  
53          boolean eatTheRest = false;
54  
55          for (int i = 0; i < arguments.length; i++)
56          {
57              String arg = arguments[i];
58  
59              if ("--".equals(arg))
60              {
61                  eatTheRest = true;
62                  tokens.add("--");
63              }
64              else if ("-".equals(arg))
65              {
66                  tokens.add("-");
67              }
68              else if (arg.startsWith("-"))
69              {
70                  String opt = Util.stripLeadingHyphens(arg);
71  
72                  if (options.hasOption(opt))
73                  {
74                      tokens.add(arg);
75                  }
76                  else
77                  {
78                      if (opt.indexOf('=') != -1 && options.hasOption(opt.substring(0, opt.indexOf('='))))
79                      {
80                          // the format is --foo=value or -foo=value
81                          tokens.add(arg.substring(0, arg.indexOf('='))); // --foo
82                          tokens.add(arg.substring(arg.indexOf('=') + 1)); // value
83                      }
84                      else if (options.hasOption(arg.substring(0, 2)))
85                      {
86                          // the format is a special properties option (-Dproperty=value)
87                          tokens.add(arg.substring(0, 2)); // -D
88                          tokens.add(arg.substring(2)); // property=value
89                      }
90                      else
91                      {
92                          eatTheRest = stopAtNonOption;
93                          tokens.add(arg);
94                      }
95                  }
96              }
97              else
98              {
99                  tokens.add(arg);
100             }
101 
102             if (eatTheRest)
103             {
104                 for (i++; i < arguments.length; i++)
105                 {
106                     tokens.add(arguments[i]);
107                 }
108             }
109         }
110 
111         return (String[]) tokens.toArray(new String[tokens.size()]);
112     }
113 }