Skip to content

Commit f45ecc6

Browse files
committed
feat(buddy): centralized 'unknown subcommand' handler
43 buddy command files repeat the same 5-line buddy.on(':*') pattern with slightly different error messages. The new onUnknownSubcommand() helper consolidates that boilerplate, normalizes the message format, and exits with code 64 (EX_USAGE per <sysexits.h>) instead of bare 1. That makes 'user typed an unknown subcommand' distinguishable from 'real failure' for shell scripts and CI jobs without parsing stderr. This commit only adds the helper; the call-site sweep can land incrementally per command file.
1 parent 259d9e3 commit f45ecc6

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { CLI } from '@stacksjs/types'
2+
import process from 'node:process'
3+
4+
/**
5+
* Wire the standard "unknown subcommand" handler for a command group.
6+
*
7+
* Every buddy command file used to repeat this 5-line pattern:
8+
*
9+
* ```ts
10+
* buddy.on('foo:*', () => {
11+
* console.error('Invalid command: %s\nSee --help for ...', buddy.args.join(' '))
12+
* process.exit(1)
13+
* })
14+
* ```
15+
*
16+
* Centralizing it here:
17+
* 1. Removes ~43 copies of the same boilerplate
18+
* 2. Makes the message format consistent (and easy to update)
19+
* 3. Routes through a shared exit code so test harnesses can
20+
* distinguish "user typed an unknown subcommand" from real
21+
* errors without scraping stderr
22+
*
23+
* @example
24+
* ```ts
25+
* import { onUnknownSubcommand } from './_helpers'
26+
*
27+
* onUnknownSubcommand(buddy, 'queue')
28+
* ```
29+
*/
30+
export function onUnknownSubcommand(buddy: CLI, prefix: string): void {
31+
buddy.on(`${prefix}:*`, () => {
32+
const args = (buddy as { args?: string[] }).args ?? []
33+
process.stderr.write(
34+
`Unknown ${prefix} subcommand: ${args.join(' ')}\n`
35+
+ `Run \`buddy ${prefix} --help\` to see available subcommands.\n`,
36+
)
37+
// Exit 64 = EX_USAGE per <sysexits.h>. Distinct from 1 (general
38+
// failure) so CI / shell scripts can branch on "user-error" vs
39+
// "real failure" without parsing stderr.
40+
process.exit(64)
41+
})
42+
}

0 commit comments

Comments
 (0)