[
https://issues.apache.org/jira/browse/CLI-350?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18021234#comment-18021234
]
David Larochette commented on CLI-350:
--------------------------------------
Corresponding patch
{code:java}
Subject: [PATCH] feat: add option counting functions
---
Index: pom.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/pom.xml b/pom.xml
--- a/pom.xml (revision 423048b164c61f0eb7ca907108168ff7a36996b1)
+++ b/pom.xml (revision 0587fe80b1d32843cbd7c28a6f4eec4330dfe002)
@@ -24,7 +24,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
- <version>1.10.1-SNAPSHOT</version>
+ <version>1.11.0-SNAPSHOT</version>
<name>Apache Commons CLI</name>
<inceptionYear>2002</inceptionYear>
<description>
Index: src/main/java/org/apache/commons/cli/CommandLine.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/org/apache/commons/cli/CommandLine.java
b/src/main/java/org/apache/commons/cli/CommandLine.java
--- a/src/main/java/org/apache/commons/cli/CommandLine.java (revision
423048b164c61f0eb7ca907108168ff7a36996b1)
+++ b/src/main/java/org/apache/commons/cli/CommandLine.java (revision
0587fe80b1d32843cbd7c28a6f4eec4330dfe002)
@@ -300,6 +300,43 @@
return options.toArray(Option.EMPTY_ARRAY);
}
+ /**
+ * Gets the number of times this option appears in the command line.
+ *
+ * @param option the option.
+ * @return Number of times the option is present
+ * @since 1.11.0
+ */
+ public int getOptionCount(final Option option) {
+ int result = 0;
+ for (Option opt : options) {
+ if (Objects.equals(opt, option)) ++result;
+ }
+ return result;
+ }
+
+ /**
+ * Gets the number of times this option appears in the command line
+ *
+ * @param optionChar the character name of the option.
+ * @return Number of times the option is present
+ * @since 1.11.0
+ */
+ public int getOptionCount(final char optionChar) {
+ return getOptionCount(String.valueOf(optionChar));
+ }
+
+ /**
+ * Gets the number of times this option appears in the command line
+ *
+ * @param optionName the name of the option.
+ * @return Number of times the option is present
+ * @since 1.11.0
+ */
+ public int getOptionCount(final String optionName) {
+ return getOptionCount(resolveOption(optionName));
+ }
+
/**
* Gets the first argument, if any, of this option.
*
Index: src/test/java/org/apache/commons/cli/OptionCountTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/test/java/org/apache/commons/cli/OptionCountTest.java
b/src/test/java/org/apache/commons/cli/OptionCountTest.java
new file mode 100644
--- /dev/null (revision 0587fe80b1d32843cbd7c28a6f4eec4330dfe002)
+++ b/src/test/java/org/apache/commons/cli/OptionCountTest.java (revision
0587fe80b1d32843cbd7c28a6f4eec4330dfe002)
@@ -0,0 +1,61 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ https://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+package org.apache.commons.cli;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class OptionCountTest {
+ final char verbosityChar = 'v';
+ final String verbosityStr = String.valueOf(verbosityChar);
+ final Option verbosity = new Option(verbosityStr, "verbosity: use
multiple times for more");
+ final Options options = new Options().addOption(verbosity);
+
+ @Test
+ void noSwitchTest() throws ParseException {
+ final CommandLine cmdLine = new DefaultParser().parse(options, new
String[]{});
+ assertEquals(0, cmdLine.getOptionCount(verbosity));
+ }
+
+ @Test
+ void oneSwitchTest() throws ParseException {
+ final CommandLine cmdLine = new DefaultParser().parse(options, new
String[]{"-v"});
+ assertEquals(1, cmdLine.getOptionCount(verbosity));
+ assertEquals(1, cmdLine.getOptionCount(verbosityStr));
+ assertEquals(1, cmdLine.getOptionCount(verbosityChar));
+ }
+
+ @Test
+ void threeSwitchesTest() throws ParseException {
+ final CommandLine cmdLine = new DefaultParser().parse(options, new
String[]{"-v", "-v", "-v"});
+ assertEquals(3, cmdLine.getOptionCount(verbosity));
+ }
+
+ @Test
+ void threeSwitchesCompactTest() throws ParseException {
+ final CommandLine cmdLine = new DefaultParser().parse(options, new
String[]{"-vvv"});
+ assertEquals(3, cmdLine.getOptionCount(verbosity));
+ }
+
+ @Test
+ void fiveSwitchesMixedTest() throws ParseException {
+ final CommandLine cmdLine = new DefaultParser().parse(options, new
String[]{"-v", "-vvv", "-v"});
+ assertEquals(5, cmdLine.getOptionCount(verbosity));
+ }
+}
{code}
> Add getOptionCount to CommandLine to measure option repetition
> --------------------------------------------------------------
>
> Key: CLI-350
> URL: https://issues.apache.org/jira/browse/CLI-350
> Project: Commons CLI
> Issue Type: Improvement
> Components: CLI-1.x
> Affects Versions: 1.10.0
> Reporter: David Larochette
> Priority: Trivial
>
> For example a verbosity level switch could be repeated :
> `mycommand -vvv`
> giving
> {code:java}
> CommandLine cmdLine = DefaultParser.parse(options, new String[] {"-vvv"});
> assert cmdLine.getOptionCount("v") == 3;{code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)