@boneskull/bargs 2.0.0 → 3.0.0
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/README.md +305 -299
- package/dist/bargs.cjs +465 -135
- package/dist/bargs.cjs.map +1 -1
- package/dist/bargs.d.cts +35 -17
- package/dist/bargs.d.cts.map +1 -1
- package/dist/bargs.d.ts +35 -17
- package/dist/bargs.d.ts.map +1 -1
- package/dist/bargs.js +463 -135
- package/dist/bargs.js.map +1 -1
- package/dist/help.cjs +1 -2
- package/dist/help.cjs.map +1 -1
- package/dist/help.d.cts +20 -3
- package/dist/help.d.cts.map +1 -1
- package/dist/help.d.ts +20 -3
- package/dist/help.d.ts.map +1 -1
- package/dist/help.js +1 -2
- package/dist/help.js.map +1 -1
- package/dist/index.cjs +27 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -79
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +15 -79
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -30
- package/dist/index.js.map +1 -1
- package/dist/opt.cjs +148 -122
- package/dist/opt.cjs.map +1 -1
- package/dist/opt.d.cts +87 -113
- package/dist/opt.d.cts.map +1 -1
- package/dist/opt.d.ts +87 -113
- package/dist/opt.d.ts.map +1 -1
- package/dist/opt.js +147 -121
- package/dist/opt.js.map +1 -1
- package/dist/parser.cjs +3 -230
- package/dist/parser.cjs.map +1 -1
- package/dist/parser.d.cts +3 -51
- package/dist/parser.d.cts.map +1 -1
- package/dist/parser.d.ts +3 -51
- package/dist/parser.d.ts.map +1 -1
- package/dist/parser.js +2 -223
- package/dist/parser.js.map +1 -1
- package/dist/types.cjs +1 -3
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +111 -233
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.ts +111 -233
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -3
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/dist/validate.cjs +0 -463
- package/dist/validate.cjs.map +0 -1
- package/dist/validate.d.cts +0 -28
- package/dist/validate.d.cts.map +0 -1
- package/dist/validate.d.ts +0 -28
- package/dist/validate.d.ts.map +0 -1
- package/dist/validate.js +0 -459
- package/dist/validate.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,97 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Main entry point for the bargs CLI argument parser.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* attached option builder methods (e.g., `bargs.string()`, `bargs.boolean()`),
|
|
6
|
-
* allowing both function-call and builder-namespace usage patterns. It also
|
|
7
|
-
* re-exports all public types, error classes, help generators, theme utilities,
|
|
8
|
-
* and OSC hyperlink functions.
|
|
4
|
+
* Provides a combinator-style API for building type-safe CLIs.
|
|
9
5
|
*
|
|
10
6
|
* @example
|
|
11
7
|
*
|
|
12
8
|
* ```typescript
|
|
13
|
-
* import { bargs } from 'bargs';
|
|
9
|
+
* import { bargs, opt, pos } from '@boneskull/bargs';
|
|
14
10
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
11
|
+
* await bargs
|
|
12
|
+
* .create('my-app', { version: '1.0.0' })
|
|
13
|
+
* .globals(opt.options({ verbose: opt.boolean({ aliases: ['v'] }) }))
|
|
14
|
+
* .command(
|
|
15
|
+
* 'greet',
|
|
16
|
+
* pos.positionals(pos.string({ name: 'name', required: true })),
|
|
17
|
+
* ({ positionals }) => console.log(`Hello, ${positionals[0]}!`),
|
|
18
|
+
* 'Say hello',
|
|
19
|
+
* )
|
|
20
|
+
* .parseAsync();
|
|
23
21
|
* ```
|
|
24
22
|
*
|
|
25
23
|
* @packageDocumentation
|
|
26
24
|
*/
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Main bargs entry point (sync). Also provides access to all opt builders via
|
|
30
|
-
* bargs.string(), bargs.boolean(), etc.
|
|
31
|
-
*/
|
|
32
|
-
export declare const bargs: typeof bargsBase & {
|
|
33
|
-
array: (items: "number" | "string", props?: Omit<import("./types.js").ArrayOption, "items" | "type">) => import("./types.js").ArrayOption;
|
|
34
|
-
boolean: <P extends Omit<import("./types.js").BooleanOption, "type"> = Omit<import("./types.js").BooleanOption, "type">>(props?: P) => import("./types.js").BooleanOption & P;
|
|
35
|
-
command: import("./opt.js").CommandBuilder;
|
|
36
|
-
count: (props?: Omit<import("./types.js").CountOption, "type">) => import("./types.js").CountOption;
|
|
37
|
-
enum: <const T extends readonly string[], P extends Omit<import("./types.js").EnumOption<T[number]>, "choices" | "type"> = Omit<import("./types.js").EnumOption<T[number]>, "type" | "choices">>(choices: T, props?: P) => import("./types.js").EnumOption<T[number]> & P;
|
|
38
|
-
enumPos: <const T extends readonly string[], P extends Omit<import("./types.js").EnumPositional<T[number]>, "choices" | "type"> = Omit<import("./types.js").EnumPositional<T[number]>, "type" | "choices">>(choices: T, props?: P) => import("./types.js").EnumPositional<T[number]> & P;
|
|
39
|
-
number: <P extends Omit<import("./types.js").NumberOption, "type"> = Omit<import("./types.js").NumberOption, "type">>(props?: P) => import("./types.js").NumberOption & P;
|
|
40
|
-
numberPos: <P extends Omit<import("./types.js").NumberPositional, "type"> = Omit<import("./types.js").NumberPositional, "type">>(props?: P) => import("./types.js").NumberPositional & P;
|
|
41
|
-
options: {
|
|
42
|
-
<A extends import("./types.js").OptionsSchema>(a: A): A;
|
|
43
|
-
<A extends import("./types.js").OptionsSchema, B extends import("./types.js").OptionsSchema>(a: A, b: B): A & B;
|
|
44
|
-
<A extends import("./types.js").OptionsSchema, B extends import("./types.js").OptionsSchema, C extends import("./types.js").OptionsSchema>(a: A, b: B, c: C): A & B & C;
|
|
45
|
-
<A extends import("./types.js").OptionsSchema, B extends import("./types.js").OptionsSchema, C extends import("./types.js").OptionsSchema, D extends import("./types.js").OptionsSchema>(a: A, b: B, c: C, d: D): A & B & C & D;
|
|
46
|
-
(...schemas: import("./types.js").OptionsSchema[]): import("./types.js").OptionsSchema;
|
|
47
|
-
};
|
|
48
|
-
positionals: {
|
|
49
|
-
<A extends import("./types.js").PositionalDef>(a: A): [A];
|
|
50
|
-
<A extends import("./types.js").PositionalDef, B extends import("./types.js").PositionalDef>(a: A, b: B): [A, B];
|
|
51
|
-
<A extends import("./types.js").PositionalDef, B extends import("./types.js").PositionalDef, C extends import("./types.js").PositionalDef>(a: A, b: B, c: C): [A, B, C];
|
|
52
|
-
<A extends import("./types.js").PositionalDef, B extends import("./types.js").PositionalDef, C extends import("./types.js").PositionalDef, D extends import("./types.js").PositionalDef>(a: A, b: B, c: C, d: D): [A, B, C, D];
|
|
53
|
-
(...positionals: import("./types.js").PositionalDef[]): import("./types.js").PositionalsSchema;
|
|
54
|
-
};
|
|
55
|
-
string: <P extends Omit<import("./types.js").StringOption, "type"> = Omit<import("./types.js").StringOption, "type">>(props?: P) => P & import("./types.js").StringOption;
|
|
56
|
-
stringPos: <P extends Omit<import("./types.js").StringPositional, "type"> = Omit<import("./types.js").StringPositional, "type">>(props?: P) => P & import("./types.js").StringPositional;
|
|
57
|
-
variadic: (items: "number" | "string", props?: Omit<import("./types.js").VariadicPositional, "items" | "type">) => import("./types.js").VariadicPositional;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Async bargs entry point. Also provides access to all opt builders via
|
|
61
|
-
* bargsAsync.string(), etc.
|
|
62
|
-
*/
|
|
63
|
-
export declare const bargsAsync: typeof bargsAsyncBase & {
|
|
64
|
-
array: (items: "number" | "string", props?: Omit<import("./types.js").ArrayOption, "items" | "type">) => import("./types.js").ArrayOption;
|
|
65
|
-
boolean: <P extends Omit<import("./types.js").BooleanOption, "type"> = Omit<import("./types.js").BooleanOption, "type">>(props?: P) => import("./types.js").BooleanOption & P;
|
|
66
|
-
command: import("./opt.js").CommandBuilder;
|
|
67
|
-
count: (props?: Omit<import("./types.js").CountOption, "type">) => import("./types.js").CountOption;
|
|
68
|
-
enum: <const T extends readonly string[], P extends Omit<import("./types.js").EnumOption<T[number]>, "choices" | "type"> = Omit<import("./types.js").EnumOption<T[number]>, "type" | "choices">>(choices: T, props?: P) => import("./types.js").EnumOption<T[number]> & P;
|
|
69
|
-
enumPos: <const T extends readonly string[], P extends Omit<import("./types.js").EnumPositional<T[number]>, "choices" | "type"> = Omit<import("./types.js").EnumPositional<T[number]>, "type" | "choices">>(choices: T, props?: P) => import("./types.js").EnumPositional<T[number]> & P;
|
|
70
|
-
number: <P extends Omit<import("./types.js").NumberOption, "type"> = Omit<import("./types.js").NumberOption, "type">>(props?: P) => import("./types.js").NumberOption & P;
|
|
71
|
-
numberPos: <P extends Omit<import("./types.js").NumberPositional, "type"> = Omit<import("./types.js").NumberPositional, "type">>(props?: P) => import("./types.js").NumberPositional & P;
|
|
72
|
-
options: {
|
|
73
|
-
<A extends import("./types.js").OptionsSchema>(a: A): A;
|
|
74
|
-
<A extends import("./types.js").OptionsSchema, B extends import("./types.js").OptionsSchema>(a: A, b: B): A & B;
|
|
75
|
-
<A extends import("./types.js").OptionsSchema, B extends import("./types.js").OptionsSchema, C extends import("./types.js").OptionsSchema>(a: A, b: B, c: C): A & B & C;
|
|
76
|
-
<A extends import("./types.js").OptionsSchema, B extends import("./types.js").OptionsSchema, C extends import("./types.js").OptionsSchema, D extends import("./types.js").OptionsSchema>(a: A, b: B, c: C, d: D): A & B & C & D;
|
|
77
|
-
(...schemas: import("./types.js").OptionsSchema[]): import("./types.js").OptionsSchema;
|
|
78
|
-
};
|
|
79
|
-
positionals: {
|
|
80
|
-
<A extends import("./types.js").PositionalDef>(a: A): [A];
|
|
81
|
-
<A extends import("./types.js").PositionalDef, B extends import("./types.js").PositionalDef>(a: A, b: B): [A, B];
|
|
82
|
-
<A extends import("./types.js").PositionalDef, B extends import("./types.js").PositionalDef, C extends import("./types.js").PositionalDef>(a: A, b: B, c: C): [A, B, C];
|
|
83
|
-
<A extends import("./types.js").PositionalDef, B extends import("./types.js").PositionalDef, C extends import("./types.js").PositionalDef, D extends import("./types.js").PositionalDef>(a: A, b: B, c: C, d: D): [A, B, C, D];
|
|
84
|
-
(...positionals: import("./types.js").PositionalDef[]): import("./types.js").PositionalsSchema;
|
|
85
|
-
};
|
|
86
|
-
string: <P extends Omit<import("./types.js").StringOption, "type"> = Omit<import("./types.js").StringOption, "type">>(props?: P) => P & import("./types.js").StringOption;
|
|
87
|
-
stringPos: <P extends Omit<import("./types.js").StringPositional, "type"> = Omit<import("./types.js").StringPositional, "type">>(props?: P) => P & import("./types.js").StringPositional;
|
|
88
|
-
variadic: (items: "number" | "string", props?: Omit<import("./types.js").VariadicPositional, "items" | "type">) => import("./types.js").VariadicPositional;
|
|
89
|
-
};
|
|
25
|
+
export { bargs, handle, map, merge } from "./bargs.js";
|
|
90
26
|
export { BargsError, HelpError, ValidationError } from "./errors.js";
|
|
91
27
|
export { generateCommandHelp, generateHelp } from "./help.js";
|
|
92
|
-
export
|
|
28
|
+
export { opt, pos } from "./opt.js";
|
|
93
29
|
export { link, linkifyUrls, supportsHyperlinks } from "./osc.js";
|
|
94
30
|
export { ansi, createStyler, defaultTheme, stripAnsi, themes, } from "./theme.js";
|
|
95
31
|
export type { StyleFn, Styler, Theme, ThemeColors, ThemeInput, } from "./theme.js";
|
|
96
|
-
export type {
|
|
32
|
+
export type { ArrayOption, BooleanOption, CliBuilder, CliResult, Command, CommandDef, CountOption, CreateOptions, EnumOption, EnumPositional, HandlerFn, InferOption, InferOptions, InferPositional, InferPositionals, InferTransformedPositionals, InferTransformedValues, NumberOption, NumberPositional, OptionDef, OptionsSchema, Parser, ParseResult, PositionalDef, PositionalsSchema, PositionalsTransformFn, StringOption, StringPositional, TransformsConfig, ValuesTransformFn, VariadicPositional, } from "./types.js";
|
|
97
33
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,mBAAmB;AAGvD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,oBAAoB;AAGrE,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,kBAAkB;AAG9D,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,iBAAiB;AAGpC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,kBAAkB,EAAE,iBAAiB;AAGjE,OAAO,EACL,IAAI,EACJ,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,MAAM,GACP,mBAAmB;AAGpB,YAAY,EACV,OAAO,EACP,MAAM,EACN,KAAK,EACL,WAAW,EACX,UAAU,GACX,mBAAmB;AAGpB,YAAY,EAEV,WAAW,EACX,aAAa,EAEb,UAAU,EACV,SAAS,EACT,OAAO,EACP,UAAU,EACV,WAAW,EACX,aAAa,EACb,UAAU,EAEV,cAAc,EACd,SAAS,EAET,WAAW,EACX,YAAY,EACZ,eAAe,EACf,gBAAgB,EAChB,2BAA2B,EAC3B,sBAAsB,EACtB,YAAY,EACZ,gBAAgB,EAChB,SAAS,EACT,aAAa,EACb,MAAM,EACN,WAAW,EACX,aAAa,EACb,iBAAiB,EAEjB,sBAAsB,EACtB,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,GACnB,mBAAmB"}
|
package/dist/index.js
CHANGED
|
@@ -1,47 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Main entry point for the bargs CLI argument parser.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* attached option builder methods (e.g., `bargs.string()`, `bargs.boolean()`),
|
|
6
|
-
* allowing both function-call and builder-namespace usage patterns. It also
|
|
7
|
-
* re-exports all public types, error classes, help generators, theme utilities,
|
|
8
|
-
* and OSC hyperlink functions.
|
|
4
|
+
* Provides a combinator-style API for building type-safe CLIs.
|
|
9
5
|
*
|
|
10
6
|
* @example
|
|
11
7
|
*
|
|
12
8
|
* ```typescript
|
|
13
|
-
* import { bargs } from 'bargs';
|
|
9
|
+
* import { bargs, opt, pos } from '@boneskull/bargs';
|
|
14
10
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
11
|
+
* await bargs
|
|
12
|
+
* .create('my-app', { version: '1.0.0' })
|
|
13
|
+
* .globals(opt.options({ verbose: opt.boolean({ aliases: ['v'] }) }))
|
|
14
|
+
* .command(
|
|
15
|
+
* 'greet',
|
|
16
|
+
* pos.positionals(pos.string({ name: 'name', required: true })),
|
|
17
|
+
* ({ positionals }) => console.log(`Hello, ${positionals[0]}!`),
|
|
18
|
+
* 'Say hello',
|
|
19
|
+
* )
|
|
20
|
+
* .parseAsync();
|
|
23
21
|
* ```
|
|
24
22
|
*
|
|
25
23
|
* @packageDocumentation
|
|
26
24
|
*/
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
* Main bargs entry point (sync). Also provides access to all opt builders via
|
|
31
|
-
* bargs.string(), bargs.boolean(), etc.
|
|
32
|
-
*/
|
|
33
|
-
export const bargs = Object.assign(bargsBase, opt);
|
|
34
|
-
/**
|
|
35
|
-
* Async bargs entry point. Also provides access to all opt builders via
|
|
36
|
-
* bargsAsync.string(), etc.
|
|
37
|
-
*/
|
|
38
|
-
export const bargsAsync = Object.assign(bargsAsyncBase, opt);
|
|
39
|
-
// Re-export errors
|
|
25
|
+
// Main API
|
|
26
|
+
export { bargs, handle, map, merge } from "./bargs.js";
|
|
27
|
+
// Errors
|
|
40
28
|
export { BargsError, HelpError, ValidationError } from "./errors.js";
|
|
41
|
-
//
|
|
29
|
+
// Help generators
|
|
42
30
|
export { generateCommandHelp, generateHelp } from "./help.js";
|
|
43
|
-
//
|
|
31
|
+
// Option and positional builders
|
|
32
|
+
export { opt, pos } from "./opt.js";
|
|
33
|
+
// OSC utilities for terminal hyperlinks
|
|
44
34
|
export { link, linkifyUrls, supportsHyperlinks } from "./osc.js";
|
|
45
|
-
//
|
|
35
|
+
// Theme utilities
|
|
46
36
|
export { ansi, createStyler, defaultTheme, stripAnsi, themes, } from "./theme.js";
|
|
47
37
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,WAAW;AACX,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,mBAAmB;AAEvD,SAAS;AACT,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,oBAAoB;AAErE,kBAAkB;AAClB,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,kBAAkB;AAE9D,iCAAiC;AACjC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,iBAAiB;AAEpC,wCAAwC;AACxC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,kBAAkB,EAAE,iBAAiB;AAEjE,kBAAkB;AAClB,OAAO,EACL,IAAI,EACJ,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,MAAM,GACP,mBAAmB"}
|
package/dist/opt.cjs
CHANGED
|
@@ -1,34 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Builder functions for defining CLI options
|
|
3
|
+
* Builder functions for defining CLI options and positionals.
|
|
4
4
|
*
|
|
5
5
|
* Provides ergonomic helpers with full TypeScript type inference for
|
|
6
6
|
* constructing option schemas (`opt.string()`, `opt.boolean()`, `opt.enum()`,
|
|
7
|
-
* etc.)
|
|
8
|
-
* `opt.variadic()`)
|
|
9
|
-
* composition utilities for merging schemas (`opt.options()`,
|
|
10
|
-
* `opt.positionals()`).
|
|
7
|
+
* etc.) and positional schemas (`opt.stringPos()`, `opt.numberPos()`,
|
|
8
|
+
* `opt.variadic()`).
|
|
11
9
|
*
|
|
12
10
|
* @packageDocumentation
|
|
13
11
|
*/
|
|
14
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.opt = void 0;
|
|
13
|
+
exports.pos = exports.opt = void 0;
|
|
16
14
|
const errors_js_1 = require("./errors.cjs");
|
|
17
15
|
/**
|
|
18
|
-
*
|
|
19
|
-
* config (direct) or without args (curried for global options).
|
|
20
|
-
*/
|
|
21
|
-
const commandBuilder = (configOrNothing) => {
|
|
22
|
-
if (configOrNothing === undefined) {
|
|
23
|
-
// Curried usage: return function that accepts config
|
|
24
|
-
return (config) => config;
|
|
25
|
-
}
|
|
26
|
-
// Direct usage: return config as-is
|
|
27
|
-
return configOrNothing;
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Validate that no alias conflicts exist in a merged options schema. Throws
|
|
31
|
-
* BargsError if the same alias is used by multiple options.
|
|
16
|
+
* Validate that no alias conflicts exist in a merged options schema.
|
|
32
17
|
*/
|
|
33
18
|
const validateAliasConflicts = (schema) => {
|
|
34
19
|
const aliasToOption = new Map();
|
|
@@ -46,32 +31,56 @@ const validateAliasConflicts = (schema) => {
|
|
|
46
31
|
}
|
|
47
32
|
};
|
|
48
33
|
/**
|
|
49
|
-
*
|
|
34
|
+
* Create a Parser from an options schema that can also merge with existing
|
|
35
|
+
* parsers.
|
|
36
|
+
*
|
|
37
|
+
* Supports two usage patterns:
|
|
38
|
+
*
|
|
39
|
+
* 1. Standalone: `opt.options({ ... })` - returns a Parser
|
|
40
|
+
* 2. Merging: `pos.positionals(...)(opt.options(...))` - merges positionals into
|
|
41
|
+
* options
|
|
50
42
|
*/
|
|
51
|
-
const optionsImpl = (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
43
|
+
const optionsImpl = (schema) => {
|
|
44
|
+
validateAliasConflicts(schema);
|
|
45
|
+
// Create the merge function
|
|
46
|
+
const merger = (parser) => {
|
|
47
|
+
const mergedSchema = { ...parser.__optionsSchema, ...schema };
|
|
48
|
+
validateAliasConflicts(mergedSchema);
|
|
49
|
+
// Preserve transforms from the incoming parser
|
|
50
|
+
const transformed = parser;
|
|
51
|
+
const result = {
|
|
52
|
+
...parser,
|
|
53
|
+
__brand: 'Parser',
|
|
54
|
+
__optionsSchema: mergedSchema,
|
|
55
|
+
__values: {},
|
|
56
|
+
};
|
|
57
|
+
if (transformed.__transform) {
|
|
58
|
+
result.__transform = transformed.__transform;
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
};
|
|
62
|
+
// Add Parser properties to the function
|
|
63
|
+
const parserProps = {
|
|
64
|
+
__brand: 'Parser',
|
|
65
|
+
__optionsSchema: schema,
|
|
66
|
+
__positionals: [],
|
|
67
|
+
__positionalsSchema: [],
|
|
68
|
+
__values: {},
|
|
69
|
+
};
|
|
70
|
+
return Object.assign(merger, parserProps);
|
|
55
71
|
};
|
|
56
|
-
/**
|
|
57
|
-
* Create a positionals schema from positional definitions.
|
|
58
|
-
*/
|
|
59
|
-
const positionalsImpl = (...positionals) => positionals;
|
|
60
72
|
/**
|
|
61
73
|
* Namespaced option builders.
|
|
62
74
|
*
|
|
63
|
-
* Provides ergonomic helpers for defining CLI options, positionals, and
|
|
64
|
-
* commands with full TypeScript type inference.
|
|
65
|
-
*
|
|
66
75
|
* @example
|
|
67
76
|
*
|
|
68
77
|
* ```typescript
|
|
69
78
|
* import { opt } from 'bargs';
|
|
70
79
|
*
|
|
71
|
-
* const
|
|
80
|
+
* const parser = opt.options({
|
|
72
81
|
* verbose: opt.boolean({ aliases: ['v'] }),
|
|
73
82
|
* name: opt.string({ default: 'world' }),
|
|
74
|
-
* level: opt.enum(['low', 'medium', 'high']
|
|
83
|
+
* level: opt.enum(['low', 'medium', 'high']),
|
|
75
84
|
* });
|
|
76
85
|
* ```
|
|
77
86
|
*/
|
|
@@ -86,61 +95,12 @@ exports.opt = {
|
|
|
86
95
|
...props,
|
|
87
96
|
}),
|
|
88
97
|
/**
|
|
89
|
-
* Define a boolean option.
|
|
90
|
-
* inference.
|
|
98
|
+
* Define a boolean option.
|
|
91
99
|
*/
|
|
92
100
|
boolean: (props = {}) => ({
|
|
93
101
|
type: 'boolean',
|
|
94
102
|
...props,
|
|
95
103
|
}),
|
|
96
|
-
/**
|
|
97
|
-
* Define a command with proper type inference.
|
|
98
|
-
*
|
|
99
|
-
* Three usage patterns:
|
|
100
|
-
*
|
|
101
|
-
* 1. Simple usage (no global options): `bargs.command({ ... })`
|
|
102
|
-
* 2. With global options: `bargs.command<typeof globalOptions>()({ ... })`
|
|
103
|
-
* 3. With global options AND transforms: `bargs.command<typeof globalOptions,
|
|
104
|
-
* typeof globalTransforms>()({ ... })`
|
|
105
|
-
*
|
|
106
|
-
* @example
|
|
107
|
-
*
|
|
108
|
-
* ```typescript
|
|
109
|
-
* // Simple usage - no global options typed
|
|
110
|
-
* const simpleCmd = bargs.command({
|
|
111
|
-
* description: 'Simple command',
|
|
112
|
-
* handler: ({ values }) => { ... },
|
|
113
|
-
* });
|
|
114
|
-
*
|
|
115
|
-
* // With global options typed
|
|
116
|
-
* const globalOptions = {
|
|
117
|
-
* verbose: bargs.boolean({ aliases: ['v'] }),
|
|
118
|
-
* } as const;
|
|
119
|
-
*
|
|
120
|
-
* const greetCmd = bargs.command<typeof globalOptions>()({
|
|
121
|
-
* description: 'Greet someone',
|
|
122
|
-
* options: { name: bargs.string({ default: 'world' }) },
|
|
123
|
-
* handler: ({ values }) => {
|
|
124
|
-
* // values.verbose is properly typed as boolean | undefined
|
|
125
|
-
* console.log(`Hello, ${values.name}!`);
|
|
126
|
-
* },
|
|
127
|
-
* });
|
|
128
|
-
*
|
|
129
|
-
* // With global options AND global transforms typed
|
|
130
|
-
* const globalTransforms = {
|
|
131
|
-
* values: (v) => ({ ...v, timestamp: Date.now() }),
|
|
132
|
-
* } as const;
|
|
133
|
-
*
|
|
134
|
-
* const timedCmd = bargs.command<typeof globalOptions, typeof globalTransforms>()({
|
|
135
|
-
* description: 'Time-aware command',
|
|
136
|
-
* handler: ({ values }) => {
|
|
137
|
-
* // values.timestamp is properly typed from global transforms
|
|
138
|
-
* console.log(`Ran at ${values.timestamp}`);
|
|
139
|
-
* },
|
|
140
|
-
* });
|
|
141
|
-
* ```
|
|
142
|
-
*/
|
|
143
|
-
command: commandBuilder,
|
|
144
104
|
/**
|
|
145
105
|
* Define a count option (--verbose --verbose = 2).
|
|
146
106
|
*/
|
|
@@ -149,9 +109,7 @@ exports.opt = {
|
|
|
149
109
|
...props,
|
|
150
110
|
}),
|
|
151
111
|
/**
|
|
152
|
-
* Define an enum option with string choices.
|
|
153
|
-
* a tuple of literal types automatically. Props type is preserved to enable
|
|
154
|
-
* default inference.
|
|
112
|
+
* Define an enum option with string choices.
|
|
155
113
|
*/
|
|
156
114
|
enum: (choices, props = {}) => ({
|
|
157
115
|
choices,
|
|
@@ -159,8 +117,7 @@ exports.opt = {
|
|
|
159
117
|
...props,
|
|
160
118
|
}),
|
|
161
119
|
/**
|
|
162
|
-
* Define an enum positional argument with string choices.
|
|
163
|
-
* is inferred as a tuple of literal types automatically.
|
|
120
|
+
* Define an enum positional argument with string choices.
|
|
164
121
|
*/
|
|
165
122
|
enumPos: (choices, props = {}) => ({
|
|
166
123
|
choices,
|
|
@@ -168,75 +125,144 @@ exports.opt = {
|
|
|
168
125
|
...props,
|
|
169
126
|
}),
|
|
170
127
|
/**
|
|
171
|
-
* Define a number option.
|
|
172
|
-
* inference.
|
|
128
|
+
* Define a number option.
|
|
173
129
|
*/
|
|
174
130
|
number: (props = {}) => ({
|
|
175
131
|
type: 'number',
|
|
176
132
|
...props,
|
|
177
133
|
}),
|
|
178
|
-
// ─── Positional Builders ───────────────────────────────────────────
|
|
179
134
|
/**
|
|
180
|
-
* Define a number positional argument.
|
|
181
|
-
* required inference.
|
|
135
|
+
* Define a number positional argument.
|
|
182
136
|
*/
|
|
183
137
|
numberPos: (props = {}) => ({
|
|
184
138
|
type: 'number',
|
|
185
139
|
...props,
|
|
186
140
|
}),
|
|
187
141
|
/**
|
|
188
|
-
*
|
|
189
|
-
* ones for duplicate option names. Validates that no alias conflicts exist.
|
|
142
|
+
* Create a Parser from an options schema.
|
|
190
143
|
*
|
|
191
144
|
* @example
|
|
192
145
|
*
|
|
193
146
|
* ```typescript
|
|
194
|
-
*
|
|
195
|
-
* const loggingOpts = opt.options({
|
|
147
|
+
* const parser = opt.options({
|
|
196
148
|
* verbose: opt.boolean({ aliases: ['v'] }),
|
|
197
|
-
*
|
|
198
|
-
* });
|
|
199
|
-
*
|
|
200
|
-
* // Merge multiple schemas
|
|
201
|
-
* const allOpts = opt.options(loggingOpts, ioOpts, {
|
|
202
|
-
* format: opt.enum(['json', 'yaml'] as const),
|
|
149
|
+
* name: opt.string({ default: 'world' }),
|
|
203
150
|
* });
|
|
151
|
+
* // Type: Parser<{ verbose: boolean | undefined, name: string }, []>
|
|
204
152
|
* ```
|
|
205
|
-
*
|
|
206
|
-
* @throws BargsError if multiple options use the same alias
|
|
207
153
|
*/
|
|
208
154
|
options: optionsImpl,
|
|
209
155
|
/**
|
|
210
|
-
*
|
|
156
|
+
* Define a string option.
|
|
157
|
+
*/
|
|
158
|
+
string: (props = {}) => ({
|
|
159
|
+
type: 'string',
|
|
160
|
+
...props,
|
|
161
|
+
}),
|
|
162
|
+
/**
|
|
163
|
+
* Define a string positional argument.
|
|
164
|
+
*/
|
|
165
|
+
stringPos: (props = {}) => ({
|
|
166
|
+
type: 'string',
|
|
167
|
+
...props,
|
|
168
|
+
}),
|
|
169
|
+
/**
|
|
170
|
+
* Define a variadic positional (rest args).
|
|
171
|
+
*/
|
|
172
|
+
variadic: (items, props = {}) => ({
|
|
173
|
+
items,
|
|
174
|
+
type: 'variadic',
|
|
175
|
+
...props,
|
|
176
|
+
}),
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Create a Parser from positional definitions that can also merge with existing
|
|
180
|
+
* parsers.
|
|
181
|
+
*
|
|
182
|
+
* Supports two usage patterns:
|
|
183
|
+
*
|
|
184
|
+
* 1. Standalone: `pos.positionals(...)` - returns a Parser
|
|
185
|
+
* 2. Merging: `pos.positionals(...)(opt.options(...))` - merges positionals into
|
|
186
|
+
* options
|
|
187
|
+
*/
|
|
188
|
+
const positionalsImpl = (...positionals) => {
|
|
189
|
+
// Create the merge function - just passes through V2, no intersection needed
|
|
190
|
+
const merger = (parser) => {
|
|
191
|
+
// Preserve transforms from the incoming parser
|
|
192
|
+
const transformed = parser;
|
|
193
|
+
const result = {
|
|
194
|
+
...parser,
|
|
195
|
+
__brand: 'Parser',
|
|
196
|
+
__positionals: [],
|
|
197
|
+
__positionalsSchema: [...parser.__positionalsSchema, ...positionals],
|
|
198
|
+
};
|
|
199
|
+
if (transformed.__transform) {
|
|
200
|
+
result.__transform = transformed.__transform;
|
|
201
|
+
}
|
|
202
|
+
return result;
|
|
203
|
+
};
|
|
204
|
+
// Add Parser properties to the function
|
|
205
|
+
// Use empty object {} instead of Record<string, never> for better intersection behavior
|
|
206
|
+
const parserProps = {
|
|
207
|
+
__brand: 'Parser',
|
|
208
|
+
__optionsSchema: {},
|
|
209
|
+
__positionals: [],
|
|
210
|
+
__positionalsSchema: positionals,
|
|
211
|
+
__values: {},
|
|
212
|
+
};
|
|
213
|
+
return Object.assign(merger, parserProps);
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Namespaced positional builders.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
*
|
|
220
|
+
* ```typescript
|
|
221
|
+
* import { pos } from 'bargs';
|
|
222
|
+
*
|
|
223
|
+
* const parser = pos.positionals(
|
|
224
|
+
* pos.string({ name: 'input', required: true }),
|
|
225
|
+
* pos.string({ name: 'output' }),
|
|
226
|
+
* );
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
exports.pos = {
|
|
230
|
+
/**
|
|
231
|
+
* Define an enum positional argument with string choices.
|
|
232
|
+
*/
|
|
233
|
+
enum: (choices, props = {}) => ({
|
|
234
|
+
choices,
|
|
235
|
+
type: 'enum',
|
|
236
|
+
...props,
|
|
237
|
+
}),
|
|
238
|
+
/**
|
|
239
|
+
* Define a number positional argument.
|
|
240
|
+
*/
|
|
241
|
+
number: (props = {}) => ({
|
|
242
|
+
type: 'number',
|
|
243
|
+
...props,
|
|
244
|
+
}),
|
|
245
|
+
/**
|
|
246
|
+
* Create a Parser from positional definitions.
|
|
211
247
|
*
|
|
212
248
|
* @example
|
|
213
249
|
*
|
|
214
250
|
* ```typescript
|
|
215
|
-
* const
|
|
216
|
-
*
|
|
217
|
-
*
|
|
251
|
+
* const parser = pos.positionals(
|
|
252
|
+
* pos.string({ name: 'input', required: true }),
|
|
253
|
+
* pos.string({ name: 'output' }),
|
|
218
254
|
* );
|
|
255
|
+
* // Type: Parser<{}, readonly [string, string | undefined]>
|
|
219
256
|
* ```
|
|
220
257
|
*/
|
|
221
258
|
positionals: positionalsImpl,
|
|
222
259
|
/**
|
|
223
|
-
* Define a string
|
|
224
|
-
* inference.
|
|
260
|
+
* Define a string positional argument.
|
|
225
261
|
*/
|
|
226
262
|
string: (props = {}) => ({
|
|
227
263
|
type: 'string',
|
|
228
264
|
...props,
|
|
229
265
|
}),
|
|
230
|
-
// ─── Composition ───────────────────────────────────────────────────
|
|
231
|
-
/**
|
|
232
|
-
* Define a string positional argument. Props type is preserved to enable
|
|
233
|
-
* required inference.
|
|
234
|
-
*/
|
|
235
|
-
stringPos: (props = {}) => ({
|
|
236
|
-
type: 'string',
|
|
237
|
-
...props,
|
|
238
|
-
}),
|
|
239
|
-
// ─── Command Builder ───────────────────────────────────────────────
|
|
240
266
|
/**
|
|
241
267
|
* Define a variadic positional (rest args).
|
|
242
268
|
*/
|
package/dist/opt.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"opt.js","sourceRoot":"","sources":["../src/opt.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"opt.js","sourceRoot":"","sources":["../src/opt.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAqBH,4CAAyC;AAEzC;;GAEG;AACH,MAAM,sBAAsB,GAAG,CAAC,MAAqB,EAAQ,EAAE;IAC7D,MAAM,aAAa,GAAG,IAAI,GAAG,EAAkB,CAAC;IAEhD,KAAK,MAAM,CAAC,UAAU,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACvD,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;YACjB,SAAS;QACX,CAAC;QAED,KAAK,MAAM,KAAK,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;YAChC,MAAM,QAAQ,GAAG,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YAC1C,IAAI,QAAQ,IAAI,QAAQ,KAAK,UAAU,EAAE,CAAC;gBACxC,MAAM,IAAI,sBAAU,CAClB,qBAAqB,KAAK,wBAAwB,QAAQ,YAAY,UAAU,GAAG,CACpF,CAAC;YACJ,CAAC;YACD,aAAa,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;QACvC,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAcF;;;;;;;;;GASG;AACH,MAAM,WAAW,GAAG,CAClB,MAAS,EAC+B,EAAE;IAC1C,sBAAsB,CAAC,MAAM,CAAC,CAAC;IAE/B,4BAA4B;IAC5B,MAAM,MAAM,GAAG,CACb,MAAsB,EACY,EAAE;QACpC,MAAM,YAAY,GAAG,EAAE,GAAG,MAAM,CAAC,eAAe,EAAE,GAAG,MAA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