icore 0.1.38 → 1.0.0-alpha

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/dist/argv.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { OptionsSchema, RawOptionValue } from './options';
2
+ /**
3
+ * Parsed CLI arguments split into positional command path segments and raw
4
+ * named options.
5
+ */
6
+ export type ParsedArgv = {
7
+ positionals: string[];
8
+ options: Record<string, RawOptionValue>;
9
+ };
10
+ /**
11
+ * Parses raw CLI arguments into positionals and raw long-option values.
12
+ */
13
+ export declare function parseArgv(args: readonly string[], schema?: OptionsSchema): ParsedArgv;
14
+ //# sourceMappingURL=argv.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"argv.d.ts","sourceRoot":"","sources":["../src/argv.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,cAAc,EACf,MAAM,WAAW,CAAC;AAEnB;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;CACzC,CAAC;AAEF;;GAEG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,MAAM,CAAC,EAAE,aAAa,GACrB,UAAU,CAqEZ"}
package/dist/argv.js ADDED
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseArgv = parseArgv;
4
+ /**
5
+ * Parses raw CLI arguments into positionals and raw long-option values.
6
+ */
7
+ function parseArgv(args, schema) {
8
+ const positionals = [];
9
+ const options = {};
10
+ let parseOptions = true;
11
+ for (let index = 0; index < args.length; index += 1) {
12
+ const arg = args[index];
13
+ if (arg === undefined) {
14
+ continue;
15
+ }
16
+ if (!parseOptions || !arg.startsWith('--') || arg === '--') {
17
+ if (arg === '--') {
18
+ parseOptions = false;
19
+ continue;
20
+ }
21
+ positionals.push(arg);
22
+ continue;
23
+ }
24
+ const option = arg.slice(2);
25
+ const separatorIndex = option.indexOf('=');
26
+ const name = separatorIndex === -1
27
+ ? option
28
+ : option.slice(0, separatorIndex);
29
+ if (name === '') {
30
+ throw new Error(`Unexpected argument '${arg}'`);
31
+ }
32
+ if (Object.hasOwn(options, name)) {
33
+ throw new Error(`Unexpected duplicate argument '--${name}'`);
34
+ }
35
+ if (separatorIndex !== -1) {
36
+ options[name] = option.slice(separatorIndex + 1);
37
+ continue;
38
+ }
39
+ const definition = schema?.[name];
40
+ if (definition?.type === 'boolean') {
41
+ options[name] = true;
42
+ continue;
43
+ }
44
+ const nextArg = args[index + 1];
45
+ if (definition !== undefined && nextArg !== undefined && nextArg !== '--') {
46
+ options[name] = nextArg;
47
+ index += 1;
48
+ continue;
49
+ }
50
+ if (nextArg !== undefined && !nextArg.startsWith('-')) {
51
+ options[name] = nextArg;
52
+ index += 1;
53
+ continue;
54
+ }
55
+ options[name] = true;
56
+ }
57
+ return {
58
+ positionals,
59
+ options
60
+ };
61
+ }
62
+ //# sourceMappingURL=argv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"argv.js","sourceRoot":"","sources":["../src/argv.ts"],"names":[],"mappings":";;AAiBA,8BAwEC;AA3ED;;GAEG;AACH,SAAgB,SAAS,CACvB,IAAuB,EACvB,MAAsB;IAEtB,MAAM,WAAW,GAAa,EAAE,CAAC;IACjC,MAAM,OAAO,GAAmC,EAAE,CAAC;IACnD,IAAI,YAAY,GAAG,IAAI,CAAC;IAExB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QACpD,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;QAExB,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtB,SAAS;QACX,CAAC;QAED,IAAI,CAAC,YAAY,IAAI,C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I,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;YAC1E,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;YACxB,KAAK,IAAI,CAAC,CAAC;YACX,SAAS;QACX,CAAC;QAED,IAAI,OAAO,KAAK,SAAS,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACtD,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;YACxB,KAAK,IAAI,CAAC,CAAC;YACX,SAAS;QACX,CAAC;QAED,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACvB,CAAC;IAED,OAAO;QACL,WAAW;QACX,OAAO;KACR,CAAC;AACJ,CAAC"}
package/dist/cli.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * The CLI mechanics module describes declarative command contracts and performs
3
+ * generic user argument parsing.
4
+ *
5
+ * Allowed here:
6
+ * - parsing raw argv into positionals and long options;
7
+ * - validating primitive CLI options against declarative schemas;
8
+ * - applying and validating defaults;
9
+ * - checking command paths and extra positionals;
10
+ * - running command handlers with typed options and caller-provided context.
11
+ *
12
+ * Not allowed here:
13
+ * - application-specific business rules;
14
+ * - SDK/API request building;
15
+ * - external client lifecycle management;
16
+ * - JSON/table/CSV output formatting;
17
+ * - domain-specific command DSLs.
18
+ */
19
+ export { parseArgv, type ParsedArgv } from './argv';
20
+ export { defineCommand, defineCommandRegistry, isCommandName, resolveCommand, resolveCommandFromArgs, runCommand, runCommandFromRegistry, type CommandDefinition, type CommandInput, type CommandName, type CommandRegistry, type ResolvedCommand } from './commands';
21
+ export { mergeOptionsSchema, parseOptions, parseOptionsDetailed, type BooleanOption, type InferOptions, type InferProvidedOptions, type MergeOptionsSchemas, type NumberOption, type OptionDefinition, type OptionsSchema, type ParseOptionsResult, type RawOptionValue, type StringOption } from './options';
22
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EACL,SAAS,EACT,KAAK,UAAU,EAChB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,aAAa,EACb,cAAc,EACd,sBAAsB,EACtB,UAAU,EACV,sBAAsB,EACtB,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,eAAe,EACrB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,oBAAoB,EACpB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACxB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,YAAY,EAClB,MAAM,WAAW,CAAC"}
package/dist/cli.js ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /**
3
+ * The CLI mechanics module describes declarative command contracts and performs
4
+ * generic user argument parsing.
5
+ *
6
+ * Allowed here:
7
+ * - parsing raw argv into positionals and long options;
8
+ * - validating primitive CLI options against declarative schemas;
9
+ * - applying and validating defaults;
10
+ * - checking command paths and extra positionals;
11
+ * - running command handlers with typed options and caller-provided context.
12
+ *
13
+ * Not allowed here:
14
+ * - application-specific business rules;
15
+ * - SDK/API request building;
16
+ * - external client lifecycle management;
17
+ * - JSON/table/CSV output formatting;
18
+ * - domain-specific command DSLs.
19
+ */
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.parseOptionsDetailed = exports.parseOptions = exports.mergeOptionsSchema = exports.runCommandFromRegistry = exports.runCommand = exports.resolveCommandFromArgs = exports.resolveCommand = exports.isCommandName = exports.defineCommandRegistry = exports.defineCommand = exports.parseArgv = void 0;
22
+ var argv_1 = require("./argv");
23
+ Object.defineProperty(exports, "parseArgv", { enumerable: true, get: function () { return argv_1.parseArgv; } });
24
+ var commands_1 = require("./commands");
25
+ Object.defineProperty(exports, "defineCommand", { enumerable: true, get: function () { return commands_1.defineCommand; } });
26
+ Object.defineProperty(exports, "defineCommandRegistry", { enumerable: true, get: function () { return commands_1.defineCommandRegistry; } });
27
+ Object.defineProperty(exports, "isCommandName", { enumerable: true, get: function () { return commands_1.isCommandName; } });
28
+ Object.defineProperty(exports, "resolveCommand", { enumerable: true, get: function () { return commands_1.resolveCommand; } });
29
+ Object.defineProperty(exports, "resolveCommandFromArgs", { enumerable: true, get: function () { return commands_1.resolveCommandFromArgs; } });
30
+ Object.defineProperty(exports, "runCommand", { enumerable: true, get: function () { return commands_1.runCommand; } });
31
+ Object.defineProperty(exports, "runCommandFromRegistry", { enumerable: true, get: function () { return commands_1.runCommandFromRegistry; } });
32
+ var options_1 = require("./options");
33
+ Object.defineProperty(exports, "mergeOptionsSchema", { enumerable: true, get: function () { return options_1.mergeOptionsSchema; } });
34
+ Object.defineProperty(exports, "parseOptions", { enumerable: true, get: function () { return options_1.parseOptions; } });
35
+ Object.defineProperty(exports, "parseOptionsDetailed", { enumerable: true, get: function () { return options_1.parseOptionsDetailed; } });
36
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;;AAEH,+BAGgB;AAFd,iGAAA,SAAS,OAAA;AAGX,uCAaoB;AAZlB,yGAAA,aAAa,OAAA;AACb,iHAAA,qBAAqB,OAAA;AACrB,yGAAA,aAAa,OAAA;AACb,0GAAA,cAAc,OAAA;AACd,kHAAA,sBAAsB,OAAA;AACtB,sGAAA,UAAU,OAAA;AACV,kHAAA,sBAAsB,OAAA;AAOxB,qCAcmB;AAbjB,6GAAA,kBAAkB,OAAA;AAClB,uGAAA,YAAY,OAAA;AACZ,+GAAA,oBAAoB,OAAA"}
@@ -0,0 +1,81 @@
1
+ import { type InferOptions, type InferProvidedOptions, type OptionsSchema } from './options';
2
+ /**
3
+ * Input passed to a command handler after command path and option validation.
4
+ */
5
+ export type CommandInput<TSchema extends OptionsSchema, TContext> = {
6
+ options: InferOptions<TSchema>;
7
+ provided: InferProvidedOptions<TSchema>;
8
+ positionals: string[];
9
+ context: TContext;
10
+ };
11
+ /**
12
+ * Declarative command contract.
13
+ *
14
+ * `icore` owns command mechanics. The handler remains responsible for
15
+ * application-specific work such as API calls, request building, and output
16
+ * formatting.
17
+ */
18
+ export type CommandDefinition<TSchema extends OptionsSchema, TContext, TResult, TPath extends readonly [string, ...string[]] = readonly [string, ...string[]]> = {
19
+ path: TPath;
20
+ options: TSchema;
21
+ allowExtraPositionals?: boolean;
22
+ handle(input: CommandInput<TSchema, TContext>): TResult | Promise<TResult>;
23
+ };
24
+ type AnyCommandDefinition = CommandDefinition<OptionsSchema, any, any>;
25
+ type CommandPathName<TPath extends readonly string[]> = number extends TPath['length'] ? string : TPath extends readonly [infer THead extends string] ? THead : TPath extends readonly [
26
+ infer THead extends string,
27
+ ...infer TRest extends readonly string[]
28
+ ] ? `${THead} ${CommandPathName<TRest>}` : never;
29
+ type CommandContext<TCommand extends AnyCommandDefinition> = TCommand extends CommandDefinition<OptionsSchema, infer TContext, any> ? TContext : never;
30
+ type CommandResult<TCommand extends AnyCommandDefinition> = TCommand extends CommandDefinition<OptionsSchema, any, infer TResult> ? Awaited<TResult> : never;
31
+ /**
32
+ * Infers the public command name from a command path.
33
+ */
34
+ export type CommandName<TCommand extends AnyCommandDefinition> = CommandPathName<TCommand['path']>;
35
+ /**
36
+ * Declarative command registry used to resolve command paths.
37
+ */
38
+ export type CommandRegistry<TCommands extends readonly AnyCommandDefinition[]> = {
39
+ commands: TCommands;
40
+ commandNames: readonly CommandName<TCommands[number]>[];
41
+ };
42
+ /**
43
+ * Result of resolving a command from user positionals.
44
+ */
45
+ export type ResolvedCommand<TCommand extends AnyCommandDefinition> = {
46
+ name: CommandName<TCommand>;
47
+ path: TCommand['path'];
48
+ command: TCommand;
49
+ positionals: string[];
50
+ };
51
+ /**
52
+ * Defines a command while preserving literal option schema types.
53
+ */
54
+ export declare function defineCommand<const TSchema extends OptionsSchema, const TPath extends readonly [string, ...string[]], TContext = undefined, TResult = unknown>(command: CommandDefinition<TSchema, TContext, TResult, TPath>): CommandDefinition<TSchema, TContext, TResult, TPath>;
55
+ /**
56
+ * Defines a command registry while preserving literal command path types.
57
+ */
58
+ export declare function defineCommandRegistry<const TCommands extends readonly AnyCommandDefinition[]>(commands: TCommands): CommandRegistry<TCommands>;
59
+ /**
60
+ * Checks whether a value is a command name registered in the given registry.
61
+ */
62
+ export declare function isCommandName<const TCommands extends readonly AnyCommandDefinition[]>(registry: CommandRegistry<TCommands>, value: unknown): value is CommandName<TCommands[number]>;
63
+ /**
64
+ * Resolves a command from already parsed positional arguments.
65
+ */
66
+ export declare function resolveCommand<const TCommands extends readonly AnyCommandDefinition[]>(registry: CommandRegistry<TCommands>, positionals: readonly string[]): ResolvedCommand<TCommands[number]>;
67
+ /**
68
+ * Resolves a command from raw CLI arguments using each command's option schema.
69
+ */
70
+ export declare function resolveCommandFromArgs<const TCommands extends readonly AnyCommandDefinition[]>(registry: CommandRegistry<TCommands>, args: readonly string[]): ResolvedCommand<TCommands[number]>;
71
+ /**
72
+ * Resolves a command from a registry and runs its handler.
73
+ */
74
+ export declare function runCommandFromRegistry<const TCommands extends readonly AnyCommandDefinition[]>(registry: CommandRegistry<TCommands>, args: readonly string[], context: CommandContext<TCommands[number]>): Promise<CommandResult<TCommands[number]>>;
75
+ /**
76
+ * Parses arguments, validates command mechanics, and executes a command
77
+ * handler.
78
+ */
79
+ export declare function runCommand<const TSchema extends OptionsSchema, TContext, TResult>(command: CommandDefinition<TSchema, TContext, TResult>, args: readonly string[], context: TContext): Promise<TResult>;
80
+ export {};
81
+ //# sourceMappingURL=commands.d.ts.map
@@ -0,0 +1 @@
1
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AsB,UAAU,CAC9B,KAAK,CAAC,OAAO,SAAS,aAAa,EACnC,QAAQ,EACR,OAAO,EAEP,OAAO,EAAE,iBAAiB,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EACtD,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,OAAO,EAAE,QAAQ,GAChB,OAAO,CAAC,OAAO,CAAC,CAkBlB"}
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineCommand = defineCommand;
4
+ exports.defineCommandRegistry = defineCommandRegistry;
5
+ exports.isCommandName = isCommandName;
6
+ exports.resolveCommand = resolveCommand;
7
+ exports.resolveCommandFromArgs = resolveCommandFromArgs;
8
+ exports.runCommandFromRegistry = runCommandFromRegistry;
9
+ exports.runCommand = runCommand;
10
+ const argv_1 = require("./argv");
11
+ const options_1 = require("./options");
12
+ /**
13
+ * Defines a command while preserving literal option schema types.
14
+ */
15
+ function defineCommand(command) {
16
+ return command;
17
+ }
18
+ /**
19
+ * Defines a command registry while preserving literal command path types.
20
+ */
21
+ function defineCommandRegistry(commands) {
22
+ const commandNames = commands.map((command) => commandPathToName(command.path));
23
+ assertNoDuplicateCommandNames(commandNames);
24
+ return {
25
+ commands,
26
+ commandNames: commandNames
27
+ };
28
+ }
29
+ /**
30
+ * Checks whether a value is a command name registered in the given registry.
31
+ */
32
+ function isCommandName(registry, value) {
33
+ return typeof value === 'string'
34
+ && registry.commandNames.includes(value);
35
+ }
36
+ /**
37
+ * Resolves a command from already parsed positional arguments.
38
+ */
39
+ function resolveCommand(registry, positionals) {
40
+ for (const command of commandsBySpecificity(registry.commands)) {
41
+ const resolved = resolveCommandCandidate(command, positionals);
42
+ if (resolved !== undefined) {
43
+ return resolved;
44
+ }
45
+ }
46
+ throw new Error(`Unknown command: ${formatCommandPositionals(positionals)}`);
47
+ }
48
+ /**
49
+ * Resolves a command from raw CLI arguments using each command's option schema.
50
+ */
51
+ function resolveCommandFromArgs(registry, args) {
52
+ for (const command of commandsBySpecificity(registry.commands)) {
53
+ const argv = (0, argv_1.parseArgv)(args, command.options);
54
+ const resolved = resolveCommandCandidate(command, argv.positionals);
55
+ if (resolved !== undefined) {
56
+ return resolved;
57
+ }
58
+ }
59
+ throw new Error(`Unknown command: ${formatCommandPositionals((0, argv_1.parseArgv)(args).positionals)}`);
60
+ }
61
+ /**
62
+ * Resolves a command from a registry and runs its handler.
63
+ */
64
+ async function runCommandFromRegistry(registry, args, context) {
65
+ const resolved = resolveCommandFromArgs(registry, args);
66
+ return runCommand(resolved.command, args, context);
67
+ }
68
+ /**
69
+ * Parses arguments, validates command mechanics, and executes a command
70
+ * handler.
71
+ */
72
+ async function runCommand(command, args, context) {
73
+ const argv = (0, argv_1.parseArgv)(args, command.options);
74
+ const extraPositionals = resolveCommandPositionals(command.path, argv.positionals);
75
+ if (extraPositionals.length > 0 && command.allowExtraPositionals !== true) {
76
+ throw new Error(`Unexpected positional argument for '${command.path.join(' ')}': ${extraPositionals[0] ?? ''}`);
77
+ }
78
+ const parsed = (0, options_1.parseOptionsDetailed)(command.options, argv.options);
79
+ return command.handle({
80
+ options: parsed.options,
81
+ provided: parsed.provided,
82
+ positionals: extraPositionals,
83
+ context
84
+ });
85
+ }
86
+ function resolveCommandPositionals(path, positionals) {
87
+ for (let index = 0; index < path.length; index += 1) {
88
+ if (positionals[index] !== path[index]) {
89
+ throw new Error(`Expected command '${path.join(' ')}'`);
90
+ }
91
+ }
92
+ return positionals.slice(path.length);
93
+ }
94
+ function commandPathToName(path) {
95
+ return path.join(' ');
96
+ }
97
+ function assertNoDuplicateCommandNames(commandNames) {
98
+ const seen = new Set();
99
+ for (const name of commandNames) {
100
+ if (seen.has(name)) {
101
+ throw new Error(`Unexpected duplicate command '${name}'`);
102
+ }
103
+ seen.add(name);
104
+ }
105
+ }
106
+ function commandsBySpecificity(commands) {
107
+ return [...commands].sort((left, right) => right.path.length - left.path.length);
108
+ }
109
+ function resolveCommandCandidate(command, positionals) {
110
+ const extraPositionals = resolveMatchingCommandPositionals(command.path, positionals);
111
+ if (extraPositionals === undefined) {
112
+ return undefined;
113
+ }
114
+ return {
115
+ name: commandPathToName(command.path),
116
+ path: command.path,
117
+ command,
118
+ positionals: extraPositionals
119
+ };
120
+ }
121
+ function resolveMatchingCommandPositionals(path, positionals) {
122
+ for (let index = 0; index < path.length; index += 1) {
123
+ if (positionals[index] !== path[index]) {
124
+ return undefined;
125
+ }
126
+ }
127
+ return positionals.slice(path.length);
128
+ }
129
+ function formatCommandPositionals(positionals) {
130
+ return positionals.length === 0 ? '<empty>' : positionals.join(' ');
131
+ }
132
+ //# sourceMappingURL=commands.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.js","sourceRoot":"","sources":["../src/commands.ts"],"names":[],"mappings":";;AA6FA,sCASC;AAKD,sDAaC;AAKD,sCAQC;AAKD,wCAeC;AAKD,wDAgBC;AAKD,wDAcC;AAMD,gCA0BC;AAjOD,iCAEgB;AAChB,uCAKmB;AAkFnB;;GAEG;AACH,SAAgB,aAAa,CAM3B,OAA6D;IAE7D,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,SAAgB,qBAAqB,CAGnC,QAAmB;IAEnB,MAAM,YAAY,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,iBAAiB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAEhF,6BAA6B,CAAC,YAAY,CAAC,CAAC;IAE5C,OAAO;QACL,QAAQ;QACR,YAAY,EAAE,YAAoE;KACnF,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAgB,aAAa,CAG3B,QAAoC,EACpC,KAAc;IAEd,OAAO,OAAO,KAAK,KAAK,QAAQ;WAC3B,QAAQ,CAAC,YAAY,CAAC,QAAQ,CAAC,KAAuC,CAAC,CAAC;AAC/E,CAAC;AAED;;GAEG;AACH,SAAgB,cAAc,CAG5B,QAAoC,EACpC,WAA8B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AC,IAAI,CAAC,MAAM,CAAC,CAAC;AACxC,CAAC;AAED,SAAS,wBAAwB,CAAC,WAA8B;IAC9D,OAAO,WAAW,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACtE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { defineCommand, defineCommandRegistry, isCommandName, mergeOptionsSchema, parseArgv, parseOptions, parseOptionsDetailed, resolveCommand, resolveCommandFromArgs, runCommand, runCommandFromRegistry, type BooleanOption, type CommandDefinition, type CommandInput, type CommandName, type CommandRegistry, type InferOptions, type InferProvidedOptions, type MergeOptionsSchemas, type NumberOption, type OptionDefinition, type OptionsSchema, type ParseOptionsResult, type ParsedArgv, type RawOptionValue, type ResolvedCommand, type StringOption } from './cli';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,aAAa,EACb,kBAAkB,EAClB,SAAS,EACT,YAAY,EACZ,oBAAoB,EACpB,cAAc,EACd,sBAAsB,EACtB,UAAU,EACV,sBAAsB,EACtB,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,YAAY,EACjB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACxB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,YAAY,EAClB,MAAM,OAAO,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runCommandFromRegistry = exports.runCommand = exports.resolveCommandFromArgs = exports.resolveCommand = exports.parseOptionsDetailed = exports.parseOptions = exports.parseArgv = exports.mergeOptionsSchema = exports.isCommandName = exports.defineCommandRegistry = exports.defineCommand = void 0;
4
+ var cli_1 = require("./cli");
5
+ Object.defineProperty(exports, "defineCommand", { enumerable: true, get: function () { return cli_1.defineCommand; } });
6
+ Object.defineProperty(exports, "defineCommandRegistry", { enumerable: true, get: function () { return cli_1.defineCommandRegistry; } });
7
+ Object.defineProperty(exports, "isCommandName", { enumerable: true, get: function () { return cli_1.isCommandName; } });
8
+ Object.defineProperty(exports, "mergeOptionsSchema", { enumerable: true, get: function () { return cli_1.mergeOptionsSchema; } });
9
+ Object.defineProperty(exports, "parseArgv", { enumerable: true, get: function () { return cli_1.parseArgv; } });
10
+ Object.defineProperty(exports, "parseOptions", { enumerable: true, get: function () { return cli_1.parseOptions; } });
11
+ Object.defineProperty(exports, "parseOptionsDetailed", { enumerable: true, get: function () { return cli_1.parseOptionsDetailed; } });
12
+ Object.defineProperty(exports, "resolveCommand", { enumerable: true, get: function () { return cli_1.resolveCommand; } });
13
+ Object.defineProperty(exports, "resolveCommandFromArgs", { enumerable: true, get: function () { return cli_1.resolveCommandFromArgs; } });
14
+ Object.defineProperty(exports, "runCommand", { enumerable: true, get: function () { return cli_1.runCommand; } });
15
+ Object.defineProperty(exports, "runCommandFromRegistry", { enumerable: true, get: function () { return cli_1.runCommandFromRegistry; } });
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,6BA4Be;AA3Bb,oGAAA,aAAa,OAAA;AACb,4GAAA,qBAAqB,OAAA;AACrB,oGAAA,aAAa,OAAA;AACb,yGAAA,kBAAkB,OAAA;AAClB,gGAAA,SAAS,OAAA;AACT,mGAAA,YAAY,OAAA;AACZ,2GAAA,oBAAoB,OAAA;AACpB,qGAAA,cAAc,OAAA;AACd,6GAAA,sBAAsB,OAAA;AACtB,iGAAA,UAAU,OAAA;AACV,6GAAA,sBAAsB,OAAA"}
@@ -0,0 +1,119 @@
1
+ type OptionBase<TType extends string, TValue> = {
2
+ type: TType;
3
+ required?: boolean;
4
+ default?: TValue;
5
+ };
6
+ /**
7
+ * Raw option value produced by `parseArgv` before schema validation.
8
+ *
9
+ * Long options with a value are stored as strings, and flag-only options are
10
+ * stored as `true`.
11
+ */
12
+ export type RawOptionValue = string | boolean;
13
+ /**
14
+ * Declarative string option contract.
15
+ *
16
+ * `choices` narrows the parsed value to a string literal union when the schema
17
+ * is declared with `as const`.
18
+ */
19
+ export type StringOption<TChoices extends readonly string[] = readonly string[]> = OptionBase<'string', TChoices[number] | string> & {
20
+ choices?: TChoices;
21
+ };
22
+ /**
23
+ * Declarative boolean flag contract.
24
+ *
25
+ * Boolean options accept flag form only, for example `--insecure`.
26
+ */
27
+ export type BooleanOption = OptionBase<'boolean', boolean>;
28
+ /**
29
+ * Declarative number option contract.
30
+ *
31
+ * Number options can require integer values and enforce inclusive `min` / `max`
32
+ * bounds.
33
+ */
34
+ export type NumberOption<TChoices extends readonly number[] = readonly number[]> = OptionBase<'number', TChoices[number] | number> & {
35
+ choices?: TChoices;
36
+ integer?: boolean;
37
+ min?: number;
38
+ max?: number;
39
+ };
40
+ /**
41
+ * Any supported option definition.
42
+ */
43
+ export type OptionDefinition = StringOption | BooleanOption | NumberOption;
44
+ /**
45
+ * Command option schema keyed by exact public CLI option names.
46
+ */
47
+ export type OptionsSchema = Record<string, OptionDefinition>;
48
+ type Simplify<TValue> = {
49
+ [TName in keyof TValue]: TValue[TName];
50
+ } & {};
51
+ type MergeOptionsSchemaPair<TLeft extends OptionsSchema, TRight extends OptionsSchema> = Simplify<Omit<TLeft, keyof TRight> & TRight>;
52
+ type MergeOptionsSchemasWithResult<TSchemas extends readonly OptionsSchema[], TResult extends OptionsSchema> = TSchemas extends readonly [
53
+ infer THead extends OptionsSchema,
54
+ ...infer TRest extends readonly OptionsSchema[]
55
+ ] ? MergeOptionsSchemasWithResult<TRest, MergeOptionsSchemaPair<TResult, THead>> : TResult;
56
+ /**
57
+ * Infers the schema produced by `mergeOptionsSchema`.
58
+ */
59
+ export type MergeOptionsSchemas<TSchemas extends readonly OptionsSchema[]> = MergeOptionsSchemasWithResult<TSchemas, Record<never, never>>;
60
+ type StringOptionValue<TOption> = TOption extends {
61
+ choices: readonly (infer TChoice extends string)[];
62
+ } ? TChoice : string;
63
+ type NumberOptionValue<TOption> = TOption extends {
64
+ choices: readonly (infer TChoice extends number)[];
65
+ } ? TChoice : number;
66
+ type OptionValue<TOption> = TOption extends {
67
+ type: 'string';
68
+ } ? StringOptionValue<TOption> : TOption extends {
69
+ type: 'boolean';
70
+ } ? boolean : TOption extends {
71
+ type: 'number';
72
+ } ? NumberOptionValue<TOption> : never;
73
+ type OptionIsAlwaysPresent<TOption> = TOption extends {
74
+ required: true;
75
+ } ? true : TOption extends {
76
+ default: unknown;
77
+ } ? true : false;
78
+ /**
79
+ * Infers parsed option values from an option schema.
80
+ *
81
+ * Required options and options with defaults are always present. Optional
82
+ * options without defaults are returned as `T | undefined`.
83
+ */
84
+ export type InferOptions<TSchema extends OptionsSchema> = {
85
+ [TName in keyof TSchema]: OptionIsAlwaysPresent<TSchema[TName]> extends true ? OptionValue<TSchema[TName]> : OptionValue<TSchema[TName]> | undefined;
86
+ };
87
+ /**
88
+ * Infers an option presence map from an option schema.
89
+ *
90
+ * `true` means the user provided the option explicitly. Defaults do not make an
91
+ * option provided.
92
+ */
93
+ export type InferProvidedOptions<TSchema extends OptionsSchema> = {
94
+ [TName in keyof TSchema]: boolean;
95
+ };
96
+ /**
97
+ * Detailed option parsing result with values and user-provided metadata.
98
+ */
99
+ export type ParseOptionsResult<TSchema extends OptionsSchema> = {
100
+ options: InferOptions<TSchema>;
101
+ provided: InferProvidedOptions<TSchema>;
102
+ };
103
+ /**
104
+ * Merges option schemas while preserving literal option definition types.
105
+ *
106
+ * Later schemas override earlier schemas with the same option name.
107
+ */
108
+ export declare function mergeOptionsSchema<const TSchema extends OptionsSchema, const TSchemas extends readonly OptionsSchema[]>(schema: TSchema, ...schemas: TSchemas): MergeOptionsSchemas<readonly [TSchema, ...TSchemas]>;
109
+ /**
110
+ * Validates raw option values against a declarative option schema.
111
+ */
112
+ export declare function parseOptions<const TSchema extends OptionsSchema>(schema: TSchema, values: Record<string, RawOptionValue>): InferOptions<TSchema>;
113
+ /**
114
+ * Validates raw option values and returns parsed values with user-provided
115
+ * metadata.
116
+ */
117
+ export declare function parseOptionsDetailed<const TSchema extends OptionsSchema>(schema: TSchema, values: Record<string, RawOptionValue>): ParseOptionsResult<TSchema>;
118
+ export {};
119
+ //# sourceMappingURL=options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,CAAC,KAAK,SAAS,MAAM,EAAE,MAAM,IAAI;IAC9C,IAAI,EAAE,KAAK,CAAC;IACZ,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,OAAO,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,MAAM,YAAY,CAAC,QAAQ,SAAS,SAAS,MAAM,EAAE,GAAG,SAAS,MAAM,EAAE,IAC7E,UAAU,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG;IAChD,OAAO,CAAC,EAAE,QAAQ,CAAC;CACpB,CAAC;AAEJ;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;AAE3D;;;;;GAKG;AACH,MAAM,MAAM,YAAY,CAAC,QAAQ,SAAS,SAAS,MAAM,EAAE,GAAG,SAAS,MAAM,EAAE,IAC7E,UAAU,CAAC,QAAQ,EAAE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