Skip to content

Commit 0149b8a

Browse files
committed
Simplified the GnuParser by removing the unused code due to mutually exclusive conditions. Also added some comments to the code. Cobertura coverage is now 100%
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@661593 13f79535-47bb-0310-9956-ffa450edef68
1 parent 92e4871 commit 0149b8a

1 file changed

Lines changed: 18 additions & 70 deletions

File tree

src/java/org/apache/commons/cli/GnuParser.java

Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -49,109 +49,57 @@ public class GnuParser extends Parser {
4949
* flattening when a non option has been encountered
5050
* @return a String array of the flattened arguments
5151
*/
52-
protected String[] flatten(Options options, String[] arguments,
53-
boolean stopAtNonOption)
52+
protected String[] flatten(Options options, String[] arguments, boolean stopAtNonOption)
5453
{
5554
List tokens = new ArrayList();
5655

5756
boolean eatTheRest = false;
58-
Option currentOption = null;
5957

6058
for (int i = 0; i < arguments.length; i++)
6159
{
62-
if ("--".equals(arguments[i]))
60+
String arg = arguments[i];
61+
62+
if ("--".equals(arg))
6363
{
6464
eatTheRest = true;
6565
tokens.add("--");
6666
}
67-
else if ("-".equals(arguments[i]))
67+
else if ("-".equals(arg))
6868
{
6969
tokens.add("-");
7070
}
71-
else if (arguments[i].startsWith("-"))
71+
else if (arg.startsWith("-"))
7272
{
73-
Option option = options.getOption(arguments[i]);
73+
Option option = options.getOption(arg);
74+
75+
String head = arg.substring(0, 2); // "--" if --foo, "-f" if -foo
76+
String tail = arg.substring(2); // "foo" if -foo, "oo" if -foo
7477

7578
// this is not an Option
7679
if (option == null)
7780
{
78-
// handle special properties Option
79-
Option specialOption =
80-
options.getOption(arguments[i].substring(0, 2));
81+
// handle special properties Option (-Dproperty=value for example)
82+
Option specialOption = options.getOption(head);
8183

8284
if (specialOption != null)
8385
{
84-
tokens.add(arguments[i].substring(0, 2));
85-
tokens.add(arguments[i].substring(2));
86-
}
87-
else if (stopAtNonOption)
88-
{
89-
eatTheRest = true;
90-
tokens.add(arguments[i]);
86+
tokens.add(head); // -D
87+
tokens.add(tail); // property=value
9188
}
9289
else
9390
{
94-
tokens.add(arguments[i]);
91+
eatTheRest = stopAtNonOption;
92+
tokens.add(arg);
9593
}
9694
}
9795
else
9896
{
99-
// WARNING: Findbugs reports major problems with the following code.
100-
// As option cannot be null, currentOption cannot and
101-
// much of the code below is never going to be run.
102-
103-
currentOption = option;
104-
105-
// special option
106-
Option specialOption =
107-
options.getOption(arguments[i].substring(0, 2));
108-
109-
if ((specialOption != null) && (option == null))
110-
{
111-
tokens.add(arguments[i].substring(0, 2));
112-
tokens.add(arguments[i].substring(2));
113-
}
114-
else if ((currentOption != null) && currentOption.hasArg())
115-
{
116-
if (currentOption.hasArg())
117-
{
118-
tokens.add(arguments[i]);
119-
currentOption = null;
120-
}
121-
else if (currentOption.hasArgs())
122-
{
123-
tokens.add(arguments[i]);
124-
}
125-
else if (stopAtNonOption)
126-
{
127-
eatTheRest = true;
128-
tokens.add("--");
129-
tokens.add(arguments[i]);
130-
}
131-
else
132-
{
133-
tokens.add(arguments[i]);
134-
}
135-
}
136-
else if (currentOption != null)
137-
{
138-
tokens.add(arguments[i]);
139-
}
140-
else if (stopAtNonOption)
141-
{
142-
eatTheRest = true;
143-
tokens.add("--");
144-
tokens.add(arguments[i]);
145-
}
146-
else
147-
{
148-
tokens.add(arguments[i]);
149-
}
97+
tokens.add(arg);
15098
}
15199
}
152100
else
153101
{
154-
tokens.add(arguments[i]);
102+
tokens.add(arg);
155103
}
156104

157105
if (eatTheRest)

0 commit comments

Comments
 (0)