@contember/cli-common 2.2.0-alpha.1 → 2.2.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/dist/development/index.cjs +20 -0
  2. package/dist/development/index.cjs.map +1 -1
  3. package/dist/development/index.js +22 -2
  4. package/dist/development/index.js.map +1 -1
  5. package/dist/development/src/application/Application.cjs +116 -59
  6. package/dist/development/src/application/Application.cjs.map +1 -1
  7. package/dist/development/src/application/Application.js +116 -59
  8. package/dist/development/src/application/Application.js.map +1 -1
  9. package/dist/development/src/application/CliError.cjs +104 -0
  10. package/dist/development/src/application/CliError.cjs.map +1 -0
  11. package/dist/development/src/application/CliError.js +104 -0
  12. package/dist/development/src/application/CliError.js.map +1 -0
  13. package/dist/development/src/application/Command.cjs +18 -5
  14. package/dist/development/src/application/Command.cjs.map +1 -1
  15. package/dist/development/src/application/Command.js +18 -5
  16. package/dist/development/src/application/Command.js.map +1 -1
  17. package/dist/development/src/application/CommandConfiguration.cjs +21 -3
  18. package/dist/development/src/application/CommandConfiguration.cjs.map +1 -1
  19. package/dist/development/src/application/CommandConfiguration.js +23 -5
  20. package/dist/development/src/application/CommandConfiguration.js.map +1 -1
  21. package/dist/development/src/application/CommandManager.cjs +139 -21
  22. package/dist/development/src/application/CommandManager.cjs.map +1 -1
  23. package/dist/development/src/application/CommandManager.js +139 -21
  24. package/dist/development/src/application/CommandManager.js.map +1 -1
  25. package/dist/development/src/application/ErrorRenderer.cjs +38 -0
  26. package/dist/development/src/application/ErrorRenderer.cjs.map +1 -0
  27. package/dist/development/src/application/ErrorRenderer.js +38 -0
  28. package/dist/development/src/application/ErrorRenderer.js.map +1 -0
  29. package/dist/development/src/application/GlobalOptions.cjs +54 -0
  30. package/dist/development/src/application/GlobalOptions.cjs.map +1 -0
  31. package/dist/development/src/application/GlobalOptions.js +54 -0
  32. package/dist/development/src/application/GlobalOptions.js.map +1 -0
  33. package/dist/development/src/application/Input.cjs +5 -0
  34. package/dist/development/src/application/Input.cjs.map +1 -1
  35. package/dist/development/src/application/Input.js +5 -0
  36. package/dist/development/src/application/Input.js.map +1 -1
  37. package/dist/development/src/application/InputParser.cjs +7 -3
  38. package/dist/development/src/application/InputParser.cjs.map +1 -1
  39. package/dist/development/src/application/InputParser.js +7 -3
  40. package/dist/development/src/application/InputParser.js.map +1 -1
  41. package/dist/development/src/application/Option.cjs +11 -0
  42. package/dist/development/src/application/Option.cjs.map +1 -1
  43. package/dist/development/src/application/Option.js +12 -1
  44. package/dist/development/src/application/Option.js.map +1 -1
  45. package/dist/development/src/application/Output.cjs +242 -0
  46. package/dist/development/src/application/Output.cjs.map +1 -0
  47. package/dist/development/src/application/Output.js +242 -0
  48. package/dist/development/src/application/Output.js.map +1 -0
  49. package/dist/development/src/application/exitProcess.cjs +7 -0
  50. package/dist/development/src/application/exitProcess.cjs.map +1 -0
  51. package/dist/development/src/application/exitProcess.js +7 -0
  52. package/dist/development/src/application/exitProcess.js.map +1 -0
  53. package/dist/development/src/npm/CommandRunner.cjs +126 -24
  54. package/dist/development/src/npm/CommandRunner.cjs.map +1 -1
  55. package/dist/development/src/npm/CommandRunner.js +126 -24
  56. package/dist/development/src/npm/CommandRunner.js.map +1 -1
  57. package/dist/development/src/npm/packageManagers/Bun.cjs +1 -2
  58. package/dist/development/src/npm/packageManagers/Bun.cjs.map +1 -1
  59. package/dist/development/src/npm/packageManagers/Bun.js +1 -2
  60. package/dist/development/src/npm/packageManagers/Bun.js.map +1 -1
  61. package/dist/development/src/npm/packageManagers/Npm.cjs +1 -2
  62. package/dist/development/src/npm/packageManagers/Npm.cjs.map +1 -1
  63. package/dist/development/src/npm/packageManagers/Npm.js +1 -2
  64. package/dist/development/src/npm/packageManagers/Npm.js.map +1 -1
  65. package/dist/development/src/npm/packageManagers/Pnpm.cjs +1 -2
  66. package/dist/development/src/npm/packageManagers/Pnpm.cjs.map +1 -1
  67. package/dist/development/src/npm/packageManagers/Pnpm.js +1 -2
  68. package/dist/development/src/npm/packageManagers/Pnpm.js.map +1 -1
  69. package/dist/development/src/npm/packageManagers/Yarn.cjs +1 -2
  70. package/dist/development/src/npm/packageManagers/Yarn.cjs.map +1 -1
  71. package/dist/development/src/npm/packageManagers/Yarn.js +1 -2
  72. package/dist/development/src/npm/packageManagers/Yarn.js.map +1 -1
  73. package/dist/development/src/npm/packageManagers/YarnClassic.cjs +1 -2
  74. package/dist/development/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
  75. package/dist/development/src/npm/packageManagers/YarnClassic.js +1 -2
  76. package/dist/development/src/npm/packageManagers/YarnClassic.js.map +1 -1
  77. package/dist/production/index.cjs +20 -0
  78. package/dist/production/index.cjs.map +1 -1
  79. package/dist/production/index.js +22 -2
  80. package/dist/production/index.js.map +1 -1
  81. package/dist/production/src/application/Application.cjs +116 -59
  82. package/dist/production/src/application/Application.cjs.map +1 -1
  83. package/dist/production/src/application/Application.js +116 -59
  84. package/dist/production/src/application/Application.js.map +1 -1
  85. package/dist/production/src/application/CliError.cjs +104 -0
  86. package/dist/production/src/application/CliError.cjs.map +1 -0
  87. package/dist/production/src/application/CliError.js +104 -0
  88. package/dist/production/src/application/CliError.js.map +1 -0
  89. package/dist/production/src/application/Command.cjs +18 -5
  90. package/dist/production/src/application/Command.cjs.map +1 -1
  91. package/dist/production/src/application/Command.js +18 -5
  92. package/dist/production/src/application/Command.js.map +1 -1
  93. package/dist/production/src/application/CommandConfiguration.cjs +21 -3
  94. package/dist/production/src/application/CommandConfiguration.cjs.map +1 -1
  95. package/dist/production/src/application/CommandConfiguration.js +23 -5
  96. package/dist/production/src/application/CommandConfiguration.js.map +1 -1
  97. package/dist/production/src/application/CommandManager.cjs +139 -21
  98. package/dist/production/src/application/CommandManager.cjs.map +1 -1
  99. package/dist/production/src/application/CommandManager.js +139 -21
  100. package/dist/production/src/application/CommandManager.js.map +1 -1
  101. package/dist/production/src/application/ErrorRenderer.cjs +38 -0
  102. package/dist/production/src/application/ErrorRenderer.cjs.map +1 -0
  103. package/dist/production/src/application/ErrorRenderer.js +38 -0
  104. package/dist/production/src/application/ErrorRenderer.js.map +1 -0
  105. package/dist/production/src/application/GlobalOptions.cjs +54 -0
  106. package/dist/production/src/application/GlobalOptions.cjs.map +1 -0
  107. package/dist/production/src/application/GlobalOptions.js +54 -0
  108. package/dist/production/src/application/GlobalOptions.js.map +1 -0
  109. package/dist/production/src/application/Input.cjs +5 -0
  110. package/dist/production/src/application/Input.cjs.map +1 -1
  111. package/dist/production/src/application/Input.js +5 -0
  112. package/dist/production/src/application/Input.js.map +1 -1
  113. package/dist/production/src/application/InputParser.cjs +7 -3
  114. package/dist/production/src/application/InputParser.cjs.map +1 -1
  115. package/dist/production/src/application/InputParser.js +7 -3
  116. package/dist/production/src/application/InputParser.js.map +1 -1
  117. package/dist/production/src/application/Option.cjs +11 -0
  118. package/dist/production/src/application/Option.cjs.map +1 -1
  119. package/dist/production/src/application/Option.js +12 -1
  120. package/dist/production/src/application/Option.js.map +1 -1
  121. package/dist/production/src/application/Output.cjs +242 -0
  122. package/dist/production/src/application/Output.cjs.map +1 -0
  123. package/dist/production/src/application/Output.js +242 -0
  124. package/dist/production/src/application/Output.js.map +1 -0
  125. package/dist/production/src/application/exitProcess.cjs +7 -0
  126. package/dist/production/src/application/exitProcess.cjs.map +1 -0
  127. package/dist/production/src/application/exitProcess.js +7 -0
  128. package/dist/production/src/application/exitProcess.js.map +1 -0
  129. package/dist/production/src/npm/CommandRunner.cjs +126 -24
  130. package/dist/production/src/npm/CommandRunner.cjs.map +1 -1
  131. package/dist/production/src/npm/CommandRunner.js +126 -24
  132. package/dist/production/src/npm/CommandRunner.js.map +1 -1
  133. package/dist/production/src/npm/packageManagers/Bun.cjs +1 -2
  134. package/dist/production/src/npm/packageManagers/Bun.cjs.map +1 -1
  135. package/dist/production/src/npm/packageManagers/Bun.js +1 -2
  136. package/dist/production/src/npm/packageManagers/Bun.js.map +1 -1
  137. package/dist/production/src/npm/packageManagers/Npm.cjs +1 -2
  138. package/dist/production/src/npm/packageManagers/Npm.cjs.map +1 -1
  139. package/dist/production/src/npm/packageManagers/Npm.js +1 -2
  140. package/dist/production/src/npm/packageManagers/Npm.js.map +1 -1
  141. package/dist/production/src/npm/packageManagers/Pnpm.cjs +1 -2
  142. package/dist/production/src/npm/packageManagers/Pnpm.cjs.map +1 -1
  143. package/dist/production/src/npm/packageManagers/Pnpm.js +1 -2
  144. package/dist/production/src/npm/packageManagers/Pnpm.js.map +1 -1
  145. package/dist/production/src/npm/packageManagers/Yarn.cjs +1 -2
  146. package/dist/production/src/npm/packageManagers/Yarn.cjs.map +1 -1
  147. package/dist/production/src/npm/packageManagers/Yarn.js +1 -2
  148. package/dist/production/src/npm/packageManagers/Yarn.js.map +1 -1
  149. package/dist/production/src/npm/packageManagers/YarnClassic.cjs +1 -2
  150. package/dist/production/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
  151. package/dist/production/src/npm/packageManagers/YarnClassic.js +1 -2
  152. package/dist/production/src/npm/packageManagers/YarnClassic.js.map +1 -1
  153. package/dist/types/application/Application.d.ts +22 -11
  154. package/dist/types/application/Application.d.ts.map +1 -1
  155. package/dist/types/application/Argument.d.ts.map +1 -1
  156. package/dist/types/application/CliError.d.ts +40 -0
  157. package/dist/types/application/CliError.d.ts.map +1 -0
  158. package/dist/types/application/Command.d.ts +12 -2
  159. package/dist/types/application/Command.d.ts.map +1 -1
  160. package/dist/types/application/CommandConfiguration.d.ts +7 -4
  161. package/dist/types/application/CommandConfiguration.d.ts.map +1 -1
  162. package/dist/types/application/CommandManager.d.ts +40 -2
  163. package/dist/types/application/CommandManager.d.ts.map +1 -1
  164. package/dist/types/application/ErrorRenderer.d.ts +8 -0
  165. package/dist/types/application/ErrorRenderer.d.ts.map +1 -0
  166. package/dist/types/application/GlobalOptions.d.ts +28 -0
  167. package/dist/types/application/GlobalOptions.d.ts.map +1 -0
  168. package/dist/types/application/Input.d.ts +2 -0
  169. package/dist/types/application/Input.d.ts.map +1 -1
  170. package/dist/types/application/InputParser.d.ts +5 -1
  171. package/dist/types/application/InputParser.d.ts.map +1 -1
  172. package/dist/types/application/Option.d.ts +2 -0
  173. package/dist/types/application/Option.d.ts.map +1 -1
  174. package/dist/types/application/Output.d.ts +81 -0
  175. package/dist/types/application/Output.d.ts.map +1 -0
  176. package/dist/types/application/UsageFormatter.d.ts.map +1 -1
  177. package/dist/types/application/exitProcess.d.ts +11 -0
  178. package/dist/types/application/exitProcess.d.ts.map +1 -0
  179. package/dist/types/application/index.d.ts +5 -0
  180. package/dist/types/application/index.d.ts.map +1 -1
  181. package/dist/types/index.d.ts +2 -2
  182. package/dist/types/index.d.ts.map +1 -1
  183. package/dist/types/npm/CommandRunner.d.ts +13 -0
  184. package/dist/types/npm/CommandRunner.d.ts.map +1 -1
  185. package/dist/types/npm/Package.d.ts.map +1 -1
  186. package/dist/types/npm/PackageWorkspace.d.ts.map +1 -1
  187. package/dist/types/npm/PackageWorkspaceResolver.d.ts.map +1 -1
  188. package/dist/types/npm/packageManagers/Bun.d.ts.map +1 -1
  189. package/dist/types/npm/packageManagers/Npm.d.ts.map +1 -1
  190. package/dist/types/npm/packageManagers/PackageManagerHelpers.d.ts.map +1 -1
  191. package/dist/types/npm/packageManagers/Pnpm.d.ts.map +1 -1
  192. package/dist/types/npm/packageManagers/Yarn.d.ts.map +1 -1
  193. package/dist/types/npm/packageManagers/YarnClassic.d.ts.map +1 -1
  194. package/dist/types/tsconfig.tsbuildinfo +1 -1
  195. package/dist/types/utils/findPackageRoot.d.ts.map +1 -1
  196. package/package.json +3 -2
  197. package/src/application/Application.ts +137 -75
  198. package/src/application/CliError.ts +117 -0
  199. package/src/application/Command.ts +27 -6
  200. package/src/application/CommandConfiguration.ts +22 -4
  201. package/src/application/CommandManager.ts +172 -22
  202. package/src/application/ErrorRenderer.ts +40 -0
  203. package/src/application/GlobalOptions.ts +76 -0
  204. package/src/application/Input.ts +6 -0
  205. package/src/application/InputParser.ts +13 -4
  206. package/src/application/Option.ts +12 -0
  207. package/src/application/Output.ts +329 -0
  208. package/src/application/exitProcess.ts +12 -0
  209. package/src/application/index.ts +5 -0
  210. package/src/index.ts +40 -2
  211. package/src/npm/CommandRunner.ts +166 -23
  212. package/src/npm/packageManagers/Bun.ts +1 -2
  213. package/src/npm/packageManagers/Npm.ts +1 -2
  214. package/src/npm/packageManagers/Pnpm.ts +1 -2
  215. package/src/npm/packageManagers/Yarn.ts +1 -2
  216. package/src/npm/packageManagers/YarnClassic.ts +1 -2
  217. package/tests/cases/unit/application.test.ts +426 -0
  218. package/tests/cases/unit/cliError.test.ts +127 -0
  219. package/tests/cases/unit/commandManager.test.ts +270 -0
  220. package/tests/cases/unit/commandRunner.test.ts +192 -0
  221. package/tests/cases/unit/inputParser.test.ts +21 -0
  222. package/tests/cases/unit/output.test.ts +393 -0
  223. package/tests/lib/testOutput.ts +35 -0
  224. package/types/chalk-table/index.d.ts +9 -0
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const InputParser = require("./InputParser.cjs");
4
+ var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ var ExitCode = /* @__PURE__ */ ((ExitCode2) => {
8
+ ExitCode2[ExitCode2["Success"] = 0] = "Success";
9
+ ExitCode2[ExitCode2["InputError"] = 1] = "InputError";
10
+ ExitCode2[ExitCode2["InternalError"] = 2] = "InternalError";
11
+ ExitCode2[ExitCode2["NotFound"] = 3] = "NotFound";
12
+ ExitCode2[ExitCode2["Transient"] = 4] = "Transient";
13
+ ExitCode2[ExitCode2["Conflict"] = 5] = "Conflict";
14
+ ExitCode2[ExitCode2["Forbidden"] = 6] = "Forbidden";
15
+ return ExitCode2;
16
+ })(ExitCode || {});
17
+ class CliError extends Error {
18
+ constructor(message, options) {
19
+ super(message, { cause: options.cause });
20
+ __publicField(this, "code");
21
+ __publicField(this, "exitCode");
22
+ __publicField(this, "retryable");
23
+ __publicField(this, "details");
24
+ this.name = "CliError";
25
+ this.code = options.code;
26
+ this.exitCode = options.exitCode ?? 1;
27
+ this.retryable = options.retryable ?? this.exitCode === 4;
28
+ this.details = normalizeJsonValue(options.details);
29
+ }
30
+ }
31
+ const unexpectedCliErrors = /* @__PURE__ */ new WeakSet();
32
+ const isUnexpectedCliError = (error) => unexpectedCliErrors.has(error);
33
+ const toCliError = (error) => {
34
+ if (error instanceof CliError) {
35
+ return error;
36
+ }
37
+ if (error instanceof InputParser.InvalidInputError) {
38
+ return new CliError(error.message, { code: "INVALID_INPUT", exitCode: 1, cause: error });
39
+ }
40
+ if (typeof error === "string") {
41
+ return new CliError(error, {
42
+ code: "ERROR",
43
+ exitCode: 1
44
+ /* InputError */
45
+ });
46
+ }
47
+ if (error instanceof Error) {
48
+ const cliError2 = new CliError(error.message, { code: "UNKNOWN", exitCode: 2, details: error.stack, cause: error });
49
+ unexpectedCliErrors.add(cliError2);
50
+ return cliError2;
51
+ }
52
+ const cliError = new CliError(safeString(error), { code: "UNKNOWN", exitCode: 2, details: error });
53
+ unexpectedCliErrors.add(cliError);
54
+ return cliError;
55
+ };
56
+ const safeString = (value) => {
57
+ try {
58
+ return String(value);
59
+ } catch {
60
+ return "Unknown error";
61
+ }
62
+ };
63
+ const normalizeJsonValue = (value) => normalizeValue(value, /* @__PURE__ */ new WeakSet());
64
+ const normalizeValue = (value, ancestors) => {
65
+ if (value === void 0) {
66
+ return void 0;
67
+ }
68
+ if (value === null || typeof value === "string" || typeof value === "boolean") {
69
+ return value;
70
+ }
71
+ if (typeof value === "number") {
72
+ return Number.isFinite(value) ? value : String(value);
73
+ }
74
+ if (typeof value === "bigint" || typeof value === "symbol" || typeof value === "function") {
75
+ return safeString(value);
76
+ }
77
+ if (ancestors.has(value)) {
78
+ return "[Circular]";
79
+ }
80
+ ancestors.add(value);
81
+ try {
82
+ if (Array.isArray(value)) {
83
+ return value.map((item) => normalizeValue(item, ancestors) ?? null);
84
+ }
85
+ const result = {};
86
+ for (const [key, item] of Object.entries(value)) {
87
+ const normalized = normalizeValue(item, ancestors);
88
+ if (normalized !== void 0) {
89
+ result[key] = normalized;
90
+ }
91
+ }
92
+ return result;
93
+ } catch {
94
+ return safeString(value);
95
+ } finally {
96
+ ancestors.delete(value);
97
+ }
98
+ };
99
+ exports.CliError = CliError;
100
+ exports.ExitCode = ExitCode;
101
+ exports.isUnexpectedCliError = isUnexpectedCliError;
102
+ exports.normalizeJsonValue = normalizeJsonValue;
103
+ exports.toCliError = toCliError;
104
+ //# sourceMappingURL=CliError.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CliError.cjs","sources":["../../../../../packages/cli-common/src/application/CliError.ts"],"sourcesContent":["import { InvalidInputError } from './InputParser.js'\n\nexport enum ExitCode {\n\tSuccess = 0,\n\t/** invalid input / validation */\n\tInputError = 1,\n\t/** unexpected, a bug */\n\tInternalError = 2,\n\tNotFound = 3,\n\t/** network, timeout, 5xx — the caller SHOULD retry */\n\tTransient = 4,\n\t/** already exists */\n\tConflict = 5,\n\t/** auth / permission denied */\n\tForbidden = 6,\n}\n\nexport interface CliErrorOptions {\n\t/** stable machine-readable slug, e.g. 'PROJECT_NOT_DEFINED' */\n\tcode: string\n\texitCode?: ExitCode\n\tretryable?: boolean\n\tdetails?: unknown\n\tcause?: unknown\n}\n\nexport type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue }\n\nexport class CliError extends Error {\n\tpublic readonly code: string\n\tpublic readonly exitCode: ExitCode\n\tpublic readonly retryable: boolean\n\tpublic readonly details?: JsonValue\n\n\tconstructor(message: string, options: CliErrorOptions) {\n\t\tsuper(message, { cause: options.cause })\n\t\tthis.name = 'CliError'\n\t\tthis.code = options.code\n\t\tthis.exitCode = options.exitCode ?? ExitCode.InputError\n\t\tthis.retryable = options.retryable ?? this.exitCode === ExitCode.Transient\n\t\tthis.details = normalizeJsonValue(options.details)\n\t}\n}\n\nconst unexpectedCliErrors = new WeakSet<CliError>()\n\nexport const isUnexpectedCliError = (error: CliError): boolean => unexpectedCliErrors.has(error)\n\n/**\n * Normalizes anything thrown by a command into a {@link CliError}. Legacy `throw 'some message'` is\n * treated as an input error, everything else unexpected as an internal one.\n */\nexport const toCliError = (error: unknown): CliError => {\n\tif (error instanceof CliError) {\n\t\treturn error\n\t}\n\tif (error instanceof InvalidInputError) {\n\t\treturn new CliError(error.message, { code: 'INVALID_INPUT', exitCode: ExitCode.InputError, cause: error })\n\t}\n\tif (typeof error === 'string') {\n\t\treturn new CliError(error, { code: 'ERROR', exitCode: ExitCode.InputError })\n\t}\n\tif (error instanceof Error) {\n\t\tconst cliError = new CliError(error.message, { code: 'UNKNOWN', exitCode: ExitCode.InternalError, details: error.stack, cause: error })\n\t\tunexpectedCliErrors.add(cliError)\n\t\treturn cliError\n\t}\n\tconst cliError = new CliError(safeString(error), { code: 'UNKNOWN', exitCode: ExitCode.InternalError, details: error })\n\tunexpectedCliErrors.add(cliError)\n\treturn cliError\n}\n\nconst safeString = (value: unknown): string => {\n\ttry {\n\t\treturn String(value)\n\t} catch {\n\t\treturn 'Unknown error'\n\t}\n}\n\nexport const normalizeJsonValue = (value: unknown): JsonValue | undefined => normalizeValue(value, new WeakSet())\n\nconst normalizeValue = (value: unknown, ancestors: WeakSet<object>): JsonValue | undefined => {\n\tif (value === undefined) {\n\t\treturn undefined\n\t}\n\tif (value === null || typeof value === 'string' || typeof value === 'boolean') {\n\t\treturn value\n\t}\n\tif (typeof value === 'number') {\n\t\treturn Number.isFinite(value) ? value : String(value)\n\t}\n\tif (typeof value === 'bigint' || typeof value === 'symbol' || typeof value === 'function') {\n\t\treturn safeString(value)\n\t}\n\tif (ancestors.has(value)) {\n\t\treturn '[Circular]'\n\t}\n\tancestors.add(value)\n\ttry {\n\t\tif (Array.isArray(value)) {\n\t\t\treturn value.map(item => normalizeValue(item, ancestors) ?? null)\n\t\t}\n\t\tconst result: { [key: string]: JsonValue } = {}\n\t\tfor (const [key, item] of Object.entries(value)) {\n\t\t\tconst normalized = normalizeValue(item, ancestors)\n\t\t\tif (normalized !== undefined) {\n\t\t\t\tresult[key] = normalized\n\t\t\t}\n\t\t}\n\t\treturn result\n\t} catch {\n\t\treturn safeString(value)\n\t} finally {\n\t\tancestors.delete(value)\n\t}\n}\n"],"names":["ExitCode","InvalidInputError","cliError"],"mappings":";;;;;;AAEO,IAAK,6BAAAA,cAAL;AACNA,YAAAA,UAAA,aAAU,CAAA,IAAV;AAEAA,YAAAA,UAAA,gBAAa,CAAA,IAAb;AAEAA,YAAAA,UAAA,mBAAgB,CAAA,IAAhB;AACAA,YAAAA,UAAA,cAAW,CAAA,IAAX;AAEAA,YAAAA,UAAA,eAAY,CAAA,IAAZ;AAEAA,YAAAA,UAAA,cAAW,CAAA,IAAX;AAEAA,YAAAA,UAAA,eAAY,CAAA,IAAZ;AAZW,SAAAA;AAAA,GAAA,YAAA,CAAA,CAAA;AA0BL,MAAM,iBAAiB,MAAM;AAAA,EAMnC,YAAY,SAAiB,SAA0B;AACtD,UAAM,SAAS,EAAE,OAAO,QAAQ,OAAO;AANxC,kBAAA,MAAgB,MAAA;AAChB,kBAAA,MAAgB,UAAA;AAChB,kBAAA,MAAgB,WAAA;AAChB,kBAAA,MAAgB,SAAA;AAIf,SAAK,OAAO;AACZ,SAAK,OAAO,QAAQ;AACpB,SAAK,WAAW,QAAQ,YAAY;AACpC,SAAK,YAAY,QAAQ,aAAa,KAAK,aAAa;AACxD,SAAK,UAAU,mBAAmB,QAAQ,OAAO;AAAA,EAClD;AACD;AAEA,MAAM,0CAA0B,QAAA;AAEzB,MAAM,uBAAuB,CAAC,UAA6B,oBAAoB,IAAI,KAAK;AAMxF,MAAM,aAAa,CAAC,UAA6B;AACvD,MAAI,iBAAiB,UAAU;AAC9B,WAAO;AAAA,EACR;AACA,MAA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,QAAQ,KAAK,GAAG;AACzB,aAAO,MAAM,IAAI,CAAA,SAAQ,eAAe,MAAM,SAAS,KAAK,IAAI;AAAA,IACjE;AACA,UAAM,SAAuC,CAAA;AAC7C,eAAW,CAAC,KAAK,IAAI,KAAK,OAAO,QAAQ,KAAK,GAAG;AAChD,YAAM,aAAa,eAAe,MAAM,SAAS;AACjD,UAAI,eAAe,QAAW;AAC7B,eAAO,GAAG,IAAI;AAAA,MACf;AAAA,IACD;AACA,WAAO;AAAA,EACR,QAAQ;AACP,WAAO,WAAW,KAAK;AAAA,EACxB,UAAA;AACC,cAAU,OAAO,KAAK;AAAA,EACvB;AACD;;;;;;"}
@@ -0,0 +1,104 @@
1
+ import { InvalidInputError } from "./InputParser.js";
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ var ExitCode = /* @__PURE__ */ ((ExitCode2) => {
6
+ ExitCode2[ExitCode2["Success"] = 0] = "Success";
7
+ ExitCode2[ExitCode2["InputError"] = 1] = "InputError";
8
+ ExitCode2[ExitCode2["InternalError"] = 2] = "InternalError";
9
+ ExitCode2[ExitCode2["NotFound"] = 3] = "NotFound";
10
+ ExitCode2[ExitCode2["Transient"] = 4] = "Transient";
11
+ ExitCode2[ExitCode2["Conflict"] = 5] = "Conflict";
12
+ ExitCode2[ExitCode2["Forbidden"] = 6] = "Forbidden";
13
+ return ExitCode2;
14
+ })(ExitCode || {});
15
+ class CliError extends Error {
16
+ constructor(message, options) {
17
+ super(message, { cause: options.cause });
18
+ __publicField(this, "code");
19
+ __publicField(this, "exitCode");
20
+ __publicField(this, "retryable");
21
+ __publicField(this, "details");
22
+ this.name = "CliError";
23
+ this.code = options.code;
24
+ this.exitCode = options.exitCode ?? 1;
25
+ this.retryable = options.retryable ?? this.exitCode === 4;
26
+ this.details = normalizeJsonValue(options.details);
27
+ }
28
+ }
29
+ const unexpectedCliErrors = /* @__PURE__ */ new WeakSet();
30
+ const isUnexpectedCliError = (error) => unexpectedCliErrors.has(error);
31
+ const toCliError = (error) => {
32
+ if (error instanceof CliError) {
33
+ return error;
34
+ }
35
+ if (error instanceof InvalidInputError) {
36
+ return new CliError(error.message, { code: "INVALID_INPUT", exitCode: 1, cause: error });
37
+ }
38
+ if (typeof error === "string") {
39
+ return new CliError(error, {
40
+ code: "ERROR",
41
+ exitCode: 1
42
+ /* InputError */
43
+ });
44
+ }
45
+ if (error instanceof Error) {
46
+ const cliError2 = new CliError(error.message, { code: "UNKNOWN", exitCode: 2, details: error.stack, cause: error });
47
+ unexpectedCliErrors.add(cliError2);
48
+ return cliError2;
49
+ }
50
+ const cliError = new CliError(safeString(error), { code: "UNKNOWN", exitCode: 2, details: error });
51
+ unexpectedCliErrors.add(cliError);
52
+ return cliError;
53
+ };
54
+ const safeString = (value) => {
55
+ try {
56
+ return String(value);
57
+ } catch {
58
+ return "Unknown error";
59
+ }
60
+ };
61
+ const normalizeJsonValue = (value) => normalizeValue(value, /* @__PURE__ */ new WeakSet());
62
+ const normalizeValue = (value, ancestors) => {
63
+ if (value === void 0) {
64
+ return void 0;
65
+ }
66
+ if (value === null || typeof value === "string" || typeof value === "boolean") {
67
+ return value;
68
+ }
69
+ if (typeof value === "number") {
70
+ return Number.isFinite(value) ? value : String(value);
71
+ }
72
+ if (typeof value === "bigint" || typeof value === "symbol" || typeof value === "function") {
73
+ return safeString(value);
74
+ }
75
+ if (ancestors.has(value)) {
76
+ return "[Circular]";
77
+ }
78
+ ancestors.add(value);
79
+ try {
80
+ if (Array.isArray(value)) {
81
+ return value.map((item) => normalizeValue(item, ancestors) ?? null);
82
+ }
83
+ const result = {};
84
+ for (const [key, item] of Object.entries(value)) {
85
+ const normalized = normalizeValue(item, ancestors);
86
+ if (normalized !== void 0) {
87
+ result[key] = normalized;
88
+ }
89
+ }
90
+ return result;
91
+ } catch {
92
+ return safeString(value);
93
+ } finally {
94
+ ancestors.delete(value);
95
+ }
96
+ };
97
+ export {
98
+ CliError,
99
+ ExitCode,
100
+ isUnexpectedCliError,
101
+ normalizeJsonValue,
102
+ toCliError
103
+ };
104
+ //# sourceMappingURL=CliError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CliError.js","sources":["../../../../../packages/cli-common/src/application/CliError.ts"],"sourcesContent":["import { InvalidInputError } from './InputParser.js'\n\nexport enum ExitCode {\n\tSuccess = 0,\n\t/** invalid input / validation */\n\tInputError = 1,\n\t/** unexpected, a bug */\n\tInternalError = 2,\n\tNotFound = 3,\n\t/** network, timeout, 5xx — the caller SHOULD retry */\n\tTransient = 4,\n\t/** already exists */\n\tConflict = 5,\n\t/** auth / permission denied */\n\tForbidden = 6,\n}\n\nexport interface CliErrorOptions {\n\t/** stable machine-readable slug, e.g. 'PROJECT_NOT_DEFINED' */\n\tcode: string\n\texitCode?: ExitCode\n\tretryable?: boolean\n\tdetails?: unknown\n\tcause?: unknown\n}\n\nexport type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue }\n\nexport class CliError extends Error {\n\tpublic readonly code: string\n\tpublic readonly exitCode: ExitCode\n\tpublic readonly retryable: boolean\n\tpublic readonly details?: JsonValue\n\n\tconstructor(message: string, options: CliErrorOptions) {\n\t\tsuper(message, { cause: options.cause })\n\t\tthis.name = 'CliError'\n\t\tthis.code = options.code\n\t\tthis.exitCode = options.exitCode ?? ExitCode.InputError\n\t\tthis.retryable = options.retryable ?? this.exitCode === ExitCode.Transient\n\t\tthis.details = normalizeJsonValue(options.details)\n\t}\n}\n\nconst unexpectedCliErrors = new WeakSet<CliError>()\n\nexport const isUnexpectedCliError = (error: CliError): boolean => unexpectedCliErrors.has(error)\n\n/**\n * Normalizes anything thrown by a command into a {@link CliError}. Legacy `throw 'some message'` is\n * treated as an input error, everything else unexpected as an internal one.\n */\nexport const toCliError = (error: unknown): CliError => {\n\tif (error instanceof CliError) {\n\t\treturn error\n\t}\n\tif (error instanceof InvalidInputError) {\n\t\treturn new CliError(error.message, { code: 'INVALID_INPUT', exitCode: ExitCode.InputError, cause: error })\n\t}\n\tif (typeof error === 'string') {\n\t\treturn new CliError(error, { code: 'ERROR', exitCode: ExitCode.InputError })\n\t}\n\tif (error instanceof Error) {\n\t\tconst cliError = new CliError(error.message, { code: 'UNKNOWN', exitCode: ExitCode.InternalError, details: error.stack, cause: error })\n\t\tunexpectedCliErrors.add(cliError)\n\t\treturn cliError\n\t}\n\tconst cliError = new CliError(safeString(error), { code: 'UNKNOWN', exitCode: ExitCode.InternalError, details: error })\n\tunexpectedCliErrors.add(cliError)\n\treturn cliError\n}\n\nconst safeString = (value: unknown): string => {\n\ttry {\n\t\treturn String(value)\n\t} catch {\n\t\treturn 'Unknown error'\n\t}\n}\n\nexport const normalizeJsonValue = (value: unknown): JsonValue | undefined => normalizeValue(value, new WeakSet())\n\nconst normalizeValue = (value: unknown, ancestors: WeakSet<object>): JsonValue | undefined => {\n\tif (value === undefined) {\n\t\treturn undefined\n\t}\n\tif (value === null || typeof value === 'string' || typeof value === 'boolean') {\n\t\treturn value\n\t}\n\tif (typeof value === 'number') {\n\t\treturn Number.isFinite(value) ? value : String(value)\n\t}\n\tif (typeof value === 'bigint' || typeof value === 'symbol' || typeof value === 'function') {\n\t\treturn safeString(value)\n\t}\n\tif (ancestors.has(value)) {\n\t\treturn '[Circular]'\n\t}\n\tancestors.add(value)\n\ttry {\n\t\tif (Array.isArray(value)) {\n\t\t\treturn value.map(item => normalizeValue(item, ancestors) ?? null)\n\t\t}\n\t\tconst result: { [key: string]: JsonValue } = {}\n\t\tfor (const [key, item] of Object.entries(value)) {\n\t\t\tconst normalized = normalizeValue(item, ancestors)\n\t\t\tif (normalized !== undefined) {\n\t\t\t\tresult[key] = normalized\n\t\t\t}\n\t\t}\n\t\treturn result\n\t} catch {\n\t\treturn safeString(value)\n\t} finally {\n\t\tancestors.delete(value)\n\t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AAO;AAAA,EACR,QAAQ;AACP,WAAO,WAAW,KAAK;AAAA,EACxB,UAAA;AACC,cAAU,OAAO,KAAK;AAAA,EACvB;AACD;"}
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const CommandConfiguration = require("./CommandConfiguration.cjs");
4
+ const GlobalOptions = require("./GlobalOptions.cjs");
5
+ const Output = require("./Output.cjs");
4
6
  var __defProp = Object.defineProperty;
5
7
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
8
  var __publicField = (obj, key, value) => __defNormalProp(obj, key + "", value);
@@ -12,17 +14,28 @@ class Command {
12
14
  if (this.configuration === void 0) {
13
15
  const configuration = new CommandConfiguration.CommandConfiguration();
14
16
  this.configure(configuration);
15
- configuration.validate();
17
+ configuration.validate(GlobalOptions.globalOptions);
16
18
  this.configuration = configuration;
17
19
  }
18
20
  return this.configuration;
19
21
  }
20
- async run(args) {
21
- const parser = this.getConfiguration().createParser();
22
- const input = parser.parse(args);
23
- const result = await this.execute(input);
22
+ /** Parses argv in a single pass over the command options merged with the global ones. */
23
+ parse(args) {
24
+ const parsed = this.getConfiguration().createParser(GlobalOptions.globalOptions).parse(args);
25
+ return { input: parsed.input, globals: GlobalOptions.readGlobalOptions(parsed.input), diagnostics: parsed.diagnostics };
26
+ }
27
+ async runParsed(parsed, output) {
28
+ for (const diagnostic of parsed.diagnostics) {
29
+ output.warn(diagnostic);
30
+ }
31
+ const result = await this.execute(parsed.input, output);
24
32
  return result || 0;
25
33
  }
34
+ async run(args, output = new Output.Output()) {
35
+ const parsed = this.parse(args);
36
+ output.applyGlobalOptions(parsed.globals);
37
+ return this.runParsed(parsed, output);
38
+ }
26
39
  }
27
40
  exports.Command = Command;
28
41
  //# sourceMappingURL=Command.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"Command.cjs","sources":["../../../../../packages/cli-common/src/application/Command.ts"],"sourcesContent":["import { CommandConfiguration } from './CommandConfiguration.js'\nimport { Arguments, Input, Options } from './Input.js'\n\nexport abstract class Command<Args extends Arguments, TOptions extends Options> {\n\tprivate configuration: CommandConfiguration<Args, TOptions> | undefined\n\n\tprotected abstract configure(configuration: CommandConfiguration<Args, TOptions>): void\n\n\tpublic getConfiguration(): CommandConfiguration<Args, TOptions> {\n\t\tif (this.configuration === undefined) {\n\t\t\tconst configuration = new CommandConfiguration()\n\t\t\tthis.configure(configuration)\n\t\t\tconfiguration.validate()\n\t\t\tthis.configuration = configuration\n\t\t}\n\t\treturn this.configuration\n\t}\n\n\tprotected abstract execute(input: Input<Args, TOptions>): Promise<void | number>\n\n\tpublic async run(args: string[]): Promise<number> {\n\t\tconst parser = this.getConfiguration().createParser()\n\t\tconst input = parser.parse<Args, TOptions>(args)\n\t\tconst result = await this.execute(input)\n\t\treturn result || 0\n\t}\n}\n"],"names":["CommandConfiguration"],"mappings":";;;;;;AAGO,MAAe,QAA0D;AAAA,EAAzE,cAAA;AACN,kBAAA,MAAQ,eAAA;AAAA,EAAA;AAAA,EAID,mBAAyD;AAC/D,QAAI,KAAK,kBAAkB,QAAW;AACrC,YAAM,gBAAgB,IAAIA,0CAAA;AAC1B,WAAK,UAAU,aAAa;AAC5B,oBAAc,SAAA;AACd,WAAK,gBAAgB;AAAA,IACtB;AACA,WAAO,KAAK;AAAA,EACb;AAAA,EAIA,MAAa,IAAI,MAAiC;AACjD,UAAM,SAAS,KAAK,iBAAA,EAAmB,aAAA;AACvC,UAAM,QAAQ,OAAO,MAAsB,IAAI;AAC/C,UAAM,SAAS,MAAM,KAAK,QAAQ,KAAK;AACvC,WAAO,UAAU;AAAA,EAClB;AACD;;"}
1
+ {"version":3,"file":"Command.cjs","sources":["../../../../../packages/cli-common/src/application/Command.ts"],"sourcesContent":["import { CommandConfiguration } from './CommandConfiguration.js'\nimport { Arguments, Input, Options } from './Input.js'\nimport { globalOptions, GlobalOptionValues, readGlobalOptions } from './GlobalOptions.js'\nimport { Output } from './Output.js'\n\nexport interface ParsedCommandInput<Args extends Arguments, TOptions extends Options> {\n\treadonly input: Input<Args, TOptions>\n\treadonly globals: GlobalOptionValues\n\treadonly diagnostics: readonly string[]\n}\n\nexport abstract class Command<Args extends Arguments, TOptions extends Options> {\n\tprivate configuration: CommandConfiguration<Args, TOptions> | undefined\n\n\tprotected abstract configure(configuration: CommandConfiguration<Args, TOptions>): void\n\n\tpublic getConfiguration(): CommandConfiguration<Args, TOptions> {\n\t\tif (this.configuration === undefined) {\n\t\t\tconst configuration = new CommandConfiguration()\n\t\t\tthis.configure(configuration)\n\t\t\tconfiguration.validate(globalOptions)\n\t\t\tthis.configuration = configuration\n\t\t}\n\t\treturn this.configuration\n\t}\n\n\tprotected abstract execute(input: Input<Args, TOptions>, output: Output): Promise<void | number>\n\n\t/** Parses argv in a single pass over the command options merged with the global ones. */\n\tpublic parse(args: string[]): ParsedCommandInput<Args, TOptions> {\n\t\tconst parsed = this.getConfiguration().createParser(globalOptions).parse<Args, TOptions>(args)\n\t\treturn { input: parsed.input, globals: readGlobalOptions(parsed.input), diagnostics: parsed.diagnostics }\n\t}\n\n\tpublic async runParsed(parsed: ParsedCommandInput<Args, TOptions>, output: Output): Promise<number> {\n\t\tfor (const diagnostic of parsed.diagnostics) {\n\t\t\toutput.warn(diagnostic)\n\t\t}\n\t\tconst result = await this.execute(parsed.input, output)\n\t\treturn result || 0\n\t}\n\n\tpublic async run(args: string[], output: Output = new Output()): Promise<number> {\n\t\tconst parsed = this.parse(args)\n\t\toutput.applyGlobalOptions(parsed.globals)\n\t\treturn this.runParsed(parsed, output)\n\t}\n}\n"],"names":["CommandConfiguration","globalOptions","readGlobalOptions","Output"],"mappings":";;;;;;;;AAWO,MAAe,QAA0D;AAAA,EAAzE,cAAA;AACN,kBAAA,MAAQ,eAAA;AAAA,EAAA;AAAA,EAID,mBAAyD;AAC/D,QAAI,KAAK,kBAAkB,QAAW;AACrC,YAAM,gBAAgB,IAAIA,0CAAA;AAC1B,WAAK,UAAU,aAAa;AAC5B,oBAAc,SAASC,2BAAa;AACpC,WAAK,gBAAgB;AAAA,IACtB;AACA,WAAO,KAAK;AAAA,EACb;AAAA;AAAA,EAKO,MAAM,MAAoD;AAChE,UAAM,SAAS,KAAK,iBAAA,EAAmB,aAAaA,2BAAa,EAAE,MAAsB,IAAI;AAC7F,WAAO,EAAE,OAAO,OAAO,OAAO,SAASC,cAAAA,kBAAkB,OAAO,KAAK,GAAG,aAAa,OAAO,YAAA;AAAA,EAC7F;AAAA,EAEA,MAAa,UAAU,QAA4C,QAAiC;AACnG,eAAW,cAAc,OAAO,aAAa;AAC5C,aAAO,KAAK,UAAU;AAAA,IACvB;AACA,UAAM,SAAS,MAAM,KAAK,QAAQ,OAAO,OAAO,MAAM;AACtD,WAAO,UAAU;AAAA,EAClB;AAAA,EAEA,MAAa,IAAI,MAAgB,SAAiB,IAAIC,OAAAA,UAA2B;AAChF,UAAM,SAAS,KAAK,MAAM,IAAI;AAC9B,WAAO,mBAAmB,OAAO,OAAO;AACxC,WAAO,KAAK,UAAU,QAAQ,MAAM;AAAA,EACrC;AACD;;"}
@@ -1,4 +1,6 @@
1
1
  import { CommandConfiguration } from "./CommandConfiguration.js";
2
+ import { globalOptions, readGlobalOptions } from "./GlobalOptions.js";
3
+ import { Output } from "./Output.js";
2
4
  var __defProp = Object.defineProperty;
3
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
6
  var __publicField = (obj, key, value) => __defNormalProp(obj, key + "", value);
@@ -10,17 +12,28 @@ class Command {
10
12
  if (this.configuration === void 0) {
11
13
  const configuration = new CommandConfiguration();
12
14
  this.configure(configuration);
13
- configuration.validate();
15
+ configuration.validate(globalOptions);
14
16
  this.configuration = configuration;
15
17
  }
16
18
  return this.configuration;
17
19
  }
18
- async run(args) {
19
- const parser = this.getConfiguration().createParser();
20
- const input = parser.parse(args);
21
- const result = await this.execute(input);
20
+ /** Parses argv in a single pass over the command options merged with the global ones. */
21
+ parse(args) {
22
+ const parsed = this.getConfiguration().createParser(globalOptions).parse(args);
23
+ return { input: parsed.input, globals: readGlobalOptions(parsed.input), diagnostics: parsed.diagnostics };
24
+ }
25
+ async runParsed(parsed, output) {
26
+ for (const diagnostic of parsed.diagnostics) {
27
+ output.warn(diagnostic);
28
+ }
29
+ const result = await this.execute(parsed.input, output);
22
30
  return result || 0;
23
31
  }
32
+ async run(args, output = new Output()) {
33
+ const parsed = this.parse(args);
34
+ output.applyGlobalOptions(parsed.globals);
35
+ return this.runParsed(parsed, output);
36
+ }
24
37
  }
25
38
  export {
26
39
  Command
@@ -1 +1 @@
1
- {"version":3,"file":"Command.js","sources":["../../../../../packages/cli-common/src/application/Command.ts"],"sourcesContent":["import { CommandConfiguration } from './CommandConfiguration.js'\nimport { Arguments, Input, Options } from './Input.js'\n\nexport abstract class Command<Args extends Arguments, TOptions extends Options> {\n\tprivate configuration: CommandConfiguration<Args, TOptions> | undefined\n\n\tprotected abstract configure(configuration: CommandConfiguration<Args, TOptions>): void\n\n\tpublic getConfiguration(): CommandConfiguration<Args, TOptions> {\n\t\tif (this.configuration === undefined) {\n\t\t\tconst configuration = new CommandConfiguration()\n\t\t\tthis.configure(configuration)\n\t\t\tconfiguration.validate()\n\t\t\tthis.configuration = configuration\n\t\t}\n\t\treturn this.configuration\n\t}\n\n\tprotected abstract execute(input: Input<Args, TOptions>): Promise<void | number>\n\n\tpublic async run(args: string[]): Promise<number> {\n\t\tconst parser = this.getConfiguration().createParser()\n\t\tconst input = parser.parse<Args, TOptions>(args)\n\t\tconst result = await this.execute(input)\n\t\treturn result || 0\n\t}\n}\n"],"names":[],"mappings":";;;;AAGO,MAAe,QAA0D;AAAA,EAAzE,cAAA;AACN,kBAAA,MAAQ,eAAA;AAAA,EAAA;AAAA,EAID,mBAAyD;AAC/D,QAAI,KAAK,kBAAkB,QAAW;AACrC,YAAM,gBAAgB,IAAI,qBAAA;AAC1B,WAAK,UAAU,aAAa;AAC5B,oBAAc,SAAA;AACd,WAAK,gBAAgB;AAAA,IACtB;AACA,WAAO,KAAK;AAAA,EACb;AAAA,EAIA,MAAa,IAAI,MAAiC;AACjD,UAAM,SAAS,KAAK,iBAAA,EAAmB,aAAA;AACvC,UAAM,QAAQ,OAAO,MAAsB,IAAI;AAC/C,UAAM,SAAS,MAAM,KAAK,QAAQ,KAAK;AACvC,WAAO,UAAU;AAAA,EAClB;AACD;"}
1
+ {"version":3,"file":"Command.js","sources":["../../../../../packages/cli-common/src/application/Command.ts"],"sourcesContent":["import { CommandConfiguration } from './CommandConfiguration.js'\nimport { Arguments, Input, Options } from './Input.js'\nimport { globalOptions, GlobalOptionValues, readGlobalOptions } from './GlobalOptions.js'\nimport { Output } from './Output.js'\n\nexport interface ParsedCommandInput<Args extends Arguments, TOptions extends Options> {\n\treadonly input: Input<Args, TOptions>\n\treadonly globals: GlobalOptionValues\n\treadonly diagnostics: readonly string[]\n}\n\nexport abstract class Command<Args extends Arguments, TOptions extends Options> {\n\tprivate configuration: CommandConfiguration<Args, TOptions> | undefined\n\n\tprotected abstract configure(configuration: CommandConfiguration<Args, TOptions>): void\n\n\tpublic getConfiguration(): CommandConfiguration<Args, TOptions> {\n\t\tif (this.configuration === undefined) {\n\t\t\tconst configuration = new CommandConfiguration()\n\t\t\tthis.configure(configuration)\n\t\t\tconfiguration.validate(globalOptions)\n\t\t\tthis.configuration = configuration\n\t\t}\n\t\treturn this.configuration\n\t}\n\n\tprotected abstract execute(input: Input<Args, TOptions>, output: Output): Promise<void | number>\n\n\t/** Parses argv in a single pass over the command options merged with the global ones. */\n\tpublic parse(args: string[]): ParsedCommandInput<Args, TOptions> {\n\t\tconst parsed = this.getConfiguration().createParser(globalOptions).parse<Args, TOptions>(args)\n\t\treturn { input: parsed.input, globals: readGlobalOptions(parsed.input), diagnostics: parsed.diagnostics }\n\t}\n\n\tpublic async runParsed(parsed: ParsedCommandInput<Args, TOptions>, output: Output): Promise<number> {\n\t\tfor (const diagnostic of parsed.diagnostics) {\n\t\t\toutput.warn(diagnostic)\n\t\t}\n\t\tconst result = await this.execute(parsed.input, output)\n\t\treturn result || 0\n\t}\n\n\tpublic async run(args: string[], output: Output = new Output()): Promise<number> {\n\t\tconst parsed = this.parse(args)\n\t\toutput.applyGlobalOptions(parsed.globals)\n\t\treturn this.runParsed(parsed, output)\n\t}\n}\n"],"names":[],"mappings":";;;;;;AAWO,MAAe,QAA0D;AAAA,EAAzE,cAAA;AACN,kBAAA,MAAQ,eAAA;AAAA,EAAA;AAAA,EAID,mBAAyD;AAC/D,QAAI,KAAK,kBAAkB,QAAW;AACrC,YAAM,gBAAgB,IAAI,qBAAA;AAC1B,WAAK,UAAU,aAAa;AAC5B,oBAAc,SAAS,aAAa;AACpC,WAAK,gBAAgB;AAAA,IACtB;AACA,WAAO,KAAK;AAAA,EACb;AAAA;AAAA,EAKO,MAAM,MAAoD;AAChE,UAAM,SAAS,KAAK,iBAAA,EAAmB,aAAa,aAAa,EAAE,MAAsB,IAAI;AAC7F,WAAO,EAAE,OAAO,OAAO,OAAO,SAAS,kBAAkB,OAAO,KAAK,GAAG,aAAa,OAAO,YAAA;AAAA,EAC7F;AAAA,EAEA,MAAa,UAAU,QAA4C,QAAiC;AACnG,eAAW,cAAc,OAAO,aAAa;AAC5C,aAAO,KAAK,UAAU;AAAA,IACvB;AACA,UAAM,SAAS,MAAM,KAAK,QAAQ,OAAO,OAAO,MAAM;AACtD,WAAO,UAAU;AAAA,EAClB;AAAA,EAEA,MAAa,IAAI,MAAgB,SAAiB,IAAI,UAA2B;AAChF,UAAM,SAAS,KAAK,MAAM,IAAI;AAC9B,WAAO,mBAAmB,OAAO,OAAO;AACxC,WAAO,KAAK,UAAU,QAAQ,MAAM;AAAA,EACrC;AACD;"}
@@ -26,19 +26,37 @@ class CommandConfiguration {
26
26
  this.options.push(option);
27
27
  return new Option.OptionConfiguration(option);
28
28
  }
29
- validate() {
29
+ validate(reservedOptions = []) {
30
30
  for (let argument of this.arguments) {
31
31
  }
32
+ for (const option of this.options) {
33
+ const collision = reservedOptions.find(
34
+ (reserved) => reserved.name === option.name || option.shortcut !== void 0 && reserved.shortcut === option.shortcut
35
+ );
36
+ if (collision !== void 0) {
37
+ throw new InvalidConfigurationError(`Option --${option.name} conflicts with reserved global option --${collision.name}`);
38
+ }
39
+ }
32
40
  }
33
41
  getDescription() {
34
42
  return this.descriptionValue;
35
43
  }
36
- createParser() {
37
- return new InputParser.InputParser(this.arguments, this.options);
44
+ getArguments() {
45
+ return [...this.arguments];
46
+ }
47
+ getOptions() {
48
+ return [...this.options];
49
+ }
50
+ /** Appends shared options for a single parser pass after configuration validation. */
51
+ createParser(extraOptions = []) {
52
+ return new InputParser.InputParser(this.arguments, Option.mergeOptions(this.options, extraOptions));
38
53
  }
39
54
  getUsage(args = {}) {
40
55
  return UsageFormatter.UsageFormatter.format(this.arguments, this.options, args);
41
56
  }
42
57
  }
58
+ class InvalidConfigurationError extends Error {
59
+ }
43
60
  exports.CommandConfiguration = CommandConfiguration;
61
+ exports.InvalidConfigurationError = InvalidConfigurationError;
44
62
  //# sourceMappingURL=CommandConfiguration.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"CommandConfiguration.cjs","sources":["../../../../../packages/cli-common/src/application/CommandConfiguration.ts"],"sourcesContent":["import { Argument, ArgumentConfiguration } from './Argument.js'\nimport { Option, OptionConfiguration, OptionMode } from './Option.js'\nimport { InputParser } from './InputParser.js'\nimport { UsageFormat, UsageFormatter } from './UsageFormatter.js'\nimport { Arguments, Options } from './Input.js'\n\nexport class CommandConfiguration<Args extends Arguments, TOptions extends Options> {\n\tprivate descriptionValue: string = ''\n\n\tprivate arguments: Argument[] = []\n\tprivate options: Option[] = []\n\n\tpublic description(description: string) {\n\t\tthis.descriptionValue = description\n\t}\n\n\tpublic argument(name: Extract<keyof Args, string>): ArgumentConfiguration {\n\t\tconst options = { name, optional: false, variadic: false }\n\t\tthis.arguments.push(options)\n\t\treturn new ArgumentConfiguration(options)\n\t}\n\n\tpublic option(name: Extract<keyof TOptions, string>): OptionConfiguration {\n\t\tconst option: Option = { name, required: false, mode: OptionMode.VALUE_NONE, deprecated: false }\n\t\tthis.options.push(option)\n\t\treturn new OptionConfiguration(option)\n\t}\n\n\tpublic validate() {\n\t\tlet hasVariadic = false\n\t\tlet hasOptional = false\n\t\tfor (let argument of this.arguments) {\n\t\t\tif (hasOptional && !argument.optional) {\n\t\t\t\tthrow new InvalidConfigurationError(`Required argument ${argument.name} cannot follow an optional `)\n\t\t\t}\n\t\t\tif (hasVariadic) {\n\t\t\t\tthrow new InvalidConfigurationError(`A variadic argument must be the last`)\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic getDescription(): string {\n\t\treturn this.descriptionValue\n\t}\n\n\tpublic createParser(): InputParser {\n\t\treturn new InputParser(this.arguments, this.options)\n\t}\n\n\tpublic getUsage(args: { format?: UsageFormat; indent?: string } = {}): string {\n\t\treturn UsageFormatter.format(this.arguments, this.options, args)\n\t}\n}\n\nexport class InvalidConfigurationError extends Error {}\n"],"names":["ArgumentConfiguration","OptionMode","OptionConfiguration","InputParser","UsageFormatter"],"mappings":";;;;;;;;;AAMO,MAAM,qBAAuE;AAAA,EAA7E,cAAA;AACN,kBAAA,MAAQ,oBAA2B,EAAA;AAEnC,kBAAA,MAAQ,aAAwB,EAAC;AACjC,kBAAA,MAAQ,WAAoB,EAAC;AAAA,EAAA;AAAA,EAEtB,YAAY,aAAqB;AACvC,SAAK,mBAAmB;AAAA,EACzB;AAAA,EAEO,SAAS,MAA0D;AACzE,UAAM,UAAU,EAAE,MAAM,UAAU,OAAO,UAAU,MAAA;AACnD,SAAK,UAAU,KAAK,OAAO;AAC3B,WAAO,IAAIA,SAAAA,sBAAsB,OAAO;AAAA,EACzC;AAAA,EAEO,OAAO,MAA4D;AACzE,UAAM,SAAiB,EAAE,MAAM,UAAU,OAAO,MAAMC,kBAAW,YAAY,YAAY,MAAA;AACzF,SAAK,QAAQ,KAAK,MAAM;AACxB,WAAO,IAAIC,OAAAA,oBAAoB,MAAM;AAAA,EACtC;AAAA,EAEO,WAAW;AAGjB,aAAS,YAAY,KAAK,WAAW;AAAA,IAOrC;AAAA,EACD;AAAA,EAEO,iBAAyB;AAC/B,WAAO,KAAK;AAAA,EACb;AAAA,EAEO,eAA4B;AAClC,WAAO,IAAIC,YAAAA,YAAY,KAAK,WAAW,KAAK,OAAO;AAAA,EACpD;AAAA,EAEO,SAAS,OAAkD,IAAY;AAC7E,WAAOC,eAAAA,eAAe,OAAO,KAAK,WAAW,KAAK,SAAS,IAAI;AAAA,EAChE;AACD;;"}
1
+ {"version":3,"file":"CommandConfiguration.cjs","sources":["../../../../../packages/cli-common/src/application/CommandConfiguration.ts"],"sourcesContent":["import { Argument, ArgumentConfiguration } from './Argument.js'\nimport { mergeOptions, Option, OptionConfiguration, OptionMode } from './Option.js'\nimport { InputParser } from './InputParser.js'\nimport { UsageFormat, UsageFormatter } from './UsageFormatter.js'\nimport { Arguments, Options } from './Input.js'\n\nexport class CommandConfiguration<Args extends Arguments, TOptions extends Options> {\n\tprivate descriptionValue: string = ''\n\n\tprivate arguments: Argument[] = []\n\tprivate options: Option[] = []\n\n\tpublic description(description: string) {\n\t\tthis.descriptionValue = description\n\t}\n\n\tpublic argument(name: Extract<keyof Args, string>): ArgumentConfiguration {\n\t\tconst options = { name, optional: false, variadic: false }\n\t\tthis.arguments.push(options)\n\t\treturn new ArgumentConfiguration(options)\n\t}\n\n\tpublic option(name: Extract<keyof TOptions, string>): OptionConfiguration {\n\t\tconst option: Option = { name, required: false, mode: OptionMode.VALUE_NONE, deprecated: false }\n\t\tthis.options.push(option)\n\t\treturn new OptionConfiguration(option)\n\t}\n\n\tpublic validate(reservedOptions: readonly Option[] = []) {\n\t\tlet hasVariadic = false\n\t\tlet hasOptional = false\n\t\tfor (let argument of this.arguments) {\n\t\t\tif (hasOptional && !argument.optional) {\n\t\t\t\tthrow new InvalidConfigurationError(`Required argument ${argument.name} cannot follow an optional `)\n\t\t\t}\n\t\t\tif (hasVariadic) {\n\t\t\t\tthrow new InvalidConfigurationError(`A variadic argument must be the last`)\n\t\t\t}\n\t\t}\n\t\tfor (const option of this.options) {\n\t\t\tconst collision = reservedOptions.find(reserved =>\n\t\t\t\treserved.name === option.name\n\t\t\t\t|| (option.shortcut !== undefined && reserved.shortcut === option.shortcut)\n\t\t\t)\n\t\t\tif (collision !== undefined) {\n\t\t\t\tthrow new InvalidConfigurationError(`Option --${option.name} conflicts with reserved global option --${collision.name}`)\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic getDescription(): string {\n\t\treturn this.descriptionValue\n\t}\n\n\tpublic getArguments(): readonly Argument[] {\n\t\treturn [...this.arguments]\n\t}\n\n\tpublic getOptions(): readonly Option[] {\n\t\treturn [...this.options]\n\t}\n\n\t/** Appends shared options for a single parser pass after configuration validation. */\n\tpublic createParser(extraOptions: readonly Option[] = []): InputParser {\n\t\treturn new InputParser(this.arguments, mergeOptions(this.options, extraOptions))\n\t}\n\n\tpublic getUsage(args: { format?: UsageFormat; indent?: string } = {}): string {\n\t\treturn UsageFormatter.format(this.arguments, this.options, args)\n\t}\n}\n\nexport class InvalidConfigurationError extends Error {}\n"],"names":["ArgumentConfiguration","OptionMode","OptionConfiguration","InputParser","mergeOptions","UsageFormatter"],"mappings":";;;;;;;;;AAMO,MAAM,qBAAuE;AAAA,EAA7E,cAAA;AACN,kBAAA,MAAQ,oBAA2B,EAAA;AAEnC,kBAAA,MAAQ,aAAwB,EAAC;AACjC,kBAAA,MAAQ,WAAoB,EAAC;AAAA,EAAA;AAAA,EAEtB,YAAY,aAAqB;AACvC,SAAK,mBAAmB;AAAA,EACzB;AAAA,EAEO,SAAS,MAA0D;AACzE,UAAM,UAAU,EAAE,MAAM,UAAU,OAAO,UAAU,MAAA;AACnD,SAAK,UAAU,KAAK,OAAO;AAC3B,WAAO,IAAIA,SAAAA,sBAAsB,OAAO;AAAA,EACzC;AAAA,EAEO,OAAO,MAA4D;AACzE,UAAM,SAAiB,EAAE,MAAM,UAAU,OAAO,MAAMC,kBAAW,YAAY,YAAY,MAAA;AACzF,SAAK,QAAQ,KAAK,MAAM;AACxB,WAAO,IAAIC,OAAAA,oBAAoB,MAAM;AAAA,EACtC;AAAA,EAEO,SAAS,kBAAqC,IAAI;AAGxD,aAAS,YAAY,KAAK,WAAW;AAAA,IAOrC;AACA,eAAW,UAAU,KAAK,SAAS;AAClC,YAAM,YAAY,gBAAgB;AAAA,QAAK,CAAA,aACtC,SAAS,SAAS,OAAO,QACrB,OAAO,aAAa,UAAa,SAAS,aAAa,OAAO;AAAA,MAAA;AAEnE,UAAI,cAAc,QAAW;AAC5B,cAAM,IAAI,0BAA0B,YAAY,OAAO,IAAI,4CAA4C,UAAU,IAAI,EAAE;AAAA,MACxH;AAAA,IACD;AAAA,EACD;AAAA,EAEO,iBAAyB;AAC/B,WAAO,KAAK;AAAA,EACb;AAAA,EAEO,eAAoC;AAC1C,WAAO,CAAC,GAAG,KAAK,SAAS;AAAA,EAC1B;AAAA,EAEO,aAAgC;AACtC,WAAO,CAAC,GAAG,KAAK,OAAO;AAAA,EACxB;AAAA;AAAA,EAGO,aAAa,eAAkC,IAAiB;AACtE,WAAO,IAAIC,YAAAA,YAAY,KAAK,WAAWC,OAAAA,aAAa,KAAK,SAAS,YAAY,CAAC;AAAA,EAChF;AAAA,EAEO,SAAS,OAAkD,IAAY;AAC7E,WAAOC,eAAAA,eAAe,OAAO,KAAK,WAAW,KAAK,SAAS,IAAI;AAAA,EAChE;AACD;AAEO,MAAM,kCAAkC,MAAM;AAAC;;;"}
@@ -1,5 +1,5 @@
1
1
  import { ArgumentConfiguration } from "./Argument.js";
2
- import { OptionMode, OptionConfiguration } from "./Option.js";
2
+ import { OptionMode, OptionConfiguration, mergeOptions } from "./Option.js";
3
3
  import { InputParser } from "./InputParser.js";
4
4
  import { UsageFormatter } from "./UsageFormatter.js";
5
5
  var __defProp = Object.defineProperty;
@@ -24,21 +24,39 @@ class CommandConfiguration {
24
24
  this.options.push(option);
25
25
  return new OptionConfiguration(option);
26
26
  }
27
- validate() {
27
+ validate(reservedOptions = []) {
28
28
  for (let argument of this.arguments) {
29
29
  }
30
+ for (const option of this.options) {
31
+ const collision = reservedOptions.find(
32
+ (reserved) => reserved.name === option.name || option.shortcut !== void 0 && reserved.shortcut === option.shortcut
33
+ );
34
+ if (collision !== void 0) {
35
+ throw new InvalidConfigurationError(`Option --${option.name} conflicts with reserved global option --${collision.name}`);
36
+ }
37
+ }
30
38
  }
31
39
  getDescription() {
32
40
  return this.descriptionValue;
33
41
  }
34
- createParser() {
35
- return new InputParser(this.arguments, this.options);
42
+ getArguments() {
43
+ return [...this.arguments];
44
+ }
45
+ getOptions() {
46
+ return [...this.options];
47
+ }
48
+ /** Appends shared options for a single parser pass after configuration validation. */
49
+ createParser(extraOptions = []) {
50
+ return new InputParser(this.arguments, mergeOptions(this.options, extraOptions));
36
51
  }
37
52
  getUsage(args = {}) {
38
53
  return UsageFormatter.format(this.arguments, this.options, args);
39
54
  }
40
55
  }
56
+ class InvalidConfigurationError extends Error {
57
+ }
41
58
  export {
42
- CommandConfiguration
59
+ CommandConfiguration,
60
+ InvalidConfigurationError
43
61
  };
44
62
  //# sourceMappingURL=CommandConfiguration.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"CommandConfiguration.js","sources":["../../../../../packages/cli-common/src/application/CommandConfiguration.ts"],"sourcesContent":["import { Argument, ArgumentConfiguration } from './Argument.js'\nimport { Option, OptionConfiguration, OptionMode } from './Option.js'\nimport { InputParser } from './InputParser.js'\nimport { UsageFormat, UsageFormatter } from './UsageFormatter.js'\nimport { Arguments, Options } from './Input.js'\n\nexport class CommandConfiguration<Args extends Arguments, TOptions extends Options> {\n\tprivate descriptionValue: string = ''\n\n\tprivate arguments: Argument[] = []\n\tprivate options: Option[] = []\n\n\tpublic description(description: string) {\n\t\tthis.descriptionValue = description\n\t}\n\n\tpublic argument(name: Extract<keyof Args, string>): ArgumentConfiguration {\n\t\tconst options = { name, optional: false, variadic: false }\n\t\tthis.arguments.push(options)\n\t\treturn new ArgumentConfiguration(options)\n\t}\n\n\tpublic option(name: Extract<keyof TOptions, string>): OptionConfiguration {\n\t\tconst option: Option = { name, required: false, mode: OptionMode.VALUE_NONE, deprecated: false }\n\t\tthis.options.push(option)\n\t\treturn new OptionConfiguration(option)\n\t}\n\n\tpublic validate() {\n\t\tlet hasVariadic = false\n\t\tlet hasOptional = false\n\t\tfor (let argument of this.arguments) {\n\t\t\tif (hasOptional && !argument.optional) {\n\t\t\t\tthrow new InvalidConfigurationError(`Required argument ${argument.name} cannot follow an optional `)\n\t\t\t}\n\t\t\tif (hasVariadic) {\n\t\t\t\tthrow new InvalidConfigurationError(`A variadic argument must be the last`)\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic getDescription(): string {\n\t\treturn this.descriptionValue\n\t}\n\n\tpublic createParser(): InputParser {\n\t\treturn new InputParser(this.arguments, this.options)\n\t}\n\n\tpublic getUsage(args: { format?: UsageFormat; indent?: string } = {}): string {\n\t\treturn UsageFormatter.format(this.arguments, this.options, args)\n\t}\n}\n\nexport class InvalidConfigurationError extends Error {}\n"],"names":[],"mappings":";;;;;;;AAMO,MAAM,qBAAuE;AAAA,EAA7E,cAAA;AACN,kBAAA,MAAQ,oBAA2B,EAAA;AAEnC,kBAAA,MAAQ,aAAwB,EAAC;AACjC,kBAAA,MAAQ,WAAoB,EAAC;AAAA,EAAA;AAAA,EAEtB,YAAY,aAAqB;AACvC,SAAK,mBAAmB;AAAA,EACzB;AAAA,EAEO,SAAS,MAA0D;AACzE,UAAM,UAAU,EAAE,MAAM,UAAU,OAAO,UAAU,MAAA;AACnD,SAAK,UAAU,KAAK,OAAO;AAC3B,WAAO,IAAI,sBAAsB,OAAO;AAAA,EACzC;AAAA,EAEO,OAAO,MAA4D;AACzE,UAAM,SAAiB,EAAE,MAAM,UAAU,OAAO,MAAM,WAAW,YAAY,YAAY,MAAA;AACzF,SAAK,QAAQ,KAAK,MAAM;AACxB,WAAO,IAAI,oBAAoB,MAAM;AAAA,EACtC;AAAA,EAEO,WAAW;AAGjB,aAAS,YAAY,KAAK,WAAW;AAAA,IAOrC;AAAA,EACD;AAAA,EAEO,iBAAyB;AAC/B,WAAO,KAAK;AAAA,EACb;AAAA,EAEO,eAA4B;AAClC,WAAO,IAAI,YAAY,KAAK,WAAW,KAAK,OAAO;AAAA,EACpD;AAAA,EAEO,SAAS,OAAkD,IAAY;AAC7E,WAAO,eAAe,OAAO,KAAK,WAAW,KAAK,SAAS,IAAI;AAAA,EAChE;AACD;"}
1
+ {"version":3,"file":"CommandConfiguration.js","sources":["../../../../../packages/cli-common/src/application/CommandConfiguration.ts"],"sourcesContent":["import { Argument, ArgumentConfiguration } from './Argument.js'\nimport { mergeOptions, Option, OptionConfiguration, OptionMode } from './Option.js'\nimport { InputParser } from './InputParser.js'\nimport { UsageFormat, UsageFormatter } from './UsageFormatter.js'\nimport { Arguments, Options } from './Input.js'\n\nexport class CommandConfiguration<Args extends Arguments, TOptions extends Options> {\n\tprivate descriptionValue: string = ''\n\n\tprivate arguments: Argument[] = []\n\tprivate options: Option[] = []\n\n\tpublic description(description: string) {\n\t\tthis.descriptionValue = description\n\t}\n\n\tpublic argument(name: Extract<keyof Args, string>): ArgumentConfiguration {\n\t\tconst options = { name, optional: false, variadic: false }\n\t\tthis.arguments.push(options)\n\t\treturn new ArgumentConfiguration(options)\n\t}\n\n\tpublic option(name: Extract<keyof TOptions, string>): OptionConfiguration {\n\t\tconst option: Option = { name, required: false, mode: OptionMode.VALUE_NONE, deprecated: false }\n\t\tthis.options.push(option)\n\t\treturn new OptionConfiguration(option)\n\t}\n\n\tpublic validate(reservedOptions: readonly Option[] = []) {\n\t\tlet hasVariadic = false\n\t\tlet hasOptional = false\n\t\tfor (let argument of this.arguments) {\n\t\t\tif (hasOptional && !argument.optional) {\n\t\t\t\tthrow new InvalidConfigurationError(`Required argument ${argument.name} cannot follow an optional `)\n\t\t\t}\n\t\t\tif (hasVariadic) {\n\t\t\t\tthrow new InvalidConfigurationError(`A variadic argument must be the last`)\n\t\t\t}\n\t\t}\n\t\tfor (const option of this.options) {\n\t\t\tconst collision = reservedOptions.find(reserved =>\n\t\t\t\treserved.name === option.name\n\t\t\t\t|| (option.shortcut !== undefined && reserved.shortcut === option.shortcut)\n\t\t\t)\n\t\t\tif (collision !== undefined) {\n\t\t\t\tthrow new InvalidConfigurationError(`Option --${option.name} conflicts with reserved global option --${collision.name}`)\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic getDescription(): string {\n\t\treturn this.descriptionValue\n\t}\n\n\tpublic getArguments(): readonly Argument[] {\n\t\treturn [...this.arguments]\n\t}\n\n\tpublic getOptions(): readonly Option[] {\n\t\treturn [...this.options]\n\t}\n\n\t/** Appends shared options for a single parser pass after configuration validation. */\n\tpublic createParser(extraOptions: readonly Option[] = []): InputParser {\n\t\treturn new InputParser(this.arguments, mergeOptions(this.options, extraOptions))\n\t}\n\n\tpublic getUsage(args: { format?: UsageFormat; indent?: string } = {}): string {\n\t\treturn UsageFormatter.format(this.arguments, this.options, args)\n\t}\n}\n\nexport class InvalidConfigurationError extends Error {}\n"],"names":[],"mappings":";;;;;;;AAMO,MAAM,qBAAuE;AAAA,EAA7E,cAAA;AACN,kBAAA,MAAQ,oBAA2B,EAAA;AAEnC,kBAAA,MAAQ,aAAwB,EAAC;AACjC,kBAAA,MAAQ,WAAoB,EAAC;AAAA,EAAA;AAAA,EAEtB,YAAY,aAAqB;AACvC,SAAK,mBAAmB;AAAA,EACzB;AAAA,EAEO,SAAS,MAA0D;AACzE,UAAM,UAAU,EAAE,MAAM,UAAU,OAAO,UAAU,MAAA;AACnD,SAAK,UAAU,KAAK,OAAO;AAC3B,WAAO,IAAI,sBAAsB,OAAO;AAAA,EACzC;AAAA,EAEO,OAAO,MAA4D;AACzE,UAAM,SAAiB,EAAE,MAAM,UAAU,OAAO,MAAM,WAAW,YAAY,Y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