Skip to content

Commit 1f03795

Browse files
committed
feat(cli): add command-specific help topics
1 parent bc89062 commit 1f03795

6 files changed

Lines changed: 210 additions & 9 deletions

File tree

README.en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ coder-studio config root set /srv/coder-studio/workspaces
7676
coder-studio config password set --stdin
7777
coder-studio auth status
7878
coder-studio auth ip list
79+
coder-studio help start
7980
```
8081

8182
For the detailed command reference, see `docs/development/cli.en.md`.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ coder-studio config root set /srv/coder-studio/workspaces
7575
coder-studio config password set --stdin
7676
coder-studio auth status
7777
coder-studio auth ip list
78+
coder-studio help start
7879
```
7980

8081
详细命令说明见:`docs/development/cli.md`

docs/development/cli.en.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ Version:
8080
coder-studio --version
8181
```
8282

83+
Help:
84+
85+
```bash
86+
coder-studio help
87+
coder-studio help start
88+
coder-studio help config
89+
coder-studio help auth
90+
```
91+
8392
## Global Flags
8493

8594
Most runtime commands support:

docs/development/cli.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ coder-studio auth ip unblock --all
8080
coder-studio --version
8181
```
8282

83+
帮助:
84+
85+
```bash
86+
coder-studio help
87+
coder-studio help start
88+
coder-studio help config
89+
coder-studio help auth
90+
```
91+
8392
## 全局参数
8493

8594
大部分运行时命令支持:

packages/coder-studio/lib/cli.mjs

Lines changed: 171 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function printHelp() {
9696
console.log(`Coder Studio CLI
9797
9898
Usage:
99+
coder-studio help [command]
99100
coder-studio start [--host 127.0.0.1] [--port 41033] [--foreground] [--json]
100101
coder-studio stop [--json]
101102
coder-studio restart [--json]
@@ -119,6 +120,7 @@ Exit Codes:
119120
2 usage or argument error
120121
121122
Examples:
123+
coder-studio help start
122124
coder-studio start
123125
coder-studio config show --json
124126
coder-studio config root set /srv/coder-studio/workspaces
@@ -128,6 +130,123 @@ Run \`coder-studio config --help\` or \`coder-studio auth --help\` for detailed
128130
`);
129131
}
130132

133+
function printStartHelp() {
134+
console.log(`coder-studio start
135+
136+
Usage:
137+
coder-studio start [--host <host>] [--port <port>] [--foreground] [--json]
138+
139+
Options:
140+
--host <host> override configured host for this invocation
141+
--port <port> override configured port for this invocation
142+
--foreground keep the runtime in the foreground
143+
--json machine-readable output
144+
145+
Examples:
146+
coder-studio start
147+
coder-studio start --foreground
148+
coder-studio start --port 42033 --json
149+
`);
150+
}
151+
152+
function printStopHelp() {
153+
console.log(`coder-studio stop
154+
155+
Usage:
156+
coder-studio stop [--json]
157+
158+
Options:
159+
--json machine-readable output
160+
161+
Examples:
162+
coder-studio stop
163+
coder-studio stop --json
164+
`);
165+
}
166+
167+
function printRestartHelp() {
168+
console.log(`coder-studio restart
169+
170+
Usage:
171+
coder-studio restart [--json]
172+
173+
Options:
174+
--json machine-readable output
175+
176+
Examples:
177+
coder-studio restart
178+
coder-studio restart --json
179+
`);
180+
}
181+
182+
function printStatusHelp() {
183+
console.log(`coder-studio status
184+
185+
Usage:
186+
coder-studio status [--host <host>] [--port <port>] [--json]
187+
188+
Options:
189+
--host <host> override configured host for this invocation
190+
--port <port> override configured port for this invocation
191+
--json machine-readable output
192+
193+
Examples:
194+
coder-studio status
195+
coder-studio status --json
196+
`);
197+
}
198+
199+
function printLogsHelp() {
200+
console.log(`coder-studio logs
201+
202+
Usage:
203+
coder-studio logs [-f] [-n <lines>]
204+
205+
Options:
206+
-f, --follow follow the runtime log
207+
-n, --lines <n> read the last <n> lines
208+
209+
Examples:
210+
coder-studio logs
211+
coder-studio logs -n 200
212+
coder-studio logs -f
213+
`);
214+
}
215+
216+
function printOpenHelp() {
217+
console.log(`coder-studio open
218+
219+
Usage:
220+
coder-studio open [--host <host>] [--port <port>] [--json]
221+
222+
Options:
223+
--host <host> override configured host for this invocation
224+
--port <port> override configured port for this invocation
225+
--json machine-readable output
226+
227+
Examples:
228+
coder-studio open
229+
coder-studio open --json
230+
`);
231+
}
232+
233+
function printDoctorHelp() {
234+
console.log(`coder-studio doctor
235+
236+
Usage:
237+
coder-studio doctor [--host <host>] [--port <port>] [--json]
238+
239+
Options:
240+
--host <host> override configured host for this invocation
241+
--port <port> override configured port for this invocation
242+
--json machine-readable output
243+
244+
Examples:
245+
coder-studio doctor
246+
coder-studio doctor --json
247+
`);
248+
}
249+
131250
function printConfigHelp() {
132251
console.log(`coder-studio config
133252
@@ -172,6 +291,46 @@ Examples:
172291
`);
173292
}
174293

294+
function printHelpTopic(topic) {
295+
switch (topic) {
296+
case undefined:
297+
case null:
298+
case '':
299+
case 'main':
300+
printHelp();
301+
return EXIT_SUCCESS;
302+
case 'start':
303+
printStartHelp();
304+
return EXIT_SUCCESS;
305+
case 'stop':
306+
printStopHelp();
307+
return EXIT_SUCCESS;
308+
case 'restart':
309+
printRestartHelp();
310+
return EXIT_SUCCESS;
311+
case 'status':
312+
printStatusHelp();
313+
return EXIT_SUCCESS;
314+
case 'logs':
315+
printLogsHelp();
316+
return EXIT_SUCCESS;
317+
case 'open':
318+
printOpenHelp();
319+
return EXIT_SUCCESS;
320+
case 'doctor':
321+
printDoctorHelp();
322+
return EXIT_SUCCESS;
323+
case 'config':
324+
printConfigHelp();
325+
return EXIT_SUCCESS;
326+
case 'auth':
327+
printAuthHelp();
328+
return EXIT_SUCCESS;
329+
default:
330+
throw usageError(`unsupported help topic: ${topic}`, 'main');
331+
}
332+
}
333+
175334
function printStatus(status) {
176335
console.log(`status: ${status.status}`);
177336
console.log(`managed: ${status.managed ? 'yes' : 'no'}`);
@@ -734,17 +893,20 @@ function printCliError(error, flags) {
734893
export async function runCli(argv = process.argv.slice(2)) {
735894
const { command, flags, positionals } = parseArgv(argv);
736895

737-
if (command === '--version' || command === '-v' || flags.version) {
738-
console.log(await readPackageVersion());
739-
return EXIT_SUCCESS;
740-
}
896+
try {
897+
if (command === '--version' || command === '-v' || flags.version) {
898+
console.log(await readPackageVersion());
899+
return EXIT_SUCCESS;
900+
}
741901

742-
if (command === 'help' || flags.help && !['config', 'auth'].includes(command)) {
743-
printHelp();
744-
return EXIT_SUCCESS;
745-
}
902+
if (command === 'help') {
903+
return printHelpTopic(positionals[0]);
904+
}
905+
906+
if (flags.help) {
907+
return printHelpTopic(command);
908+
}
746909

747-
try {
748910
if (command === 'config') {
749911
const context = await resolveCommandContext(flags);
750912
return await handleConfigCommand(positionals, flags, context);

tests/cli/config.test.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,36 @@ test('validateConfigSnapshot reports missing root when public mode is enabled',
142142
test('cli help documents exit codes and common examples', async () => {
143143
const result = await runCli(['help']);
144144
assert.match(result.stdout, /Exit Codes:/);
145+
assert.match(result.stdout, /coder-studio help start/);
145146
assert.match(result.stdout, /coder-studio config show --json/);
146147
assert.match(result.stdout, /coder-studio auth ip list/);
147148
});
148149

150+
test('cli supports dedicated help topics', async () => {
151+
const startHelp = await runCli(['help', 'start']);
152+
assert.match(startHelp.stdout, /^coder-studio start/m);
153+
assert.match(startHelp.stdout, /--foreground/);
154+
155+
const configHelp = await runCli(['config', '--help']);
156+
assert.match(configHelp.stdout, /^coder-studio config/m);
157+
158+
const commandHelp = await runCli(['start', '--help']);
159+
assert.match(commandHelp.stdout, /^coder-studio start/m);
160+
});
161+
149162
test('cli returns usage exit code and hint for unsupported commands', async () => {
150163
const result = await runCli(['wat'], { allowFailure: true });
151164
assert.equal(result.code, 2);
152165
assert.match(result.stderr, /unsupported command: wat/);
153166
assert.match(result.stderr, /coder-studio help/);
154167
});
155168

169+
test('cli returns usage exit code for unsupported help topics', async () => {
170+
const result = await runCli(['help', 'wat'], { allowFailure: true });
171+
assert.equal(result.code, 2);
172+
assert.match(result.stderr, /unsupported help topic: wat/);
173+
});
174+
156175
test('cli returns usage exit code in json mode for invalid config usage', async () => {
157176
const result = await runCli(['config', 'set', 'server.port'], { allowFailure: true });
158177
assert.equal(result.code, 2);

0 commit comments

Comments
 (0)