@boneskull/bargs 0.1.1

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.
Files changed (91) hide show
  1. package/LICENSE +55 -0
  2. package/README.md +483 -0
  3. package/dist/bargs.cjs +167 -0
  4. package/dist/bargs.cjs.map +1 -0
  5. package/dist/bargs.d.cts +31 -0
  6. package/dist/bargs.d.cts.map +1 -0
  7. package/dist/bargs.d.ts +31 -0
  8. package/dist/bargs.d.ts.map +1 -0
  9. package/dist/bargs.js +163 -0
  10. package/dist/bargs.js.map +1 -0
  11. package/dist/errors.cjs +57 -0
  12. package/dist/errors.cjs.map +1 -0
  13. package/dist/errors.d.cts +40 -0
  14. package/dist/errors.d.cts.map +1 -0
  15. package/dist/errors.d.ts +40 -0
  16. package/dist/errors.d.ts.map +1 -0
  17. package/dist/errors.js +51 -0
  18. package/dist/errors.js.map +1 -0
  19. package/dist/help.cjs +309 -0
  20. package/dist/help.cjs.map +1 -0
  21. package/dist/help.d.cts +21 -0
  22. package/dist/help.d.cts.map +1 -0
  23. package/dist/help.d.ts +21 -0
  24. package/dist/help.d.ts.map +1 -0
  25. package/dist/help.js +304 -0
  26. package/dist/help.js.map +1 -0
  27. package/dist/index.cjs +63 -0
  28. package/dist/index.cjs.map +1 -0
  29. package/dist/index.d.cts +96 -0
  30. package/dist/index.d.cts.map +1 -0
  31. package/dist/index.d.ts +96 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +47 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/opt.cjs +205 -0
  36. package/dist/opt.cjs.map +1 -0
  37. package/dist/opt.d.cts +145 -0
  38. package/dist/opt.d.cts.map +1 -0
  39. package/dist/opt.d.ts +145 -0
  40. package/dist/opt.d.ts.map +1 -0
  41. package/dist/opt.js +202 -0
  42. package/dist/opt.js.map +1 -0
  43. package/dist/osc.cjs +190 -0
  44. package/dist/osc.cjs.map +1 -0
  45. package/dist/osc.d.cts +30 -0
  46. package/dist/osc.d.cts.map +1 -0
  47. package/dist/osc.d.ts +30 -0
  48. package/dist/osc.d.ts.map +1 -0
  49. package/dist/osc.js +181 -0
  50. package/dist/osc.js.map +1 -0
  51. package/dist/parser.cjs +293 -0
  52. package/dist/parser.cjs.map +1 -0
  53. package/dist/parser.d.cts +47 -0
  54. package/dist/parser.d.cts.map +1 -0
  55. package/dist/parser.d.ts +47 -0
  56. package/dist/parser.d.ts.map +1 -0
  57. package/dist/parser.js +285 -0
  58. package/dist/parser.js.map +1 -0
  59. package/dist/theme.cjs +203 -0
  60. package/dist/theme.cjs.map +1 -0
  61. package/dist/theme.d.cts +227 -0
  62. package/dist/theme.d.cts.map +1 -0
  63. package/dist/theme.d.ts +227 -0
  64. package/dist/theme.d.ts.map +1 -0
  65. package/dist/theme.js +198 -0
  66. package/dist/theme.js.map +1 -0
  67. package/dist/types.cjs +18 -0
  68. package/dist/types.cjs.map +1 -0
  69. package/dist/types.d.cts +244 -0
  70. package/dist/types.d.cts.map +1 -0
  71. package/dist/types.d.ts +244 -0
  72. package/dist/types.d.ts.map +1 -0
  73. package/dist/types.js +17 -0
  74. package/dist/types.js.map +1 -0
  75. package/dist/validate.cjs +452 -0
  76. package/dist/validate.cjs.map +1 -0
  77. package/dist/validate.d.cts +28 -0
  78. package/dist/validate.d.cts.map +1 -0
  79. package/dist/validate.d.ts +28 -0
  80. package/dist/validate.d.ts.map +1 -0
  81. package/dist/validate.js +448 -0
  82. package/dist/validate.js.map +1 -0
  83. package/dist/version.cjs +134 -0
  84. package/dist/version.cjs.map +1 -0
  85. package/dist/version.d.cts +27 -0
  86. package/dist/version.d.cts.map +1 -0
  87. package/dist/version.d.ts +27 -0
  88. package/dist/version.d.ts.map +1 -0
  89. package/dist/version.js +129 -0
  90. package/dist/version.js.map +1 -0
  91. package/package.json +149 -0
package/dist/index.js ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Main entry point for the bargs CLI argument parser.
3
+ *
4
+ * This module exports the primary `bargs` and `bargsAsync` functions with
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.
9
+ *
10
+ * @example
11
+ *
12
+ * ```typescript
13
+ * import { bargs } from 'bargs';
14
+ *
15
+ * // Use as function
16
+ * const result = bargs({
17
+ * name: 'myapp',
18
+ * options: { verbose: bargs.boolean({ aliases: ['v'] }) },
19
+ * });
20
+ *
21
+ * // Access builder namespace
22
+ * const opts = bargs.options({ name: bargs.string() });
23
+ * ```
24
+ *
25
+ * @packageDocumentation
26
+ */
27
+ import { bargsAsync as bargsAsyncBase, bargs as bargsBase } from "./bargs.js";
28
+ import { opt } from "./opt.js";
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
40
+ export { BargsError, HelpError, ValidationError } from "./errors.js";
41
+ // Re-export help generators
42
+ export { generateCommandHelp, generateHelp } from "./help.js";
43
+ // Re-export OSC utilities for terminal hyperlinks
44
+ export { link, linkifyUrls, supportsHyperlinks } from "./osc.js";
45
+ // Re-export theme utilities
46
+ export { ansi, createStyler, defaultTheme, stripAnsi, themes, } from "./theme.js";
47
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAE,UAAU,IAAI,cAAc,EAAE,KAAK,IAAI,SAAS,EAAE,mBAAmB;AAC9E,OAAO,EAAE,GAAG,EAAE,iBAAiB;AAE/B;;;GAGG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;AAEnD;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;AAE7D,mBAAmB;AACnB,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,oBAAoB;AAErE,4BAA4B;AAC5B,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,kBAAkB;AAE9D,kDAAkD;AAClD,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,kBAAkB,EAAE,iBAAiB;AAEjE,4BAA4B;AAC5B,OAAO,EACL,IAAI,EACJ,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,MAAM,GACP,mBAAmB"}
package/dist/opt.cjs ADDED
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ /**
3
+ * Builder functions for defining CLI options, positionals, and commands.
4
+ *
5
+ * Provides ergonomic helpers with full TypeScript type inference for
6
+ * constructing option schemas (`opt.string()`, `opt.boolean()`, `opt.enum()`,
7
+ * etc.), positional schemas (`opt.stringPos()`, `opt.numberPos()`,
8
+ * `opt.variadic()`), and command definitions (`opt.command()`). Includes
9
+ * composition utilities for merging schemas (`opt.options()`,
10
+ * `opt.positionals()`).
11
+ *
12
+ * @packageDocumentation
13
+ */
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.opt = void 0;
16
+ const errors_js_1 = require("./errors.cjs");
17
+ /**
18
+ * Validate that no alias conflicts exist in a merged options schema. Throws
19
+ * BargsError if the same alias is used by multiple options.
20
+ */
21
+ const validateAliasConflicts = (schema) => {
22
+ const aliasToOption = new Map();
23
+ for (const [optionName, def] of Object.entries(schema)) {
24
+ if (!def.aliases) {
25
+ continue;
26
+ }
27
+ for (const alias of def.aliases) {
28
+ const existing = aliasToOption.get(alias);
29
+ if (existing && existing !== optionName) {
30
+ throw new errors_js_1.BargsError(`Alias conflict: "-${alias}" is used by both "--${existing}" and "--${optionName}"`);
31
+ }
32
+ aliasToOption.set(alias, optionName);
33
+ }
34
+ }
35
+ };
36
+ /**
37
+ * Compose multiple option schemas into one.
38
+ */
39
+ const optionsImpl = (...schemas) => {
40
+ const merged = Object.assign({}, ...schemas);
41
+ validateAliasConflicts(merged);
42
+ return merged;
43
+ };
44
+ /**
45
+ * Create a positionals schema from positional definitions.
46
+ */
47
+ const positionalsImpl = (...positionals) => positionals;
48
+ /**
49
+ * Namespaced option builders.
50
+ *
51
+ * Provides ergonomic helpers for defining CLI options, positionals, and
52
+ * commands with full TypeScript type inference.
53
+ *
54
+ * @example
55
+ *
56
+ * ```typescript
57
+ * import { opt } from 'bargs';
58
+ *
59
+ * const options = opt.options({
60
+ * verbose: opt.boolean({ aliases: ['v'] }),
61
+ * name: opt.string({ default: 'world' }),
62
+ * level: opt.enum(['low', 'medium', 'high'] as const),
63
+ * });
64
+ * ```
65
+ */
66
+ exports.opt = {
67
+ // ─── Option Builders ───────────────────────────────────────────────
68
+ /**
69
+ * Define an array option (--flag value --flag value2).
70
+ */
71
+ array: (items, props = {}) => ({
72
+ items,
73
+ type: 'array',
74
+ ...props,
75
+ }),
76
+ /**
77
+ * Define a boolean option. Props type is preserved to enable default
78
+ * inference.
79
+ */
80
+ boolean: (props = {}) => ({
81
+ type: 'boolean',
82
+ ...props,
83
+ }),
84
+ /**
85
+ * Define a command with proper type inference.
86
+ *
87
+ * @example
88
+ *
89
+ * ```typescript
90
+ * const greetCmd = opt.command({
91
+ * description: 'Greet someone',
92
+ * options: opt.options({
93
+ * name: opt.string({ default: 'world' }),
94
+ * }),
95
+ * handler: ({ values }) => {
96
+ * console.log(`Hello, ${values.name}!`);
97
+ * },
98
+ * });
99
+ * ```
100
+ */
101
+ command: (config) => config,
102
+ /**
103
+ * Define a count option (--verbose --verbose = 2).
104
+ */
105
+ count: (props = {}) => ({
106
+ type: 'count',
107
+ ...props,
108
+ }),
109
+ /**
110
+ * Define an enum option with string choices. The choices array is inferred as
111
+ * a tuple of literal types automatically. Props type is preserved to enable
112
+ * default inference.
113
+ */
114
+ enum: (choices, props = {}) => ({
115
+ choices,
116
+ type: 'enum',
117
+ ...props,
118
+ }),
119
+ /**
120
+ * Define an enum positional argument with string choices. The choices array
121
+ * is inferred as a tuple of literal types automatically.
122
+ */
123
+ enumPos: (choices, props = {}) => ({
124
+ choices,
125
+ type: 'enum',
126
+ ...props,
127
+ }),
128
+ /**
129
+ * Define a number option. Props type is preserved to enable default
130
+ * inference.
131
+ */
132
+ number: (props = {}) => ({
133
+ type: 'number',
134
+ ...props,
135
+ }),
136
+ // ─── Positional Builders ───────────────────────────────────────────
137
+ /**
138
+ * Define a number positional argument.
139
+ */
140
+ numberPos: (props = {}) => ({
141
+ type: 'number',
142
+ ...props,
143
+ }),
144
+ /**
145
+ * Compose multiple option schemas into one. Later schemas override earlier
146
+ * ones for duplicate option names. Validates that no alias conflicts exist.
147
+ *
148
+ * @example
149
+ *
150
+ * ```typescript
151
+ * // Single schema (identity, enables reuse)
152
+ * const loggingOpts = opt.options({
153
+ * verbose: opt.boolean({ aliases: ['v'] }),
154
+ * quiet: opt.boolean({ aliases: ['q'] }),
155
+ * });
156
+ *
157
+ * // Merge multiple schemas
158
+ * const allOpts = opt.options(loggingOpts, ioOpts, {
159
+ * format: opt.enum(['json', 'yaml'] as const),
160
+ * });
161
+ * ```
162
+ *
163
+ * @throws BargsError if multiple options use the same alias
164
+ */
165
+ options: optionsImpl,
166
+ /**
167
+ * Create a positionals schema with proper tuple type inference.
168
+ *
169
+ * @example
170
+ *
171
+ * ```typescript
172
+ * const positionals = opt.positionals(
173
+ * opt.stringPos({ description: 'Input file', required: true }),
174
+ * opt.stringPos({ description: 'Output file' }),
175
+ * );
176
+ * ```
177
+ */
178
+ positionals: positionalsImpl,
179
+ /**
180
+ * Define a string option. Props type is preserved to enable default
181
+ * inference.
182
+ */
183
+ string: (props = {}) => ({
184
+ type: 'string',
185
+ ...props,
186
+ }),
187
+ // ─── Composition ───────────────────────────────────────────────────
188
+ /**
189
+ * Define a string positional argument.
190
+ */
191
+ stringPos: (props = {}) => ({
192
+ type: 'string',
193
+ ...props,
194
+ }),
195
+ // ─── Command Builder ───────────────────────────────────────────────
196
+ /**
197
+ * Define a variadic positional (rest args).
198
+ */
199
+ variadic: (items, props = {}) => ({
200
+ items,
201
+ type: 'variadic',
202
+ ...props,
203
+ }),
204
+ };
205
+ //# sourceMappingURL=opt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opt.js","sourceRoot":"","sources":["../src/opt.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;AAmBH,4CAAyC;AAEzC;;;GAGG;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;AAEF;;GAEG;AACH,MAAM,WAAW,GAAG,CAAC,GAAG,OAAwB,EAAiB,EAAE;IACjE,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAA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,EAAE,EACxB,EAAE,CAAC,CAAC;QACtB,IAAI,EAAE,QAAQ;QACd,GAAG,KAAK;KACT,CAAC;IAEF;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,OAAO,EAAE,WAoBR;IAED;;;;;;;;;;;OAWG;IACH,WAAW,EAAE,eAoBZ;IAED;;;OAGG;IACH,MAAM,EAAE,CACN,QAAW,EAAO,EACA,EAAE,CACpB,CAAC;QACC,IAAI,EAAE,QAAQ;QACd,GAAG,KAAK;KACT,CAAqB;IAExB,sEAAsE;IAEtE;;OAEG;IACH,SAAS,EAAE,CACT,QAAwC,EAAE,EACxB,EAAE,CAAC,CAAC;QACtB,IAAI,EAAE,QAAQ;QACd,GAAG,KAAK;KACT,CAAC;IAEF,sEAAsE;IAEtE;;OAEG;IACH,QAAQ,EAAE,CACR,KAA0B,EAC1B,QAAoD,EAAE,EAClC,EAAE,CAAC,CAAC;QACxB,KAAK;QACL,IAAI,EAAE,UAAU;QAChB,GAAG,KAAK;KACT,CAAC;CACH,CAAC"}
package/dist/opt.d.cts ADDED
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Builder functions for defining CLI options, positionals, and commands.
3
+ *
4
+ * Provides ergonomic helpers with full TypeScript type inference for
5
+ * constructing option schemas (`opt.string()`, `opt.boolean()`, `opt.enum()`,
6
+ * etc.), positional schemas (`opt.stringPos()`, `opt.numberPos()`,
7
+ * `opt.variadic()`), and command definitions (`opt.command()`). Includes
8
+ * composition utilities for merging schemas (`opt.options()`,
9
+ * `opt.positionals()`).
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ import type { ArrayOption, BooleanOption, CommandConfig, CountOption, EnumOption, EnumPositional, NumberOption, NumberPositional, OptionsSchema, PositionalDef, PositionalsSchema, StringOption, StringPositional, VariadicPositional } from "./types.cjs";
14
+ /**
15
+ * Namespaced option builders.
16
+ *
17
+ * Provides ergonomic helpers for defining CLI options, positionals, and
18
+ * commands with full TypeScript type inference.
19
+ *
20
+ * @example
21
+ *
22
+ * ```typescript
23
+ * import { opt } from 'bargs';
24
+ *
25
+ * const options = opt.options({
26
+ * verbose: opt.boolean({ aliases: ['v'] }),
27
+ * name: opt.string({ default: 'world' }),
28
+ * level: opt.enum(['low', 'medium', 'high'] as const),
29
+ * });
30
+ * ```
31
+ */
32
+ export declare const opt: {
33
+ /**
34
+ * Define an array option (--flag value --flag value2).
35
+ */
36
+ array: (items: "number" | "string", props?: Omit<ArrayOption, "items" | "type">) => ArrayOption;
37
+ /**
38
+ * Define a boolean option. Props type is preserved to enable default
39
+ * inference.
40
+ */
41
+ boolean: <P extends Omit<BooleanOption, "type"> = Omit<BooleanOption, "type">>(props?: P) => BooleanOption & P;
42
+ /**
43
+ * Define a command with proper type inference.
44
+ *
45
+ * @example
46
+ *
47
+ * ```typescript
48
+ * const greetCmd = opt.command({
49
+ * description: 'Greet someone',
50
+ * options: opt.options({
51
+ * name: opt.string({ default: 'world' }),
52
+ * }),
53
+ * handler: ({ values }) => {
54
+ * console.log(`Hello, ${values.name}!`);
55
+ * },
56
+ * });
57
+ * ```
58
+ */
59
+ command: <TOptions extends OptionsSchema = OptionsSchema, TPositionals extends PositionalsSchema = PositionalsSchema>(config: CommandConfig<TOptions, TPositionals>) => CommandConfig<TOptions, TPositionals>;
60
+ /**
61
+ * Define a count option (--verbose --verbose = 2).
62
+ */
63
+ count: (props?: Omit<CountOption, "type">) => CountOption;
64
+ /**
65
+ * Define an enum option with string choices. The choices array is inferred as
66
+ * a tuple of literal types automatically. Props type is preserved to enable
67
+ * default inference.
68
+ */
69
+ enum: <const T extends readonly string[], P extends Omit<EnumOption<T[number]>, "choices" | "type"> = Omit<EnumOption<T[number]>, "type" | "choices">>(choices: T, props?: P) => EnumOption<T[number]> & P;
70
+ /**
71
+ * Define an enum positional argument with string choices. The choices array
72
+ * is inferred as a tuple of literal types automatically.
73
+ */
74
+ enumPos: <const T extends readonly string[], P extends Omit<EnumPositional<T[number]>, "choices" | "type"> = Omit<EnumPositional<T[number]>, "type" | "choices">>(choices: T, props?: P) => EnumPositional<T[number]> & P;
75
+ /**
76
+ * Define a number option. Props type is preserved to enable default
77
+ * inference.
78
+ */
79
+ number: <P extends Omit<NumberOption, "type"> = Omit<NumberOption, "type">>(props?: P) => NumberOption & P;
80
+ /**
81
+ * Define a number positional argument.
82
+ */
83
+ numberPos: (props?: Omit<NumberPositional, "type">) => NumberPositional;
84
+ /**
85
+ * Compose multiple option schemas into one. Later schemas override earlier
86
+ * ones for duplicate option names. Validates that no alias conflicts exist.
87
+ *
88
+ * @example
89
+ *
90
+ * ```typescript
91
+ * // Single schema (identity, enables reuse)
92
+ * const loggingOpts = opt.options({
93
+ * verbose: opt.boolean({ aliases: ['v'] }),
94
+ * quiet: opt.boolean({ aliases: ['q'] }),
95
+ * });
96
+ *
97
+ * // Merge multiple schemas
98
+ * const allOpts = opt.options(loggingOpts, ioOpts, {
99
+ * format: opt.enum(['json', 'yaml'] as const),
100
+ * });
101
+ * ```
102
+ *
103
+ * @throws BargsError if multiple options use the same alias
104
+ */
105
+ options: {
106
+ <A extends OptionsSchema>(a: A): A;
107
+ <A extends OptionsSchema, B extends OptionsSchema>(a: A, b: B): A & B;
108
+ <A extends OptionsSchema, B extends OptionsSchema, C extends OptionsSchema>(a: A, b: B, c: C): A & B & C;
109
+ <A extends OptionsSchema, B extends OptionsSchema, C extends OptionsSchema, D extends OptionsSchema>(a: A, b: B, c: C, d: D): A & B & C & D;
110
+ (...schemas: OptionsSchema[]): OptionsSchema;
111
+ };
112
+ /**
113
+ * Create a positionals schema with proper tuple type inference.
114
+ *
115
+ * @example
116
+ *
117
+ * ```typescript
118
+ * const positionals = opt.positionals(
119
+ * opt.stringPos({ description: 'Input file', required: true }),
120
+ * opt.stringPos({ description: 'Output file' }),
121
+ * );
122
+ * ```
123
+ */
124
+ positionals: {
125
+ <A extends PositionalDef>(a: A): [A];
126
+ <A extends PositionalDef, B extends PositionalDef>(a: A, b: B): [A, B];
127
+ <A extends PositionalDef, B extends PositionalDef, C extends PositionalDef>(a: A, b: B, c: C): [A, B, C];
128
+ <A extends PositionalDef, B extends PositionalDef, C extends PositionalDef, D extends PositionalDef>(a: A, b: B, c: C, d: D): [A, B, C, D];
129
+ (...positionals: PositionalDef[]): PositionalsSchema;
130
+ };
131
+ /**
132
+ * Define a string option. Props type is preserved to enable default
133
+ * inference.
134
+ */
135
+ string: <P extends Omit<StringOption, "type"> = Omit<StringOption, "type">>(props?: P) => P & StringOption;
136
+ /**
137
+ * Define a string positional argument.
138
+ */
139
+ stringPos: (props?: Omit<StringPositional, "type">) => StringPositional;
140
+ /**
141
+ * Define a variadic positional (rest args).
142
+ */
143
+ variadic: (items: "number" | "string", props?: Omit<VariadicPositional, "items" | "type">) => VariadicPositional;
144
+ };
145
+ //# sourceMappingURL=opt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opt.d.ts","sourceRoot":"","sources":["../src/opt.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,aAAa,EACb,aAAa,EACb,WAAW,EACX,UAAU,EACV,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,YAAY,EACZ,gBAAgB,EAChB,kBAAkB,EACnB,oBAAmB;AA2CpB;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,GAAG;IAGd;;OAEG;mBAEM,QAAQ,GAAG,QAAQ,UACnB,IAAI,CAAC,WAAW,EAAE,OAAO,GAAG,MAAM,CAAC,KACzC,WAAW;IAMd;;;OAGG;cAED,CAAC,SAAS,IAAI,CAAC,aAAa,EAAE,MAAM,CAAC,wCAE9B,CAAC,KACP,aAAa,GAAG,CAAC;IAMpB;;;;;;;;;;;;;;;;OAgBG;cAED,QAAQ,SAAS,aAAa,kBAC9B,YAAY,SAAS,iBAAiB,8BAE9B,aAAa,CAAC,QAAQ,EAAE,YAAY,CAAC,KAC5C,aAAa,CAAC,QAAQ,EAAE,Y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gB;IAOnB;;OAEG;sBAEM,QAAQ,GAAG,QAAQ,UACnB,IAAI,CAAC,kBAAkB,EAAE,OAAO,GAAG,MAAM,CAAC,KAChD,kBAAkB;CAKtB,CAAC"}
package/dist/opt.d.ts ADDED
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Builder functions for defining CLI options, positionals, and commands.
3
+ *
4
+ * Provides ergonomic helpers with full TypeScript type inference for
5
+ * constructing option schemas (`opt.string()`, `opt.boolean()`, `opt.enum()`,
6
+ * etc.), positional schemas (`opt.stringPos()`, `opt.numberPos()`,
7
+ * `opt.variadic()`), and command definitions (`opt.command()`). Includes
8
+ * composition utilities for merging schemas (`opt.options()`,
9
+ * `opt.positionals()`).
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ import type { ArrayOption, BooleanOption, CommandConfig, CountOption, EnumOption, EnumPositional, NumberOption, NumberPositional, OptionsSchema, PositionalDef, PositionalsSchema, StringOption, StringPositional, VariadicPositional } from "./types.js";
14
+ /**
15
+ * Namespaced option builders.
16
+ *
17
+ * Provides ergonomic helpers for defining CLI options, positionals, and
18
+ * commands with full TypeScript type inference.
19
+ *
20
+ * @example
21
+ *
22
+ * ```typescript
23
+ * import { opt } from 'bargs';
24
+ *
25
+ * const options = opt.options({
26
+ * verbose: opt.boolean({ aliases: ['v'] }),
27
+ * name: opt.string({ default: 'world' }),
28
+ * level: opt.enum(['low', 'medium', 'high'] as const),
29
+ * });
30
+ * ```
31
+ */
32
+ export declare const opt: {
33
+ /**
34
+ * Define an array option (--flag value --flag value2).
35
+ */
36
+ array: (items: "number" | "string", props?: Omit<ArrayOption, "items" | "type">) => ArrayOption;
37
+ /**
38
+ * Define a boolean option. Props type is preserved to enable default
39
+ * inference.
40
+ */
41
+ boolean: <P extends Omit<BooleanOption, "type"> = Omit<BooleanOption, "type">>(props?: P) => BooleanOption & P;
42
+ /**
43
+ * Define a command with proper type inference.
44
+ *
45
+ * @example
46
+ *
47
+ * ```typescript
48
+ * const greetCmd = opt.command({
49
+ * description: 'Greet someone',
50
+ * options: opt.options({
51
+ * name: opt.string({ default: 'world' }),
52
+ * }),
53
+ * handler: ({ values }) => {
54
+ * console.log(`Hello, ${values.name}!`);
55
+ * },
56
+ * });
57
+ * ```
58
+ */
59
+ command: <TOptions extends OptionsSchema = OptionsSchema, TPositionals extends PositionalsSchema = PositionalsSchema>(config: CommandConfig<TOptions, TPositionals>) => CommandConfig<TOptions, TPositionals>;
60
+ /**
61
+ * Define a count option (--verbose --verbose = 2).
62
+ */
63
+ count: (props?: Omit<CountOption, "type">) => CountOption;
64
+ /**
65
+ * Define an enum option with string choices. The choices array is inferred as
66
+ * a tuple of literal types automatically. Props type is preserved to enable
67
+ * default inference.
68
+ */
69
+ enum: <const T extends readonly string[], P extends Omit<EnumOption<T[number]>, "choices" | "type"> = Omit<EnumOption<T[number]>, "type" | "choices">>(choices: T, props?: P) => EnumOption<T[number]> & P;
70
+ /**
71
+ * Define an enum positional argument with string choices. The choices array
72
+ * is inferred as a tuple of literal types automatically.
73
+ */
74
+ enumPos: <const T extends readonly string[], P extends Omit<EnumPositional<T[number]>, "choices" | "type"> = Omit<EnumPositional<T[number]>, "type" | "choices">>(choices: T, props?: P) => EnumPositional<T[number]> & P;
75
+ /**
76
+ * Define a number option. Props type is preserved to enable default
77
+ * inference.
78
+ */
79
+ number: <P extends Omit<NumberOption, "type"> = Omit<NumberOption, "type">>(props?: P) => NumberOption & P;
80
+ /**
81
+ * Define a number positional argument.
82
+ */
83
+ numberPos: (props?: Omit<NumberPositional, "type">) => NumberPositional;
84
+ /**
85
+ * Compose multiple option schemas into one. Later schemas override earlier
86
+ * ones for duplicate option names. Validates that no alias conflicts exist.
87
+ *
88
+ * @example
89
+ *
90
+ * ```typescript
91
+ * // Single schema (identity, enables reuse)
92
+ * const loggingOpts = opt.options({
93
+ * verbose: opt.boolean({ aliases: ['v'] }),
94
+ * quiet: opt.boolean({ aliases: ['q'] }),
95
+ * });
96
+ *
97
+ * // Merge multiple schemas
98
+ * const allOpts = opt.options(loggingOpts, ioOpts, {
99
+ * format: opt.enum(['json', 'yaml'] as const),
100
+ * });
101
+ * ```
102
+ *
103
+ * @throws BargsError if multiple options use the same alias
104
+ */
105
+ options: {
106
+ <A extends OptionsSchema>(a: A): A;
107
+ <A extends OptionsSchema, B extends OptionsSchema>(a: A, b: B): A & B;
108
+ <A extends OptionsSchema, B extends OptionsSchema, C extends OptionsSchema>(a: A, b: B, c: C): A & B & C;
109
+ <A extends OptionsSchema, B extends OptionsSchema, C extends OptionsSchema, D extends OptionsSchema>(a: A, b: B, c: C, d: D): A & B & C & D;
110
+ (...schemas: OptionsSchema[]): OptionsSchema;
111
+ };
112
+ /**
113
+ * Create a positionals schema with proper tuple type inference.
114
+ *
115
+ * @example
116
+ *
117
+ * ```typescript
118
+ * const positionals = opt.positionals(
119
+ * opt.stringPos({ description: 'Input file', required: true }),
120
+ * opt.stringPos({ description: 'Output file' }),
121
+ * );
122
+ * ```
123
+ */
124
+ positionals: {
125
+ <A extends PositionalDef>(a: A): [A];
126
+ <A extends PositionalDef, B extends PositionalDef>(a: A, b: B): [A, B];
127
+ <A extends PositionalDef, B extends PositionalDef, C extends PositionalDef>(a: A, b: B, c: C): [A, B, C];
128
+ <A extends PositionalDef, B extends PositionalDef, C extends PositionalDef, D extends PositionalDef>(a: A, b: B, c: C, d: D): [A, B, C, D];
129
+ (...positionals: PositionalDef[]): PositionalsSchema;
130
+ };
131
+ /**
132
+ * Define a string option. Props type is preserved to enable default
133
+ * inference.
134
+ */
135
+ string: <P extends Omit<StringOption, "type"> = Omit<StringOption, "type">>(props?: P) => P & StringOption;
136
+ /**
137
+ * Define a string positional argument.
138
+ */
139
+ stringPos: (props?: Omit<StringPositional, "type">) => StringPositional;
140
+ /**
141
+ * Define a variadic positional (rest args).
142
+ */
143
+ variadic: (items: "number" | "string", props?: Omit<VariadicPositional, "items" | "type">) => VariadicPositional;
144
+ };
145
+ //# sourceMappingURL=opt.d.ts.map
@@ -0,0 +1 @@
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