From 3b24339e4424ff7391a640777161fd9734b867e2 Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Mon, 7 Sep 2020 18:55:16 +0200 Subject: [PATCH] fix(cli): handle option flags in any order Fixes #2039 --- core/src/cli/cli.ts | 5 +++-- core/test/unit/src/cli/cli.ts | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/core/src/cli/cli.ts b/core/src/cli/cli.ts index 2efba9c300..28af372379 100644 --- a/core/src/cli/cli.ts +++ b/core/src/cli/cli.ts @@ -367,10 +367,11 @@ ${renderCommands(commands)} return done(0, getPackageVersion()) } - const { command } = pickCommand(Object.values(this.commands), args) + const { command } = pickCommand(Object.values(this.commands), argv._) if (!command) { - return done(0, this.renderHelp()) + const exitCode = argv.h || argv.help ? 0 : 1 + return done(exitCode, this.renderHelp()) } if (command instanceof CommandGroup) { diff --git a/core/test/unit/src/cli/cli.ts b/core/test/unit/src/cli/cli.ts index e59e9cea14..7aa7ca7c4c 100644 --- a/core/test/unit/src/cli/cli.ts +++ b/core/test/unit/src/cli/cli.ts @@ -36,7 +36,7 @@ describe("cli", () => { const cli = new GardenCli() const { code, consoleOutput } = await cli.run({ args: [], exitOnError: false }) - expect(code).to.equal(0) + expect(code).to.equal(1) expect(consoleOutput).to.equal(cli.renderHelp()) }) @@ -131,6 +131,27 @@ describe("cli", () => { expect(result).to.eql({ something: "important" }) }) + it("handles params specified before the command", async () => { + class TestCommand extends Command { + name = "test-command" + help = "halp!" + noProject = true + + async action({}) { + return { result: { something: "important" } } + } + } + + const cli = new GardenCli() + const cmd = new TestCommand() + cli.addCommand(cmd) + + const { code, result } = await cli.run({ args: ["--logger-type=basic", "test-command"], exitOnError: false }) + + expect(code).to.equal(0) + expect(result).to.eql({ something: "important" }) + }) + it("updates the GardenProcess entry if given with command info before running (no server)", async () => { const args = ["test-command", "--root", projectRootA] const record = await GardenProcess.register(args)