@reliverse/rempts 1.7.65 → 2.2.7
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/LICENSE +21 -21
- package/README.md +1534 -1431
- package/cleanup.mjs +33 -0
- package/dist/cancel.d.ts +31 -0
- package/dist/cancel.js +28 -0
- package/dist/ffi.d.ts +1 -0
- package/dist/ffi.js +165 -0
- package/dist/group.d.ts +16 -0
- package/dist/group.js +22 -0
- package/dist/launcher/command.d.ts +8 -0
- package/dist/launcher/command.js +10 -0
- package/dist/launcher/discovery.d.ts +3 -0
- package/dist/launcher/discovery.js +207 -0
- package/dist/launcher/errors.d.ts +15 -0
- package/dist/launcher/errors.js +31 -0
- package/dist/launcher/help.d.ts +3 -0
- package/dist/launcher/help.js +145 -0
- package/dist/launcher/mod.d.ts +12 -0
- package/dist/launcher/mod.js +222 -0
- package/dist/launcher/parser.d.ts +14 -0
- package/dist/launcher/parser.js +255 -0
- package/dist/launcher/registry.d.ts +10 -0
- package/dist/launcher/registry.js +42 -0
- package/dist/launcher/types.d.ts +78 -0
- package/dist/launcher/validator.d.ts +3 -0
- package/dist/launcher/validator.js +39 -0
- package/dist/mod.d.ts +6 -0
- package/dist/mod.js +6 -0
- package/dist/prompt.d.ts +13 -0
- package/dist/prompt.js +53 -0
- package/dist/selection.d.ts +92 -0
- package/dist/selection.js +191 -0
- package/dist/spinner.d.ts +26 -0
- package/dist/spinner.js +141 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +11 -0
- package/package.json +41 -47
- package/bin/libs/animate/animate-mod.ts.txt +0 -78
- package/bin/libs/anykey/anykey-mod.d.ts +0 -12
- package/bin/libs/anykey/anykey-mod.js +0 -125
- package/bin/libs/cancel/cancel.d.ts +0 -45
- package/bin/libs/cancel/cancel.js +0 -72
- package/bin/libs/confirm/confirm-alias.d.ts +0 -2
- package/bin/libs/confirm/confirm-alias.js +0 -2
- package/bin/libs/confirm/confirm-mod.d.ts +0 -5
- package/bin/libs/confirm/confirm-mod.js +0 -179
- package/bin/libs/date/date.d.ts +0 -2
- package/bin/libs/date/date.js +0 -254
- package/bin/libs/editor/editor-mod.d.ts +0 -25
- package/bin/libs/editor/editor-mod.js +0 -1133
- package/bin/libs/figures/figures-mod.d.ts +0 -461
- package/bin/libs/figures/figures-mod.js +0 -285
- package/bin/libs/group/group-mod.d.ts +0 -33
- package/bin/libs/group/group-mod.js +0 -89
- package/bin/libs/input/input-alias.d.ts +0 -5
- package/bin/libs/input/input-alias.js +0 -4
- package/bin/libs/input/input-mod.d.ts +0 -16
- package/bin/libs/input/input-mod.js +0 -370
- package/bin/libs/intro/intro-alias.d.ts +0 -3
- package/bin/libs/intro/intro-alias.js +0 -3
- package/bin/libs/intro/intro-mod.d.ts +0 -19
- package/bin/libs/intro/intro-mod.js +0 -71
- package/bin/libs/launcher/command-runner.d.ts +0 -31
- package/bin/libs/launcher/command-runner.js +0 -229
- package/bin/libs/launcher/launcher-alias.d.ts +0 -2
- package/bin/libs/launcher/launcher-alias.js +0 -2
- package/bin/libs/launcher/launcher-mod.d.ts +0 -66
- package/bin/libs/launcher/launcher-mod.js +0 -1037
- package/bin/libs/launcher/launcher-types.d.ts +0 -176
- package/bin/libs/launcher/launcher-types.js +0 -0
- package/bin/libs/log/log-alias.d.ts +0 -1
- package/bin/libs/log/log-alias.js +0 -2
- package/bin/libs/msg-fmt/colors.d.ts +0 -30
- package/bin/libs/msg-fmt/colors.js +0 -42
- package/bin/libs/msg-fmt/mapping.d.ts +0 -3
- package/bin/libs/msg-fmt/mapping.js +0 -41
- package/bin/libs/msg-fmt/messages.d.ts +0 -35
- package/bin/libs/msg-fmt/messages.js +0 -305
- package/bin/libs/msg-fmt/terminal.d.ts +0 -15
- package/bin/libs/msg-fmt/terminal.js +0 -60
- package/bin/libs/msg-fmt/variants.d.ts +0 -11
- package/bin/libs/msg-fmt/variants.js +0 -52
- package/bin/libs/multiselect/multiselect-alias.d.ts +0 -2
- package/bin/libs/multiselect/multiselect-alias.js +0 -2
- package/bin/libs/multiselect/multiselect-prompt.d.ts +0 -2
- package/bin/libs/multiselect/multiselect-prompt.js +0 -340
- package/bin/libs/next-steps/next-steps.d.ts +0 -13
- package/bin/libs/next-steps/next-steps.js +0 -24
- package/bin/libs/number/number-mod.d.ts +0 -28
- package/bin/libs/number/number-mod.js +0 -234
- package/bin/libs/outro/outro-alias.d.ts +0 -3
- package/bin/libs/outro/outro-alias.js +0 -3
- package/bin/libs/outro/outro-mod.d.ts +0 -7
- package/bin/libs/outro/outro-mod.js +0 -49
- package/bin/libs/reliarg/reliarg-mod.d.ts +0 -76
- package/bin/libs/reliarg/reliarg-mod.js +0 -276
- package/bin/libs/results/results.d.ts +0 -7
- package/bin/libs/results/results.js +0 -27
- package/bin/libs/select/nummultiselect-prompt.d.ts +0 -6
- package/bin/libs/select/nummultiselect-prompt.js +0 -141
- package/bin/libs/select/numselect-prompt.d.ts +0 -7
- package/bin/libs/select/numselect-prompt.js +0 -111
- package/bin/libs/select/select-alias.d.ts +0 -9
- package/bin/libs/select/select-alias.js +0 -9
- package/bin/libs/select/select-prompt.d.ts +0 -5
- package/bin/libs/select/select-prompt.js +0 -311
- package/bin/libs/select/toggle-prompt.d.ts +0 -5
- package/bin/libs/select/toggle-prompt.js +0 -207
- package/bin/libs/spinner/spinner-impl.d.ts +0 -70
- package/bin/libs/spinner/spinner-impl.js +0 -336
- package/bin/libs/spinner/spinner-mod.d.ts +0 -167
- package/bin/libs/spinner/spinner-mod.js +0 -447
- package/bin/libs/utils/colorize.d.ts +0 -2
- package/bin/libs/utils/colorize.js +0 -122
- package/bin/libs/utils/errors.d.ts +0 -1
- package/bin/libs/utils/errors.js +0 -17
- package/bin/libs/utils/prevent.d.ts +0 -8
- package/bin/libs/utils/prevent.js +0 -62
- package/bin/libs/utils/prompt-end.d.ts +0 -8
- package/bin/libs/utils/prompt-end.js +0 -36
- package/bin/libs/utils/stream-text.d.ts +0 -18
- package/bin/libs/utils/stream-text.js +0 -138
- package/bin/libs/utils/system.d.ts +0 -6
- package/bin/libs/utils/system.js +0 -7
- package/bin/libs/utils/validate.d.ts +0 -21
- package/bin/libs/utils/validate.js +0 -17
- package/bin/libs/visual/visual-mod.ts.txt +0 -19
- package/bin/mod.d.ts +0 -50
- package/bin/mod.js +0 -127
- package/bin/types.d.ts +0 -372
- /package/{bin → dist/launcher}/types.js +0 -0
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { stdin as input, stdout as output } from "node:process";
|
|
2
|
-
import readline from "node:readline/promises";
|
|
3
|
-
import { bar, msg } from "../msg-fmt/messages.js";
|
|
4
|
-
import { deleteLastLine, deleteLastLines } from "../msg-fmt/terminal.js";
|
|
5
|
-
async function askForInput(prompt) {
|
|
6
|
-
return new Promise((resolve) => {
|
|
7
|
-
let buffer = "";
|
|
8
|
-
process.stdout.write(prompt);
|
|
9
|
-
const onData = (data) => {
|
|
10
|
-
const str = data.toString("utf-8");
|
|
11
|
-
for (const char of str) {
|
|
12
|
-
if (char === "\n" || char === "\r") {
|
|
13
|
-
process.stdout.write("\n");
|
|
14
|
-
cleanup();
|
|
15
|
-
resolve(buffer);
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
if (char === "") {
|
|
19
|
-
cleanup();
|
|
20
|
-
resolve(null);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (char === "\x7F" || char === "\b") {
|
|
24
|
-
if (buffer.length > 0) {
|
|
25
|
-
buffer = buffer.slice(0, -1);
|
|
26
|
-
}
|
|
27
|
-
redrawPrompt(buffer, prompt);
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
buffer += char;
|
|
31
|
-
redrawPrompt(buffer, prompt);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
process.stdin.on("data", onData);
|
|
35
|
-
const cleanup = () => {
|
|
36
|
-
process.stdin.removeListener("data", onData);
|
|
37
|
-
};
|
|
38
|
-
const redrawPrompt = (inputBuffer, textPrompt) => {
|
|
39
|
-
process.stdout.clearLine(0);
|
|
40
|
-
process.stdout.cursorTo(0);
|
|
41
|
-
process.stdout.write(textPrompt + inputBuffer);
|
|
42
|
-
};
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
function renderPromptUI(params) {
|
|
46
|
-
const {
|
|
47
|
-
title,
|
|
48
|
-
hint,
|
|
49
|
-
content,
|
|
50
|
-
contentColor = "dim",
|
|
51
|
-
contentTypography = "italic",
|
|
52
|
-
contentVariant,
|
|
53
|
-
titleColor = "cyan",
|
|
54
|
-
titleTypography = "none",
|
|
55
|
-
titleVariant,
|
|
56
|
-
borderColor = "dim",
|
|
57
|
-
userInput,
|
|
58
|
-
errorMessage
|
|
59
|
-
} = params;
|
|
60
|
-
let lineCount = 0;
|
|
61
|
-
const type = errorMessage !== "" ? "M_ERROR" : "M_GENERAL";
|
|
62
|
-
const msgParams = {
|
|
63
|
-
type,
|
|
64
|
-
title,
|
|
65
|
-
titleColor,
|
|
66
|
-
titleTypography,
|
|
67
|
-
titleVariant: titleVariant ?? "none",
|
|
68
|
-
content: content ?? "",
|
|
69
|
-
contentColor,
|
|
70
|
-
contentTypography,
|
|
71
|
-
contentVariant: contentVariant ?? "none",
|
|
72
|
-
borderColor,
|
|
73
|
-
hint: hint ?? "",
|
|
74
|
-
errorMessage
|
|
75
|
-
};
|
|
76
|
-
msg(msgParams);
|
|
77
|
-
lineCount++;
|
|
78
|
-
if (userInput !== "") {
|
|
79
|
-
msg({ type: "M_MIDDLE", title: ` ${userInput}` });
|
|
80
|
-
lineCount++;
|
|
81
|
-
}
|
|
82
|
-
return lineCount;
|
|
83
|
-
}
|
|
84
|
-
export async function numberPrompt(opts) {
|
|
85
|
-
const {
|
|
86
|
-
title = "",
|
|
87
|
-
hint,
|
|
88
|
-
hintPlaceholderColor = "blue",
|
|
89
|
-
validate,
|
|
90
|
-
defaultValue,
|
|
91
|
-
titleColor = "cyan",
|
|
92
|
-
titleTypography = "none",
|
|
93
|
-
titleVariant,
|
|
94
|
-
content,
|
|
95
|
-
contentColor = "dim",
|
|
96
|
-
contentTypography = "italic",
|
|
97
|
-
contentVariant,
|
|
98
|
-
borderColor = "dim",
|
|
99
|
-
hardcoded,
|
|
100
|
-
endTitle = "",
|
|
101
|
-
endTitleColor = "dim",
|
|
102
|
-
border = true
|
|
103
|
-
} = opts;
|
|
104
|
-
const rl = readline.createInterface({ input, output });
|
|
105
|
-
rl.on("SIGINT", () => {
|
|
106
|
-
if (endTitle !== "") {
|
|
107
|
-
msg({
|
|
108
|
-
type: "M_END",
|
|
109
|
-
title: endTitle,
|
|
110
|
-
titleColor: endTitleColor,
|
|
111
|
-
titleTypography,
|
|
112
|
-
border,
|
|
113
|
-
borderColor
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
rl.close();
|
|
117
|
-
process.exit(0);
|
|
118
|
-
});
|
|
119
|
-
let currentInput = hardcoded?.userInput || "";
|
|
120
|
-
let errorMessage = hardcoded?.errorMessage || "";
|
|
121
|
-
let isRerender = false;
|
|
122
|
-
let lastLineCount = 0;
|
|
123
|
-
const effectiveDefault = typeof defaultValue === "string" ? Number(defaultValue) : defaultValue;
|
|
124
|
-
if (hardcoded?.userInput !== void 0) {
|
|
125
|
-
renderPromptUI({
|
|
126
|
-
title,
|
|
127
|
-
hint,
|
|
128
|
-
hintPlaceholderColor,
|
|
129
|
-
content,
|
|
130
|
-
contentColor,
|
|
131
|
-
contentTypography,
|
|
132
|
-
contentVariant,
|
|
133
|
-
titleColor,
|
|
134
|
-
titleTypography,
|
|
135
|
-
titleVariant,
|
|
136
|
-
borderColor,
|
|
137
|
-
userInput: currentInput,
|
|
138
|
-
errorMessage,
|
|
139
|
-
border,
|
|
140
|
-
isRerender
|
|
141
|
-
});
|
|
142
|
-
const num = Number(currentInput);
|
|
143
|
-
if (Number.isNaN(num)) {
|
|
144
|
-
throw new Error("Please enter a valid number.");
|
|
145
|
-
}
|
|
146
|
-
const validated = await validateInput(num, validate);
|
|
147
|
-
if (validated.isValid) {
|
|
148
|
-
msg({ type: "M_MIDDLE", title: ` ${num}` });
|
|
149
|
-
msg({ type: "M_BAR", borderColor });
|
|
150
|
-
rl.close();
|
|
151
|
-
return num;
|
|
152
|
-
}
|
|
153
|
-
rl.close();
|
|
154
|
-
throw new Error(validated.errorMessage || "Invalid input.");
|
|
155
|
-
}
|
|
156
|
-
while (true) {
|
|
157
|
-
if (isRerender) {
|
|
158
|
-
deleteLastLines(lastLineCount + 1);
|
|
159
|
-
}
|
|
160
|
-
lastLineCount = renderPromptUI({
|
|
161
|
-
title,
|
|
162
|
-
hint,
|
|
163
|
-
hintPlaceholderColor,
|
|
164
|
-
content,
|
|
165
|
-
contentColor,
|
|
166
|
-
contentTypography,
|
|
167
|
-
contentVariant,
|
|
168
|
-
titleColor,
|
|
169
|
-
titleTypography,
|
|
170
|
-
titleVariant,
|
|
171
|
-
borderColor,
|
|
172
|
-
userInput: currentInput,
|
|
173
|
-
errorMessage,
|
|
174
|
-
border,
|
|
175
|
-
isRerender
|
|
176
|
-
});
|
|
177
|
-
const formattedBar = bar({ borderColor });
|
|
178
|
-
const answerInput = await askForInput(`${formattedBar} `);
|
|
179
|
-
isRerender = true;
|
|
180
|
-
if (answerInput === null) {
|
|
181
|
-
if (endTitle !== "") {
|
|
182
|
-
msg({
|
|
183
|
-
type: "M_END",
|
|
184
|
-
title: endTitle,
|
|
185
|
-
titleColor: endTitleColor,
|
|
186
|
-
titleTypography,
|
|
187
|
-
border,
|
|
188
|
-
borderColor
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
rl.close();
|
|
192
|
-
process.exit(0);
|
|
193
|
-
}
|
|
194
|
-
currentInput = answerInput.trim();
|
|
195
|
-
if (!currentInput && effectiveDefault !== void 0) {
|
|
196
|
-
deleteLastLine();
|
|
197
|
-
msg({ type: "M_MIDDLE", title: ` ${effectiveDefault}` });
|
|
198
|
-
msg({ type: "M_BAR", borderColor });
|
|
199
|
-
rl.close();
|
|
200
|
-
return effectiveDefault;
|
|
201
|
-
}
|
|
202
|
-
const num = Number(currentInput);
|
|
203
|
-
if (Number.isNaN(num)) {
|
|
204
|
-
errorMessage = "Please enter a valid number.";
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
const validated = await validateInput(num, validate);
|
|
208
|
-
if (validated.isValid) {
|
|
209
|
-
deleteLastLine();
|
|
210
|
-
msg({ type: "M_MIDDLE", title: ` ${num}` });
|
|
211
|
-
msg({ type: "M_BAR", borderColor });
|
|
212
|
-
rl.close();
|
|
213
|
-
return num;
|
|
214
|
-
}
|
|
215
|
-
errorMessage = validated.errorMessage;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
async function validateInput(input2, validate) {
|
|
219
|
-
let isValid = true;
|
|
220
|
-
let errorMessage = "";
|
|
221
|
-
if (validate && isValid) {
|
|
222
|
-
const validationResult = await validate(input2);
|
|
223
|
-
if (validationResult === true || validationResult === void 0) {
|
|
224
|
-
isValid = true;
|
|
225
|
-
} else if (typeof validationResult === "string") {
|
|
226
|
-
isValid = false;
|
|
227
|
-
errorMessage = validationResult;
|
|
228
|
-
} else {
|
|
229
|
-
isValid = false;
|
|
230
|
-
errorMessage = "Invalid input.";
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
return { isValid, errorMessage };
|
|
234
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { msg } from "../msg-fmt/messages.js";
|
|
2
|
-
import { getExactTerminalWidth } from "../msg-fmt/terminal.js";
|
|
3
|
-
export async function outroPrompt(optionsOrTitle) {
|
|
4
|
-
const options = typeof optionsOrTitle === "string" ? { title: optionsOrTitle } : optionsOrTitle;
|
|
5
|
-
const {
|
|
6
|
-
title = "",
|
|
7
|
-
titleColor = "cyan",
|
|
8
|
-
titleTypography = "none",
|
|
9
|
-
titleVariant,
|
|
10
|
-
titleAnimation,
|
|
11
|
-
titleAnimationDelay,
|
|
12
|
-
border = true,
|
|
13
|
-
borderColor = "dim",
|
|
14
|
-
horizontalLineLength: initialHorizontalLineLength = 0,
|
|
15
|
-
variant = "footer"
|
|
16
|
-
// asciiArtFont,
|
|
17
|
-
} = options;
|
|
18
|
-
let horizontalLineLength = initialHorizontalLineLength;
|
|
19
|
-
if (variant === "ascii-art") {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
if (horizontalLineLength === 0) {
|
|
23
|
-
horizontalLineLength = getExactTerminalWidth() - 3;
|
|
24
|
-
}
|
|
25
|
-
if (titleAnimation) {
|
|
26
|
-
await msg({
|
|
27
|
-
title: title ? title : " ",
|
|
28
|
-
// anim: titleAnimation,
|
|
29
|
-
...titleAnimationDelay !== void 0 && { delay: titleAnimationDelay },
|
|
30
|
-
type: "M_END",
|
|
31
|
-
titleColor,
|
|
32
|
-
titleTypography,
|
|
33
|
-
border,
|
|
34
|
-
borderColor,
|
|
35
|
-
horizontalLineLength
|
|
36
|
-
});
|
|
37
|
-
} else {
|
|
38
|
-
msg({
|
|
39
|
-
type: "M_END",
|
|
40
|
-
title: title ? title : " ",
|
|
41
|
-
titleColor,
|
|
42
|
-
titleTypography,
|
|
43
|
-
...titleVariant !== void 0 && { titleVariant },
|
|
44
|
-
border,
|
|
45
|
-
borderColor,
|
|
46
|
-
horizontalLineLength
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Options passed to the ReliArgParser.
|
|
3
|
-
*/
|
|
4
|
-
export type ReliArgParserOptions = {
|
|
5
|
-
/**
|
|
6
|
-
* A list of flags that should be treated as booleans.
|
|
7
|
-
*/
|
|
8
|
-
boolean?: string[];
|
|
9
|
-
/**
|
|
10
|
-
* Whether to print a warning if an unknown flag is encountered.
|
|
11
|
-
*/
|
|
12
|
-
warnOnUnknown?: boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Whether encountering an unknown flag should cause a thrown error.
|
|
15
|
-
*/
|
|
16
|
-
strict?: boolean;
|
|
17
|
-
/**
|
|
18
|
-
* An object that maps a flag to one or more aliases (e.g. { force: "f" } or { f: ["force", "fast"] }).
|
|
19
|
-
*/
|
|
20
|
-
alias?: Record<string, string | string[]>;
|
|
21
|
-
/**
|
|
22
|
-
* Default values for flags (e.g. { force: false, count: 0 }).
|
|
23
|
-
*/
|
|
24
|
-
defaults?: Record<string, unknown>;
|
|
25
|
-
/**
|
|
26
|
-
* If true, allows --no-foo to set foo=false (negated booleans).
|
|
27
|
-
* Defaults to `true`.
|
|
28
|
-
*/
|
|
29
|
-
negatedBoolean?: boolean;
|
|
30
|
-
/**
|
|
31
|
-
* A callback to determine if a flag is considered "unknown."
|
|
32
|
-
* If it returns false, the parser may warn or throw, depending on config.
|
|
33
|
-
*/
|
|
34
|
-
unknown?: (flagName: string) => boolean;
|
|
35
|
-
/**
|
|
36
|
-
* If true, attempt to parse non-boolean string values as numbers when possible.
|
|
37
|
-
* Defaults to false.
|
|
38
|
-
*/
|
|
39
|
-
parseNumbers?: boolean;
|
|
40
|
-
/**
|
|
41
|
-
* If true, negative numbers like -123 will not be treated as short flags
|
|
42
|
-
* and will be parsed as positional values.
|
|
43
|
-
* Defaults to true (commonly desired).
|
|
44
|
-
*/
|
|
45
|
-
allowNegativeNumbers?: boolean;
|
|
46
|
-
/**
|
|
47
|
-
* If true, stop parsing flags at the first positional argument,
|
|
48
|
-
* pushing all subsequent arguments into result._.
|
|
49
|
-
* Defaults to false.
|
|
50
|
-
*/
|
|
51
|
-
stopEarly?: boolean;
|
|
52
|
-
/**
|
|
53
|
-
* A list of flags that should be collected as arrays if repeated.
|
|
54
|
-
* e.g. { array: ["include"] } => --include file1 --include file2
|
|
55
|
-
* results in { include: ["file1", "file2"] }
|
|
56
|
-
*/
|
|
57
|
-
array?: string[];
|
|
58
|
-
/**
|
|
59
|
-
* A list of flags that should always be treated as strings (ignoring parseNumbers).
|
|
60
|
-
*/
|
|
61
|
-
string?: string[];
|
|
62
|
-
};
|
|
63
|
-
/**
|
|
64
|
-
* The result of parsing command-line arguments.
|
|
65
|
-
*/
|
|
66
|
-
export type ReliArgParserResult = {
|
|
67
|
-
/**
|
|
68
|
-
* Positional arguments (non-flag arguments).
|
|
69
|
-
*/
|
|
70
|
-
_: string[];
|
|
71
|
-
[key: string]: unknown;
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* Parse arguments according to the provided `ReliArgParserOptions`.
|
|
75
|
-
*/
|
|
76
|
-
export declare function reliArgParser(argv: string[], opts?: ReliArgParserOptions): ReliArgParserResult;
|
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
const NEGATIVE_NUMBER_REGEX = /^[0-9]+(\.[0-9]+)?$/;
|
|
2
|
-
function isAsciiLetter(ch) {
|
|
3
|
-
const code = ch.charCodeAt(0);
|
|
4
|
-
return code >= 65 && code <= 90 || code >= 97 && code <= 122;
|
|
5
|
-
}
|
|
6
|
-
export function reliArgParser(argv, opts = {}) {
|
|
7
|
-
const context = createParserContext(opts);
|
|
8
|
-
prePopulateDefaults(context);
|
|
9
|
-
let i = 0;
|
|
10
|
-
const argvLength = argv.length;
|
|
11
|
-
while (i < argvLength) {
|
|
12
|
-
const arg = argv[i];
|
|
13
|
-
if (!arg) {
|
|
14
|
-
i++;
|
|
15
|
-
continue;
|
|
16
|
-
}
|
|
17
|
-
if (context.stopEarlyTriggered) {
|
|
18
|
-
context.result._.push(arg);
|
|
19
|
-
i++;
|
|
20
|
-
continue;
|
|
21
|
-
}
|
|
22
|
-
if (arg === "--") {
|
|
23
|
-
context.result._.push(...argv.slice(i + 1));
|
|
24
|
-
break;
|
|
25
|
-
}
|
|
26
|
-
if (context.allowNegativeNumbers && isNegativeNumber(arg)) {
|
|
27
|
-
if (context.stopEarly) {
|
|
28
|
-
context.result._.push(...argv.slice(i));
|
|
29
|
-
break;
|
|
30
|
-
} else {
|
|
31
|
-
context.result._.push(arg);
|
|
32
|
-
i++;
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (shouldHandleNegatedBoolean(arg, context)) {
|
|
37
|
-
handleNegatedBooleanFlag(arg, context);
|
|
38
|
-
i++;
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
if (arg.length > 2 && arg[0] === "-" && arg[1] === "-") {
|
|
42
|
-
i = handleLongFlag(argv, i, context);
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
if (arg.length > 1 && arg[0] === "-" && arg[1] !== "-") {
|
|
46
|
-
i = handleShortFlags(argv, i, context);
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
if (context.stopEarly) {
|
|
50
|
-
context.result._.push(...argv.slice(i));
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
context.result._.push(arg);
|
|
54
|
-
i++;
|
|
55
|
-
}
|
|
56
|
-
return context.result;
|
|
57
|
-
}
|
|
58
|
-
function createParserContext(opts) {
|
|
59
|
-
const booleanFlags = new Set(opts.boolean || []);
|
|
60
|
-
const arrayFlags = new Set(opts.array || []);
|
|
61
|
-
const stringFlags = new Set(opts.string || []);
|
|
62
|
-
const aliasMap = buildAliasMap(opts.alias || {});
|
|
63
|
-
const result = { _: [] };
|
|
64
|
-
return {
|
|
65
|
-
result,
|
|
66
|
-
booleanFlags,
|
|
67
|
-
arrayFlags,
|
|
68
|
-
stringFlags,
|
|
69
|
-
aliasMap,
|
|
70
|
-
options: opts,
|
|
71
|
-
stopEarlyTriggered: false,
|
|
72
|
-
// Cache frequently accessed options for performance
|
|
73
|
-
allowNegativeNumbers: opts.allowNegativeNumbers !== false,
|
|
74
|
-
negatedBoolean: opts.negatedBoolean !== false,
|
|
75
|
-
parseNumbers: opts.parseNumbers || false,
|
|
76
|
-
stopEarly: opts.stopEarly || false,
|
|
77
|
-
warnOnUnknown: opts.warnOnUnknown || false,
|
|
78
|
-
strict: opts.strict || false
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
function prePopulateDefaults(context) {
|
|
82
|
-
const { defaults = {} } = context.options;
|
|
83
|
-
for (const [key, val] of Object.entries(defaults)) {
|
|
84
|
-
context.result[key] = val;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
function buildAliasMap(aliasObj) {
|
|
88
|
-
const map = /* @__PURE__ */ new Map();
|
|
89
|
-
for (const [key, value] of Object.entries(aliasObj)) {
|
|
90
|
-
if (Array.isArray(value)) {
|
|
91
|
-
const canonical = value[0];
|
|
92
|
-
if (canonical) {
|
|
93
|
-
map.set(key, canonical);
|
|
94
|
-
for (const alias of value) {
|
|
95
|
-
map.set(alias, canonical);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
} else {
|
|
99
|
-
map.set(key, value);
|
|
100
|
-
map.set(value, value);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return map;
|
|
104
|
-
}
|
|
105
|
-
function isNegativeNumber(arg) {
|
|
106
|
-
if (arg.length < 2 || arg[0] !== "-") return false;
|
|
107
|
-
if (arg[1] === "-") return false;
|
|
108
|
-
return NEGATIVE_NUMBER_REGEX.test(arg.slice(1));
|
|
109
|
-
}
|
|
110
|
-
function shouldHandleNegatedBoolean(arg, context) {
|
|
111
|
-
return context.negatedBoolean && arg.length > 5 && arg[0] === "-" && arg[1] === "-" && arg[2] === "n" && arg[3] === "o" && arg[4] === "-";
|
|
112
|
-
}
|
|
113
|
-
function handleNegatedBooleanFlag(arg, context) {
|
|
114
|
-
const flagName = arg.slice(5);
|
|
115
|
-
const resolvedName = resolveFlagName(flagName, context);
|
|
116
|
-
if (context.booleanFlags.has(resolvedName)) {
|
|
117
|
-
setFlagValue(resolvedName, false, context);
|
|
118
|
-
} else {
|
|
119
|
-
handleUnknownFlag(resolvedName, context);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
function handleLongFlag(argv, index, context) {
|
|
123
|
-
const arg = argv[index];
|
|
124
|
-
if (arg === void 0) {
|
|
125
|
-
return index + 1;
|
|
126
|
-
}
|
|
127
|
-
const flagBody = arg.slice(2);
|
|
128
|
-
const eqIndex = flagBody.indexOf("=");
|
|
129
|
-
let rawFlagName = flagBody;
|
|
130
|
-
let value = true;
|
|
131
|
-
let nextIndex = index;
|
|
132
|
-
if (eqIndex !== -1) {
|
|
133
|
-
rawFlagName = flagBody.slice(0, eqIndex);
|
|
134
|
-
value = flagBody.slice(eqIndex + 1);
|
|
135
|
-
} else {
|
|
136
|
-
const nextArg = argv[index + 1];
|
|
137
|
-
if (nextArg && nextArg.length > 0 && nextArg[0] !== "-") {
|
|
138
|
-
value = nextArg;
|
|
139
|
-
nextIndex++;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
const flagName = resolveFlagName(rawFlagName, context);
|
|
143
|
-
const parsedValue = parseValue(value, flagName, context);
|
|
144
|
-
setFlagValue(flagName, parsedValue, context);
|
|
145
|
-
validateOrWarn(flagName, context);
|
|
146
|
-
return nextIndex + 1;
|
|
147
|
-
}
|
|
148
|
-
function handleShortFlags(argv, index, context) {
|
|
149
|
-
const arg = argv[index];
|
|
150
|
-
if (arg === void 0) {
|
|
151
|
-
return index + 1;
|
|
152
|
-
}
|
|
153
|
-
const shortFlags = arg.slice(1);
|
|
154
|
-
const booleanFlags = context.booleanFlags;
|
|
155
|
-
const aliasMap = context.aliasMap;
|
|
156
|
-
const shortFlagsLength = shortFlags.length;
|
|
157
|
-
let j = 0;
|
|
158
|
-
while (j < shortFlagsLength) {
|
|
159
|
-
const flagChar = shortFlags[j];
|
|
160
|
-
if (!flagChar) {
|
|
161
|
-
j++;
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
const resolvedName = aliasMap.get(flagChar) || flagChar;
|
|
165
|
-
const nextChar = shortFlags[j + 1];
|
|
166
|
-
if (booleanFlags.has(resolvedName)) {
|
|
167
|
-
if (nextChar === "=") {
|
|
168
|
-
const attachedValue = shortFlags.slice(j + 2);
|
|
169
|
-
const parsed = parseValue(attachedValue, resolvedName, context);
|
|
170
|
-
setFlagValue(resolvedName, parsed, context);
|
|
171
|
-
j = shortFlagsLength;
|
|
172
|
-
} else if (nextChar && !isAsciiLetter(nextChar)) {
|
|
173
|
-
const attachedValue = shortFlags.slice(j + 1);
|
|
174
|
-
const parsed = parseValue(attachedValue, resolvedName, context);
|
|
175
|
-
setFlagValue(resolvedName, parsed, context);
|
|
176
|
-
j = shortFlagsLength;
|
|
177
|
-
} else {
|
|
178
|
-
setFlagValue(resolvedName, true, context);
|
|
179
|
-
j++;
|
|
180
|
-
}
|
|
181
|
-
} else {
|
|
182
|
-
const remainder = shortFlags.slice(j + 1);
|
|
183
|
-
if (remainder.startsWith("=")) {
|
|
184
|
-
const val = remainder.slice(1);
|
|
185
|
-
const parsedVal = parseValue(val, resolvedName, context);
|
|
186
|
-
setFlagValue(resolvedName, parsedVal, context);
|
|
187
|
-
validateOrWarn(resolvedName, context);
|
|
188
|
-
j = shortFlagsLength;
|
|
189
|
-
} else if (remainder.length > 0) {
|
|
190
|
-
const parsedVal = parseValue(remainder, resolvedName, context);
|
|
191
|
-
setFlagValue(resolvedName, parsedVal, context);
|
|
192
|
-
validateOrWarn(resolvedName, context);
|
|
193
|
-
j = shortFlagsLength;
|
|
194
|
-
} else {
|
|
195
|
-
const nextArgIndex = index + 1;
|
|
196
|
-
if (nextArgIndex < argv.length) {
|
|
197
|
-
const nextArg = argv[nextArgIndex];
|
|
198
|
-
if (nextArg && nextArg.length > 0 && nextArg[0] !== "-") {
|
|
199
|
-
const parsedVal = parseValue(nextArg, resolvedName, context);
|
|
200
|
-
setFlagValue(resolvedName, parsedVal, context);
|
|
201
|
-
validateOrWarn(resolvedName, context);
|
|
202
|
-
return index + 2;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
setFlagValue(resolvedName, true, context);
|
|
206
|
-
validateOrWarn(resolvedName, context);
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return index + 1;
|
|
212
|
-
}
|
|
213
|
-
function resolveFlagName(rawName, context) {
|
|
214
|
-
return context.aliasMap.get(rawName) || rawName;
|
|
215
|
-
}
|
|
216
|
-
function setFlagValue(flagName, value, context) {
|
|
217
|
-
if (context.arrayFlags.has(flagName)) {
|
|
218
|
-
if (!Array.isArray(context.result[flagName])) {
|
|
219
|
-
context.result[flagName] = [];
|
|
220
|
-
}
|
|
221
|
-
context.result[flagName].push(value);
|
|
222
|
-
} else {
|
|
223
|
-
context.result[flagName] = value;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
function parseValue(val, flagName, context) {
|
|
227
|
-
if (typeof val === "boolean") {
|
|
228
|
-
return val;
|
|
229
|
-
}
|
|
230
|
-
const strVal = String(val);
|
|
231
|
-
if (context.stringFlags.has(flagName)) {
|
|
232
|
-
return strVal;
|
|
233
|
-
}
|
|
234
|
-
if (context.booleanFlags.has(flagName)) {
|
|
235
|
-
if (strVal === "true") return true;
|
|
236
|
-
if (strVal === "false") return false;
|
|
237
|
-
return strVal;
|
|
238
|
-
}
|
|
239
|
-
if (context.parseNumbers && strVal.length > 0) {
|
|
240
|
-
const firstChar = strVal.charAt(0);
|
|
241
|
-
const lastChar = strVal.charAt(strVal.length - 1);
|
|
242
|
-
if (firstChar >= "0" && firstChar <= "9" || firstChar === "-" || firstChar === "+") {
|
|
243
|
-
if (lastChar >= "0" && lastChar <= "9") {
|
|
244
|
-
const maybeNum = Number(strVal);
|
|
245
|
-
if (!Number.isNaN(maybeNum)) {
|
|
246
|
-
return maybeNum;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
return strVal;
|
|
252
|
-
}
|
|
253
|
-
function validateOrWarn(flagName, context) {
|
|
254
|
-
const { unknown, defaults } = context.options;
|
|
255
|
-
if (unknown) {
|
|
256
|
-
if (!unknown(flagName)) {
|
|
257
|
-
handleUnknownFlag(flagName, context);
|
|
258
|
-
}
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
if (context.booleanFlags.has(flagName) || context.arrayFlags.has(flagName) || context.stringFlags.has(flagName) || context.aliasMap.has(flagName)) {
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
if (defaults && Object.hasOwn(defaults, flagName)) {
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
handleUnknownFlag(flagName, context);
|
|
268
|
-
}
|
|
269
|
-
function handleUnknownFlag(flagName, context) {
|
|
270
|
-
if (context.strict) {
|
|
271
|
-
throw new Error(`Unknown flag: --${flagName}`);
|
|
272
|
-
}
|
|
273
|
-
if (context.warnOnUnknown) {
|
|
274
|
-
console.warn(`Unknown flag: --${flagName}`);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { msg } from "../msg-fmt/messages.js";
|
|
2
|
-
export async function resultPrompt({
|
|
3
|
-
results,
|
|
4
|
-
inline = true
|
|
5
|
-
}) {
|
|
6
|
-
const title = "Your input results:";
|
|
7
|
-
if (inline) {
|
|
8
|
-
const formattedResults = Object.entries(results).map(([key, value]) => `${key}: ${value}`).join(", ");
|
|
9
|
-
msg({
|
|
10
|
-
type: "M_INFO",
|
|
11
|
-
title,
|
|
12
|
-
content: formattedResults,
|
|
13
|
-
titleColor: "cyan",
|
|
14
|
-
contentColor: "dim",
|
|
15
|
-
wrapContent: true
|
|
16
|
-
});
|
|
17
|
-
} else {
|
|
18
|
-
const formattedResults = JSON.stringify(results, null, 2);
|
|
19
|
-
msg({
|
|
20
|
-
type: "M_INFO",
|
|
21
|
-
title,
|
|
22
|
-
content: formattedResults,
|
|
23
|
-
titleColor: "cyan",
|
|
24
|
-
contentColor: "dim"
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
}
|