@twin.org/cli-core 0.0.3-next.8 → 0.0.3
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 +2 -2
- package/dist/es/cliDisplay.js +23 -3
- package/dist/es/cliDisplay.js.map +1 -1
- package/dist/es/cliParam.js +3 -3
- package/dist/es/cliParam.js.map +1 -1
- package/dist/types/cliDisplay.d.ts +7 -1
- package/docs/changelog.md +858 -120
- package/docs/examples.md +82 -1
- package/docs/reference/classes/CLIBase.md +3 -3
- package/docs/reference/classes/CLIDisplay.md +43 -27
- package/docs/reference/classes/CLIOptions.md +1 -1
- package/docs/reference/classes/CLIParam.md +56 -56
- package/docs/reference/classes/CLIUtils.md +17 -17
- package/docs/reference/interfaces/ICliOptions.md +12 -12
- package/docs/reference/interfaces/ICliOutputOptionsConsole.md +1 -1
- package/docs/reference/interfaces/ICliOutputOptionsEnv.md +3 -3
- package/docs/reference/interfaces/ICliOutputOptionsJson.md +3 -3
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# TWIN CLI Core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This package is part of the framework workspace and provides core classes for building a CLI to support consistent development workflows across the ecosystem.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -10,7 +10,7 @@ npm install @twin.org/cli-core
|
|
|
10
10
|
|
|
11
11
|
## Examples
|
|
12
12
|
|
|
13
|
-
Usage of the
|
|
13
|
+
Usage of the APIs is shown in the examples [docs/examples.md](docs/examples.md)
|
|
14
14
|
|
|
15
15
|
## Reference
|
|
16
16
|
|
package/dist/es/cliDisplay.js
CHANGED
|
@@ -45,15 +45,35 @@ export class CLIDisplay {
|
|
|
45
45
|
* Display an error message.
|
|
46
46
|
* @param error The error to display.
|
|
47
47
|
* @param lineBreaks Whether to add a line break after the error.
|
|
48
|
+
* @param options Options for formatting the error.
|
|
49
|
+
* @param options.includeStack Whether to include the stack trace in the output, defaults to false.
|
|
50
|
+
* @param options.includeAdditional Whether to include additional error information in the output, defaults to false.
|
|
48
51
|
*/
|
|
49
|
-
static error(error, lineBreaks = true) {
|
|
52
|
+
static error(error, lineBreaks = true, options = {}) {
|
|
50
53
|
CLIDisplay.writeError("❗ ");
|
|
51
54
|
CLIDisplay.writeError(chalk.red(I18n.formatMessage("cli.progress.error")));
|
|
52
55
|
if (lineBreaks) {
|
|
53
56
|
CLIDisplay.writeError("\n");
|
|
54
57
|
}
|
|
55
|
-
const formatted = ErrorHelper.
|
|
56
|
-
CLIDisplay.writeError(chalk.red(formatted.map(e => `\t${e}`).join("\n")));
|
|
58
|
+
const formatted = ErrorHelper.localizeErrors(error);
|
|
59
|
+
CLIDisplay.writeError(chalk.red(formatted.map(e => `\t${e.message}`).join("\n")));
|
|
60
|
+
if (options.includeAdditional) {
|
|
61
|
+
for (const err of formatted) {
|
|
62
|
+
if (Is.arrayValue(err.additional)) {
|
|
63
|
+
CLIDisplay.writeError(chalk.red(`\n\t\t${err.additional.join("\n\t\t")}\n`));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (options.includeStack) {
|
|
68
|
+
CLIDisplay.writeError("\n");
|
|
69
|
+
CLIDisplay.writeError("\n-----DEBUG STACK START-----\n\n");
|
|
70
|
+
for (const err of formatted) {
|
|
71
|
+
if (Is.stringValue(err.stack)) {
|
|
72
|
+
CLIDisplay.writeError(`${err.stack}\n`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
CLIDisplay.writeError("\n-----DEBUG STACK END-----\n");
|
|
76
|
+
}
|
|
57
77
|
if (lineBreaks) {
|
|
58
78
|
CLIDisplay.writeError("\n");
|
|
59
79
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cliDisplay.js","sourceRoot":"","sources":["../../src/cliDisplay.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,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ACxB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { clearLine, cursorTo } from \"node:readline\";\nimport { inspect } from \"node:util\";\nimport { Coerce, ErrorHelper, I18n, Is } from \"@twin.org/core\";\nimport chalk from \"chalk\";\n\n/**\n * Display utilities for the CLI.\n */\nexport class CLIDisplay {\n\t/**\n\t * The interval ID for the spinner.\n\t * @internal\n\t */\n\tprivate static _spinnerIntervalId: NodeJS.Timeout | number | undefined;\n\n\t/**\n\t * The default output method for writing standard messages.\n\t * @param buffer The message to output.\n\t */\n\tpublic static write: (buffer: string | Uint8Array) => void = buffer =>\n\t\tprocess.stdout.write(buffer);\n\n\t/**\n\t * The default output method for writing error messages.\n\t * @param buffer The message to output.\n\t */\n\tpublic static writeError: (buffer: string | Uint8Array) => void = buffer =>\n\t\tprocess.stderr.write(buffer);\n\n\t/**\n\t * The default output method for clearing the current line.\n\t */\n\tpublic static clearLine: () => void = () => {\n\t\tclearLine(process.stdout, 0);\n\t\tcursorTo(process.stdout, 0);\n\t};\n\n\t/**\n\t * Display the header for the CLI.\n\t * @param title The title of the CLI.\n\t * @param version The version of the CLI.\n\t * @param icon The icon for the CLI.\n\t */\n\tpublic static header(title: string, version: string, icon: string): void {\n\t\tconst titleVersion = `${title} v${version}`;\n\t\tCLIDisplay.write(`${icon} ${chalk.underline.bold.blue(titleVersion)}\\n`);\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display an error message.\n\t * @param error The error to display.\n\t * @param lineBreaks Whether to add a line break after the error.\n\t */\n\tpublic static error(error: unknown, lineBreaks: boolean = true): void {\n\t\tCLIDisplay.writeError(\"❗ \");\n\t\tCLIDisplay.writeError(chalk.red(I18n.formatMessage(\"cli.progress.error\")));\n\t\tif (lineBreaks) {\n\t\t\tCLIDisplay.writeError(\"\\n\");\n\t\t}\n\n\t\tconst formatted = ErrorHelper.formatErrors(error, true);\n\t\tCLIDisplay.writeError(chalk.red(formatted.map(e => `\\t${e}`).join(\"\\n\")));\n\t\tif (lineBreaks) {\n\t\t\tCLIDisplay.writeError(\"\\n\");\n\t\t}\n\t}\n\n\t/**\n\t * Display an error message in simple form.\n\t * @param error The error to display.\n\t */\n\tpublic static errorMessage(error: string): void {\n\t\tCLIDisplay.writeError(\"❗ \");\n\t\tCLIDisplay.writeError(chalk.red(error));\n\t\tCLIDisplay.writeError(\"\\n\");\n\t}\n\n\t/**\n\t * Display a section.\n\t * @param label The label for the section.\n\t */\n\tpublic static section(label: string): void {\n\t\tCLIDisplay.write(chalk.hex(\"#FFA500\").bold.underline(label));\n\t\tCLIDisplay.write(\"\\n\\n\");\n\t}\n\n\t/**\n\t * Display a value with a label.\n\t * @param label The label for the value.\n\t * @param value The value to display.\n\t * @param indentLevel The level of indentation.\n\t */\n\tpublic static value(label: string, value: unknown, indentLevel: number = 0): void {\n\t\tCLIDisplay.write(\"\\t\".repeat(indentLevel));\n\t\tif (Is.stringValue(label)) {\n\t\t\tCLIDisplay.write(chalk.hex(\"#FFA500\").bold(`${label}: `));\n\t\t}\n\t\tCLIDisplay.write(Coerce.string(value) ?? \"\");\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display a task with a label.\n\t * @param label The label for the value.\n\t * @param task The task to display.\n\t */\n\tpublic static task(label: string, task?: string): void {\n\t\tCLIDisplay.write(\"➡️ \");\n\t\tif (Is.empty(task)) {\n\t\t\tCLIDisplay.write(chalk.cyan(label));\n\t\t} else {\n\t\t\tCLIDisplay.write(chalk.cyan(`${label}: `));\n\t\t\tCLIDisplay.write(task);\n\t\t}\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display a break.\n\t */\n\tpublic static break(): void {\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display formatted and colorized JSON.\n\t * @param obj The object to display.\n\t */\n\tpublic static json(obj: unknown): void {\n\t\tCLIDisplay.write(inspect(obj, false, null, true));\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display a warning.\n\t * @param label The label for the warning.\n\t */\n\tpublic static warning(label: string): void {\n\t\tCLIDisplay.write(\"⚠️ \");\n\t\tCLIDisplay.write(chalk.hex(\"#FFA500\").bold(label));\n\t\tCLIDisplay.write(\"\\n\\n\");\n\t}\n\n\t/**\n\t * Display the processing is done.\n\t */\n\tpublic static done(): void {\n\t\tCLIDisplay.write(\"🎉 \");\n\t\tCLIDisplay.write(chalk.greenBright.bold(I18n.formatMessage(\"cli.progress.done\")));\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Start the spinner.\n\t * @param i18nMessage The message to display with the spinner.\n\t * @param spinnerCharacters The characters to use in the spinner.\n\t * @param interval The interval for the spinner.\n\t */\n\tpublic static spinnerStart(\n\t\ti18nMessage: string = \"cli.progress.pleaseWait\",\n\t\tspinnerCharacters: string[] = [\"⠋\", \"⠙\", \"⠹\", \"⠸\", \"⠼\", \"⠴\", \"⠦\", \"⠧\", \"⠇\", \"⠏\"],\n\t\tinterval: number = 100\n\t): void {\n\t\tlet spinnerIndex = 0;\n\t\tif (CLIDisplay._spinnerIntervalId) {\n\t\t\tclearInterval(CLIDisplay._spinnerIntervalId);\n\t\t}\n\t\tCLIDisplay._spinnerIntervalId = setInterval(() => {\n\t\t\tCLIDisplay.clearLine();\n\t\t\tCLIDisplay.write(\n\t\t\t\t`${spinnerCharacters[spinnerIndex++ % spinnerCharacters.length]} ${I18n.formatMessage(i18nMessage)}`\n\t\t\t);\n\t\t}, interval);\n\t}\n\n\t/**\n\t * Stop the spinner.\n\t */\n\tpublic static spinnerStop(): void {\n\t\tif (CLIDisplay._spinnerIntervalId) {\n\t\t\tclearInterval(CLIDisplay._spinnerIntervalId);\n\t\t\tCLIDisplay._spinnerIntervalId = undefined;\n\t\t}\n\t\tCLIDisplay.clearLine();\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"cliDisplay.js","sourceRoot":"","sources":["../../src/c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AAC,YAAY,CACzB,cAAsB,yBAAyB,EAC/C,oBAA8B,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAChF,WAAmB,GAAG;QAEtB,IAAI,YAAY,GAAG,CAAC,CAAC;QACrB,IAAI,UAAU,CAAC,kBAAkB,EAAE,CAAC;YACnC,aAAa,CAAC,UAAU,CAAC,kBAAkB,CAAC,CAAC;QAC9C,CAAC;QACD,UAAU,CAAC,kBAAkB,GAAG,WAAW,CAAC,GAAG,EAAE;YAChD,UAAU,CAAC,SAAS,EAAE,CAAC;YACvB,UAAU,CAAC,KAAK,CACf,GAAG,iBAAiB,CAAC,YAAY,EAAE,GAAG,iBAAiB,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE,CACpG,CAAC;QACH,CAAC,EAAE,QAAQ,CAAC,CAAC;IACd,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,WAAW;QACxB,IAAI,UAAU,CAAC,kBAAkB,EAAE,CAAC;YACnC,aAAa,CAAC,UAAU,CAAC,kBAAkB,CAAC,CAAC;YAC7C,UAAU,CAAC,kBAAkB,GAAG,SAAS,CAAC;QAC3C,CAAC;QACD,UAAU,CAAC,SAAS,EAAE,CAAC;IACxB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { clearLine, cursorTo } from \"node:readline\";\nimport { inspect } from \"node:util\";\nimport { Coerce, ErrorHelper, I18n, Is } from \"@twin.org/core\";\nimport chalk from \"chalk\";\n\n/**\n * Display utilities for the CLI.\n */\nexport class CLIDisplay {\n\t/**\n\t * The interval ID for the spinner.\n\t * @internal\n\t */\n\tprivate static _spinnerIntervalId: NodeJS.Timeout | number | undefined;\n\n\t/**\n\t * The default output method for writing standard messages.\n\t * @param buffer The message to output.\n\t */\n\tpublic static write: (buffer: string | Uint8Array) => void = buffer =>\n\t\tprocess.stdout.write(buffer);\n\n\t/**\n\t * The default output method for writing error messages.\n\t * @param buffer The message to output.\n\t */\n\tpublic static writeError: (buffer: string | Uint8Array) => void = buffer =>\n\t\tprocess.stderr.write(buffer);\n\n\t/**\n\t * The default output method for clearing the current line.\n\t */\n\tpublic static clearLine: () => void = () => {\n\t\tclearLine(process.stdout, 0);\n\t\tcursorTo(process.stdout, 0);\n\t};\n\n\t/**\n\t * Display the header for the CLI.\n\t * @param title The title of the CLI.\n\t * @param version The version of the CLI.\n\t * @param icon The icon for the CLI.\n\t */\n\tpublic static header(title: string, version: string, icon: string): void {\n\t\tconst titleVersion = `${title} v${version}`;\n\t\tCLIDisplay.write(`${icon} ${chalk.underline.bold.blue(titleVersion)}\\n`);\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display an error message.\n\t * @param error The error to display.\n\t * @param lineBreaks Whether to add a line break after the error.\n\t * @param options Options for formatting the error.\n\t * @param options.includeStack Whether to include the stack trace in the output, defaults to false.\n\t * @param options.includeAdditional Whether to include additional error information in the output, defaults to false.\n\t */\n\tpublic static error(\n\t\terror: unknown,\n\t\tlineBreaks: boolean = true,\n\t\toptions: { includeStack?: boolean; includeAdditional?: boolean } = {}\n\t): void {\n\t\tCLIDisplay.writeError(\"❗ \");\n\t\tCLIDisplay.writeError(chalk.red(I18n.formatMessage(\"cli.progress.error\")));\n\t\tif (lineBreaks) {\n\t\t\tCLIDisplay.writeError(\"\\n\");\n\t\t}\n\n\t\tconst formatted = ErrorHelper.localizeErrors(error);\n\t\tCLIDisplay.writeError(chalk.red(formatted.map(e => `\\t${e.message}`).join(\"\\n\")));\n\n\t\tif (options.includeAdditional) {\n\t\t\tfor (const err of formatted) {\n\t\t\t\tif (Is.arrayValue(err.additional)) {\n\t\t\t\t\tCLIDisplay.writeError(chalk.red(`\\n\\t\\t${err.additional.join(\"\\n\\t\\t\")}\\n`));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (options.includeStack) {\n\t\t\tCLIDisplay.writeError(\"\\n\");\n\t\t\tCLIDisplay.writeError(\"\\n-----DEBUG STACK START-----\\n\\n\");\n\t\t\tfor (const err of formatted) {\n\t\t\t\tif (Is.stringValue(err.stack)) {\n\t\t\t\t\tCLIDisplay.writeError(`${err.stack}\\n`);\n\t\t\t\t}\n\t\t\t}\n\t\t\tCLIDisplay.writeError(\"\\n-----DEBUG STACK END-----\\n\");\n\t\t}\n\n\t\tif (lineBreaks) {\n\t\t\tCLIDisplay.writeError(\"\\n\");\n\t\t}\n\t}\n\n\t/**\n\t * Display an error message in simple form.\n\t * @param error The error to display.\n\t */\n\tpublic static errorMessage(error: string): void {\n\t\tCLIDisplay.writeError(\"❗ \");\n\t\tCLIDisplay.writeError(chalk.red(error));\n\t\tCLIDisplay.writeError(\"\\n\");\n\t}\n\n\t/**\n\t * Display a section.\n\t * @param label The label for the section.\n\t */\n\tpublic static section(label: string): void {\n\t\tCLIDisplay.write(chalk.hex(\"#FFA500\").bold.underline(label));\n\t\tCLIDisplay.write(\"\\n\\n\");\n\t}\n\n\t/**\n\t * Display a value with a label.\n\t * @param label The label for the value.\n\t * @param value The value to display.\n\t * @param indentLevel The level of indentation.\n\t */\n\tpublic static value(label: string, value: unknown, indentLevel: number = 0): void {\n\t\tCLIDisplay.write(\"\\t\".repeat(indentLevel));\n\t\tif (Is.stringValue(label)) {\n\t\t\tCLIDisplay.write(chalk.hex(\"#FFA500\").bold(`${label}: `));\n\t\t}\n\t\tCLIDisplay.write(Coerce.string(value) ?? \"\");\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display a task with a label.\n\t * @param label The label for the value.\n\t * @param task The task to display.\n\t */\n\tpublic static task(label: string, task?: string): void {\n\t\tCLIDisplay.write(\"➡️ \");\n\t\tif (Is.empty(task)) {\n\t\t\tCLIDisplay.write(chalk.cyan(label));\n\t\t} else {\n\t\t\tCLIDisplay.write(chalk.cyan(`${label}: `));\n\t\t\tCLIDisplay.write(task);\n\t\t}\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display a break.\n\t */\n\tpublic static break(): void {\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display formatted and colorized JSON.\n\t * @param obj The object to display.\n\t */\n\tpublic static json(obj: unknown): void {\n\t\tCLIDisplay.write(inspect(obj, false, null, true));\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Display a warning.\n\t * @param label The label for the warning.\n\t */\n\tpublic static warning(label: string): void {\n\t\tCLIDisplay.write(\"⚠️ \");\n\t\tCLIDisplay.write(chalk.hex(\"#FFA500\").bold(label));\n\t\tCLIDisplay.write(\"\\n\\n\");\n\t}\n\n\t/**\n\t * Display the processing is done.\n\t */\n\tpublic static done(): void {\n\t\tCLIDisplay.write(\"🎉 \");\n\t\tCLIDisplay.write(chalk.greenBright.bold(I18n.formatMessage(\"cli.progress.done\")));\n\t\tCLIDisplay.write(\"\\n\");\n\t}\n\n\t/**\n\t * Start the spinner.\n\t * @param i18nMessage The message to display with the spinner.\n\t * @param spinnerCharacters The characters to use in the spinner.\n\t * @param interval The interval for the spinner.\n\t */\n\tpublic static spinnerStart(\n\t\ti18nMessage: string = \"cli.progress.pleaseWait\",\n\t\tspinnerCharacters: string[] = [\"⠋\", \"⠙\", \"⠹\", \"⠸\", \"⠼\", \"⠴\", \"⠦\", \"⠧\", \"⠇\", \"⠏\"],\n\t\tinterval: number = 100\n\t): void {\n\t\tlet spinnerIndex = 0;\n\t\tif (CLIDisplay._spinnerIntervalId) {\n\t\t\tclearInterval(CLIDisplay._spinnerIntervalId);\n\t\t}\n\t\tCLIDisplay._spinnerIntervalId = setInterval(() => {\n\t\t\tCLIDisplay.clearLine();\n\t\t\tCLIDisplay.write(\n\t\t\t\t`${spinnerCharacters[spinnerIndex++ % spinnerCharacters.length]} ${I18n.formatMessage(i18nMessage)}`\n\t\t\t);\n\t\t}, interval);\n\t}\n\n\t/**\n\t * Stop the spinner.\n\t */\n\tpublic static spinnerStop(): void {\n\t\tif (CLIDisplay._spinnerIntervalId) {\n\t\t\tclearInterval(CLIDisplay._spinnerIntervalId);\n\t\t\tCLIDisplay._spinnerIntervalId = undefined;\n\t\t}\n\t\tCLIDisplay.clearLine();\n\t}\n}\n"]}
|
package/dist/es/cliParam.js
CHANGED
|
@@ -54,9 +54,9 @@ export class CLIParam {
|
|
|
54
54
|
* @throws An error if the option is invalid.
|
|
55
55
|
*/
|
|
56
56
|
static arrayOneOf(optionName, optionValue, validValues, allowEnvVar = true) {
|
|
57
|
-
|
|
58
|
-
Guards.arrayOneOf("commands", optionName,
|
|
59
|
-
return
|
|
57
|
+
const stringValue = CLIParam.env(optionName, optionValue, allowEnvVar);
|
|
58
|
+
Guards.arrayOneOf("commands", optionName, stringValue, validValues);
|
|
59
|
+
return stringValue;
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
62
|
* Check the option to see if it is a url.
|
package/dist/es/cliParam.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cliParam.js","sourceRoot":"","sources":["../../src/cliParam.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAElF;;GAEG;AACH,MAAM,OAAO,QAAQ;IACpB;;;;;;;OAOG;IACI,MAAM,CAAC,GAAG,CAChB,UAAkB,EAClB,WAA+B,EAC/B,WAAoB;QAEpB,IAAI,WAAW,IAAI,WAAW,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACjD,MAAM,YAAY,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC1C,6DAA6D;YAC7D,gDAAgD;YAChD,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,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,WAAW;SAClB,CAAC,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CACnB,UAAkB,EAClB,WAA+B,EAC/B,cAAuB,IAAI;QAE3B,WAAW,GAAG,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;QAEjE,IAAI,EAAE,CAAC,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC;YAClC,OAAO,SAAS,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;QAC7C,CAAC;QACD,MAAM,IAAI,YAAY,CAAC,UAAU,EAAE,qCAAqC,EAAE;YACzE,MAAM,EAAE,UAAU;YAClB,KAAK,EAAE,WAAW;SAClB,CAAC,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,SAAS,CACtB,UAAkB,EAClB,WAA+B,EAC/B,cAAuB,IAAI;QAE3B,WAAW,GAAG,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;QAEjE,IAAI,EAAE,CAAC,SAAS,CAAC,WAAW,EAAE,IAAI,CAAC,EAAE,CAAC;YACrC,OAAO,SAAS,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;QAC1C,CAAC;aAAM,IAAI,EAAE,CAAC,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC;YACzC,OAAO,SAAS,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;QAC7C,CAAC;QACD,MAAM,IAAI,YAAY,CAAC,UAAU,EAAE,wCAAwC,EAAE;YAC5E,MAAM,EAAE,UAAU;YAClB,KAAK,EAAE,WAAW;SAClB,CAAC,CAAC;IACJ,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Coerce, Converter, GeneralError, Guards, Is, Url } from \"@twin.org/core\";\n\n/**\n * Parameter utilities for the CLI.\n */\nexport class CLIParam {\n\t/**\n\t * Check the option to see if it exists.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static env(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean\n\t): string | undefined {\n\t\tif (allowEnvVar && optionValue?.startsWith(\"!\")) {\n\t\t\tconst envValueName = optionValue.slice(1);\n\t\t\t// This is reading from an env var so it really has no choice\n\t\t\t// eslint-disable-next-line no-restricted-syntax\n\t\t\tconst envValue = process.env[envValueName];\n\t\t\tif (Is.empty(envValue)) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.missingEnv\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: envValueName\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\toptionValue = envValue;\n\t\t\t}\n\t\t}\n\t\treturn optionValue;\n\t}\n\n\t/**\n\t * Check the option to see if the String exists.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static stringValue(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): string {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tGuards.stringValue(\"commands\", optionName, optionValue);\n\t\treturn optionValue;\n\t}\n\n\t/**\n\t * Check the option to see if the value exists in the specific array.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param validValues The valid values.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static arrayOneOf<T = string>(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tvalidValues: T[],\n\t\tallowEnvVar: boolean = true\n\t): T {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tGuards.arrayOneOf<T>(\"commands\", optionName, optionValue as T, validValues);\n\t\treturn optionValue as T;\n\t}\n\n\t/**\n\t * Check the option to see if it is a url.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static url(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): string {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tUrl.guard(\"commands\", optionName, optionValue);\n\t\treturn optionValue;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is a number.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @param minValue The minimum value.\n\t * @param maxValue The maximum value.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static number(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true,\n\t\tminValue: number = 0,\n\t\tmaxValue?: number\n\t): number {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tconst coerced = Coerce.number(optionValue);\n\t\tif (Is.number(coerced)) {\n\t\t\tif (Is.number(minValue) && coerced < minValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMinValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tminValue\n\t\t\t\t});\n\t\t\t} else if (Is.number(maxValue) && coerced > maxValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMaxValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tmaxValue\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn coerced;\n\t\t}\n\t\tGuards.number(\"commands\", optionName, optionValue);\n\t\treturn 0;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is an integer.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @param minValue The minimum value.\n\t * @param maxValue The maximum value.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static integer(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true,\n\t\tminValue: number = 0,\n\t\tmaxValue?: number\n\t): number {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tconst coerced = Coerce.number(optionValue);\n\t\tif (Is.integer(coerced)) {\n\t\t\tif (Is.number(minValue) && coerced < minValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMinValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tminValue\n\t\t\t\t});\n\t\t\t} else if (Is.number(maxValue) && coerced > maxValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMaxValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tmaxValue\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn coerced;\n\t\t}\n\t\tGuards.integer(\"commands\", optionName, optionValue);\n\t\treturn 0;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is a big number.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @param minValue The minimum value.\n\t * @param maxValue The maximum value.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static bigint(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true,\n\t\tminValue: bigint = 0n,\n\t\tmaxValue?: bigint\n\t): bigint {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tconst coerced = Coerce.bigint(optionValue);\n\t\tif (Is.bigint(coerced)) {\n\t\t\tif (Is.bigint(minValue) && coerced < minValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMinValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tminValue\n\t\t\t\t});\n\t\t\t} else if (Is.bigint(maxValue) && coerced > maxValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMaxValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tmaxValue\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn coerced;\n\t\t}\n\t\tGuards.bigint(\"commands\", optionName, optionValue);\n\t\treturn 0n;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is a boolean.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static boolean(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): boolean {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tconst coerced = Coerce.boolean(optionValue);\n\t\tif (Is.boolean(coerced)) {\n\t\t\treturn coerced;\n\t\t}\n\t\tGuards.boolean(\"commands\", optionName, optionValue);\n\t\treturn false;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is hex.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static hex(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): Uint8Array {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\n\t\tif (Is.stringHex(optionValue, true)) {\n\t\t\treturn Converter.hexToBytes(optionValue);\n\t\t}\n\t\tthrow new GeneralError(\"commands\", \"commands.common.optionInvalidHex\", {\n\t\t\toption: optionName,\n\t\t\tvalue: optionValue\n\t\t});\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is base64.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static base64(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): Uint8Array {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\n\t\tif (Is.stringBase64(optionValue)) {\n\t\t\treturn Converter.base64ToBytes(optionValue);\n\t\t}\n\t\tthrow new GeneralError(\"commands\", \"commands.common.optionInvalidBase64\", {\n\t\t\toption: optionName,\n\t\t\tvalue: optionValue\n\t\t});\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is hex or base64.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static hexBase64(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): Uint8Array {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\n\t\tif (Is.stringHex(optionValue, true)) {\n\t\t\treturn Converter.hexToBytes(optionValue);\n\t\t} else if (Is.stringBase64(optionValue)) {\n\t\t\treturn Converter.base64ToBytes(optionValue);\n\t\t}\n\t\tthrow new GeneralError(\"commands\", \"commands.common.optionInvalidHexBase64\", {\n\t\t\toption: optionName,\n\t\t\tvalue: optionValue\n\t\t});\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"cliParam.js","sourceRoot":"","sources":["../../src/cliParam.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAElF;;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W,CAAC,CAAC;QAC7C,CAAC;QACD,MAAM,IAAI,YAAY,CAAC,UAAU,EAAE,wCAAwC,EAAE;YAC5E,MAAM,EAAE,UAAU;YAClB,KAAK,EAAE,WAAW;SAClB,CAAC,CAAC;IACJ,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Coerce, Converter, GeneralError, Guards, Is, Url } from \"@twin.org/core\";\n\n/**\n * Parameter utilities for the CLI.\n */\nexport class CLIParam {\n\t/**\n\t * Check the option to see if it exists.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static env(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean\n\t): string | undefined {\n\t\tif (allowEnvVar && optionValue?.startsWith(\"!\")) {\n\t\t\tconst envValueName = optionValue.slice(1);\n\t\t\t// This is reading from an env var so it really has no choice\n\t\t\t// eslint-disable-next-line no-restricted-syntax\n\t\t\tconst envValue = process.env[envValueName];\n\t\t\tif (Is.empty(envValue)) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.missingEnv\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: envValueName\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\toptionValue = envValue;\n\t\t\t}\n\t\t}\n\t\treturn optionValue;\n\t}\n\n\t/**\n\t * Check the option to see if the String exists.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static stringValue(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): string {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tGuards.stringValue(\"commands\", optionName, optionValue);\n\t\treturn optionValue;\n\t}\n\n\t/**\n\t * Check the option to see if the value exists in the specific array.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param validValues The valid values.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static arrayOneOf<T = string>(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tvalidValues: T[],\n\t\tallowEnvVar: boolean = true\n\t): T {\n\t\tconst stringValue = CLIParam.env(optionName, optionValue, allowEnvVar) as T;\n\t\tGuards.arrayOneOf<T>(\"commands\", optionName, stringValue, validValues);\n\t\treturn stringValue;\n\t}\n\n\t/**\n\t * Check the option to see if it is a url.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static url(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): string {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tUrl.guard(\"commands\", optionName, optionValue);\n\t\treturn optionValue;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is a number.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @param minValue The minimum value.\n\t * @param maxValue The maximum value.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static number(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true,\n\t\tminValue: number = 0,\n\t\tmaxValue?: number\n\t): number {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tconst coerced = Coerce.number(optionValue);\n\t\tif (Is.number(coerced)) {\n\t\t\tif (Is.number(minValue) && coerced < minValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMinValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tminValue\n\t\t\t\t});\n\t\t\t} else if (Is.number(maxValue) && coerced > maxValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMaxValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tmaxValue\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn coerced;\n\t\t}\n\t\tGuards.number(\"commands\", optionName, optionValue);\n\t\treturn 0;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is an integer.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @param minValue The minimum value.\n\t * @param maxValue The maximum value.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static integer(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true,\n\t\tminValue: number = 0,\n\t\tmaxValue?: number\n\t): number {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tconst coerced = Coerce.number(optionValue);\n\t\tif (Is.integer(coerced)) {\n\t\t\tif (Is.number(minValue) && coerced < minValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMinValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tminValue\n\t\t\t\t});\n\t\t\t} else if (Is.number(maxValue) && coerced > maxValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMaxValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tmaxValue\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn coerced;\n\t\t}\n\t\tGuards.integer(\"commands\", optionName, optionValue);\n\t\treturn 0;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is a big number.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @param minValue The minimum value.\n\t * @param maxValue The maximum value.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static bigint(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true,\n\t\tminValue: bigint = 0n,\n\t\tmaxValue?: bigint\n\t): bigint {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tconst coerced = Coerce.bigint(optionValue);\n\t\tif (Is.bigint(coerced)) {\n\t\t\tif (Is.bigint(minValue) && coerced < minValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMinValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tminValue\n\t\t\t\t});\n\t\t\t} else if (Is.bigint(maxValue) && coerced > maxValue) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.common.optionMaxValue\", {\n\t\t\t\t\toption: optionName,\n\t\t\t\t\tvalue: optionValue,\n\t\t\t\t\tmaxValue\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn coerced;\n\t\t}\n\t\tGuards.bigint(\"commands\", optionName, optionValue);\n\t\treturn 0n;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is a boolean.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static boolean(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): boolean {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\t\tconst coerced = Coerce.boolean(optionValue);\n\t\tif (Is.boolean(coerced)) {\n\t\t\treturn coerced;\n\t\t}\n\t\tGuards.boolean(\"commands\", optionName, optionValue);\n\t\treturn false;\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is hex.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static hex(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): Uint8Array {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\n\t\tif (Is.stringHex(optionValue, true)) {\n\t\t\treturn Converter.hexToBytes(optionValue);\n\t\t}\n\t\tthrow new GeneralError(\"commands\", \"commands.common.optionInvalidHex\", {\n\t\t\toption: optionName,\n\t\t\tvalue: optionValue\n\t\t});\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is base64.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static base64(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): Uint8Array {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\n\t\tif (Is.stringBase64(optionValue)) {\n\t\t\treturn Converter.base64ToBytes(optionValue);\n\t\t}\n\t\tthrow new GeneralError(\"commands\", \"commands.common.optionInvalidBase64\", {\n\t\t\toption: optionName,\n\t\t\tvalue: optionValue\n\t\t});\n\t}\n\n\t/**\n\t * Check the option to see if it exists and is hex or base64.\n\t * @param optionName The name of the option.\n\t * @param optionValue The option value.\n\t * @param allowEnvVar Allow the option to be read from an env var.\n\t * @returns The final option value.\n\t * @throws An error if the option is invalid.\n\t */\n\tpublic static hexBase64(\n\t\toptionName: string,\n\t\toptionValue: string | undefined,\n\t\tallowEnvVar: boolean = true\n\t): Uint8Array {\n\t\toptionValue = CLIParam.env(optionName, optionValue, allowEnvVar);\n\n\t\tif (Is.stringHex(optionValue, true)) {\n\t\t\treturn Converter.hexToBytes(optionValue);\n\t\t} else if (Is.stringBase64(optionValue)) {\n\t\t\treturn Converter.base64ToBytes(optionValue);\n\t\t}\n\t\tthrow new GeneralError(\"commands\", \"commands.common.optionInvalidHexBase64\", {\n\t\t\toption: optionName,\n\t\t\tvalue: optionValue\n\t\t});\n\t}\n}\n"]}
|
|
@@ -27,8 +27,14 @@ export declare class CLIDisplay {
|
|
|
27
27
|
* Display an error message.
|
|
28
28
|
* @param error The error to display.
|
|
29
29
|
* @param lineBreaks Whether to add a line break after the error.
|
|
30
|
+
* @param options Options for formatting the error.
|
|
31
|
+
* @param options.includeStack Whether to include the stack trace in the output, defaults to false.
|
|
32
|
+
* @param options.includeAdditional Whether to include additional error information in the output, defaults to false.
|
|
30
33
|
*/
|
|
31
|
-
static error(error: unknown, lineBreaks?: boolean
|
|
34
|
+
static error(error: unknown, lineBreaks?: boolean, options?: {
|
|
35
|
+
includeStack?: boolean;
|
|
36
|
+
includeAdditional?: boolean;
|
|
37
|
+
}): void;
|
|
32
38
|
/**
|
|
33
39
|
* Display an error message in simple form.
|
|
34
40
|
* @param error The error to display.
|