Skip to content

Commit 464101f

Browse files
committed
Disable interactive prompt for the version option
1 parent 80239fc commit 464101f

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ More examples can be found in the [examples](/examples/) directory.
110110

111111
### Interactive options for the root command
112112

113-
Interactive options are not supported for the root command. However, you can
114-
mark a subcommand as the default command to achieve a similar effect:
113+
Interactive options on main command won't be prompted for in interactive mode
114+
if no subcommand is invoked. That is because Commander.js doesn't support
115+
pre-parse hooks for the main command. As a workaround, you can define a
116+
subcommand as the default command to achieve a similar effect:
115117

116118
```typescript
117119
import { InteractiveCommand } from "interactive-commander";

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "interactive-commander",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "Commander.js with integrated interactive prompts",
55
"keywords": [
66
"commander",

src/interactive-command.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,18 @@ await test("parseAsync", async (t) => {
175175
{ code: "commander.missingMandatoryOptionValue" },
176176
);
177177
});
178+
179+
await t.test("version option", async (t) => {
180+
const readFunctionMock = t.mock.fn(async () => true);
181+
182+
rootCommand.version("1.0.0", "-V, --version");
183+
const versionOption = rootCommand.options.find(
184+
(option) => option.short === "-V",
185+
) as InteractiveOption;
186+
versionOption.prompt(readFunctionMock);
187+
188+
await rootCommand.parseAsync(["node", "test", "sub", "-i"]);
189+
190+
assert.strictEqual(readFunctionMock.mock.calls.length, 0);
191+
});
178192
});

src/interactive-command.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ export class InteractiveCommand extends Command {
3232
return new InteractiveOption(flags, description);
3333
}
3434

35+
version(...arguments_: Parameters<Command["version"]>): this {
36+
const returnValue = super.version(...arguments_);
37+
38+
const versionOptionName = (this as this & { _versionOptionName?: string })
39+
._versionOptionName;
40+
41+
if (!versionOptionName) {
42+
return returnValue;
43+
}
44+
45+
const versionOption = this.options.find(
46+
(option) => option.attributeName() === versionOptionName,
47+
) as InteractiveOption;
48+
// eslint-disable-next-line unicorn/no-useless-undefined
49+
versionOption.prompt(undefined);
50+
51+
return returnValue;
52+
}
53+
3554
/**
3655
* Enable interactive mode
3756
*

0 commit comments

Comments
 (0)