Cli: parsing multiple times throws `AlreadySelectedException`

This issue has been fixed in commons-cli:1.3 project which sadly has not been released yet.
See https://issues.apache.org/jira/browse/CLI-183

This patch builds another list of options with no selected groups by default.

When commons-cli:1.3 will be released, we need to remove this patch.

Closes #7282.
This commit is contained in:
David Pilato 2014-08-14 15:22:25 +02:00
parent 81ced48f99
commit 122c2b7a12
2 changed files with 28 additions and 1 deletions

View File

@ -148,7 +148,16 @@ public class CliToolConfig {
}
public Options options() {
return options;
// TODO Remove this when commons-cli 1.3 will be released
// and replace by return options;
// See https://issues.apache.org/jira/browse/CLI-183
Options copy = new Options();
for (Object oOption : options.getOptions()) {
Option option = (Option) oOption;
copy.addOption(option);
}
OptionsSource.VERBOSITY.populate(copy);
return copy;
}
public void printUsage(Terminal terminal) {

View File

@ -22,6 +22,7 @@ package org.elasticsearch.common.cli;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.cli.CommandLine;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.junit.Test;
@ -253,6 +254,23 @@ public class CliToolTests extends CliToolTestCase {
}
}
@Test
public void testMultipleLaunch() throws Exception {
Terminal terminal = new MockTerminal();
final AtomicReference<Boolean> executed = new AtomicReference<>(false);
final NamedCommand cmd = new NamedCommand("cmd", terminal) {
@Override
public CliTool.ExitStatus execute(Settings settings, Environment env) {
executed.set(true);
return CliTool.ExitStatus.OK;
}
};
SingleCmdTool tool = new SingleCmdTool("tool", terminal, cmd);
tool.parse("cmd", Strings.splitStringByCommaToArray("--verbose"));
tool.parse("cmd", Strings.splitStringByCommaToArray("--silent"));
tool.parse("cmd", Strings.splitStringByCommaToArray("--help"));
}
private void assertStatus(int status, CliTool.ExitStatus expectedStatus) {
assertThat(status, is(expectedStatus.status()));
}