Skip to content

Commit 0521818

Browse files
committed
feat: add option counting functions
Fixes: CLI-350
1 parent 423048b commit 0521818

3 files changed

Lines changed: 99 additions & 1 deletion

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<modelVersion>4.0.0</modelVersion>
2525
<groupId>commons-cli</groupId>
2626
<artifactId>commons-cli</artifactId>
27-
<version>1.10.1-SNAPSHOT</version>
27+
<version>1.11.0-SNAPSHOT</version>
2828
<name>Apache Commons CLI</name>
2929
<inceptionYear>2002</inceptionYear>
3030
<description>

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,43 @@ public Option[] getOptions() {
300300
return options.toArray(Option.EMPTY_ARRAY);
301301
}
302302

303+
/**
304+
* Gets the number of times this option appears in the command line.
305+
*
306+
* @param option the option.
307+
* @return Number of times the option is present
308+
* @since 1.11.0
309+
*/
310+
public int getOptionCount(final Option option) {
311+
int result = 0;
312+
for (Option opt : options) {
313+
if (Objects.equals(opt, option)) ++result;
314+
}
315+
return result;
316+
}
317+
318+
/**
319+
* Gets the number of times this option appears in the command line
320+
*
321+
* @param optionChar the character name of the option.
322+
* @return Number of times the option is present
323+
* @since 1.11.0
324+
*/
325+
public int getOptionCount(final char optionChar) {
326+
return getOptionCount(String.valueOf(optionChar));
327+
}
328+
329+
/**
330+
* Gets the number of times this option appears in the command line
331+
*
332+
* @param optionName the name of the option.
333+
* @return Number of times the option is present
334+
* @since 1.11.0
335+
*/
336+
public int getOptionCount(final String optionName) {
337+
return getOptionCount(resolveOption(optionName));
338+
}
339+
303340
/**
304341
* Gets the first argument, if any, of this option.
305342
*
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package org.apache.commons.cli;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
24+
class OptionCountTest {
25+
final char verbosityChar = 'v';
26+
final String verbosityStr = String.valueOf(verbosityChar);
27+
final Option verbosity = new Option(verbosityStr, "verbosity: use multiple times for more");
28+
final Options options = new Options().addOption(verbosity);
29+
30+
@Test
31+
void testNoSwitch() throws ParseException {
32+
final CommandLine cmdLine = new DefaultParser().parse(options, new String[]{});
33+
assertEquals(0, cmdLine.getOptionCount(verbosity));
34+
}
35+
36+
@Test
37+
void testOneSwitch() throws ParseException {
38+
final CommandLine cmdLine = new DefaultParser().parse(options, new String[]{"-v"});
39+
assertEquals(1, cmdLine.getOptionCount(verbosity));
40+
assertEquals(1, cmdLine.getOptionCount(verbosityStr));
41+
assertEquals(1, cmdLine.getOptionCount(verbosityChar));
42+
}
43+
44+
@Test
45+
void testThreeSwitches() throws ParseException {
46+
final CommandLine cmdLine = new DefaultParser().parse(options, new String[]{"-v", "-v", "-v"});
47+
assertEquals(3, cmdLine.getOptionCount(verbosity));
48+
}
49+
50+
@Test
51+
void testThreeSwitchesCompact() throws ParseException {
52+
final CommandLine cmdLine = new DefaultParser().parse(options, new String[]{"-vvv"});
53+
assertEquals(3, cmdLine.getOptionCount(verbosity));
54+
}
55+
56+
@Test
57+
void testFiveSwitchesMixed() throws ParseException {
58+
final CommandLine cmdLine = new DefaultParser().parse(options, new String[]{"-v", "-vvv", "-v"});
59+
assertEquals(5, cmdLine.getOptionCount(verbosity));
60+
}
61+
}

0 commit comments

Comments
 (0)