@optique/core 0.1.0-dev.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.
- package/README.md +475 -0
- package/dist/doc.cjs +104 -0
- package/dist/doc.d.cts +129 -0
- package/dist/doc.d.ts +129 -0
- package/dist/doc.js +104 -0
- package/dist/facade.cjs +98 -0
- package/dist/facade.d.cts +113 -0
- package/dist/facade.d.ts +113 -0
- package/dist/facade.js +97 -0
- package/dist/index.cjs +42 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/message.cjs +202 -0
- package/dist/message.d.cts +199 -0
- package/dist/message.d.ts +199 -0
- package/dist/message.js +194 -0
- package/dist/parser.cjs +1065 -0
- package/dist/parser.d.cts +580 -0
- package/dist/parser.d.ts +580 -0
- package/dist/parser.js +1053 -0
- package/dist/usage.cjs +242 -0
- package/dist/usage.d.cts +217 -0
- package/dist/usage.d.ts +217 -0
- package/dist/usage.js +239 -0
- package/dist/valueparser.cjs +332 -0
- package/dist/valueparser.d.cts +332 -0
- package/dist/valueparser.d.ts +332 -0
- package/dist/valueparser.js +325 -0
- package/package.json +117 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const require_message = require('./message.cjs');
|
|
2
|
+
const require_usage = require('./usage.cjs');
|
|
3
|
+
const require_doc = require('./doc.cjs');
|
|
4
|
+
const require_valueparser = require('./valueparser.cjs');
|
|
5
|
+
const require_parser = require('./parser.cjs');
|
|
6
|
+
const require_facade = require('./facade.cjs');
|
|
7
|
+
|
|
8
|
+
exports.RunError = require_facade.RunError;
|
|
9
|
+
exports.argument = require_parser.argument;
|
|
10
|
+
exports.choice = require_valueparser.choice;
|
|
11
|
+
exports.command = require_parser.command;
|
|
12
|
+
exports.constant = require_parser.constant;
|
|
13
|
+
exports.float = require_valueparser.float;
|
|
14
|
+
exports.formatDocPage = require_doc.formatDocPage;
|
|
15
|
+
exports.formatMessage = require_message.formatMessage;
|
|
16
|
+
exports.formatUsage = require_usage.formatUsage;
|
|
17
|
+
exports.formatUsageTerm = require_usage.formatUsageTerm;
|
|
18
|
+
exports.getDocPage = require_parser.getDocPage;
|
|
19
|
+
exports.integer = require_valueparser.integer;
|
|
20
|
+
exports.isValueParser = require_valueparser.isValueParser;
|
|
21
|
+
exports.locale = require_valueparser.locale;
|
|
22
|
+
exports.merge = require_parser.merge;
|
|
23
|
+
exports.message = require_message.message;
|
|
24
|
+
exports.metavar = require_message.metavar;
|
|
25
|
+
exports.multiple = require_parser.multiple;
|
|
26
|
+
exports.normalizeUsage = require_usage.normalizeUsage;
|
|
27
|
+
exports.object = require_parser.object;
|
|
28
|
+
exports.option = require_parser.option;
|
|
29
|
+
exports.optionName = require_message.optionName;
|
|
30
|
+
exports.optionNames = require_message.optionNames;
|
|
31
|
+
exports.optional = require_parser.optional;
|
|
32
|
+
exports.or = require_parser.or;
|
|
33
|
+
exports.parse = require_parser.parse;
|
|
34
|
+
exports.run = require_facade.run;
|
|
35
|
+
exports.string = require_valueparser.string;
|
|
36
|
+
exports.text = require_message.text;
|
|
37
|
+
exports.tuple = require_parser.tuple;
|
|
38
|
+
exports.url = require_valueparser.url;
|
|
39
|
+
exports.uuid = require_valueparser.uuid;
|
|
40
|
+
exports.value = require_message.value;
|
|
41
|
+
exports.values = require_message.values;
|
|
42
|
+
exports.withDefault = require_parser.withDefault;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.cjs";
|
|
2
|
+
import { OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, formatUsage, formatUsageTerm, normalizeUsage } from "./usage.cjs";
|
|
3
|
+
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, formatDocPage } from "./doc.cjs";
|
|
4
|
+
import { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.cjs";
|
|
5
|
+
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.cjs";
|
|
6
|
+
import { RunError, RunOptions, run } from "./facade.cjs";
|
|
7
|
+
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.js";
|
|
2
|
+
import { OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, formatUsage, formatUsageTerm, normalizeUsage } from "./usage.js";
|
|
3
|
+
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, formatDocPage } from "./doc.js";
|
|
4
|
+
import { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
5
|
+
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
6
|
+
import { RunError, RunOptions, run } from "./facade.js";
|
|
7
|
+
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.js";
|
|
2
|
+
import { formatUsage, formatUsageTerm, normalizeUsage } from "./usage.js";
|
|
3
|
+
import { formatDocPage } from "./doc.js";
|
|
4
|
+
import { choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
5
|
+
import { argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
6
|
+
import { RunError, run } from "./facade.js";
|
|
7
|
+
|
|
8
|
+
export { RunError, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/message.cjs
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/message.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a structured message with template strings and values.
|
|
5
|
+
*
|
|
6
|
+
* This function allows creating messages where specific values can be
|
|
7
|
+
* highlighted or styled differently when displayed to the user.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const error = message`Expected number between ${min} and ${max}, got ${value}`;
|
|
12
|
+
* const concat = message`${optionName("--age")}: ${error}`;
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param message Template strings array (from template literal).
|
|
16
|
+
* @param values Values to be interpolated into the template.
|
|
17
|
+
* @returns A structured Message object.
|
|
18
|
+
*/
|
|
19
|
+
function message(message$1, ...values$1) {
|
|
20
|
+
const messageTerms = [];
|
|
21
|
+
for (let i = 0; i < message$1.length; i++) {
|
|
22
|
+
if (message$1[i] !== "") messageTerms.push({
|
|
23
|
+
type: "text",
|
|
24
|
+
text: message$1[i]
|
|
25
|
+
});
|
|
26
|
+
if (i >= values$1.length) continue;
|
|
27
|
+
const value$1 = values$1[i];
|
|
28
|
+
if (typeof value$1 === "string") messageTerms.push({
|
|
29
|
+
type: "value",
|
|
30
|
+
value: value$1
|
|
31
|
+
});
|
|
32
|
+
else if (Array.isArray(value$1)) messageTerms.push(...value$1);
|
|
33
|
+
else if (typeof value$1 === "object" && value$1 != null && "type" in value$1) messageTerms.push(value$1);
|
|
34
|
+
else throw new TypeError(`Invalid value type in message: ${typeof value$1}.`);
|
|
35
|
+
}
|
|
36
|
+
return messageTerms;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates a {@link MessageTerm} for plain text. Usually used for
|
|
40
|
+
* dynamically generated messages.
|
|
41
|
+
* @param text The plain text to be included in the message.
|
|
42
|
+
* @returns A {@link MessageTerm} representing the plain text.
|
|
43
|
+
*/
|
|
44
|
+
function text(text$1) {
|
|
45
|
+
return {
|
|
46
|
+
type: "text",
|
|
47
|
+
text: text$1
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates a {@link MessageTerm} for an option name.
|
|
52
|
+
* @param name The name of the option, which can be a short or long option name.
|
|
53
|
+
* For example, `"-f"` or `"--foo"`.
|
|
54
|
+
* @returns A {@link MessageTerm} representing the option name.
|
|
55
|
+
*/
|
|
56
|
+
function optionName(name) {
|
|
57
|
+
return {
|
|
58
|
+
type: "optionName",
|
|
59
|
+
optionName: name
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a {@link MessageTerm} for a list of option names.
|
|
64
|
+
* @param names The list of option names, which can include both short and long
|
|
65
|
+
* option names. For example, `["--foo", "--bar"]`.
|
|
66
|
+
* @returns A {@link MessageTerm} representing the list of option names.
|
|
67
|
+
*/
|
|
68
|
+
function optionNames(names) {
|
|
69
|
+
return {
|
|
70
|
+
type: "optionNames",
|
|
71
|
+
optionNames: names
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a {@link MessageTerm} for a metavariable.
|
|
76
|
+
* @param metavar The metavariable name, which is a string that represents
|
|
77
|
+
* a variable in the message. For example, `"VALUE"` or
|
|
78
|
+
* `"ARG"`.
|
|
79
|
+
* @returns A {@link MessageTerm} representing the metavariable.
|
|
80
|
+
*/
|
|
81
|
+
function metavar(metavar$1) {
|
|
82
|
+
return {
|
|
83
|
+
type: "metavar",
|
|
84
|
+
metavar: metavar$1
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Creates a {@link MessageTerm} for a single value. However, you usually
|
|
89
|
+
* don't need to use this function directly, as {@link message} string template
|
|
90
|
+
* will automatically create a {@link MessageTerm} for a value when
|
|
91
|
+
* you use a string in a template literal.
|
|
92
|
+
* @param value The value, which can be any string representation of a value.
|
|
93
|
+
* For example, `"42"` or `"hello"`.
|
|
94
|
+
* @returns A {@link MessageTerm} representing the value.
|
|
95
|
+
*/
|
|
96
|
+
function value(value$1) {
|
|
97
|
+
return {
|
|
98
|
+
type: "value",
|
|
99
|
+
value: value$1
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Creates a {@link MessageTerm} for a list of consecutive values.
|
|
104
|
+
* @param values The list of consecutive values, which can include multiple
|
|
105
|
+
* string representations of consecutive values.
|
|
106
|
+
* For example, `["42", "hello"]`.
|
|
107
|
+
* @returns A {@link MessageTerm} representing the list of values.
|
|
108
|
+
*/
|
|
109
|
+
function values(values$1) {
|
|
110
|
+
return {
|
|
111
|
+
type: "values",
|
|
112
|
+
values: values$1
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Formats a {@link Message} into a human-readable string for
|
|
117
|
+
* the terminal.
|
|
118
|
+
* @param msg The message to format, which is an array of
|
|
119
|
+
* {@link MessageTerm} objects.
|
|
120
|
+
* @param options Optional formatting options to customize the output.
|
|
121
|
+
* @returns A formatted string representation of the message.
|
|
122
|
+
*/
|
|
123
|
+
function formatMessage(msg, options = {}) {
|
|
124
|
+
const useColors = options.colors ?? false;
|
|
125
|
+
const useQuotes = options.quotes ?? true;
|
|
126
|
+
function* stream() {
|
|
127
|
+
const wordPattern = /\s*\S+\s*/g;
|
|
128
|
+
for (const term of msg) if (term.type === "text") while (true) {
|
|
129
|
+
const match = wordPattern.exec(term.text);
|
|
130
|
+
if (match == null) break;
|
|
131
|
+
yield {
|
|
132
|
+
text: match[0],
|
|
133
|
+
width: match[0].length
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
else if (term.type === "optionName") {
|
|
137
|
+
const name = useQuotes ? `\`${term.optionName}\`` : term.optionName;
|
|
138
|
+
yield {
|
|
139
|
+
text: useColors ? `\x1b[3m${name}\x1b[0m` : name,
|
|
140
|
+
width: name.length
|
|
141
|
+
};
|
|
142
|
+
} else if (term.type === "optionNames") {
|
|
143
|
+
const names = term.optionNames.map((name) => useQuotes ? `\`${name}\`` : name);
|
|
144
|
+
let i = 0;
|
|
145
|
+
for (const name of names) {
|
|
146
|
+
if (i > 0) yield {
|
|
147
|
+
text: "/",
|
|
148
|
+
width: 1
|
|
149
|
+
};
|
|
150
|
+
yield {
|
|
151
|
+
text: useColors ? `\x1b[3m${name}\x1b[0m` : name,
|
|
152
|
+
width: name.length
|
|
153
|
+
};
|
|
154
|
+
i++;
|
|
155
|
+
}
|
|
156
|
+
} else if (term.type === "metavar") {
|
|
157
|
+
const metavar$1 = useQuotes ? `\`${term.metavar}\`` : term.metavar;
|
|
158
|
+
yield {
|
|
159
|
+
text: useColors ? `\x1b[1m${metavar$1}\x1b[0m` : metavar$1,
|
|
160
|
+
width: metavar$1.length
|
|
161
|
+
};
|
|
162
|
+
} else if (term.type === "value") {
|
|
163
|
+
const value$1 = useQuotes ? `${JSON.stringify(term.value)}` : term.value;
|
|
164
|
+
yield {
|
|
165
|
+
text: useColors ? `\x1b[32m${value$1}\x1b[0m` : value$1,
|
|
166
|
+
width: value$1.length
|
|
167
|
+
};
|
|
168
|
+
} else if (term.type === "values") for (let i = 0; i < term.values.length; i++) {
|
|
169
|
+
if (i > 0) yield {
|
|
170
|
+
text: " ",
|
|
171
|
+
width: 1
|
|
172
|
+
};
|
|
173
|
+
const value$1 = useQuotes ? JSON.stringify(term.values[i]) : term.values[i];
|
|
174
|
+
yield {
|
|
175
|
+
text: useColors ? i <= 0 ? `\x1b[32m${value$1}` : i + 1 >= term.values.length ? `${value$1}\x1b[0m` : value$1 : value$1,
|
|
176
|
+
width: value$1.length
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
else throw new TypeError(`Invalid MessageTerm type: ${term["type"]}.`);
|
|
180
|
+
}
|
|
181
|
+
let output = "";
|
|
182
|
+
let totalWidth = 0;
|
|
183
|
+
for (const { text: text$1, width } of stream()) {
|
|
184
|
+
if (options.maxWidth != null && totalWidth + width > options.maxWidth) {
|
|
185
|
+
output += "\n";
|
|
186
|
+
totalWidth = 0;
|
|
187
|
+
}
|
|
188
|
+
output += text$1;
|
|
189
|
+
totalWidth += width;
|
|
190
|
+
}
|
|
191
|
+
return output;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
//#endregion
|
|
195
|
+
exports.formatMessage = formatMessage;
|
|
196
|
+
exports.message = message;
|
|
197
|
+
exports.metavar = metavar;
|
|
198
|
+
exports.optionName = optionName;
|
|
199
|
+
exports.optionNames = optionNames;
|
|
200
|
+
exports.text = text;
|
|
201
|
+
exports.value = value;
|
|
202
|
+
exports.values = values;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
//#region src/message.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Represents a single term in a message, which can be a text, an option
|
|
4
|
+
* name, a list of option names, a metavariable, a value, or a list of
|
|
5
|
+
* consecutive values.
|
|
6
|
+
*/
|
|
7
|
+
type MessageTerm =
|
|
8
|
+
/**
|
|
9
|
+
* A plain text term in the message.
|
|
10
|
+
*/
|
|
11
|
+
{
|
|
12
|
+
/**
|
|
13
|
+
* The type of the term, which is always `"text"` for plain text.
|
|
14
|
+
*/
|
|
15
|
+
readonly type: "text";
|
|
16
|
+
/**
|
|
17
|
+
* The text content of the term.
|
|
18
|
+
*/
|
|
19
|
+
readonly text: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* An option name term in the message, which can be a single
|
|
23
|
+
* option name. Although it is named option name, it can also
|
|
24
|
+
* represent a subcommand.
|
|
25
|
+
*/ | {
|
|
26
|
+
/**
|
|
27
|
+
* The type of the term, which is `"optionName"` for a single option name.
|
|
28
|
+
*/
|
|
29
|
+
readonly type: "optionName";
|
|
30
|
+
/**
|
|
31
|
+
* The name of the option, which can be a short or long option name.
|
|
32
|
+
* For example, `"-f"` or `"--foo"`.
|
|
33
|
+
*/
|
|
34
|
+
readonly optionName: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A list of option names term in the message, which can be a
|
|
38
|
+
* list of option names.
|
|
39
|
+
*/ | {
|
|
40
|
+
/**
|
|
41
|
+
* The type of the term, which is `"optionNames"` for a list of option
|
|
42
|
+
* names.
|
|
43
|
+
*/
|
|
44
|
+
readonly type: "optionNames";
|
|
45
|
+
/**
|
|
46
|
+
* The list of option names, which can include both short and long
|
|
47
|
+
* option names. For example, `["--foo", "--bar"]`.
|
|
48
|
+
*/
|
|
49
|
+
readonly optionNames: readonly string[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A metavariable term in the message, which can be a single
|
|
53
|
+
* metavariable.
|
|
54
|
+
*/ | {
|
|
55
|
+
/**
|
|
56
|
+
* The type of the term, which is `"metavar"` for a metavariable.
|
|
57
|
+
*/
|
|
58
|
+
readonly type: "metavar";
|
|
59
|
+
/**
|
|
60
|
+
* The metavariable name, which is a string that represents
|
|
61
|
+
* a variable in the message. For example, `"VALUE"` or `"ARG"`.
|
|
62
|
+
*/
|
|
63
|
+
readonly metavar: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A value term in the message, which can be a single value.
|
|
67
|
+
*/ | {
|
|
68
|
+
/**
|
|
69
|
+
* The type of the term, which is `"value"` for a single value.
|
|
70
|
+
*/
|
|
71
|
+
readonly type: "value";
|
|
72
|
+
/**
|
|
73
|
+
* The value, which can be any string representation of a value.
|
|
74
|
+
* For example, `"42"` or `"hello"`.
|
|
75
|
+
*/
|
|
76
|
+
readonly value: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A list of values term in the message, which can be a
|
|
80
|
+
* list of values.
|
|
81
|
+
*/ | {
|
|
82
|
+
/**
|
|
83
|
+
* The type of the term, which is `"values"` for a list of consecutive
|
|
84
|
+
* values.
|
|
85
|
+
*/
|
|
86
|
+
readonly type: "values";
|
|
87
|
+
/**
|
|
88
|
+
* The list of values, which can include multiple string
|
|
89
|
+
* representations of consecutive values. For example, `["42", "hello"]`.
|
|
90
|
+
*/
|
|
91
|
+
readonly values: readonly string[];
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Type representing a message that can include styled/colored values.
|
|
95
|
+
* This type is used to create structured messages that can be
|
|
96
|
+
* displayed to the user with specific formatting.
|
|
97
|
+
*/
|
|
98
|
+
type Message = readonly MessageTerm[];
|
|
99
|
+
/**
|
|
100
|
+
* Creates a structured message with template strings and values.
|
|
101
|
+
*
|
|
102
|
+
* This function allows creating messages where specific values can be
|
|
103
|
+
* highlighted or styled differently when displayed to the user.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const error = message`Expected number between ${min} and ${max}, got ${value}`;
|
|
108
|
+
* const concat = message`${optionName("--age")}: ${error}`;
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @param message Template strings array (from template literal).
|
|
112
|
+
* @param values Values to be interpolated into the template.
|
|
113
|
+
* @returns A structured Message object.
|
|
114
|
+
*/
|
|
115
|
+
declare function message(message: TemplateStringsArray, ...values: readonly (MessageTerm | Message | string)[]): Message;
|
|
116
|
+
/**
|
|
117
|
+
* Creates a {@link MessageTerm} for plain text. Usually used for
|
|
118
|
+
* dynamically generated messages.
|
|
119
|
+
* @param text The plain text to be included in the message.
|
|
120
|
+
* @returns A {@link MessageTerm} representing the plain text.
|
|
121
|
+
*/
|
|
122
|
+
declare function text(text: string): MessageTerm;
|
|
123
|
+
/**
|
|
124
|
+
* Creates a {@link MessageTerm} for an option name.
|
|
125
|
+
* @param name The name of the option, which can be a short or long option name.
|
|
126
|
+
* For example, `"-f"` or `"--foo"`.
|
|
127
|
+
* @returns A {@link MessageTerm} representing the option name.
|
|
128
|
+
*/
|
|
129
|
+
declare function optionName(name: string): MessageTerm;
|
|
130
|
+
/**
|
|
131
|
+
* Creates a {@link MessageTerm} for a list of option names.
|
|
132
|
+
* @param names The list of option names, which can include both short and long
|
|
133
|
+
* option names. For example, `["--foo", "--bar"]`.
|
|
134
|
+
* @returns A {@link MessageTerm} representing the list of option names.
|
|
135
|
+
*/
|
|
136
|
+
declare function optionNames(names: readonly string[]): MessageTerm;
|
|
137
|
+
/**
|
|
138
|
+
* Creates a {@link MessageTerm} for a metavariable.
|
|
139
|
+
* @param metavar The metavariable name, which is a string that represents
|
|
140
|
+
* a variable in the message. For example, `"VALUE"` or
|
|
141
|
+
* `"ARG"`.
|
|
142
|
+
* @returns A {@link MessageTerm} representing the metavariable.
|
|
143
|
+
*/
|
|
144
|
+
declare function metavar(metavar: string): MessageTerm;
|
|
145
|
+
/**
|
|
146
|
+
* Creates a {@link MessageTerm} for a single value. However, you usually
|
|
147
|
+
* don't need to use this function directly, as {@link message} string template
|
|
148
|
+
* will automatically create a {@link MessageTerm} for a value when
|
|
149
|
+
* you use a string in a template literal.
|
|
150
|
+
* @param value The value, which can be any string representation of a value.
|
|
151
|
+
* For example, `"42"` or `"hello"`.
|
|
152
|
+
* @returns A {@link MessageTerm} representing the value.
|
|
153
|
+
*/
|
|
154
|
+
declare function value(value: string): MessageTerm;
|
|
155
|
+
/**
|
|
156
|
+
* Creates a {@link MessageTerm} for a list of consecutive values.
|
|
157
|
+
* @param values The list of consecutive values, which can include multiple
|
|
158
|
+
* string representations of consecutive values.
|
|
159
|
+
* For example, `["42", "hello"]`.
|
|
160
|
+
* @returns A {@link MessageTerm} representing the list of values.
|
|
161
|
+
*/
|
|
162
|
+
declare function values(values: readonly string[]): MessageTerm;
|
|
163
|
+
/**
|
|
164
|
+
* Options for the {@link formatMessage} function.
|
|
165
|
+
*/
|
|
166
|
+
interface MessageFormatOptions {
|
|
167
|
+
/**
|
|
168
|
+
* Whether to use colors in the formatted message. If `true`,
|
|
169
|
+
* the formatted message will include ANSI escape codes for colors.
|
|
170
|
+
* If `false`, the message will be plain text without colors.
|
|
171
|
+
* @default `false`
|
|
172
|
+
*/
|
|
173
|
+
readonly colors?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Whether to use quotes around values in the formatted message.
|
|
176
|
+
* If `true`, values will be wrapped in quotes (e.g., `"value"`).
|
|
177
|
+
* If `false`, values will be displayed without quotes.
|
|
178
|
+
* @default `true`
|
|
179
|
+
*/
|
|
180
|
+
readonly quotes?: boolean;
|
|
181
|
+
/**
|
|
182
|
+
* The maximum width of the formatted message. If specified,
|
|
183
|
+
* the message will be wrapped to fit within this width.
|
|
184
|
+
* If not specified, the message will not be wrapped.
|
|
185
|
+
* @default `undefined`
|
|
186
|
+
*/
|
|
187
|
+
readonly maxWidth?: number;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Formats a {@link Message} into a human-readable string for
|
|
191
|
+
* the terminal.
|
|
192
|
+
* @param msg The message to format, which is an array of
|
|
193
|
+
* {@link MessageTerm} objects.
|
|
194
|
+
* @param options Optional formatting options to customize the output.
|
|
195
|
+
* @returns A formatted string representation of the message.
|
|
196
|
+
*/
|
|
197
|
+
declare function formatMessage(msg: Message, options?: MessageFormatOptions): string;
|
|
198
|
+
//#endregion
|
|
199
|
+
export { Message, MessageFormatOptions, MessageTerm, formatMessage, message, metavar, optionName, optionNames, text, value, values };
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
//#region src/message.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Represents a single term in a message, which can be a text, an option
|
|
4
|
+
* name, a list of option names, a metavariable, a value, or a list of
|
|
5
|
+
* consecutive values.
|
|
6
|
+
*/
|
|
7
|
+
type MessageTerm =
|
|
8
|
+
/**
|
|
9
|
+
* A plain text term in the message.
|
|
10
|
+
*/
|
|
11
|
+
{
|
|
12
|
+
/**
|
|
13
|
+
* The type of the term, which is always `"text"` for plain text.
|
|
14
|
+
*/
|
|
15
|
+
readonly type: "text";
|
|
16
|
+
/**
|
|
17
|
+
* The text content of the term.
|
|
18
|
+
*/
|
|
19
|
+
readonly text: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* An option name term in the message, which can be a single
|
|
23
|
+
* option name. Although it is named option name, it can also
|
|
24
|
+
* represent a subcommand.
|
|
25
|
+
*/ | {
|
|
26
|
+
/**
|
|
27
|
+
* The type of the term, which is `"optionName"` for a single option name.
|
|
28
|
+
*/
|
|
29
|
+
readonly type: "optionName";
|
|
30
|
+
/**
|
|
31
|
+
* The name of the option, which can be a short or long option name.
|
|
32
|
+
* For example, `"-f"` or `"--foo"`.
|
|
33
|
+
*/
|
|
34
|
+
readonly optionName: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A list of option names term in the message, which can be a
|
|
38
|
+
* list of option names.
|
|
39
|
+
*/ | {
|
|
40
|
+
/**
|
|
41
|
+
* The type of the term, which is `"optionNames"` for a list of option
|
|
42
|
+
* names.
|
|
43
|
+
*/
|
|
44
|
+
readonly type: "optionNames";
|
|
45
|
+
/**
|
|
46
|
+
* The list of option names, which can include both short and long
|
|
47
|
+
* option names. For example, `["--foo", "--bar"]`.
|
|
48
|
+
*/
|
|
49
|
+
readonly optionNames: readonly string[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A metavariable term in the message, which can be a single
|
|
53
|
+
* metavariable.
|
|
54
|
+
*/ | {
|
|
55
|
+
/**
|
|
56
|
+
* The type of the term, which is `"metavar"` for a metavariable.
|
|
57
|
+
*/
|
|
58
|
+
readonly type: "metavar";
|
|
59
|
+
/**
|
|
60
|
+
* The metavariable name, which is a string that represents
|
|
61
|
+
* a variable in the message. For example, `"VALUE"` or `"ARG"`.
|
|
62
|
+
*/
|
|
63
|
+
readonly metavar: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A value term in the message, which can be a single value.
|
|
67
|
+
*/ | {
|
|
68
|
+
/**
|
|
69
|
+
* The type of the term, which is `"value"` for a single value.
|
|
70
|
+
*/
|
|
71
|
+
readonly type: "value";
|
|
72
|
+
/**
|
|
73
|
+
* The value, which can be any string representation of a value.
|
|
74
|
+
* For example, `"42"` or `"hello"`.
|
|
75
|
+
*/
|
|
76
|
+
readonly value: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A list of values term in the message, which can be a
|
|
80
|
+
* list of values.
|
|
81
|
+
*/ | {
|
|
82
|
+
/**
|
|
83
|
+
* The type of the term, which is `"values"` for a list of consecutive
|
|
84
|
+
* values.
|
|
85
|
+
*/
|
|
86
|
+
readonly type: "values";
|
|
87
|
+
/**
|
|
88
|
+
* The list of values, which can include multiple string
|
|
89
|
+
* representations of consecutive values. For example, `["42", "hello"]`.
|
|
90
|
+
*/
|
|
91
|
+
readonly values: readonly string[];
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Type representing a message that can include styled/colored values.
|
|
95
|
+
* This type is used to create structured messages that can be
|
|
96
|
+
* displayed to the user with specific formatting.
|
|
97
|
+
*/
|
|
98
|
+
type Message = readonly MessageTerm[];
|
|
99
|
+
/**
|
|
100
|
+
* Creates a structured message with template strings and values.
|
|
101
|
+
*
|
|
102
|
+
* This function allows creating messages where specific values can be
|
|
103
|
+
* highlighted or styled differently when displayed to the user.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const error = message`Expected number between ${min} and ${max}, got ${value}`;
|
|
108
|
+
* const concat = message`${optionName("--age")}: ${error}`;
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @param message Template strings array (from template literal).
|
|
112
|
+
* @param values Values to be interpolated into the template.
|
|
113
|
+
* @returns A structured Message object.
|
|
114
|
+
*/
|
|
115
|
+
declare function message(message: TemplateStringsArray, ...values: readonly (MessageTerm | Message | string)[]): Message;
|
|
116
|
+
/**
|
|
117
|
+
* Creates a {@link MessageTerm} for plain text. Usually used for
|
|
118
|
+
* dynamically generated messages.
|
|
119
|
+
* @param text The plain text to be included in the message.
|
|
120
|
+
* @returns A {@link MessageTerm} representing the plain text.
|
|
121
|
+
*/
|
|
122
|
+
declare function text(text: string): MessageTerm;
|
|
123
|
+
/**
|
|
124
|
+
* Creates a {@link MessageTerm} for an option name.
|
|
125
|
+
* @param name The name of the option, which can be a short or long option name.
|
|
126
|
+
* For example, `"-f"` or `"--foo"`.
|
|
127
|
+
* @returns A {@link MessageTerm} representing the option name.
|
|
128
|
+
*/
|
|
129
|
+
declare function optionName(name: string): MessageTerm;
|
|
130
|
+
/**
|
|
131
|
+
* Creates a {@link MessageTerm} for a list of option names.
|
|
132
|
+
* @param names The list of option names, which can include both short and long
|
|
133
|
+
* option names. For example, `["--foo", "--bar"]`.
|
|
134
|
+
* @returns A {@link MessageTerm} representing the list of option names.
|
|
135
|
+
*/
|
|
136
|
+
declare function optionNames(names: readonly string[]): MessageTerm;
|
|
137
|
+
/**
|
|
138
|
+
* Creates a {@link MessageTerm} for a metavariable.
|
|
139
|
+
* @param metavar The metavariable name, which is a string that represents
|
|
140
|
+
* a variable in the message. For example, `"VALUE"` or
|
|
141
|
+
* `"ARG"`.
|
|
142
|
+
* @returns A {@link MessageTerm} representing the metavariable.
|
|
143
|
+
*/
|
|
144
|
+
declare function metavar(metavar: string): MessageTerm;
|
|
145
|
+
/**
|
|
146
|
+
* Creates a {@link MessageTerm} for a single value. However, you usually
|
|
147
|
+
* don't need to use this function directly, as {@link message} string template
|
|
148
|
+
* will automatically create a {@link MessageTerm} for a value when
|
|
149
|
+
* you use a string in a template literal.
|
|
150
|
+
* @param value The value, which can be any string representation of a value.
|
|
151
|
+
* For example, `"42"` or `"hello"`.
|
|
152
|
+
* @returns A {@link MessageTerm} representing the value.
|
|
153
|
+
*/
|
|
154
|
+
declare function value(value: string): MessageTerm;
|
|
155
|
+
/**
|
|
156
|
+
* Creates a {@link MessageTerm} for a list of consecutive values.
|
|
157
|
+
* @param values The list of consecutive values, which can include multiple
|
|
158
|
+
* string representations of consecutive values.
|
|
159
|
+
* For example, `["42", "hello"]`.
|
|
160
|
+
* @returns A {@link MessageTerm} representing the list of values.
|
|
161
|
+
*/
|
|
162
|
+
declare function values(values: readonly string[]): MessageTerm;
|
|
163
|
+
/**
|
|
164
|
+
* Options for the {@link formatMessage} function.
|
|
165
|
+
*/
|
|
166
|
+
interface MessageFormatOptions {
|
|
167
|
+
/**
|
|
168
|
+
* Whether to use colors in the formatted message. If `true`,
|
|
169
|
+
* the formatted message will include ANSI escape codes for colors.
|
|
170
|
+
* If `false`, the message will be plain text without colors.
|
|
171
|
+
* @default `false`
|
|
172
|
+
*/
|
|
173
|
+
readonly colors?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Whether to use quotes around values in the formatted message.
|
|
176
|
+
* If `true`, values will be wrapped in quotes (e.g., `"value"`).
|
|
177
|
+
* If `false`, values will be displayed without quotes.
|
|
178
|
+
* @default `true`
|
|
179
|
+
*/
|
|
180
|
+
readonly quotes?: boolean;
|
|
181
|
+
/**
|
|
182
|
+
* The maximum width of the formatted message. If specified,
|
|
183
|
+
* the message will be wrapped to fit within this width.
|
|
184
|
+
* If not specified, the message will not be wrapped.
|
|
185
|
+
* @default `undefined`
|
|
186
|
+
*/
|
|
187
|
+
readonly maxWidth?: number;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Formats a {@link Message} into a human-readable string for
|
|
191
|
+
* the terminal.
|
|
192
|
+
* @param msg The message to format, which is an array of
|
|
193
|
+
* {@link MessageTerm} objects.
|
|
194
|
+
* @param options Optional formatting options to customize the output.
|
|
195
|
+
* @returns A formatted string representation of the message.
|
|
196
|
+
*/
|
|
197
|
+
declare function formatMessage(msg: Message, options?: MessageFormatOptions): string;
|
|
198
|
+
//#endregion
|
|
199
|
+
export { Message, MessageFormatOptions, MessageTerm, formatMessage, message, metavar, optionName, optionNames, text, value, values };
|