Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions src/main/java/org/apache/commons/cli/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public abstract class Parser implements CommandLineParser {
* Constructs a new instance.
*/
public Parser() {
// empty
}

/**
Expand Down Expand Up @@ -245,21 +244,16 @@ public void processArgs(final Option opt, final ListIterator<String> iter) throw
* @throws ParseException if {@code arg} does not represent an Option.
*/
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) {
final boolean optionIsRecognized = getOptions().hasOption(arg);
if (!optionIsRecognized) {
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);
final Option optionToProcess = (Option) getOptions().getOption(arg).clone();
updateRequiredOptions(optionToProcess);
if (optionToProcess.hasArg()) {
processArgs(optionToProcess, iter);
}
// set the option on the command line
cmd.addOption(opt);
cmd.addOption(optionToProcess);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/apache/commons/cli/AbstractParserTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -187,6 +188,18 @@ void testLongOptionQuoteHandling() throws Exception {
assertEquals("quoted string", cl.getOptionValue("b"), "Confirm --bfile \"arg\" strips quotes");
}

@Test
void testParsedArgumentIsStoredOnOptionClone() throws Exception {
final Option option = Option.builder("b").hasArg().get();
final Options options = new Options().addOption(option);
final CommandLine commandLine = parser.parse(options, new String[] { "-b", "value" });
final Option parsedOption = commandLine.getOptions()[0];

assertNotSame(option, parsedOption);
assertEquals("value", parsedOption.getValue());
assertTrue(option.isValuesEmpty());
}

@Test
void testLongOptionWithEqualsQuoteHandling() throws Exception {
final String[] args = { "--bfile=\"quoted string\"" };
Expand Down