~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/commons-cli-1.1-src/src/java/org/apache/commons/cli/GnuParser.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
package org.apache.commons.cli;
 
18
 
 
19
import java.util.ArrayList;
 
20
 
 
21
/**
 
22
 * The class GnuParser provides an implementation of the 
 
23
 * {@link Parser#flatten(Options,String[],boolean) flatten} method.
 
24
 *
 
25
 * @author John Keyes (john at integralsource.com)
 
26
 * @see Parser
 
27
 * @version $Revision: 1.2 $
 
28
 */
 
29
public class GnuParser extends Parser {
 
30
 
 
31
    /** holder for flattened tokens */
 
32
    private ArrayList tokens = new ArrayList();
 
33
 
 
34
    /**
 
35
     * <p>Resets the members to their original state i.e. remove
 
36
     * all of <code>tokens</code> entries.
 
37
     */
 
38
    private void init()
 
39
    {
 
40
        tokens.clear();
 
41
    }
 
42
 
 
43
    /**
 
44
     * <p>This flatten method does so using the following rules:
 
45
     * <ol>
 
46
     *  <li>If an {@link Option} exists for the first character of 
 
47
     *  the <code>arguments</code> entry <b>AND</b> an {@link Option} 
 
48
     *  does not exist for the whole <code>argument</code> then
 
49
     *  add the first character as an option to the processed tokens
 
50
     *  list e.g. "-D" and add the rest of the entry to the also.</li>
 
51
     *  <li>Otherwise just add the token to the processed tokens list.
 
52
     *  </li>
 
53
     * </ol>
 
54
     * </p>
 
55
     *
 
56
     * @param options The Options to parse the arguments by.
 
57
     * @param arguments The arguments that have to be flattened.
 
58
     * @param stopAtNonOption specifies whether to stop 
 
59
     * flattening when a non option has been encountered
 
60
     * @return a String array of the flattened arguments
 
61
     */
 
62
    protected String[] flatten(Options options, String[] arguments, 
 
63
                               boolean stopAtNonOption)
 
64
    {
 
65
        init();
 
66
 
 
67
        boolean eatTheRest = false;
 
68
        Option currentOption = null;
 
69
 
 
70
        for (int i = 0; i < arguments.length; i++)
 
71
        {
 
72
            if ("--".equals(arguments[i]))
 
73
            {
 
74
                eatTheRest = true;
 
75
                tokens.add("--");
 
76
            }
 
77
            else if ("-".equals(arguments[i]))
 
78
            {
 
79
                tokens.add("-");
 
80
            }
 
81
            else if (arguments[i].startsWith("-"))
 
82
            {
 
83
                Option option = options.getOption(arguments[i]);
 
84
 
 
85
                // this is not an Option
 
86
                if (option == null)
 
87
                {
 
88
                    // handle special properties Option
 
89
                    Option specialOption = 
 
90
                            options.getOption(arguments[i].substring(0, 2));
 
91
 
 
92
                    if (specialOption != null)
 
93
                    {
 
94
                        tokens.add(arguments[i].substring(0, 2));
 
95
                        tokens.add(arguments[i].substring(2));
 
96
                    }
 
97
                    else if (stopAtNonOption)
 
98
                    {
 
99
                        eatTheRest = true;
 
100
                        tokens.add(arguments[i]);
 
101
                    }
 
102
                    else
 
103
                    {
 
104
                        tokens.add(arguments[i]);
 
105
                    }
 
106
                }
 
107
                else
 
108
                {
 
109
                    // WARNING: Findbugs reports major problems with the following code. 
 
110
                    //          As option cannot be null, currentOption cannot and 
 
111
                    //          much of the code below is never going to be run.
 
112
 
 
113
                    currentOption = option;
 
114
 
 
115
                    // special option
 
116
                    Option specialOption = 
 
117
                            options.getOption(arguments[i].substring(0, 2));
 
118
 
 
119
                    if ((specialOption != null) && (option == null))
 
120
                    {
 
121
                        tokens.add(arguments[i].substring(0, 2));
 
122
                        tokens.add(arguments[i].substring(2));
 
123
                    }
 
124
                    else if ((currentOption != null) && currentOption.hasArg())
 
125
                    {
 
126
                        if (currentOption.hasArg())
 
127
                        {
 
128
                            tokens.add(arguments[i]);
 
129
                            currentOption = null;
 
130
                        }
 
131
                        else if (currentOption.hasArgs())
 
132
                        {
 
133
                            tokens.add(arguments[i]);
 
134
                        }
 
135
                        else if (stopAtNonOption)
 
136
                        {
 
137
                            eatTheRest = true;
 
138
                            tokens.add("--");
 
139
                            tokens.add(arguments[i]);
 
140
                        }
 
141
                        else
 
142
                        {
 
143
                            tokens.add(arguments[i]);
 
144
                        }
 
145
                    }
 
146
                    else if (currentOption != null)
 
147
                    {
 
148
                        tokens.add(arguments[i]);
 
149
                    }
 
150
                    else if (stopAtNonOption)
 
151
                    {
 
152
                        eatTheRest = true;
 
153
                        tokens.add("--");
 
154
                        tokens.add(arguments[i]);
 
155
                    }
 
156
                    else
 
157
                    {
 
158
                        tokens.add(arguments[i]);
 
159
                    }
 
160
                }
 
161
            }
 
162
            else
 
163
            {
 
164
                tokens.add(arguments[i]);
 
165
            }
 
166
 
 
167
            if (eatTheRest)
 
168
            {
 
169
                for (i++; i < arguments.length; i++)
 
170
                {
 
171
                    tokens.add(arguments[i]);
 
172
                }
 
173
            }
 
174
        }
 
175
 
 
176
        return (String[]) tokens.toArray(new String[tokens.size()]);
 
177
    }
 
178
}