Skip to content

Commit 5379169

Browse files
committed
Update docs
1 parent 6c9bc7d commit 5379169

File tree

6 files changed

+96
-9
lines changed

6 files changed

+96
-9
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,57 @@ await program
106106

107107
More examples can be found in the [examples](/examples/) directory.
108108

109+
## Recipes
110+
111+
### Interactive options for the root command
112+
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.
115+
116+
```typescript
117+
import { InteractiveCommand } from "interactive-commander";
118+
119+
const program = new InteractiveCommand();
120+
121+
program
122+
.command("hello", { isDefault: true })
123+
.requiredOption("-n, --name <name>", "your name")
124+
.action((options) => {
125+
console.log("Hello %s!", options.name);
126+
});
127+
128+
await program.interactive().parseAsync(process.argv);
129+
130+
// Try the following commands:
131+
// default-subcommand
132+
// default-subcommand -n John
133+
// default-subcommand -i
134+
```
135+
136+
### Enable interactive mode by default
137+
138+
The interactive mode flags can be [negatable boolean options][3]
139+
(e.g. `--no-interactive`). Negatable boolean options are disabled by default.
140+
141+
```typescript
142+
const program = new InteractiveCommand();
143+
144+
program
145+
.command("hello")
146+
.option("-n, --name <name>", "your name")
147+
.action((options) => {
148+
console.log("Hello %s!", options.name);
149+
});
150+
151+
await program
152+
.interactive("-I, --no-interactive", "disable interactive mode")
153+
.parseAsync(process.argv);
154+
155+
// Try the following commands:
156+
// no-interactive hello
157+
// no-interactive hello -I
158+
```
159+
109160
[1]: https://github.com/tj/commander.js
110161
[2]: https://github.com/SBoudrias/Inquirer.js
162+
[3]: https://github.com/tj/commander.js#other-option-types-negatable-boolean-and-booleanvalue

examples/default-subcommand.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { InteractiveCommand } from "../src/index.ts";
2+
3+
const program = new InteractiveCommand();
4+
5+
program
6+
.command("hello", { isDefault: true })
7+
.requiredOption("-n, --name <name>", "your name")
8+
.action((options) => {
9+
console.log("Hello %s!", options.name);
10+
});
11+
12+
await program.interactive().parseAsync(process.argv);
13+
14+
// Try the following commands:
15+
// default-subcommand
16+
// default-subcommand -n John
17+
// default-subcommand -i

examples/no-interactive.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { InteractiveCommand } from "../src/index.ts";
2+
3+
const program = new InteractiveCommand();
4+
5+
program
6+
.command("hello")
7+
.option("-n, --name <name>", "your name")
8+
.action((options) => {
9+
console.log("Hello %s!", options.name);
10+
});
11+
12+
await program
13+
.interactive("-I, --no-interactive", "disable interactive mode")
14+
.parseAsync(process.argv);
15+
16+
// Try the following commands:
17+
// no-interactive hello
18+
// no-interactive hello -I

examples/order-pizza.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ await program
6565
.parseAsync(process.argv);
6666

6767
// Try the following commands:
68-
// command-name pizza
69-
// command-name pizza -i
70-
// command-name pizza -i --count 2
71-
// command-name pizza -i --count 2 --no-cheese
72-
// command-name pizza -i --name "John Doe"
73-
// command-name pizza -i --name "John Doe" --non-interactive-option abc
68+
// order-pizza pizza
69+
// order-pizza pizza -i
70+
// order-pizza pizza -i --count 2
71+
// order-pizza pizza -i --count 2 --no-cheese
72+
// order-pizza pizza -i --name "John Doe"
73+
// order-pizza pizza -i --name "John Doe" --non-interactive-option abc

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.0",
3+
"version": "0.2.1",
44
"description": "Commander.js with integrated interactive prompts",
55
"keywords": [
66
"commander",

0 commit comments

Comments
 (0)