Skip to content

Commit 4e63862

Browse files
authored
fix(command): command action not triggered for standalone options without action handler (#654)
1 parent b732a44 commit 4e63862

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

command/command.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,15 +1766,15 @@ export class Command<
17661766
await Promise.all(
17671767
ctx.actions.map((action) => action.call(this, options, ...args)),
17681768
);
1769-
}
17701769

1771-
if (ctx.standalone) {
1772-
return {
1773-
options,
1774-
args,
1775-
cmd: this,
1776-
literal: this.literalArgs,
1777-
};
1770+
if (ctx.standalone) {
1771+
return {
1772+
options,
1773+
args,
1774+
cmd: this,
1775+
literal: this.literalArgs,
1776+
};
1777+
}
17781778
}
17791779

17801780
return await this.execute(options, args);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
assertEquals,
3+
assertRejects,
4+
assertSpyCalls,
5+
spy,
6+
} from "../../../dev_deps.ts";
7+
import { Command } from "../../command.ts";
8+
9+
Deno.test("[command] should execute standalone option action", async () => {
10+
const actionSpy = spy();
11+
const optionActionSpy = spy();
12+
13+
const cmd = new Command()
14+
.throwErrors()
15+
.option("--standalone", "description ...", {
16+
action: optionActionSpy,
17+
standalone: true,
18+
})
19+
.action(actionSpy);
20+
21+
const { options } = await cmd.parse(["--standalone"]);
22+
23+
assertSpyCalls(optionActionSpy, 1);
24+
assertSpyCalls(actionSpy, 0);
25+
assertEquals(options, { standalone: true });
26+
});
27+
28+
Deno.test("[command] should execute main action with standalone option", async () => {
29+
const actionSpy = spy();
30+
31+
const cmd = new Command()
32+
.throwErrors()
33+
.option("--standalone", "description ...", {
34+
standalone: true,
35+
})
36+
.action(actionSpy);
37+
38+
const { options } = await cmd.parse(["--standalone"]);
39+
40+
assertSpyCalls(actionSpy, 1);
41+
assertEquals(options, { standalone: true });
42+
});
43+
44+
Deno.test("[command] should throw an error if standalone option is combined with other options", async () => {
45+
const actionSpy = spy();
46+
47+
const cmd = new Command()
48+
.throwErrors()
49+
.option("--standalone", "description ...", {
50+
standalone: true,
51+
})
52+
.option("--foo", "description ...")
53+
.action(actionSpy);
54+
55+
await assertRejects(
56+
() =>
57+
cmd.parse(
58+
["--standalone", "--foo"],
59+
),
60+
Error,
61+
'Option "--standalone" cannot be combined with other options.',
62+
);
63+
});

0 commit comments

Comments
 (0)