Skip to content

Commit

Permalink
Cli: parsing multiple times throws AlreadySelectedException
Browse files Browse the repository at this point in the history
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 elastic#7282.
  • Loading branch information
dadoonet committed Aug 19, 2014
1 parent 81ced48 commit 122c2b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/org/elasticsearch/common/cli/CliToolConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,16 @@ public String name() {
}

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) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/elasticsearch/common/cli/CliToolTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -253,6 +254,23 @@ public CliTool.ExitStatus execute(Settings settings, Environment env) throws Exc
}
}

@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()));
}
Expand Down

0 comments on commit 122c2b7

Please sign in to comment.