From 122c2b7a12bcb1ab0c7e4a738a3a46bba1249064 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Thu, 14 Aug 2014 15:22:25 +0200 Subject: [PATCH] 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. --- .../common/cli/CliToolConfig.java | 11 ++++++++++- .../elasticsearch/common/cli/CliToolTests.java | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/common/cli/CliToolConfig.java b/src/main/java/org/elasticsearch/common/cli/CliToolConfig.java index 450937e9ceb..0ba4f849be2 100644 --- a/src/main/java/org/elasticsearch/common/cli/CliToolConfig.java +++ b/src/main/java/org/elasticsearch/common/cli/CliToolConfig.java @@ -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) { diff --git a/src/test/java/org/elasticsearch/common/cli/CliToolTests.java b/src/test/java/org/elasticsearch/common/cli/CliToolTests.java index cd505193e44..72db9950ded 100644 --- a/src/test/java/org/elasticsearch/common/cli/CliToolTests.java +++ b/src/test/java/org/elasticsearch/common/cli/CliToolTests.java @@ -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 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())); }