@optique/core 0.5.0-dev.79 → 0.5.0-dev.82

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.
@@ -0,0 +1,274 @@
1
+ import { Message } from "./message.cjs";
2
+ import { OptionName } from "./usage.cjs";
3
+ import { ValueParser, ValueParserResult } from "./valueparser.cjs";
4
+ import { Parser } from "./parser.cjs";
5
+
6
+ //#region src/primitives.d.ts
7
+
8
+ /**
9
+ * Creates a parser that always succeeds without consuming any input and
10
+ * produces a constant value of the type {@link T}.
11
+ * @template T The type of the constant value produced by the parser.
12
+ */
13
+ declare function constant<const T>(value: T): Parser<T, T>;
14
+ /**
15
+ * Options for the {@link option} parser.
16
+ */
17
+ interface OptionOptions {
18
+ /**
19
+ * The description of the option, which can be used for help messages.
20
+ */
21
+ readonly description?: Message;
22
+ /**
23
+ * Error message customization options.
24
+ * @since 0.5.0
25
+ */
26
+ readonly errors?: OptionErrorOptions;
27
+ }
28
+ /**
29
+ * Options for customizing error messages in the {@link option} parser.
30
+ * @since 0.5.0
31
+ */
32
+ interface OptionErrorOptions {
33
+ /**
34
+ * Custom error message when the option is missing (for required options).
35
+ * Can be a static message or a function that receives the option names.
36
+ */
37
+ missing?: Message | ((optionNames: readonly string[]) => Message);
38
+ /**
39
+ * Custom error message when options are terminated (after `--`).
40
+ */
41
+ optionsTerminated?: Message;
42
+ /**
43
+ * Custom error message when input is empty but option is expected.
44
+ */
45
+ endOfInput?: Message;
46
+ /**
47
+ * Custom error message when option is used multiple times.
48
+ * Can be a static message or a function that receives the token.
49
+ */
50
+ duplicate?: Message | ((token: string) => Message);
51
+ /**
52
+ * Custom error message when value parsing fails.
53
+ * Can be a static message or a function that receives the error message.
54
+ */
55
+ invalidValue?: Message | ((error: Message) => Message);
56
+ /**
57
+ * Custom error message when a Boolean flag receives an unexpected value.
58
+ * Can be a static message or a function that receives the value.
59
+ */
60
+ unexpectedValue?: Message | ((value: string) => Message);
61
+ }
62
+ /**
63
+ * Creates a parser for various styles of command-line options that take an
64
+ * argument value, such as `--option=value`, `-o value`, or `/option:value`.
65
+ * @template T The type of value this parser produces.
66
+ * @param args The {@link OptionName}s to parse, followed by
67
+ * a {@link ValueParser} that defines how to parse the value of
68
+ * the option. If no value parser is provided, the option is
69
+ * treated as a boolean flag.
70
+ * @returns A {@link Parser} that can parse the specified options and their
71
+ * values.
72
+ */
73
+ declare function option<T>(...args: readonly [...readonly OptionName[], ValueParser<T>]): Parser<T, ValueParserResult<T> | undefined>;
74
+ /**
75
+ * Creates a parser for various styles of command-line options that take an
76
+ * argument value, such as `--option=value`, `-o value`, or `/option:value`.
77
+ * @template T The type of value this parser produces.
78
+ * @param args The {@link OptionName}s to parse, followed by
79
+ * a {@link ValueParser} that defines how to parse the value of
80
+ * the option, and an optional {@link OptionOptions} object
81
+ * that allows you to specify a description or other metadata.
82
+ * @returns A {@link Parser} that can parse the specified options and their
83
+ * values.
84
+ */
85
+ declare function option<T>(...args: readonly [...readonly OptionName[], ValueParser<T>, OptionOptions]): Parser<T, ValueParserResult<T> | undefined>;
86
+ /**
87
+ * Creates a parser for various styles of command-line options that do not
88
+ * take an argument value, such as `--option`, `-o`, or `/option`.
89
+ * @param optionNames The {@link OptionName}s to parse.
90
+ * @return A {@link Parser} that can parse the specified options as Boolean
91
+ * flags, producing `true` if the option is present.
92
+ */
93
+ declare function option(...optionNames: readonly OptionName[]): Parser<boolean, ValueParserResult<boolean> | undefined>;
94
+ /**
95
+ * Creates a parser for various styles of command-line options that take an
96
+ * argument value, such as `--option=value`, `-o value`, or `/option:value`.
97
+ * @param args The {@link OptionName}s to parse, followed by
98
+ * an optional {@link OptionOptions} object that allows you to
99
+ * specify a description or other metadata.
100
+ * @returns A {@link Parser} that can parse the specified options and their
101
+ * values.
102
+ */
103
+ declare function option(...args: readonly [...readonly OptionName[], OptionOptions]): Parser<boolean, ValueParserResult<boolean> | undefined>;
104
+ /**
105
+ * Options for the {@link flag} parser.
106
+ */
107
+ interface FlagOptions {
108
+ /**
109
+ * The description of the flag, which can be used for help messages.
110
+ */
111
+ readonly description?: Message;
112
+ /**
113
+ * Error message customization options.
114
+ * @since 0.5.0
115
+ */
116
+ readonly errors?: FlagErrorOptions;
117
+ }
118
+ /**
119
+ * Options for customizing error messages in the {@link flag} parser.
120
+ * @since 0.5.0
121
+ */
122
+ interface FlagErrorOptions {
123
+ /**
124
+ * Custom error message when the flag is missing (for required flags).
125
+ * Can be a static message or a function that receives the option names.
126
+ */
127
+ missing?: Message | ((optionNames: readonly string[]) => Message);
128
+ /**
129
+ * Custom error message when options are terminated (after --).
130
+ */
131
+ optionsTerminated?: Message;
132
+ /**
133
+ * Custom error message when input is empty but flag is expected.
134
+ */
135
+ endOfInput?: Message;
136
+ /**
137
+ * Custom error message when flag is used multiple times.
138
+ * Can be a static message or a function that receives the token.
139
+ */
140
+ duplicate?: Message | ((token: string) => Message);
141
+ }
142
+ /**
143
+ * Creates a parser for command-line flags that must be explicitly provided.
144
+ * Unlike {@link option}, this parser fails if the flag is not present, making
145
+ * it suitable for required boolean flags that don't have a meaningful default.
146
+ *
147
+ * The key difference from {@link option} is:
148
+ * - {@link option} without a value parser: Returns `false` when not present
149
+ * - {@link flag}: Fails parsing when not present, only produces `true`
150
+ *
151
+ * This is useful for dependent options where the presence of a flag changes
152
+ * the shape of the result type.
153
+ *
154
+ * @param args The {@link OptionName}s to parse, followed by an optional
155
+ * {@link FlagOptions} object that allows you to specify
156
+ * a description or other metadata.
157
+ * @returns A {@link Parser} that produces `true` when the flag is present
158
+ * and fails when it is not present.
159
+ *
160
+ * @example
161
+ * ```typescript
162
+ * // Basic flag usage
163
+ * const parser = flag("-f", "--force");
164
+ * // Succeeds with true: parse(parser, ["-f"])
165
+ * // Fails: parse(parser, [])
166
+ *
167
+ * // With description
168
+ * const verboseFlag = flag("-v", "--verbose", {
169
+ * description: "Enable verbose output"
170
+ * });
171
+ * ```
172
+ */
173
+ declare function flag(...args: readonly [...readonly OptionName[], FlagOptions] | readonly OptionName[]): Parser<true, ValueParserResult<true> | undefined>;
174
+ /**
175
+ * Options for the {@link argument} parser.
176
+ */
177
+ interface ArgumentOptions {
178
+ /**
179
+ * The description of the argument, which can be used for help messages.
180
+ */
181
+ readonly description?: Message;
182
+ /**
183
+ * Error message customization options.
184
+ * @since 0.5.0
185
+ */
186
+ readonly errors?: ArgumentErrorOptions;
187
+ }
188
+ /**
189
+ * Options for customizing error messages in the {@link argument} parser.
190
+ * @since 0.5.0
191
+ */
192
+ interface ArgumentErrorOptions {
193
+ /**
194
+ * Custom error message when input is empty but argument is expected.
195
+ */
196
+ endOfInput?: Message;
197
+ /**
198
+ * Custom error message when value parsing fails.
199
+ * Can be a static message or a function that receives the error message.
200
+ */
201
+ invalidValue?: Message | ((error: Message) => Message);
202
+ /**
203
+ * Custom error message when argument is used multiple times.
204
+ * Can be a static message or a function that receives the metavar.
205
+ */
206
+ multiple?: Message | ((metavar: string) => Message);
207
+ }
208
+ /**
209
+ * Creates a parser that expects a single argument value.
210
+ * This parser is typically used for positional arguments
211
+ * that are not options or flags.
212
+ * @template T The type of the value produced by the parser.
213
+ * @param valueParser The {@link ValueParser} that defines how to parse
214
+ * the argument value.
215
+ * @param options Optional configuration for the argument parser,
216
+ * allowing you to specify a description or other metadata.
217
+ * @returns A {@link Parser} that expects a single argument value and produces
218
+ * the parsed value of type {@link T}.
219
+ */
220
+ declare function argument<T>(valueParser: ValueParser<T>, options?: ArgumentOptions): Parser<T, ValueParserResult<T> | undefined>;
221
+ /**
222
+ * Options for the {@link command} parser.
223
+ * @since 0.5.0
224
+ */
225
+ interface CommandOptions {
226
+ /**
227
+ * A description of the command, used for documentation.
228
+ */
229
+ readonly description?: Message;
230
+ /**
231
+ * Error messages customization.
232
+ * @since 0.5.0
233
+ */
234
+ readonly errors?: CommandErrorOptions;
235
+ }
236
+ /**
237
+ * Options for customizing error messages in the {@link command} parser.
238
+ * @since 0.5.0
239
+ */
240
+ interface CommandErrorOptions {
241
+ /**
242
+ * Error message when command is expected but not found.
243
+ */
244
+ readonly notMatched?: Message | ((expected: string, actual: string | null) => Message);
245
+ /**
246
+ * Error message when command was not matched during completion.
247
+ */
248
+ readonly notFound?: Message;
249
+ /**
250
+ * Error message for invalid command state.
251
+ */
252
+ readonly invalidState?: Message;
253
+ }
254
+ /**
255
+ * The state type for the {@link command} parser.
256
+ * @template TState The type of the inner parser's state.
257
+ */
258
+ type CommandState<TState> = undefined | ["matched", string] | ["parsing", TState];
259
+ /**
260
+ * Creates a parser that matches a specific subcommand name and then applies
261
+ * an inner parser to the remaining arguments.
262
+ * This is useful for building CLI tools with subcommands like git, npm, etc.
263
+ * @template T The type of the value returned by the inner parser.
264
+ * @template TState The type of the state used by the inner parser.
265
+ * @param name The subcommand name to match (e.g., `"show"`, `"edit"`).
266
+ * @param parser The {@link Parser} to apply after the command is matched.
267
+ * @param options Optional configuration for the command parser, such as
268
+ * a description for documentation.
269
+ * @returns A {@link Parser} that matches the command name and delegates
270
+ * to the inner parser for the remaining arguments.
271
+ */
272
+ declare function command<T, TState>(name: string, parser: Parser<T, TState>, options?: CommandOptions): Parser<T, CommandState<TState>>;
273
+ //#endregion
274
+ export { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, argument, command, constant, flag, option };
@@ -0,0 +1,274 @@
1
+ import { Message } from "./message.js";
2
+ import { OptionName } from "./usage.js";
3
+ import { ValueParser, ValueParserResult } from "./valueparser.js";
4
+ import { Parser } from "./parser.js";
5
+
6
+ //#region src/primitives.d.ts
7
+
8
+ /**
9
+ * Creates a parser that always succeeds without consuming any input and
10
+ * produces a constant value of the type {@link T}.
11
+ * @template T The type of the constant value produced by the parser.
12
+ */
13
+ declare function constant<const T>(value: T): Parser<T, T>;
14
+ /**
15
+ * Options for the {@link option} parser.
16
+ */
17
+ interface OptionOptions {
18
+ /**
19
+ * The description of the option, which can be used for help messages.
20
+ */
21
+ readonly description?: Message;
22
+ /**
23
+ * Error message customization options.
24
+ * @since 0.5.0
25
+ */
26
+ readonly errors?: OptionErrorOptions;
27
+ }
28
+ /**
29
+ * Options for customizing error messages in the {@link option} parser.
30
+ * @since 0.5.0
31
+ */
32
+ interface OptionErrorOptions {
33
+ /**
34
+ * Custom error message when the option is missing (for required options).
35
+ * Can be a static message or a function that receives the option names.
36
+ */
37
+ missing?: Message | ((optionNames: readonly string[]) => Message);
38
+ /**
39
+ * Custom error message when options are terminated (after `--`).
40
+ */
41
+ optionsTerminated?: Message;
42
+ /**
43
+ * Custom error message when input is empty but option is expected.
44
+ */
45
+ endOfInput?: Message;
46
+ /**
47
+ * Custom error message when option is used multiple times.
48
+ * Can be a static message or a function that receives the token.
49
+ */
50
+ duplicate?: Message | ((token: string) => Message);
51
+ /**
52
+ * Custom error message when value parsing fails.
53
+ * Can be a static message or a function that receives the error message.
54
+ */
55
+ invalidValue?: Message | ((error: Message) => Message);
56
+ /**
57
+ * Custom error message when a Boolean flag receives an unexpected value.
58
+ * Can be a static message or a function that receives the value.
59
+ */
60
+ unexpectedValue?: Message | ((value: string) => Message);
61
+ }
62
+ /**
63
+ * Creates a parser for various styles of command-line options that take an
64
+ * argument value, such as `--option=value`, `-o value`, or `/option:value`.
65
+ * @template T The type of value this parser produces.
66
+ * @param args The {@link OptionName}s to parse, followed by
67
+ * a {@link ValueParser} that defines how to parse the value of
68
+ * the option. If no value parser is provided, the option is
69
+ * treated as a boolean flag.
70
+ * @returns A {@link Parser} that can parse the specified options and their
71
+ * values.
72
+ */
73
+ declare function option<T>(...args: readonly [...readonly OptionName[], ValueParser<T>]): Parser<T, ValueParserResult<T> | undefined>;
74
+ /**
75
+ * Creates a parser for various styles of command-line options that take an
76
+ * argument value, such as `--option=value`, `-o value`, or `/option:value`.
77
+ * @template T The type of value this parser produces.
78
+ * @param args The {@link OptionName}s to parse, followed by
79
+ * a {@link ValueParser} that defines how to parse the value of
80
+ * the option, and an optional {@link OptionOptions} object
81
+ * that allows you to specify a description or other metadata.
82
+ * @returns A {@link Parser} that can parse the specified options and their
83
+ * values.
84
+ */
85
+ declare function option<T>(...args: readonly [...readonly OptionName[], ValueParser<T>, OptionOptions]): Parser<T, ValueParserResult<T> | undefined>;
86
+ /**
87
+ * Creates a parser for various styles of command-line options that do not
88
+ * take an argument value, such as `--option`, `-o`, or `/option`.
89
+ * @param optionNames The {@link OptionName}s to parse.
90
+ * @return A {@link Parser} that can parse the specified options as Boolean
91
+ * flags, producing `true` if the option is present.
92
+ */
93
+ declare function option(...optionNames: readonly OptionName[]): Parser<boolean, ValueParserResult<boolean> | undefined>;
94
+ /**
95
+ * Creates a parser for various styles of command-line options that take an
96
+ * argument value, such as `--option=value`, `-o value`, or `/option:value`.
97
+ * @param args The {@link OptionName}s to parse, followed by
98
+ * an optional {@link OptionOptions} object that allows you to
99
+ * specify a description or other metadata.
100
+ * @returns A {@link Parser} that can parse the specified options and their
101
+ * values.
102
+ */
103
+ declare function option(...args: readonly [...readonly OptionName[], OptionOptions]): Parser<boolean, ValueParserResult<boolean> | undefined>;
104
+ /**
105
+ * Options for the {@link flag} parser.
106
+ */
107
+ interface FlagOptions {
108
+ /**
109
+ * The description of the flag, which can be used for help messages.
110
+ */
111
+ readonly description?: Message;
112
+ /**
113
+ * Error message customization options.
114
+ * @since 0.5.0
115
+ */
116
+ readonly errors?: FlagErrorOptions;
117
+ }
118
+ /**
119
+ * Options for customizing error messages in the {@link flag} parser.
120
+ * @since 0.5.0
121
+ */
122
+ interface FlagErrorOptions {
123
+ /**
124
+ * Custom error message when the flag is missing (for required flags).
125
+ * Can be a static message or a function that receives the option names.
126
+ */
127
+ missing?: Message | ((optionNames: readonly string[]) => Message);
128
+ /**
129
+ * Custom error message when options are terminated (after --).
130
+ */
131
+ optionsTerminated?: Message;
132
+ /**
133
+ * Custom error message when input is empty but flag is expected.
134
+ */
135
+ endOfInput?: Message;
136
+ /**
137
+ * Custom error message when flag is used multiple times.
138
+ * Can be a static message or a function that receives the token.
139
+ */
140
+ duplicate?: Message | ((token: string) => Message);
141
+ }
142
+ /**
143
+ * Creates a parser for command-line flags that must be explicitly provided.
144
+ * Unlike {@link option}, this parser fails if the flag is not present, making
145
+ * it suitable for required boolean flags that don't have a meaningful default.
146
+ *
147
+ * The key difference from {@link option} is:
148
+ * - {@link option} without a value parser: Returns `false` when not present
149
+ * - {@link flag}: Fails parsing when not present, only produces `true`
150
+ *
151
+ * This is useful for dependent options where the presence of a flag changes
152
+ * the shape of the result type.
153
+ *
154
+ * @param args The {@link OptionName}s to parse, followed by an optional
155
+ * {@link FlagOptions} object that allows you to specify
156
+ * a description or other metadata.
157
+ * @returns A {@link Parser} that produces `true` when the flag is present
158
+ * and fails when it is not present.
159
+ *
160
+ * @example
161
+ * ```typescript
162
+ * // Basic flag usage
163
+ * const parser = flag("-f", "--force");
164
+ * // Succeeds with true: parse(parser, ["-f"])
165
+ * // Fails: parse(parser, [])
166
+ *
167
+ * // With description
168
+ * const verboseFlag = flag("-v", "--verbose", {
169
+ * description: "Enable verbose output"
170
+ * });
171
+ * ```
172
+ */
173
+ declare function flag(...args: readonly [...readonly OptionName[], FlagOptions] | readonly OptionName[]): Parser<true, ValueParserResult<true> | undefined>;
174
+ /**
175
+ * Options for the {@link argument} parser.
176
+ */
177
+ interface ArgumentOptions {
178
+ /**
179
+ * The description of the argument, which can be used for help messages.
180
+ */
181
+ readonly description?: Message;
182
+ /**
183
+ * Error message customization options.
184
+ * @since 0.5.0
185
+ */
186
+ readonly errors?: ArgumentErrorOptions;
187
+ }
188
+ /**
189
+ * Options for customizing error messages in the {@link argument} parser.
190
+ * @since 0.5.0
191
+ */
192
+ interface ArgumentErrorOptions {
193
+ /**
194
+ * Custom error message when input is empty but argument is expected.
195
+ */
196
+ endOfInput?: Message;
197
+ /**
198
+ * Custom error message when value parsing fails.
199
+ * Can be a static message or a function that receives the error message.
200
+ */
201
+ invalidValue?: Message | ((error: Message) => Message);
202
+ /**
203
+ * Custom error message when argument is used multiple times.
204
+ * Can be a static message or a function that receives the metavar.
205
+ */
206
+ multiple?: Message | ((metavar: string) => Message);
207
+ }
208
+ /**
209
+ * Creates a parser that expects a single argument value.
210
+ * This parser is typically used for positional arguments
211
+ * that are not options or flags.
212
+ * @template T The type of the value produced by the parser.
213
+ * @param valueParser The {@link ValueParser} that defines how to parse
214
+ * the argument value.
215
+ * @param options Optional configuration for the argument parser,
216
+ * allowing you to specify a description or other metadata.
217
+ * @returns A {@link Parser} that expects a single argument value and produces
218
+ * the parsed value of type {@link T}.
219
+ */
220
+ declare function argument<T>(valueParser: ValueParser<T>, options?: ArgumentOptions): Parser<T, ValueParserResult<T> | undefined>;
221
+ /**
222
+ * Options for the {@link command} parser.
223
+ * @since 0.5.0
224
+ */
225
+ interface CommandOptions {
226
+ /**
227
+ * A description of the command, used for documentation.
228
+ */
229
+ readonly description?: Message;
230
+ /**
231
+ * Error messages customization.
232
+ * @since 0.5.0
233
+ */
234
+ readonly errors?: CommandErrorOptions;
235
+ }
236
+ /**
237
+ * Options for customizing error messages in the {@link command} parser.
238
+ * @since 0.5.0
239
+ */
240
+ interface CommandErrorOptions {
241
+ /**
242
+ * Error message when command is expected but not found.
243
+ */
244
+ readonly notMatched?: Message | ((expected: string, actual: string | null) => Message);
245
+ /**
246
+ * Error message when command was not matched during completion.
247
+ */
248
+ readonly notFound?: Message;
249
+ /**
250
+ * Error message for invalid command state.
251
+ */
252
+ readonly invalidState?: Message;
253
+ }
254
+ /**
255
+ * The state type for the {@link command} parser.
256
+ * @template TState The type of the inner parser's state.
257
+ */
258
+ type CommandState<TState> = undefined | ["matched", string] | ["parsing", TState];
259
+ /**
260
+ * Creates a parser that matches a specific subcommand name and then applies
261
+ * an inner parser to the remaining arguments.
262
+ * This is useful for building CLI tools with subcommands like git, npm, etc.
263
+ * @template T The type of the value returned by the inner parser.
264
+ * @template TState The type of the state used by the inner parser.
265
+ * @param name The subcommand name to match (e.g., `"show"`, `"edit"`).
266
+ * @param parser The {@link Parser} to apply after the command is matched.
267
+ * @param options Optional configuration for the command parser, such as
268
+ * a description for documentation.
269
+ * @returns A {@link Parser} that matches the command name and delegates
270
+ * to the inner parser for the remaining arguments.
271
+ */
272
+ declare function command<T, TState>(name: string, parser: Parser<T, TState>, options?: CommandOptions): Parser<T, CommandState<TState>>;
273
+ //#endregion
274
+ export { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, argument, command, constant, flag, option };