Skip to content

Commit

Permalink
fix(cli): handle option flags in any order
Browse files Browse the repository at this point in the history
Fixes #2039
  • Loading branch information
edvald committed Sep 8, 2020
1 parent 3ee20dc commit 3b24339
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 3 additions & 2 deletions core/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 22 additions & 1 deletion core/test/unit/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3b24339

Please sign in to comment.