Skip to content

Commit f6c45f8

Browse files
authored
feat: add option counting functions (#396)
Fixes: CLI-350
1 parent 5969448 commit f6c45f8

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,45 @@ 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)) {
314+
++result;
315+
}
316+
}
317+
return result;
318+
}
319+
320+
/**
321+
* Gets the number of times this option appears in the command line
322+
*
323+
* @param optionChar the character name of the option.
324+
* @return Number of times the option is present
325+
* @since 1.11.0
326+
*/
327+
public int getOptionCount(final char optionChar) {
328+
return getOptionCount(String.valueOf(optionChar));
329+
}
330+
331+
/**
332+
* Gets the number of times this option appears in the command line
333+
*
334+
* @param optionName the name of the option.
335+
* @return Number of times the option is present
336+
* @since 1.11.0
337+
*/
338+
public int getOptionCount(final String optionName) {
339+
return getOptionCount(resolveOption(optionName));
340+
}
341+
303342
/**
304343
* Gets the first argument, if any, of this option.
305344
*
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 static org.junit.jupiter.api.Assertions.assertEquals;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
class OptionCountTest {
25+
private static final Option VERBOSITY = new Option("v", "verbosity: use multiple times for more");
26+
private static final Options OPTIONS = new Options().addOption(VERBOSITY);
27+
28+
@Test
29+
void testNoSwitch() throws ParseException {
30+
final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{});
31+
assertEquals(0, cmdLine.getOptionCount(VERBOSITY));
32+
}
33+
34+
@Test
35+
void testOneSwitch() throws ParseException {
36+
final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{"-v"});
37+
assertEquals(1, cmdLine.getOptionCount(VERBOSITY));
38+
assertEquals(1, cmdLine.getOptionCount("v"));
39+
assertEquals(1, cmdLine.getOptionCount('v'));
40+
}
41+
42+
@Test
43+
void testThreeSwitches() throws ParseException {
44+
final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{"-v", "-v", "-v"});
45+
assertEquals(3, cmdLine.getOptionCount(VERBOSITY));
46+
}
47+
48+
@Test
49+
void testThreeSwitchesCompact() throws ParseException {
50+
final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{"-vvv"});
51+
assertEquals(3, cmdLine.getOptionCount(VERBOSITY));
52+
}
53+
54+
@Test
55+
void testFiveSwitchesMixed() throws ParseException {
56+
final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{"-v", "-vvv", "-v"});
57+
assertEquals(5, cmdLine.getOptionCount(VERBOSITY));
58+
}
59+
}

0 commit comments

Comments
 (0)