Skip to content

Commit 73be5f9

Browse files
committed
feat: improve CLI help system with grouped command display and enhanced error handling; set NO_COLOR environment variable for better output control
1 parent cec012d commit 73be5f9

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

lib/cli.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,10 @@ function parseNameWithLang(name) {
3434

3535
program.name('snip').version(pkg.version).description(pkg.description)
3636
.enablePositionalOptions()
37-
.option('--no-color', 'Disable colored output');
37+
.option('--no-color', 'Disable colored output')
38+
.exitOverride();
3839

39-
// UX: Set NO_COLOR env before commands run so chalk auto-detects it
40-
program.hook('preAction', () => {
41-
if (program.opts().color === false) {
42-
process.env.NO_COLOR = '1';
43-
}
44-
});
45-
46-
// UX: Show rich help with command groups
47-
program.addHelpCommand('help [cmd]', 'Show help for commands');
48-
49-
// UX: Custom help with grouped commands
50-
program.on('--help', () => {
40+
function showGroupedHelp() {
5141
console.log('');
5242
console.log(c.accent('┌─────────────────────────────────────────────────────────────┐'));
5343
console.log(c.accent('│') + ' SNIPPETS ' + c.accent('│'));
@@ -87,8 +77,19 @@ program.on('--help', () => {
8777
console.log(c.dim(' snip search docker --limit 5 # Find snippets'));
8878
console.log(c.dim(' snip list --tag deploy --sort recent # Filter & sort'));
8979
console.log('');
80+
}
81+
82+
program.command('help').action(showGroupedHelp);
83+
84+
// UX: Set NO_COLOR env before commands run so chalk auto-detects it
85+
program.hook('preAction', () => {
86+
if (program.opts().color === false) {
87+
process.env.NO_COLOR = '1';
88+
}
9089
});
9190

91+
92+
9293
program
9394
.command('add <name>')
9495
.description('Add a new snippet (supports shortcuts: add:js, add:py, add:sh)')
@@ -358,8 +359,16 @@ if (process.argv.length <= 2) {
358359
} catch (_e) { }
359360
}
360361

361-
program.on('--help', () => {
362-
console.log('\nQuick start:\n echo \'echo "hello"\' | snip add test --lang sh --tags demo\n snip list\n snip search hello\n snip run test --dry-run\n');
363-
});
362+
if (process.argv.includes('--help') || process.argv.includes('-h')) {
363+
showGroupedHelp();
364+
process.exit(0);
365+
}
364366

365-
program.parse(process.argv);
367+
try {
368+
program.parse(process.argv);
369+
} catch (err) {
370+
if (err.code === 'commander.help' || err.code === 'commander.version') {
371+
process.exit(0);
372+
}
373+
throw err;
374+
}

0 commit comments

Comments
 (0)