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 2024/04/08 12:25:48 UTC
(commons-cli) branch master updated (f4ab6c9 -> 71e5b08)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git
from f4ab6c9 Add missing test
new 33b552b Better local name
new 8fa4b05 Whitespace
new 71e5b08 Add missing test branch
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/main/java/org/apache/commons/cli/Parser.java | 52 +++++-----------------
.../org/apache/commons/cli/CommandLineTest.java | 2 +
2 files changed, 12 insertions(+), 42 deletions(-)
(commons-cli) 02/03: Whitespace
Posted by gg...@apache.org.
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 8fa4b05a0797a8b5930d40151e0be24f99ae4cb2
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Apr 8 08:25:30 2024 -0400
Whitespace
---
src/main/java/org/apache/commons/cli/Parser.java | 34 +-----------------------
1 file changed, 1 insertion(+), 33 deletions(-)
diff --git a/src/main/java/org/apache/commons/cli/Parser.java b/src/main/java/org/apache/commons/cli/Parser.java
index 0aeaaee..a8f052d 100644
--- a/src/main/java/org/apache/commons/cli/Parser.java
+++ b/src/main/java/org/apache/commons/cli/Parser.java
@@ -147,32 +147,23 @@ public abstract class Parser implements CommandLineParser {
for (final Option opt : options.helpOptions()) {
opt.clearValues();
}
-
// clear the data from the groups
for (final OptionGroup group : options.getOptionGroups()) {
group.setSelected(null);
}
-
// initialize members
setOptions(options);
-
cmd = CommandLine.builder().build();
-
boolean eatTheRest = false;
-
final List<String> tokenList = Arrays.asList(flatten(getOptions(), arguments == null ? new String[0] : arguments, stopAtNonOption));
-
final ListIterator<String> iterator = tokenList.listIterator();
-
// process each flattened token
while (iterator.hasNext()) {
final String token = iterator.next();
-
// the value is the double-dash
if ("--".equals(token)) {
eatTheRest = true;
}
-
// the value is a single dash
else if ("-".equals(token)) {
if (stopAtNonOption) {
@@ -181,7 +172,6 @@ public abstract class Parser implements CommandLineParser {
cmd.addArg(token);
}
}
-
// the value is an option
else if (token.startsWith("-")) {
if (stopAtNonOption && !getOptions().hasOption(token)) {
@@ -191,21 +181,17 @@ public abstract class Parser implements CommandLineParser {
processOption(token, iterator);
}
}
-
// the value is an argument
else {
cmd.addArg(token);
-
if (stopAtNonOption) {
eatTheRest = true;
}
}
-
// eat the remaining tokens
if (eatTheRest) {
while (iterator.hasNext()) {
final String str = iterator.next();
-
// ensure only one double-dash is added
if (!"--".equals(str)) {
cmd.addArg(str);
@@ -213,10 +199,8 @@ public abstract class Parser implements CommandLineParser {
}
}
}
-
processProperties(properties);
checkRequiredOptions();
-
return cmd;
}
@@ -233,13 +217,11 @@ public abstract class Parser implements CommandLineParser {
// loop until an option is found
while (iter.hasNext()) {
final String str = iter.next();
-
// found an Option, not an argument
if (getOptions().hasOption(str) && str.startsWith("-")) {
iter.previous();
break;
}
-
// found a value
try {
opt.addValueForProcessing(Util.stripLeadingAndTrailingQuotes(str));
@@ -248,7 +230,6 @@ public abstract class Parser implements CommandLineParser {
break;
}
}
-
if (opt.getValues() == null && !opt.hasOptionalArg()) {
throw new MissingArgumentException(opt);
}
@@ -265,23 +246,18 @@ public abstract class Parser implements CommandLineParser {
*/
protected void processOption(final String arg, final ListIterator<String> iter) throws ParseException {
final boolean hasOption = getOptions().hasOption(arg);
-
// if there is no option throw an UnrecognizedOptionException
if (!hasOption) {
throw new UnrecognizedOptionException("Unrecognized option: " + arg, arg);
}
-
// get the option represented by arg
final Option opt = (Option) getOptions().getOption(arg).clone();
-
// update the required options and groups
updateRequiredOptions(opt);
-
// if the option takes an argument value
if (opt.hasArg()) {
processArgs(opt, iter);
}
-
// set the option on the command line
cmd.addOption(opt);
}
@@ -296,23 +272,18 @@ public abstract class Parser implements CommandLineParser {
if (properties == null) {
return;
}
-
for (final Enumeration<?> e = properties.propertyNames(); e.hasMoreElements();) {
final String option = e.nextElement().toString();
-
final Option opt = options.getOption(option);
if (opt == null) {
throw new UnrecognizedOptionException("Default option wasn't defined", option);
}
-
// if the option is part of a group, check if another option of the group has been selected
final OptionGroup group = options.getOptionGroup(opt);
final boolean selected = group != null && group.getSelected() != null;
-
if (!cmd.hasOption(option) && !selected) {
// get the value from the properties instance
final String value = properties.getProperty(option);
-
if (opt.hasArg()) {
if (opt.getValues() == null || opt.getValues().length == 0) {
try {
@@ -326,7 +297,6 @@ public abstract class Parser implements CommandLineParser {
// option to the CommandLine
continue;
}
-
cmd.addOption(opt);
updateRequiredOptions(opt);
}
@@ -354,17 +324,15 @@ public abstract class Parser implements CommandLineParser {
if (opt.isRequired()) {
getRequiredOptions().remove(opt.getKey());
}
-
// if the option is in an OptionGroup make that option the selected
// option of the group
if (getOptions().getOptionGroup(opt) != null) {
final OptionGroup group = getOptions().getOptionGroup(opt);
-
if (group.isRequired()) {
getRequiredOptions().remove(group);
}
-
group.setSelected(opt);
}
}
+
}
(commons-cli) 01/03: Better local name
Posted by gg...@apache.org.
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 33b552b2110b8ff651bf3f6b30550e12734225e7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Apr 8 08:22:45 2024 -0400
Better local name
---
src/main/java/org/apache/commons/cli/Parser.java | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/main/java/org/apache/commons/cli/Parser.java b/src/main/java/org/apache/commons/cli/Parser.java
index c76e68c..0aeaaee 100644
--- a/src/main/java/org/apache/commons/cli/Parser.java
+++ b/src/main/java/org/apache/commons/cli/Parser.java
@@ -166,35 +166,35 @@ public abstract class Parser implements CommandLineParser {
// process each flattened token
while (iterator.hasNext()) {
- final String t = iterator.next();
+ final String token = iterator.next();
// the value is the double-dash
- if ("--".equals(t)) {
+ if ("--".equals(token)) {
eatTheRest = true;
}
// the value is a single dash
- else if ("-".equals(t)) {
+ else if ("-".equals(token)) {
if (stopAtNonOption) {
eatTheRest = true;
} else {
- cmd.addArg(t);
+ cmd.addArg(token);
}
}
// the value is an option
- else if (t.startsWith("-")) {
- if (stopAtNonOption && !getOptions().hasOption(t)) {
+ else if (token.startsWith("-")) {
+ if (stopAtNonOption && !getOptions().hasOption(token)) {
eatTheRest = true;
- cmd.addArg(t);
+ cmd.addArg(token);
} else {
- processOption(t, iterator);
+ processOption(token, iterator);
}
}
// the value is an argument
else {
- cmd.addArg(t);
+ cmd.addArg(token);
if (stopAtNonOption) {
eatTheRest = true;
(commons-cli) 03/03: Add missing test branch
Posted by gg...@apache.org.
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 71e5b08fe9299fa3a8450b11a12d7f62ca0cd46c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Apr 8 08:25:43 2024 -0400
Add missing test branch
---
src/test/java/org/apache/commons/cli/CommandLineTest.java | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/test/java/org/apache/commons/cli/CommandLineTest.java b/src/test/java/org/apache/commons/cli/CommandLineTest.java
index d1ce424..861e736 100644
--- a/src/test/java/org/apache/commons/cli/CommandLineTest.java
+++ b/src/test/java/org/apache/commons/cli/CommandLineTest.java
@@ -135,6 +135,7 @@ public class CommandLineTest {
assertNotNull(cmd.getOptions());
assertEquals(0, cmd.getOptions().length);
+ cmd.addOption(null);
cmd.addOption(new Option("a", null));
cmd.addOption(new Option("b", null));
cmd.addOption(new Option("c", null));
@@ -151,6 +152,7 @@ public class CommandLineTest {
cmd.addOption(new Option("a", null));
cmd.addOption(new Option("b", null));
cmd.addOption(new Option("c", null));
+ cmd.addOption(null);
assertEquals(3, cmd.getOptions().length);
}