diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseApp.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseApp.java index dd4b652f239..23ca152a033 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseApp.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseApp.java @@ -282,7 +282,7 @@ public abstract class BaseApp { } private Optional parseCommand(String[] theArgs) { - Optional commandOpt = getNextCommand(theArgs); + Optional commandOpt = getNextCommand(theArgs, 0); if (! commandOpt.isPresent()) { String message = "Unrecognized command: " + ansi().bold().fg(Ansi.Color.RED) + theArgs[0] + ansi().boldOff().fg(Ansi.Color.WHITE); @@ -294,8 +294,8 @@ public abstract class BaseApp { return commandOpt; } - private Optional getNextCommand(String[] theArgs) { - return ourCommands.stream().filter(cmd -> cmd.getCommandName().equals(theArgs[0])).findFirst(); + private Optional getNextCommand(String[] theArgs, int thePosition) { + return ourCommands.stream().filter(cmd -> cmd.getCommandName().equals(theArgs[thePosition])).findFirst(); } private void processHelp(String[] theArgs) { @@ -303,7 +303,7 @@ public abstract class BaseApp { logUsage(); return; } - Optional commandOpt = getNextCommand(theArgs); + Optional commandOpt = getNextCommand(theArgs, 1); if (! commandOpt.isPresent()) { String message = "Unknown command: " + theArgs[1]; System.err.println(message); diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/BaseAppTest.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/BaseAppTest.java new file mode 100644 index 00000000000..28f602bb142 --- /dev/null +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/BaseAppTest.java @@ -0,0 +1,31 @@ +package ca.uhn.fhir.cli; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; + +public class BaseAppTest { + + private final PrintStream standardOut = System.out; + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + @BeforeEach + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + } + + @AfterEach + public void tearDown() { + System.setOut(standardOut); + } + + @Test + public void testHelpOption() { + App.main(new String[]{"help", "create-package"}); + assertThat(outputStreamCaptor.toString().trim(), outputStreamCaptor.toString().trim(), containsString("Usage")); + } +} diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/2973-CLI-option-help-fails.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/2973-CLI-option-help-fails.yaml new file mode 100644 index 00000000000..d2b6cf2ff64 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/2973-CLI-option-help-fails.yaml @@ -0,0 +1,4 @@ +--- +type: fix +issue: 2973 +title: "CLI `smileutil help {command}` returns `Unknown command` which should return the usage of `command`. This has been corrected."