Skip to content

Commit eefb1e2

Browse files
committed
Internal refactoring
1 parent e627a6e commit eefb1e2

4 files changed

Lines changed: 24 additions & 22 deletions

File tree

src/main/java/org/apache/commons/cli/DefaultParser.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ public static Builder builder() {
150150
return new Builder();
151151
}
152152

153+
static int indexOfEqual(final String token) {
154+
return token.indexOf(Char.EQUAL);
155+
}
156+
153157
/** The command-line instance. */
154158
protected CommandLine cmd;
155159

@@ -358,7 +362,7 @@ protected void handleConcatenatedOptions(final String token) throws ParseExcepti
358362
* @param token the command line token to handle
359363
*/
360364
private void handleLongOption(final String token) throws ParseException {
361-
if (token.indexOf(Char.EQUAL) == -1) {
365+
if (indexOfEqual(token) == -1) {
362366
handleLongOptionWithoutEqual(token);
363367
} else {
364368
handleLongOptionWithEqual(token);
@@ -373,7 +377,7 @@ private void handleLongOption(final String token) throws ParseException {
373377
* @param token the command line token to handle
374378
*/
375379
private void handleLongOptionWithEqual(final String token) throws ParseException {
376-
final int pos = token.indexOf(Char.EQUAL);
380+
final int pos = indexOfEqual(token);
377381
final String value = token.substring(pos + 1);
378382
final String opt = token.substring(0, pos);
379383
final List<String> matchingOpts = getMatchingLongOptions(opt);
@@ -469,7 +473,7 @@ private void handleProperties(final Properties properties) throws ParseException
469473
*/
470474
private void handleShortAndLongOption(final String token) throws ParseException {
471475
final String t = Util.stripLeadingHyphens(token);
472-
final int pos = t.indexOf(Char.EQUAL);
476+
final int pos = indexOfEqual(t);
473477
if (t.length() == 1) {
474478
// -S
475479
if (options.hasShortOption(t)) {
@@ -600,7 +604,7 @@ private boolean isLongOption(final String token) {
600604
if (token == null || !token.startsWith("-") || token.length() == 1) {
601605
return false;
602606
}
603-
final int pos = token.indexOf(Char.EQUAL);
607+
final int pos = indexOfEqual(token);
604608
final String t = pos == -1 ? token : token.substring(0, pos);
605609
if (!getMatchingLongOptions(t).isEmpty()) {
606610
// long or partial long options (--L, -L, --L=V, -L=V, --l, --l=V)
@@ -647,7 +651,7 @@ private boolean isShortOption(final String token) {
647651
return false;
648652
}
649653
// remove leading "-" and "=value"
650-
final int pos = token.indexOf(Char.EQUAL);
654+
final int pos = indexOfEqual(token);
651655
final String optName = pos == -1 ? token.substring(1) : token.substring(1, pos);
652656
if (options.hasShortOption(optName)) {
653657
return true;

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,32 @@ public class GnuParser extends Parser {
4545
@Override
4646
protected String[] flatten(final Options options, final String[] arguments, final boolean stopAtNonOption) {
4747
final List<String> tokens = new ArrayList<>();
48-
4948
boolean eatTheRest = false;
50-
5149
for (int i = 0; i < arguments.length; i++) {
5250
final String arg = arguments[i];
53-
5451
if ("--".equals(arg)) {
5552
eatTheRest = true;
5653
tokens.add("--");
5754
} else if ("-".equals(arg)) {
5855
tokens.add("-");
5956
} else if (arg.startsWith("-")) {
6057
final String opt = Util.stripLeadingHyphens(arg);
61-
6258
if (options.hasOption(opt)) {
6359
tokens.add(arg);
64-
} else if (opt.indexOf(Char.EQUAL) != -1 && options.hasOption(opt.substring(0, opt.indexOf(Char.EQUAL)))) {
65-
// the format is --foo=value or -foo=value
66-
tokens.add(arg.substring(0, arg.indexOf(Char.EQUAL))); // --foo
67-
tokens.add(arg.substring(arg.indexOf(Char.EQUAL) + 1)); // value
68-
} else if (options.hasOption(arg.substring(0, 2))) {
69-
// the format is a special properties option (-Dproperty=value)
70-
tokens.add(arg.substring(0, 2)); // -D
71-
tokens.add(arg.substring(2)); // property=value
7260
} else {
73-
eatTheRest = stopAtNonOption;
74-
tokens.add(arg);
61+
final int equalPos = DefaultParser.indexOfEqual(opt);
62+
if (equalPos != -1 && options.hasOption(opt.substring(0, equalPos))) {
63+
// the format is --foo=value or -foo=value
64+
tokens.add(arg.substring(0, arg.indexOf(Char.EQUAL))); // --foo
65+
tokens.add(arg.substring(arg.indexOf(Char.EQUAL) + 1)); // value
66+
} else if (options.hasOption(arg.substring(0, 2))) {
67+
// the format is a special properties option (-Dproperty=value)
68+
tokens.add(arg.substring(0, 2)); // -D
69+
tokens.add(arg.substring(2)); // property=value
70+
} else {
71+
eatTheRest = stopAtNonOption;
72+
tokens.add(arg);
73+
}
7574
}
7675
} else {
7776
tokens.add(arg);

src/main/java/org/apache/commons/cli/PosixParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected String[] flatten(final Options options, final String[] arguments, fina
135135

136136
// handle long option --foo or --foo=bar
137137
else if (token.startsWith("--")) {
138-
final int pos = token.indexOf(Char.EQUAL);
138+
final int pos = DefaultParser.indexOfEqual(token);
139139
final String opt = pos == -1 ? token : token.substring(0, pos); // --foo
140140

141141
final List<String> matchingOpts = options.getMatchingOptions(opt);

src/test/java/org/apache/commons/cli/AbstractParserTestCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,9 @@ public void testPropertyOptionMultipleValues() throws Exception {
697697
final Properties properties = new Properties();
698698
properties.setProperty("k", "one,two");
699699

700-
final String[] values = { "one", "two" };
701-
702700
final CommandLine cmd = parse(parser, options, null, properties);
703701
assertTrue(cmd.hasOption("k"));
702+
final String[] values = { "one", "two" };
704703
assertArrayEquals(values, cmd.getOptionValues('k'));
705704
}
706705

0 commit comments

Comments
 (0)