From f3a88cb46da30962bf7f597982199cd8770be76c Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Wed, 6 Nov 2024 11:27:50 -0500 Subject: [PATCH] Use OptionGroup to manage the port or solr options. --- .../src/java/org/apache/solr/cli/StatusTool.java | 13 ++++++------- solr/packaging/test/test_status.bats | 7 +++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/cli/StatusTool.java b/solr/core/src/java/org/apache/solr/cli/StatusTool.java index 6feed69ab57..dcf0200e511 100644 --- a/solr/core/src/java/org/apache/solr/cli/StatusTool.java +++ b/solr/core/src/java/org/apache/solr/cli/StatusTool.java @@ -30,6 +30,7 @@ import java.util.concurrent.TimeoutException; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; import org.apache.commons.cli.Options; import org.apache.solr.cli.SolrProcessManager.SolrProcess; import org.apache.solr.client.solrj.SolrClient; @@ -93,12 +94,14 @@ public String getName() { @Override public Options getOptions() { + OptionGroup optionGroup = new OptionGroup(); + optionGroup.addOption(PORT_OPTION); + optionGroup.addOption(CommonCLIOptions.SOLR_URL_OPTION); return super.getOptions() .addOption(MAX_WAIT_SECS_OPTION) - .addOption(PORT_OPTION) .addOption(SHORT_OPTION) - .addOption(CommonCLIOptions.SOLR_URL_OPTION) - .addOption(CommonCLIOptions.CREDENTIALS_OPTION); + .addOption(CommonCLIOptions.CREDENTIALS_OPTION) + .addOptionGroup(optionGroup); } @Override @@ -108,10 +111,6 @@ public void runImpl(CommandLine cli) throws Exception { boolean shortFormat = cli.hasOption(SHORT_OPTION); int maxWaitSecs = cli.getParsedOptionValue(MAX_WAIT_SECS_OPTION, 0); - if (port != null && solrUrl != null) { - throw new IllegalArgumentException("Only one of port or url can be specified"); - } - if (solrUrl != null) { if (!URLUtil.hasScheme(solrUrl)) { CLIO.err("Invalid URL provided: " + solrUrl); diff --git a/solr/packaging/test/test_status.bats b/solr/packaging/test/test_status.bats index d1f8a53bcb4..5399a0b9fd6 100644 --- a/solr/packaging/test/test_status.bats +++ b/solr/packaging/test/test_status.bats @@ -53,11 +53,14 @@ teardown() { solr stop } +@test "multiple connection options are prevented" { + run solr status --port ${SOLR_PORT} --solr-url http://localhost:${SOLR_PORT} + assert_output --partial "The option 's' was specified but an option from this group has already been selected: 'p'" +} + @test "status with invalid --solr-url from user" { - solr start run solr status --solr-url http://invalidhost:${SOLR_PORT} assert_output --partial "Solr at http://invalidhost:${SOLR_PORT} not online" - solr stop } @test "status with --short format" {