You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/11/08 20:35:20 UTC
(commons-cli) 03/04: Use forEachRemaining
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git
commit 7199a701924f0ecf95d729095bc610d6434a3a77
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Nov 8 15:30:03 2025 -0500
Use forEachRemaining
- Reduce vertical whitespace
- Better private method name
---
.../java/org/apache/commons/cli/OptionGroup.java | 2 --
.../java/org/apache/commons/cli/PosixParser.java | 26 ++++++++++------------
.../commons/cli/help/AbstractHelpFormatter.java | 1 -
3 files changed, 12 insertions(+), 17 deletions(-)
diff --git a/src/main/java/org/apache/commons/cli/OptionGroup.java b/src/main/java/org/apache/commons/cli/OptionGroup.java
index 895cba7f..ed574dc1 100644
--- a/src/main/java/org/apache/commons/cli/OptionGroup.java
+++ b/src/main/java/org/apache/commons/cli/OptionGroup.java
@@ -162,12 +162,10 @@ public class OptionGroup implements Serializable {
buff.append(OptionFormatter.DEFAULT_LONG_OPT_PREFIX);
buff.append(option.getLongOpt());
}
-
if (option.getDescription() != null) {
buff.append(Char.SP);
buff.append(option.getDescription());
}
-
if (iter.hasNext()) {
buff.append(", ");
}
diff --git a/src/main/java/org/apache/commons/cli/PosixParser.java b/src/main/java/org/apache/commons/cli/PosixParser.java
index f9adc354..20fb3bfa 100644
--- a/src/main/java/org/apache/commons/cli/PosixParser.java
+++ b/src/main/java/org/apache/commons/cli/PosixParser.java
@@ -52,6 +52,17 @@ public class PosixParser extends Parser {
// empty
}
+ /**
+ * Adds the remaining tokens to the processed tokens list.
+ *
+ * @param iter An iterator over the remaining tokens
+ */
+ private void addRemaining(final Iterator<String> iter) {
+ if (eatTheRest) {
+ iter.forEachRemaining(tokens::add);
+ }
+ }
+
/**
* Breaks {@code token} into its constituent parts using the following algorithm.
*
@@ -171,24 +182,11 @@ public class PosixParser extends Parser {
processNonOptionToken(token, stopAtNonOption);
}
}
- gobble(iter);
+ addRemaining(iter);
}
return tokens.toArray(Util.EMPTY_STRING_ARRAY);
}
- /**
- * Adds the remaining tokens to the processed tokens list.
- *
- * @param iter An iterator over the remaining tokens
- */
- private void gobble(final Iterator<String> iter) {
- if (eatTheRest) {
- while (iter.hasNext()) {
- tokens.add(iter.next());
- }
- }
- }
-
/**
* Resets the members to their original state i.e. remove all of {@code tokens} entries and set
* {@code eatTheRest} to false.
diff --git a/src/main/java/org/apache/commons/cli/help/AbstractHelpFormatter.java b/src/main/java/org/apache/commons/cli/help/AbstractHelpFormatter.java
index c9948dc2..03a7f40f 100644
--- a/src/main/java/org/apache/commons/cli/help/AbstractHelpFormatter.java
+++ b/src/main/java/org/apache/commons/cli/help/AbstractHelpFormatter.java
@@ -472,7 +472,6 @@ public abstract class AbstractHelpFormatter {
formatter = optionFormatBuilder.build(iter.next());
// whether the option is required or not is handled at group level
buff.append(formatter.toSyntaxOption(true));
-
if (iter.hasNext()) {
buff.append(optionGroupSeparator);
}