Skip to content

Commit

Permalink
fix(command): command action not triggered for standalone options wit…
Browse files Browse the repository at this point in the history
…hout action handler (#654)
  • Loading branch information
c4spar authored Aug 5, 2023
1 parent b732a44 commit 4e63862
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
16 changes: 8 additions & 8 deletions command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1766,15 +1766,15 @@ export class Command<
await Promise.all(
ctx.actions.map((action) => action.call(this, options, ...args)),
);
}

if (ctx.standalone) {
return {
options,
args,
cmd: this,
literal: this.literalArgs,
};
if (ctx.standalone) {
return {
options,
args,
cmd: this,
literal: this.literalArgs,
};
}
}

return await this.execute(options, args);
Expand Down
63 changes: 63 additions & 0 deletions command/test/command/standalone_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
assertEquals,
assertRejects,
assertSpyCalls,
spy,
} from "../../../dev_deps.ts";
import { Command } from "../../command.ts";

Deno.test("[command] should execute standalone option action", async () => {
const actionSpy = spy();
const optionActionSpy = spy();

const cmd = new Command()
.throwErrors()
.option("--standalone", "description ...", {
action: optionActionSpy,
standalone: true,
})
.action(actionSpy);

const { options } = await cmd.parse(["--standalone"]);

assertSpyCalls(optionActionSpy, 1);
assertSpyCalls(actionSpy, 0);
assertEquals(options, { standalone: true });
});

Deno.test("[command] should execute main action with standalone option", async () => {
const actionSpy = spy();

const cmd = new Command()
.throwErrors()
.option("--standalone", "description ...", {
standalone: true,
})
.action(actionSpy);

const { options } = await cmd.parse(["--standalone"]);

assertSpyCalls(actionSpy, 1);
assertEquals(options, { standalone: true });
});

Deno.test("[command] should throw an error if standalone option is combined with other options", async () => {
const actionSpy = spy();

const cmd = new Command()
.throwErrors()
.option("--standalone", "description ...", {
standalone: true,
})
.option("--foo", "description ...")
.action(actionSpy);

await assertRejects(
() =>
cmd.parse(
["--standalone", "--foo"],
),
Error,
'Option "--standalone" cannot be combined with other options.',
);
});

0 comments on commit 4e63862

Please sign in to comment.