Skip to content

Commit 9f7f4a4

Browse files
committed
@dephub/command@1.0.1
1 parent 5b69d0b commit 9f7f4a4

6 files changed

Lines changed: 45 additions & 24 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const cmd = new Command()
6060
});
6161

6262
try {
63-
await clit.run(); // (defaults tokens `process.argv.slice(2)`)
63+
await cmd.run(); // (defaults tokens `process.argv.slice(2)`)
6464
} catch (err) {
6565
if (err instanceof CommandError) {
6666
console.error(`\nError: ${err.message}\n`);
@@ -86,7 +86,7 @@ const cmd = new Command()
8686
});
8787

8888
try {
89-
await clit.run(); // (defaults tokens `process.argv.slice(2)`)
89+
await cmd.run(); // (defaults tokens `process.argv.slice(2)`)
9090
} catch (err) {
9191
if (err instanceof CommandError) {
9292
console.error(`\nError: ${err.message}\n`);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dephub/command",
33
"type": "module",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"description": "A type-safe CLI command builder for Node.js, enabling easy creation of commands with arguments, options, subcommands, and handlers.",
66
"types": "./dist/index.d.ts",
77
"main": "./dist/index.js",

src/cli.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
#!/usr/bin/env node
2+
/* eslint-disable no-console */
23
import { description, name, version } from '@/helpers/pkg.js';
34

4-
import { Command } from './index.js';
5+
import { Command, CommandError } from './index.js';
56

67
const cmd = new Command()
78
.name(name.split('/')[1] ?? 'command')
89
.description(description)
910
.version(version);
1011

11-
await cmd.run();
12+
try {
13+
await cmd.run();
14+
} catch (err) {
15+
if (err instanceof CommandError) {
16+
console.error(`\nError: ${err.message}\n`);
17+
cmd.help();
18+
process.exit(1);
19+
}
20+
throw err;
21+
}

src/core/command.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class Command<
7676

7777
/**
7878
* Parses the provided tokens (or default CLI arguments) into a typed CommandInput.
79-
* @param tokens - Optional array of tokens to parse; defaults to process/Deno args.
79+
* @param tokens - Optional array of tokens to parse; defaults to process.argv.slice(2).
8080
* @returns The parsed CommandInput with typed args, options, and unparsed tokens.
8181
*/
8282
parse(tokens?: string[]): CommandInput<Options, Arguments> {
@@ -90,26 +90,10 @@ export class Command<
9090
* Runs the command with the provided tokens (or default CLI arguments).
9191
* Parses input, handles flags like --help/--version, executes handlers, and processes subcommands.
9292
* Automatically adds --help/-h and --version/-v options unless hidden.
93-
* @param tokens - Optional array of tokens to run with; defaults to process/Deno args.
93+
* @param tokens - Optional array of tokens to run with; defaults to process.argv.slice(2).
9494
* @returns A Promise that resolves when the command execution completes.
9595
*/
9696
async run(tokens?: string[]): Promise<void> {
97-
// Auto-add --help / --version unless hidden
98-
if (!this[$config]().hidden.help) {
99-
this.option('--help', {
100-
description: `Show help`,
101-
kind: 'flag',
102-
shortFlag: '-h',
103-
});
104-
}
105-
106-
if (!this[$config]().hidden.version) {
107-
this.option('--version', {
108-
description: `Show version`,
109-
kind: 'flag',
110-
shortFlag: '-v',
111-
});
112-
}
11397
await runner(this[$config](), tokens);
11498
}
11599

src/helpers/help.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,3 @@ export const showHelp = <
148148
printSubcommands();
149149
console.log('');
150150
};
151-
// deno --allow-all test.ts url -h

src/helpers/runner.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@ export const runner = async <
2727
config: CommandConfig<Options, Arguments>,
2828
tokens?: string[],
2929
) => {
30+
// Auto-add --help unless hidden
31+
if (
32+
!config.hidden.help &&
33+
!config.options.some((o) => o.longFlag === '--help')
34+
) {
35+
config.options.push({
36+
description: 'Show help',
37+
kind: 'flag',
38+
longFlag: '--help',
39+
optional: true,
40+
shortFlag: '-h',
41+
});
42+
}
43+
44+
// Auto-add --version unless hidden
45+
if (
46+
!config.hidden.version &&
47+
!config.options.some((o) => o.longFlag === '--version')
48+
) {
49+
config.options.push({
50+
description: 'Show version',
51+
kind: 'flag',
52+
longFlag: '--version',
53+
optional: true,
54+
shortFlag: '-v',
55+
});
56+
}
57+
3058
const parsed = parseRuntime(config, tokens);
3159
const { handlers, hidden, name, subcommands, version } = config;
3260

0 commit comments

Comments
 (0)