@bemoje/cli 2.0.1 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +453 -296
- package/index.mjs.map +4 -4
- package/lib/Command.d.ts +69 -45
- package/lib/Help.d.ts +4 -1
- package/lib/helpers/findOption.d.ts +2 -1
- package/lib/internal/validateParsed.d.ts +2 -1
- package/lib/types.d.ts +4 -1
- package/package.json +2 -9
package/lib/Command.d.ts
CHANGED
|
@@ -1,125 +1,149 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ActionHandler } from './types';
|
|
2
|
+
import type { AllowedArgumentUsage } from './types';
|
|
3
|
+
import type { Argument } from './types';
|
|
4
|
+
import type { Arguments } from './types';
|
|
5
|
+
import type { BooleanOptionOptions } from './types';
|
|
6
|
+
import type { CamelCase } from 'type-fest';
|
|
2
7
|
import { Help } from './Help';
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
8
|
+
import type { HookActionHandler } from './types';
|
|
9
|
+
import type { HookDefinition } from './types';
|
|
10
|
+
import type { ICommand } from './types';
|
|
11
|
+
import type { InferAddOptionResult } from './types';
|
|
12
|
+
import type { InferAddedArgumentType } from './types';
|
|
13
|
+
import type { Option } from './types';
|
|
14
|
+
import type { OptionalArgumentOptions } from './types';
|
|
15
|
+
import type { OptionalArgumentOptionsWithDefaultValue } from './types';
|
|
16
|
+
import type { OptionalOptionOptions } from './types';
|
|
17
|
+
import type { OptionalVariadicArgumentOptions } from './types';
|
|
18
|
+
import type { OptionalVariadicOptionOptions } from './types';
|
|
19
|
+
import type { Options } from './types';
|
|
20
|
+
import type { ParseArgvResult } from './types';
|
|
21
|
+
import type { RequiredArgumentOptions } from './types';
|
|
22
|
+
import type { RequiredOptionOptions } from './types';
|
|
23
|
+
import type { RequiredVariadicArgumentOptions } from './types';
|
|
24
|
+
import type { RequiredVariadicOptionOptions } from './types';
|
|
25
|
+
import type { SetFieldType } from 'type-fest';
|
|
26
|
+
import type { SetRequired } from 'type-fest';
|
|
27
|
+
import type { Simplify } from 'type-fest';
|
|
28
|
+
import type { SubCommands } from './types';
|
|
5
29
|
/**
|
|
6
|
-
*
|
|
30
|
+
* a type-safe CLI composer that can parse argv and generate help without execution coupling.
|
|
7
31
|
*/
|
|
8
32
|
export declare class Command<A extends Arguments = [], O extends Options = {
|
|
9
33
|
help?: boolean;
|
|
10
34
|
debug?: boolean;
|
|
11
35
|
}, Subs extends SubCommands = SubCommands> implements ICommand {
|
|
12
|
-
/**
|
|
36
|
+
/** parent command in the hierarchy, undefined for root command */
|
|
13
37
|
parent?: Command<Arguments, Options & O>;
|
|
14
|
-
/**
|
|
38
|
+
/** the command name used to invoke it */
|
|
15
39
|
name: string;
|
|
16
|
-
/**
|
|
40
|
+
/** semantic version string displayed by --version flag */
|
|
17
41
|
version?: string;
|
|
18
|
-
/**
|
|
42
|
+
/** alternative names for invoking this command */
|
|
19
43
|
aliases: string[];
|
|
20
|
-
/**
|
|
44
|
+
/** brief one-line description shown in command lists */
|
|
21
45
|
summary?: string;
|
|
22
|
-
/**
|
|
46
|
+
/** full description displayed in help text */
|
|
23
47
|
description: string;
|
|
24
|
-
/**
|
|
48
|
+
/** whether to exclude from help listings */
|
|
25
49
|
hidden?: boolean;
|
|
26
|
-
/**
|
|
50
|
+
/** category for organizing related commands in help output */
|
|
27
51
|
group?: string;
|
|
28
|
-
/**
|
|
52
|
+
/** positional arguments this command accepts */
|
|
29
53
|
arguments: Argument[];
|
|
30
|
-
/**
|
|
54
|
+
/** cLI options (flags) this command recognizes */
|
|
31
55
|
options: Option[];
|
|
32
|
-
/**
|
|
56
|
+
/** subcommands registered with this command */
|
|
33
57
|
commands: Subs;
|
|
34
|
-
/**
|
|
58
|
+
/** main action handler executed when command is invoked */
|
|
35
59
|
protected action?: ActionHandler<A, O, Subs>;
|
|
36
|
-
/**
|
|
60
|
+
/** option-driven actions (e.g., --help, --version) executed when their conditions match */
|
|
37
61
|
protected hooks: HookDefinition<Arguments, Options & O>[];
|
|
38
62
|
constructor(name: string, parent?: ICommand);
|
|
39
63
|
protected get help(): Help;
|
|
40
|
-
/**
|
|
64
|
+
/** configure how the help is rendered */
|
|
41
65
|
helpConfiguration(cb?: (help: Help) => void): this;
|
|
42
|
-
/**
|
|
66
|
+
/** renders formatted help text using provided help definition */
|
|
43
67
|
renderHelp(config?: {
|
|
44
68
|
noColor?: boolean;
|
|
45
69
|
}): string;
|
|
46
|
-
/**
|
|
70
|
+
/** sets the command name */
|
|
47
71
|
setName(name: string): void;
|
|
48
|
-
/**
|
|
72
|
+
/** sets command aliases, flattening nested arrays */
|
|
49
73
|
setAliases(...aliases: (string | string[])[]): this;
|
|
50
|
-
/**
|
|
74
|
+
/** adds aliases to existing ones */
|
|
51
75
|
addAliases(...aliases: (string | string[])[]): this;
|
|
52
|
-
/**
|
|
76
|
+
/** sets the command version */
|
|
53
77
|
setVersion(version: string): InferAddOptionResult<A, O, {
|
|
54
78
|
version?: boolean;
|
|
55
79
|
}, Subs>;
|
|
56
|
-
/**
|
|
80
|
+
/** sets the command summary */
|
|
57
81
|
setSummary(summary?: string): this;
|
|
58
|
-
/**
|
|
82
|
+
/** sets command description, joining variadic lines */
|
|
59
83
|
setDescription(...lines: string[]): this;
|
|
60
|
-
/**
|
|
84
|
+
/** sets whether command is hidden from help */
|
|
61
85
|
setHidden(hidden?: boolean | undefined): this;
|
|
62
|
-
/**
|
|
86
|
+
/** sets the command group for help organization */
|
|
63
87
|
setGroup(group?: string): this;
|
|
64
|
-
/**
|
|
88
|
+
/** add a subcommand and return the subcommand. All options are inherited by the subcommand. */
|
|
65
89
|
command<Name extends string, Sub extends Command<any, any, any> = Command<[], O, {}>>(name: Name, cb?: (cmd: Command<[], O, {}>, parent: this) => Sub): Sub;
|
|
66
|
-
/**
|
|
90
|
+
/** add a subcommand and return the subcommand. All options are inherited by the subcommand. */
|
|
67
91
|
addCommand<Name extends string, Sub extends Command<any, any, any> = Command<[], O, {}>>(name: Name, cb: (cmd: Command<[], O, {}>, parent: this) => Sub): Command<A, O, (SubCommands extends Subs ? {} : Subs) & {
|
|
68
92
|
[K in Name]: Sub;
|
|
69
93
|
}>;
|
|
70
|
-
/**
|
|
94
|
+
/** add required variadic argument, eg.: `<name...>` */
|
|
71
95
|
addArgument<const Opts extends RequiredVariadicArgumentOptions>(usage: AllowedArgumentUsage<this, `<${string}...>`>, options?: Opts): Command<[...A, InferAddedArgumentType<Opts>[]], O, Subs>;
|
|
72
|
-
/**
|
|
96
|
+
/** add required argument, eg.: `<name>` */
|
|
73
97
|
addArgument<const Opts extends RequiredArgumentOptions>(usage: AllowedArgumentUsage<this, `<${string}>`>, options?: Opts): Command<[...A, InferAddedArgumentType<Opts>], O, Subs>;
|
|
74
|
-
/**
|
|
98
|
+
/** add optional variadic argument with defaults, eg.: `[name...]` */
|
|
75
99
|
addArgument<const Opts extends OptionalVariadicArgumentOptions>(usage: AllowedArgumentUsage<this, `[${string}...]`>, options?: Opts): Command<[...A, InferAddedArgumentType<Opts>[]], O, Subs>;
|
|
76
|
-
/**
|
|
100
|
+
/** add optional argument with default, eg.: `[name]` */
|
|
77
101
|
addArgument<const Opts extends OptionalArgumentOptionsWithDefaultValue>(usage: AllowedArgumentUsage<this, `[${string}]`>, options: Opts): Command<[...A, InferAddedArgumentType<Opts>], O, Subs>;
|
|
78
|
-
/**
|
|
102
|
+
/** add optional argument, eg.: `[name]` */
|
|
79
103
|
addArgument<const Opts extends OptionalArgumentOptions>(usage: AllowedArgumentUsage<this, `[${string}]`>, options?: Opts): Command<[...A, InferAddedArgumentType<Opts> | undefined], O, Subs>;
|
|
80
|
-
/**
|
|
104
|
+
/** add optional string option, eg.: `-o, --output [path]` */
|
|
81
105
|
addOption<Long extends string>(flags: `-${string}, --${Long} [${string}]`, options?: SetFieldType<OptionalOptionOptions, 'defaultValue', undefined | never>): Command<A, Simplify<{
|
|
82
106
|
[K in CamelCase<Long>]?: string;
|
|
83
107
|
} & O>, Subs>;
|
|
84
|
-
/**
|
|
108
|
+
/** add optional string option with default, eg.: `-o, --output [path]` */
|
|
85
109
|
addOption<Long extends string>(flags: `-${string}, --${Long} [${string}]`, options: SetFieldType<SetRequired<OptionalOptionOptions, 'defaultValue'>, 'defaultValue', string>): Command<A, Simplify<{
|
|
86
110
|
[K in CamelCase<Long>]: string;
|
|
87
111
|
} & O>, Subs>;
|
|
88
|
-
/**
|
|
112
|
+
/** add required variadic option, eg.: `-i, --include <patterns...>` */
|
|
89
113
|
addOption<Long extends string>(flags: `-${string}, --${Long} <${string}...>`, options?: RequiredVariadicOptionOptions): Command<A, Simplify<{
|
|
90
114
|
[K in CamelCase<Long>]: string[];
|
|
91
115
|
} & O>, Subs>;
|
|
92
|
-
/**
|
|
116
|
+
/** add optional variadic option with defaults, eg.: `-e, --exclude [patterns...]` */
|
|
93
117
|
addOption<Long extends string>(flags: `-${string}, --${Long} [${string}...]`, options?: OptionalVariadicOptionOptions): Command<A, Simplify<{
|
|
94
118
|
[K in CamelCase<Long>]: string[];
|
|
95
119
|
} & O>, Subs>;
|
|
96
|
-
/**
|
|
120
|
+
/** add required string option, eg.: `-f, --file <path>` */
|
|
97
121
|
addOption<Long extends string>(flags: `-${string}, --${Long} <${string}>`, options?: RequiredOptionOptions): Command<A, Simplify<{
|
|
98
122
|
[K in CamelCase<Long>]: string;
|
|
99
123
|
} & O>, Subs>;
|
|
100
|
-
/**
|
|
124
|
+
/** add boolean flag option with default, eg.: `-v, --verbose` */
|
|
101
125
|
addOption<Long extends string>(flags: `-${string}, --${Long}`, options: SetFieldType<SetRequired<BooleanOptionOptions, 'defaultValue'>, 'defaultValue', boolean>): Command<A, Simplify<{
|
|
102
126
|
[K in CamelCase<Long>]: boolean;
|
|
103
127
|
} & O>, Subs>;
|
|
104
|
-
/**
|
|
128
|
+
/** add boolean flag option, eg.: `-v, --verbose` */
|
|
105
129
|
addOption<Long extends string>(flags: `-${string}, --${Long}`, options?: SetFieldType<BooleanOptionOptions, 'defaultValue', undefined | never>): Command<A, Simplify<{
|
|
106
130
|
[K in CamelCase<Long>]?: boolean;
|
|
107
131
|
} & O>, Subs>;
|
|
108
132
|
/**
|
|
109
|
-
*
|
|
133
|
+
* register an action to be invoked when an option is set to true or string value.
|
|
110
134
|
*
|
|
111
135
|
* Hooks execute in addition to or instead of the main action handler,
|
|
112
136
|
* allowing for option-driven behavior. For example, `--help` and `--version`
|
|
113
137
|
* are implemented as hooks.
|
|
114
138
|
*/
|
|
115
139
|
addOptionHook(optionName: keyof O, action: HookActionHandler<Arguments, O>): this;
|
|
116
|
-
/**
|
|
140
|
+
/** parses command-line arguments with subcommand support and type-safe validation. */
|
|
117
141
|
parseArgv(argv?: string[]): ParseArgvResult<Arguments, Options & O>;
|
|
118
142
|
/**
|
|
119
|
-
*
|
|
143
|
+
* sets the main action handler for this command, which is executed after any matching option hooks when the command is invoked.
|
|
120
144
|
* The handler receives parsed arguments and options with correct typings.
|
|
121
145
|
*/
|
|
122
146
|
setAction(fn: ActionHandler<A, O, Subs>): this;
|
|
123
|
-
/**
|
|
147
|
+
/** returns a new Command instance. Override this method in subclasses. */
|
|
124
148
|
protected createSubcommand(name: string): Command<[], O, {}>;
|
|
125
149
|
}
|
package/lib/Help.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Argument } from './types';
|
|
2
|
+
import type { ICommand } from './types';
|
|
3
|
+
import type { IHelp } from './types';
|
|
4
|
+
import type { Option } from './types';
|
|
2
5
|
/**
|
|
3
6
|
* This is a fork of the Help class from the 'commander' npm package. The Help class method names as well as the
|
|
4
7
|
* expected interface of the Command instance to parse, are both similar, but different and not compatible without
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import type { Argument
|
|
1
|
+
import type { Argument } from '../types';
|
|
2
|
+
import type { Option } from '../types';
|
|
2
3
|
/** Validate parsed arguments and option values, returning errors or undefined */
|
|
3
4
|
export declare function validateParsed(args: unknown[], optionValues: Record<string, unknown>, argDefs: Argument[], optionDefs: Option[]): string[] | undefined;
|
package/lib/types.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import type { AllUnionFields } from 'type-fest';
|
|
1
2
|
import type { Command } from './Command';
|
|
2
|
-
import type {
|
|
3
|
+
import type { SetFieldType } from 'type-fest';
|
|
4
|
+
import type { SetRequired } from 'type-fest';
|
|
5
|
+
import type { Simplify } from 'type-fest';
|
|
3
6
|
/** Logger interface defining methods for different log levels. */
|
|
4
7
|
export interface Logger {
|
|
5
8
|
start: (...args: unknown[]) => void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/cli",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "A type-safe CLI composer that can parse argv and generate help without execution coupling.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -28,18 +28,11 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@sinclair/typebox": "^0.34.37",
|
|
32
31
|
"ansi-colors": "^4.1.3",
|
|
33
|
-
"cli-table": "^0.3.11",
|
|
34
32
|
"enhanced-ms": "^4.1.0",
|
|
35
33
|
"es-toolkit": "^1.44.0",
|
|
36
34
|
"humanize-duration": "^3.33.2",
|
|
37
|
-
"
|
|
38
|
-
"memoizee": "^0.4.17",
|
|
39
|
-
"mnemonist": "^0.40.3",
|
|
40
|
-
"onetime": "^7.0.0",
|
|
41
|
-
"p-queue": "^8.1.0",
|
|
42
|
-
"upath": "^2.0.1"
|
|
35
|
+
"memoizee": "^0.4.17"
|
|
43
36
|
},
|
|
44
37
|
"publishConfig": {
|
|
45
38
|
"access": "public"
|