Skip to content

Commit b258ea3

Browse files
committed
fix(cli): an unknown command or topic subcommand points at the help that lists the real ones
`os environments frobnicate` answered `command environments:frobnicate not found` and nothing else. A command_not_found hook keeps the hard error and exit 2, spells the id as typed and ends with the help of the deepest topic the words reached (`os environments --help`), or `os --help`. Claude-Session: https://claude.ai/code/session_01Utp6CZiaBg6rk7dpy1s9Me Co-authored-by: Claude <noreply@anthropic.com>
1 parent a8d84f4 commit b258ea3

3 files changed

Lines changed: 104 additions & 1 deletion

File tree

‎packages/cli/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"glob": "**/*.js"
5454
},
5555
"hooks": {
56-
"preparse": "./dist/hooks/preparse/strip-arg-separator.js"
56+
"preparse": "./dist/hooks/preparse/strip-arg-separator.js",
57+
"command_not_found": "./dist/hooks/command-not-found/point-at-help.js"
5758
},
5859
"topicSeparator": " ",
5960
"topics": {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import { readFileSync } from 'node:fs';
4+
import { dirname, resolve } from 'node:path';
5+
import { fileURLToPath } from 'node:url';
6+
import { describe, expect, it } from 'vitest';
7+
import hook, { commandNotFoundMessage, type CommandNotFoundConfig } from './point-at-help.js';
8+
9+
const HERE = dirname(fileURLToPath(import.meta.url));
10+
const PKG = resolve(HERE, '../../../package.json');
11+
12+
/** A command table with one topic (`environments`), one nested topic and one command under a topic. */
13+
const config: CommandNotFoundConfig = {
14+
bin: 'os',
15+
topicSeparator: ' ',
16+
findTopic: (id) => ['environments', 'plugin', 'plugin:trust', 'migrate'].includes(id),
17+
findCommand: (id) => ['migrate:plan', 'dev'].includes(id),
18+
};
19+
20+
const refusal = async (id: string): Promise<any> => {
21+
try {
22+
await (hook as unknown as (opts: { id: string; config: CommandNotFoundConfig }) => Promise<unknown>)({ id, config });
23+
} catch (error) {
24+
return error;
25+
}
26+
throw new Error('expected the hook to refuse');
27+
};
28+
29+
describe('an unknown command points at the help that lists the real ones', () => {
30+
it('a topic-level unknown subcommand: the id as typed, and the topic\'s own help', async () => {
31+
const error = await refusal('environments:frobnicate');
32+
expect(error.oclif?.exit).toBe(2);
33+
expect(error.message).toBe('command environments frobnicate not found\nSee more help with `os environments --help`');
34+
});
35+
36+
it('a top-level unknown command points at the root help', async () => {
37+
const error = await refusal('frobnicate');
38+
expect(error.oclif?.exit).toBe(2);
39+
expect(error.message).toBe('command frobnicate not found\nSee more help with `os --help`');
40+
});
41+
42+
it('the deepest topic or command the typed words reached is the one named', () => {
43+
expect(commandNotFoundMessage('plugin:trust:frobnicate', config)).toContain('`os plugin trust --help`');
44+
expect(commandNotFoundMessage('migrate:plan:frobnicate', config)).toContain('`os migrate plan --help`');
45+
expect(commandNotFoundMessage('nope:frobnicate', config)).toContain('`os --help`');
46+
});
47+
48+
it('is registered as the CLI\'s command_not_found hook, from the built tree', () => {
49+
const manifest = JSON.parse(readFileSync(PKG, 'utf8'));
50+
expect(manifest.oclif.hooks.command_not_found).toBe('./dist/hooks/command-not-found/point-at-help.js');
51+
});
52+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import { Errors, type Hook } from '@oclif/core';
4+
5+
/**
6+
* An unknown command ends with a pointer to the help that lists the real ones.
7+
*
8+
* Without this hook oclif answers `os environments frobnicate` with one line,
9+
* `command environments:frobnicate not found`, and nothing else: the id in its
10+
* internal colon spelling (this CLI separates topics with a space) and no way
11+
* forward. A wrong FLAG already ends with oclif's `See more help with --help`;
12+
* a wrong command name did not, at the top level or under a topic.
13+
*
14+
* The answer is unchanged in kind — a hard error, exit 2, the command never
15+
* runs, and no guess at what was meant (this CLI ships no not-found suggester;
16+
* a typo is refused, not reinterpreted). Only the words change: the id is
17+
* spelled the way it was typed, and the pointer names the help of the deepest
18+
* topic or command the typed words did reach (`os environments --help`), or
19+
* the root's (`os --help`) when the first word is already unknown.
20+
*/
21+
22+
/** The slice of oclif's `Config` the message reads. */
23+
export interface CommandNotFoundConfig {
24+
bin: string;
25+
topicSeparator: string;
26+
findTopic(id: string): unknown;
27+
findCommand(id: string): unknown;
28+
}
29+
30+
/** The refusal's text: what was not found, then where its siblings are listed. */
31+
export function commandNotFoundMessage(id: string, config: CommandNotFoundConfig): string {
32+
const separator = config.topicSeparator || ':';
33+
const segments = id.split(':').filter((segment) => segment !== '');
34+
let reached: string[] = [];
35+
for (let length = segments.length - 1; length > 0; length -= 1) {
36+
const prefix = segments.slice(0, length).join(':');
37+
if (config.findTopic(prefix) || config.findCommand(prefix)) {
38+
reached = segments.slice(0, length);
39+
break;
40+
}
41+
}
42+
const help = [config.bin, ...reached, '--help'].join(' ');
43+
return `command ${segments.join(separator)} not found\nSee more help with \`${help}\``;
44+
}
45+
46+
const hook: Hook<'command_not_found'> = async function ({ id, config }) {
47+
throw new Errors.CLIError(commandNotFoundMessage(id, config), { exit: 2 });
48+
};
49+
50+
export default hook;

0 commit comments

Comments
 (0)