@alcyone-labs/arg-parser 1.0.0 → 1.1.0
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 +844 -45
- package/dist/examples/fuzzy-demo.d.ts +8 -0
- package/dist/examples/fuzzy-demo.d.ts.map +1 -0
- package/dist/examples/fuzzy-test-example.d.ts +8 -0
- package/dist/examples/fuzzy-test-example.d.ts.map +1 -0
- package/dist/examples/fzf-search-cli.d.ts +8 -0
- package/dist/examples/fzf-search-cli.d.ts.map +1 -0
- package/dist/examples/getting-started.d.ts +27 -0
- package/dist/examples/getting-started.d.ts.map +1 -0
- package/dist/examples/mcp-preset-transports.d.ts +19 -0
- package/dist/examples/mcp-preset-transports.d.ts.map +1 -0
- package/dist/examples/simple-cli.d.ts +26 -0
- package/dist/examples/simple-cli.d.ts.map +1 -0
- package/dist/examples/v1.1.0-showcase.d.ts +16 -0
- package/dist/examples/v1.1.0-showcase.d.ts.map +1 -0
- package/dist/examples/with-env-example.d.ts +3 -0
- package/dist/examples/with-env-example.d.ts.map +1 -0
- package/dist/index-6G9StDO_.js +6445 -0
- package/dist/index-6G9StDO_.js.map +1 -0
- package/dist/index-CqU7Fj3C.cjs +6444 -0
- package/dist/index-CqU7Fj3C.cjs.map +1 -0
- package/dist/index-Dx_q1msW.js +4682 -0
- package/dist/index-Dx_q1msW.js.map +1 -0
- package/dist/index.cjs +1704 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.min.mjs +1628 -484
- package/dist/index.min.mjs.map +1 -1
- package/dist/index.mjs +1660 -59
- package/dist/index.mjs.map +1 -1
- package/dist/src/ArgParser.d.ts +156 -0
- package/dist/src/ArgParser.d.ts.map +1 -0
- package/dist/{ArgParser.d.ts → src/ArgParserBase.d.ts} +20 -13
- package/dist/src/ArgParserBase.d.ts.map +1 -0
- package/dist/{FlagManager.d.ts → src/FlagManager.d.ts} +1 -1
- package/dist/src/FlagManager.d.ts.map +1 -0
- package/dist/src/fuzzy-test-cli.d.ts +5 -0
- package/dist/src/fuzzy-test-cli.d.ts.map +1 -0
- package/dist/src/fuzzy-tester.d.ts +101 -0
- package/dist/src/fuzzy-tester.d.ts.map +1 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/mcp-integration.d.ts +31 -0
- package/dist/src/mcp-integration.d.ts.map +1 -0
- package/dist/src/types.d.ts +154 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/sse-B5Jf_YpG.cjs +121 -0
- package/dist/sse-B5Jf_YpG.cjs.map +1 -0
- package/dist/sse-BDL3h2Ll.js +121 -0
- package/dist/sse-BDL3h2Ll.js.map +1 -0
- package/dist/sse-DSjLfGFo.js +107 -0
- package/dist/sse-DSjLfGFo.js.map +1 -0
- package/dist/stdio-Cf19UQO7.js +70 -0
- package/dist/stdio-Cf19UQO7.js.map +1 -0
- package/dist/stdio-DESvSONI.cjs +94 -0
- package/dist/stdio-DESvSONI.cjs.map +1 -0
- package/dist/stdio-DLOResWr.js +94 -0
- package/dist/stdio-DLOResWr.js.map +1 -0
- package/dist/streamableHttp-DXIdDSbF.js +342 -0
- package/dist/streamableHttp-DXIdDSbF.js.map +1 -0
- package/dist/streamableHttp-DsXlAnqJ.cjs +456 -0
- package/dist/streamableHttp-DsXlAnqJ.cjs.map +1 -0
- package/dist/streamableHttp-Vd4Qsgko.js +456 -0
- package/dist/streamableHttp-Vd4Qsgko.js.map +1 -0
- package/dist/types-DSxPEImy.cjs +943 -0
- package/dist/types-DSxPEImy.cjs.map +1 -0
- package/dist/types-DdsPVLQ5.js +846 -0
- package/dist/types-DdsPVLQ5.js.map +1 -0
- package/dist/types-DpK81FWv.js +944 -0
- package/dist/types-DpK81FWv.js.map +1 -0
- package/package.json +18 -7
- package/dist/ArgParser.d.ts.map +0 -1
- package/dist/FlagManager.d.ts.map +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/types.d.ts +0 -91
- package/dist/types.d.ts.map +0 -1
package/dist/index.min.mjs
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
1
|
-
var
|
|
1
|
+
var Ve = (g) => {
|
|
2
2
|
throw TypeError(g);
|
|
3
3
|
};
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
var be = (g, e, n) => e.has(g) || Ve("Cannot " + n);
|
|
5
|
+
var c = (g, e, n) => (be(g, e, "read from private field"), n ? n.call(g) : e.get(g)), _ = (g, e, n) => e.has(g) ? Ve("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(g) : e.set(g, n), H = (g, e, n, s) => (be(g, e, "write to private field"), s ? s.call(g, n) : e.set(g, n), n), $ = (g, e, n) => (be(g, e, "access private method"), n);
|
|
6
|
+
import * as G from "node:fs";
|
|
7
|
+
import * as ce from "node:path";
|
|
8
|
+
import S from "chalk";
|
|
9
|
+
import { createRegExp as st, anyOf as at, oneOrMore as ot, char as it } from "magic-regexp";
|
|
10
|
+
import * as De from "js-yaml";
|
|
11
|
+
import * as He from "@iarna/toml";
|
|
12
|
+
import * as lt from "dotenv";
|
|
13
|
+
import { z as p } from "zod";
|
|
14
|
+
const ct = p.object({
|
|
15
|
+
name: p.string().min(1, "Flag name cannot be empty").describe(
|
|
11
16
|
"The output property name, used as a return key `{name: value}`. Must be unique."
|
|
12
17
|
),
|
|
13
|
-
allowLigature:
|
|
18
|
+
allowLigature: p.boolean().default(!0).describe(
|
|
14
19
|
"Enable both forms of flag input, e.g., `./script.js -f=value` and `-f value`."
|
|
15
20
|
),
|
|
16
|
-
allowMultiple:
|
|
21
|
+
allowMultiple: p.boolean().default(!1).describe(
|
|
17
22
|
"Allow passing the same flag multiple times, e.g., `-f val1 -f val2` results in an array."
|
|
18
23
|
),
|
|
19
|
-
description:
|
|
20
|
-
options:
|
|
21
|
-
defaultValue:
|
|
22
|
-
type:
|
|
23
|
-
|
|
24
|
+
description: p.union([p.string(), p.array(p.string())]).describe("Textual description for help messages."),
|
|
25
|
+
options: p.array(p.string().min(1)).min(1, "Flag must have at least one option (e.g., ['-f', '--flag'])").describe("Array of option strings, e.g., ['-f', '--flag']."),
|
|
26
|
+
defaultValue: p.any().optional().describe("Default value if the flag is not provided."),
|
|
27
|
+
type: p.union([
|
|
28
|
+
p.any().refine((g) => g === String, {
|
|
24
29
|
message: "Must be String constructor"
|
|
25
30
|
}),
|
|
26
|
-
|
|
31
|
+
p.any().refine((g) => g === Number, {
|
|
27
32
|
message: "Must be Number constructor"
|
|
28
33
|
}),
|
|
29
|
-
|
|
34
|
+
p.any().refine((g) => g === Boolean, {
|
|
30
35
|
message: "Must be Boolean constructor"
|
|
31
36
|
}),
|
|
32
|
-
|
|
37
|
+
p.any().refine((g) => g === Array, {
|
|
38
|
+
// Native Array constructor
|
|
33
39
|
message: "Must be Array constructor"
|
|
34
40
|
}),
|
|
35
|
-
|
|
41
|
+
p.any().refine((g) => g === Object, {
|
|
42
|
+
// Native Object constructor
|
|
36
43
|
message: "Must be Object constructor"
|
|
37
44
|
}),
|
|
38
|
-
|
|
39
|
-
// Custom parser function
|
|
40
|
-
|
|
45
|
+
p.function().args(p.string()).returns(p.any()),
|
|
46
|
+
// Custom parser function (value: string) => any
|
|
47
|
+
p.string().refine(
|
|
48
|
+
// String literal types
|
|
41
49
|
(g) => ["boolean", "string", "number", "array", "object"].includes(
|
|
42
50
|
g.toLowerCase()
|
|
43
51
|
),
|
|
@@ -46,75 +54,75 @@ const U = {}, he = f.object({
|
|
|
46
54
|
}
|
|
47
55
|
)
|
|
48
56
|
]).default("string").describe(
|
|
49
|
-
"Expected data type or a custom parser function. Defaults to 'string'."
|
|
57
|
+
"Expected data type (constructor or string literal) or a custom parser function. Defaults to 'string'."
|
|
50
58
|
),
|
|
51
|
-
mandatory:
|
|
59
|
+
mandatory: p.union([p.boolean(), p.function().args(p.any()).returns(p.boolean())]).optional().describe(
|
|
52
60
|
"Makes the flag mandatory, can be a boolean or a function conditional on other args."
|
|
53
61
|
),
|
|
54
|
-
flagOnly:
|
|
62
|
+
flagOnly: p.boolean().default(!1).describe(
|
|
55
63
|
"If true, the flag's presence is noted (true/false), and any subsequent value is not consumed by this flag."
|
|
56
64
|
),
|
|
57
|
-
validate:
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
65
|
+
validate: p.function().args(p.any().optional(), p.any().optional()).returns(
|
|
66
|
+
p.union([
|
|
67
|
+
p.boolean(),
|
|
68
|
+
p.string(),
|
|
69
|
+
p.void(),
|
|
70
|
+
p.promise(p.union([p.boolean(), p.string(), p.void()]))
|
|
63
71
|
])
|
|
64
72
|
).optional().describe(
|
|
65
73
|
"Custom validation function for the flag's value (receives value, parsedArgs)."
|
|
66
74
|
),
|
|
67
|
-
enum:
|
|
75
|
+
enum: p.array(p.any()).optional().describe("Array of allowed values for the flag.")
|
|
68
76
|
}).passthrough().transform((g) => {
|
|
69
77
|
const e = { ...g };
|
|
70
78
|
return "default" in e && e.default !== void 0 && !("defaultValue" in e) && (e.defaultValue = e.default), "required" in e && e.required !== void 0 && !("mandatory" in e) && (e.mandatory = e.required), e;
|
|
71
79
|
});
|
|
72
|
-
var
|
|
73
|
-
const
|
|
80
|
+
var J, me;
|
|
81
|
+
const Me = class Me {
|
|
74
82
|
constructor(e = {}, n = []) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
83
|
+
_(this, J, /* @__PURE__ */ new Map());
|
|
84
|
+
_(this, me);
|
|
85
|
+
H(this, me, e.throwForDuplicateFlags ?? !1), this.addFlags(n);
|
|
78
86
|
}
|
|
79
87
|
static _safeFlag(e) {
|
|
80
|
-
const n =
|
|
81
|
-
let
|
|
82
|
-
const
|
|
83
|
-
if (typeof
|
|
84
|
-
switch (
|
|
88
|
+
const n = ct.parse(e);
|
|
89
|
+
let s;
|
|
90
|
+
const r = n.type;
|
|
91
|
+
if (typeof r == "string")
|
|
92
|
+
switch (r.toLowerCase()) {
|
|
85
93
|
case "boolean":
|
|
86
|
-
|
|
94
|
+
s = Boolean;
|
|
87
95
|
break;
|
|
88
96
|
case "string":
|
|
89
|
-
|
|
97
|
+
s = String;
|
|
90
98
|
break;
|
|
91
99
|
case "number":
|
|
92
|
-
|
|
100
|
+
s = Number;
|
|
93
101
|
break;
|
|
94
102
|
case "array":
|
|
95
|
-
|
|
103
|
+
s = Array;
|
|
96
104
|
break;
|
|
97
105
|
case "object":
|
|
98
|
-
|
|
106
|
+
s = Object;
|
|
99
107
|
break;
|
|
100
108
|
default:
|
|
101
|
-
throw new Error(`Invalid type string: ${
|
|
109
|
+
throw new Error(`Invalid type string: ${r}`);
|
|
102
110
|
}
|
|
103
111
|
else
|
|
104
|
-
|
|
112
|
+
s = r;
|
|
105
113
|
return {
|
|
106
114
|
...n,
|
|
107
115
|
options: n.options,
|
|
108
|
-
type:
|
|
116
|
+
type: s,
|
|
109
117
|
validate: n.validate,
|
|
110
118
|
enum: n.enum,
|
|
111
119
|
mandatory: n.mandatory
|
|
112
120
|
};
|
|
113
121
|
}
|
|
114
122
|
addFlag(e) {
|
|
115
|
-
const n =
|
|
116
|
-
if (
|
|
117
|
-
if (
|
|
123
|
+
const n = Me._safeFlag(e);
|
|
124
|
+
if (c(this, J).has(n.name)) {
|
|
125
|
+
if (c(this, me))
|
|
118
126
|
throw new Error(
|
|
119
127
|
`FlagManager: Flag '${n.name}' already exists.`
|
|
120
128
|
);
|
|
@@ -122,10 +130,10 @@ const oe = class oe {
|
|
|
122
130
|
`Warning: FlagManager: Flag '${n.name}' already exists. Duplicate not added.`
|
|
123
131
|
), this;
|
|
124
132
|
}
|
|
125
|
-
return
|
|
133
|
+
return c(this, J).set(n.name, n), this;
|
|
126
134
|
}
|
|
127
135
|
_setProcessedFlagForInheritance(e) {
|
|
128
|
-
return
|
|
136
|
+
return c(this, J).has(e.name) ? this : (c(this, J).set(e.name, e), this);
|
|
129
137
|
}
|
|
130
138
|
addFlags(e) {
|
|
131
139
|
for (const n of e)
|
|
@@ -133,54 +141,55 @@ const oe = class oe {
|
|
|
133
141
|
return this;
|
|
134
142
|
}
|
|
135
143
|
hasFlag(e) {
|
|
136
|
-
return
|
|
144
|
+
return c(this, J).has(e);
|
|
137
145
|
}
|
|
138
146
|
getFlag(e) {
|
|
139
|
-
return
|
|
147
|
+
return c(this, J).get(e);
|
|
140
148
|
}
|
|
141
149
|
get flags() {
|
|
142
|
-
return Array.from(
|
|
150
|
+
return Array.from(c(this, J).values());
|
|
143
151
|
}
|
|
144
152
|
get flagNames() {
|
|
145
|
-
return Array.from(
|
|
153
|
+
return Array.from(c(this, J).values()).map((e) => e.name);
|
|
146
154
|
}
|
|
147
155
|
};
|
|
148
|
-
|
|
149
|
-
let
|
|
150
|
-
class
|
|
156
|
+
J = new WeakMap(), me = new WeakMap();
|
|
157
|
+
let $e = Me;
|
|
158
|
+
class Y extends Error {
|
|
151
159
|
constructor(e, n = []) {
|
|
152
160
|
super(e), this.cmdChain = n, this.name = "ArgParserError", this.commandChain = n;
|
|
153
161
|
}
|
|
154
162
|
}
|
|
155
|
-
var
|
|
156
|
-
const
|
|
163
|
+
var P, V, R, z, W, ee, L, q, Q, ge, U, T, M, ne, m, oe, Re, Le, we, ze, ue, ve, Se, Ce, Ae, Fe, Ne, We, Je, Be, qe, Ue, Ze, Ge, Ye, Ke, Qe, Xe, et, tt, nt, de;
|
|
164
|
+
const K = class K {
|
|
157
165
|
constructor(e = {}, n) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
166
|
+
_(this, m);
|
|
167
|
+
_(this, P, "Argument Parser");
|
|
168
|
+
_(this, V);
|
|
169
|
+
_(this, R, "");
|
|
170
|
+
_(this, z, {
|
|
163
171
|
extraNewLine: !0,
|
|
164
172
|
wrapAtWidth: 50,
|
|
165
173
|
blankSpaceWidth: 30,
|
|
166
174
|
mandatoryCharacter: "*"
|
|
167
175
|
});
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
_(this, W);
|
|
177
|
+
_(this, ee, !1);
|
|
178
|
+
_(this, L);
|
|
179
|
+
_(this, q, !0);
|
|
180
|
+
_(this, Q);
|
|
181
|
+
_(this, ge, {});
|
|
182
|
+
_(this, U, !1);
|
|
183
|
+
_(this, T, /* @__PURE__ */ new Map());
|
|
184
|
+
_(this, M);
|
|
185
|
+
_(this, ne, !1);
|
|
186
|
+
H(this, P, e.appName || "app"), e.blankSpaceWidth && !isNaN(Number(e.blankSpaceWidth)) && Number(e.blankSpaceWidth) > 20 && (c(this, z).blankSpaceWidth = Number(e.blankSpaceWidth)), e.wrapAtWidth && !isNaN(Number(e.wrapAtWidth)) && Number(e.wrapAtWidth) > 30 && (c(this, z).wrapAtWidth = Number(e.wrapAtWidth)), typeof e.extraNewLine == "boolean" && (c(this, z).extraNewLine = !!e.extraNewLine), typeof e.mandatoryCharacter == "string" && (c(this, z).mandatoryCharacter = e.mandatoryCharacter), typeof e.throwForDuplicateFlags == "boolean" && H(this, ee, e.throwForDuplicateFlags), H(this, M, new $e(
|
|
178
187
|
{
|
|
179
|
-
throwForDuplicateFlags:
|
|
188
|
+
throwForDuplicateFlags: c(this, ee)
|
|
180
189
|
},
|
|
181
190
|
n || []
|
|
182
|
-
)),
|
|
183
|
-
const
|
|
191
|
+
)), H(this, q, e.handleErrors ?? !0), H(this, U, e.inheritParentFlags ?? !1), H(this, L, e.description), H(this, W, e.handler), H(this, V, e.appCommandName);
|
|
192
|
+
const s = {
|
|
184
193
|
name: "help",
|
|
185
194
|
description: "Display this help message and exits",
|
|
186
195
|
mandatory: !1,
|
|
@@ -191,61 +200,79 @@ const W = class W {
|
|
|
191
200
|
allowMultiple: !1,
|
|
192
201
|
flagOnly: !0,
|
|
193
202
|
enum: [],
|
|
194
|
-
validate: (
|
|
203
|
+
validate: (r, a) => !0
|
|
195
204
|
// Ensure signature matches Zod schema for .args()
|
|
196
205
|
};
|
|
197
|
-
if (
|
|
198
|
-
for (const
|
|
199
|
-
this.addSubCommand(
|
|
206
|
+
if (c(this, M).addFlag(s), e.subCommands)
|
|
207
|
+
for (const r of e.subCommands)
|
|
208
|
+
this.addSubCommand(r);
|
|
200
209
|
}
|
|
201
210
|
get flags() {
|
|
202
|
-
return
|
|
211
|
+
return c(this, M).flags;
|
|
203
212
|
}
|
|
204
213
|
get flagNames() {
|
|
205
|
-
return
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
+
return c(this, M).flagNames;
|
|
215
|
+
}
|
|
216
|
+
getAppName() {
|
|
217
|
+
return c(this, P);
|
|
218
|
+
}
|
|
219
|
+
getAppCommandName() {
|
|
220
|
+
return c(this, V);
|
|
221
|
+
}
|
|
222
|
+
getSubCommandName() {
|
|
223
|
+
return c(this, R);
|
|
224
|
+
}
|
|
225
|
+
getDescription() {
|
|
226
|
+
return c(this, L);
|
|
227
|
+
}
|
|
228
|
+
getHandler() {
|
|
229
|
+
return c(this, W);
|
|
230
|
+
}
|
|
231
|
+
getSubCommands() {
|
|
232
|
+
return c(this, T);
|
|
233
|
+
}
|
|
234
|
+
_addToOutput(e, n, s, r) {
|
|
235
|
+
let a = n;
|
|
236
|
+
if (e.type === Boolean ? typeof n == "boolean" ? a = n : typeof n == "string" ? a = /(true|yes|1)/i.test(n) : a = new e.type(a) : typeof e.type == "function" ? a = e.type(a) : typeof e.type == "object" && (a = new e.type(a)), e.enum && e.enum.length > 0) {
|
|
237
|
+
const t = e.enum.map((i) => typeof i == "string" ? `'${i}'` : i).join(", ");
|
|
238
|
+
if (!e.enum.includes(a))
|
|
239
|
+
throw new Y(
|
|
240
|
+
`Invalid value '${a}' for flag '${S.yellow(e.name)}'. Allowed values: ${t}`,
|
|
214
241
|
this.getCommandChain()
|
|
215
242
|
);
|
|
216
243
|
}
|
|
217
244
|
if (e.validate) {
|
|
218
|
-
const
|
|
219
|
-
if (
|
|
220
|
-
throw new
|
|
221
|
-
`Validation failed for flag '${
|
|
245
|
+
const t = e.validate(a, s);
|
|
246
|
+
if (t === !1)
|
|
247
|
+
throw new Y(
|
|
248
|
+
`Validation failed for flag '${S.yellow(e.name)}' with value '${a}'`,
|
|
222
249
|
this.getCommandChain()
|
|
223
250
|
);
|
|
224
|
-
if (typeof
|
|
225
|
-
throw new
|
|
251
|
+
if (typeof t == "string")
|
|
252
|
+
throw new Y(t, this.getCommandChain());
|
|
226
253
|
}
|
|
227
|
-
return e.allowMultiple && !Array.isArray(
|
|
254
|
+
return e.allowMultiple && !Array.isArray(s[e.name]) && (s[e.name] = []), e.allowMultiple ? s[e.name].push(a) : s[e.name] = a;
|
|
228
255
|
}
|
|
229
256
|
addFlags(e) {
|
|
230
|
-
return
|
|
257
|
+
return c(this, M).addFlags(e), this;
|
|
231
258
|
}
|
|
232
259
|
addFlag(e) {
|
|
233
|
-
return
|
|
260
|
+
return c(this, M).addFlag(e), this;
|
|
234
261
|
}
|
|
235
262
|
addSubCommand(e) {
|
|
236
|
-
if (
|
|
263
|
+
if (c(this, T).has(e.name))
|
|
237
264
|
throw new Error(`Sub-command '${e.name}' already exists`);
|
|
238
265
|
const n = e.parser;
|
|
239
|
-
if (!(n instanceof
|
|
266
|
+
if (!(n instanceof K))
|
|
240
267
|
throw new Error(
|
|
241
|
-
`Parser for subcommand '${e.name}' is not an instance of
|
|
268
|
+
`Parser for subcommand '${e.name}' is not an instance of ArgParserBase. Please provide 'new ArgParserBase(...)' for the 'parser' property of an ISubCommand.`
|
|
242
269
|
);
|
|
243
|
-
if (
|
|
244
|
-
const
|
|
245
|
-
for (const
|
|
246
|
-
|
|
270
|
+
if (H(n, Q, this), H(n, R, e.name), !c(n, V) && c(this, V) && H(n, V, c(this, V)), c(n, U)) {
|
|
271
|
+
const s = c(this, M).flags;
|
|
272
|
+
for (const r of s)
|
|
273
|
+
c(n, M).hasFlag(r.name) || c(n, M)._setProcessedFlagForInheritance(r);
|
|
247
274
|
}
|
|
248
|
-
return
|
|
275
|
+
return c(this, T).set(e.name, e), e.handler && n.setHandler(e.handler), this;
|
|
249
276
|
}
|
|
250
277
|
/**
|
|
251
278
|
* Sets the handler function for this specific parser instance.
|
|
@@ -256,21 +283,21 @@ const W = class W {
|
|
|
256
283
|
* @returns The ArgParser instance for chaining.
|
|
257
284
|
*/
|
|
258
285
|
setHandler(e) {
|
|
259
|
-
return
|
|
286
|
+
return H(this, W, e), this;
|
|
260
287
|
}
|
|
261
288
|
printAll(e) {
|
|
262
289
|
if (e)
|
|
263
290
|
try {
|
|
264
|
-
const n =
|
|
265
|
-
if (
|
|
266
|
-
const
|
|
267
|
-
|
|
291
|
+
const n = ce.dirname(e);
|
|
292
|
+
if (G.existsSync(n) || G.mkdirSync(n, { recursive: !0 }), e.toLowerCase().endsWith(".json")) {
|
|
293
|
+
const s = $(this, m, Fe).call(this, this), r = JSON.stringify(s, null, 2);
|
|
294
|
+
G.writeFileSync(e, r), console.log(`ArgParser configuration JSON dumped to: ${e}`);
|
|
268
295
|
} else {
|
|
269
|
-
const
|
|
296
|
+
const r = $(this, m, Ae).call(this, this, 0).replace(
|
|
270
297
|
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
|
|
271
298
|
""
|
|
272
299
|
);
|
|
273
|
-
|
|
300
|
+
G.writeFileSync(e, r), console.log(`ArgParser configuration text dumped to: ${e}`);
|
|
274
301
|
}
|
|
275
302
|
} catch (n) {
|
|
276
303
|
console.error(
|
|
@@ -280,540 +307,1657 @@ const W = class W {
|
|
|
280
307
|
}
|
|
281
308
|
else
|
|
282
309
|
console.log(`
|
|
283
|
-
--- ArgParser Configuration Dump ---`),
|
|
310
|
+
--- ArgParser Configuration Dump ---`), $(this, m, Ce).call(this, this, 0), console.log("--- End Configuration Dump ---\\n");
|
|
284
311
|
}
|
|
285
312
|
parse(e, n) {
|
|
286
|
-
|
|
313
|
+
var a;
|
|
314
|
+
const s = [...e];
|
|
315
|
+
if (typeof process < "u" && (process.env.ARGPARSER_FUZZY_MODE === "true" || process.argv && process.argv.includes("--s-enable-fuzzy") && !e.includes("--s-enable-fuzzy")) && !(n != null && n.skipHelpHandling))
|
|
316
|
+
return {
|
|
317
|
+
_fuzzyModePreventedExecution: !0,
|
|
318
|
+
_originalInputArgs: s
|
|
319
|
+
};
|
|
320
|
+
if ($(this, m, Re).call(this, e, n))
|
|
287
321
|
return {};
|
|
288
322
|
try {
|
|
289
323
|
const {
|
|
290
|
-
finalParser:
|
|
291
|
-
commandChain:
|
|
292
|
-
parserChain:
|
|
293
|
-
} =
|
|
324
|
+
finalParser: t,
|
|
325
|
+
commandChain: i,
|
|
326
|
+
parserChain: o
|
|
327
|
+
} = $(this, m, oe).call(this, e, this, [], [this]);
|
|
328
|
+
if ($(a = t, m, We).call(a, e, o))
|
|
329
|
+
return {};
|
|
330
|
+
const { finalArgs: l, handlerToExecute: u } = this._parseRecursive(
|
|
294
331
|
e,
|
|
295
332
|
this,
|
|
296
333
|
{},
|
|
297
334
|
[],
|
|
298
|
-
n
|
|
335
|
+
n,
|
|
336
|
+
void 0
|
|
299
337
|
);
|
|
300
|
-
return
|
|
301
|
-
} catch (
|
|
302
|
-
if (
|
|
303
|
-
if (
|
|
304
|
-
return
|
|
305
|
-
throw
|
|
338
|
+
return i.length > 0 && (l.$commandChain = i), c(this, ne) && (l._originalInputArgs = s), c(this, ne) || $(this, m, Le).call(this, l, o, i), $(this, m, we).call(this, l, t), $(this, m, ze).call(this, u, l, (n == null ? void 0 : n.skipHandlers) ?? !1), l;
|
|
339
|
+
} catch (t) {
|
|
340
|
+
if (t instanceof Y) {
|
|
341
|
+
if (c(this, q))
|
|
342
|
+
return $(this, m, Se).call(this, t), {};
|
|
343
|
+
throw t;
|
|
306
344
|
} else
|
|
307
|
-
throw
|
|
345
|
+
throw t;
|
|
308
346
|
}
|
|
309
347
|
}
|
|
310
348
|
/**
|
|
311
349
|
* Recursive helper for parsing arguments and handling sub-commands.
|
|
312
350
|
* This method assumes the global help check has already been performed in `parse`.
|
|
313
351
|
*/
|
|
314
|
-
_parseRecursive(e, n,
|
|
315
|
-
var
|
|
316
|
-
let
|
|
317
|
-
for (let
|
|
318
|
-
const
|
|
319
|
-
if (
|
|
320
|
-
|
|
352
|
+
_parseRecursive(e, n, s, r, a, t) {
|
|
353
|
+
var y, j;
|
|
354
|
+
let i = -1, o = null;
|
|
355
|
+
for (let A = 0; A < e.length; A++) {
|
|
356
|
+
const F = e[A];
|
|
357
|
+
if (c(n, T).has(F)) {
|
|
358
|
+
i = A, o = F;
|
|
321
359
|
break;
|
|
322
360
|
}
|
|
323
361
|
}
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
const
|
|
327
|
-
...
|
|
328
|
-
...
|
|
362
|
+
const l = i === -1 ? e : e.slice(0, i), { parsedArgs: u, firstUnconsumedIndex: d } = $(y = n, m, ue).call(y, l, a);
|
|
363
|
+
$(j = n, m, we).call(j, u, n);
|
|
364
|
+
const f = {
|
|
365
|
+
...s,
|
|
366
|
+
...u
|
|
329
367
|
};
|
|
330
|
-
if (
|
|
331
|
-
if (
|
|
332
|
-
const
|
|
333
|
-
throw new
|
|
334
|
-
`Unknown command: '${
|
|
335
|
-
|
|
368
|
+
if (i === -1 || o === null) {
|
|
369
|
+
if (d < l.length) {
|
|
370
|
+
const N = l[d];
|
|
371
|
+
throw new Y(
|
|
372
|
+
`Unknown command: '${S.yellow(N)}'`,
|
|
373
|
+
r
|
|
336
374
|
);
|
|
337
375
|
}
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
let
|
|
341
|
-
return
|
|
342
|
-
handler:
|
|
376
|
+
const A = { ...f };
|
|
377
|
+
r.length > 0 && (A.$commandChain = r);
|
|
378
|
+
let F;
|
|
379
|
+
return c(n, W) && (F = {
|
|
380
|
+
handler: c(n, W),
|
|
343
381
|
context: {
|
|
344
|
-
args:
|
|
345
|
-
parentArgs:
|
|
346
|
-
commandChain:
|
|
347
|
-
parser: n
|
|
382
|
+
args: u,
|
|
383
|
+
parentArgs: s,
|
|
384
|
+
commandChain: r,
|
|
385
|
+
parser: n,
|
|
386
|
+
parentParser: t
|
|
348
387
|
}
|
|
349
|
-
}), { finalArgs:
|
|
388
|
+
}), { finalArgs: A, handlerToExecute: F };
|
|
350
389
|
}
|
|
351
|
-
if (
|
|
352
|
-
const
|
|
353
|
-
throw new
|
|
354
|
-
`Unknown command: '${
|
|
355
|
-
|
|
390
|
+
if (d < l.length) {
|
|
391
|
+
const A = l[d];
|
|
392
|
+
throw new Y(
|
|
393
|
+
`Unknown command: '${S.yellow(A)}'`,
|
|
394
|
+
r
|
|
356
395
|
);
|
|
357
396
|
}
|
|
358
|
-
const
|
|
359
|
-
if (!
|
|
360
|
-
throw new
|
|
361
|
-
`Internal error: Subcommand '${
|
|
362
|
-
|
|
397
|
+
const b = c(n, T).get(o);
|
|
398
|
+
if (!b || !(b.parser instanceof K))
|
|
399
|
+
throw new Y(
|
|
400
|
+
`Internal error: Subcommand '${o}' is misconfigured or its parser is not a valid ArgParser instance.`,
|
|
401
|
+
r
|
|
363
402
|
);
|
|
364
|
-
const h =
|
|
365
|
-
...
|
|
366
|
-
...
|
|
403
|
+
const h = b.parser, v = e.slice(i + 1), O = [...r, o], C = {
|
|
404
|
+
...s,
|
|
405
|
+
...u
|
|
367
406
|
};
|
|
368
407
|
return this._parseRecursive(
|
|
369
|
-
C,
|
|
370
|
-
h,
|
|
371
|
-
V,
|
|
372
408
|
v,
|
|
373
|
-
|
|
409
|
+
h,
|
|
410
|
+
C,
|
|
411
|
+
O,
|
|
412
|
+
a,
|
|
413
|
+
n
|
|
374
414
|
);
|
|
375
415
|
}
|
|
376
416
|
helpText() {
|
|
377
|
-
const e =
|
|
378
|
-
let
|
|
379
|
-
for (;
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
const
|
|
383
|
-
let
|
|
417
|
+
const e = S.cyan, n = S.green, s = S.white, r = S.red, a = S.dim;
|
|
418
|
+
let t = c(this, P), i = this;
|
|
419
|
+
for (; c(i, Q); )
|
|
420
|
+
i = c(i, Q);
|
|
421
|
+
i && (t = c(i, P));
|
|
422
|
+
const o = c(this, R) ? `${t} ${c(this, R)}` : t;
|
|
423
|
+
let l = `${e(`${o} Help`)} (${c(this, z).mandatoryCharacter} = Mandatory fields):
|
|
384
424
|
|
|
385
425
|
`;
|
|
386
|
-
|
|
426
|
+
c(this, L) && (l += `${s(c(this, L))}
|
|
387
427
|
|
|
388
428
|
`);
|
|
389
|
-
const
|
|
390
|
-
|
|
391
|
-
`,
|
|
392
|
-
const
|
|
393
|
-
if (!(
|
|
394
|
-
return `${
|
|
395
|
-
let v = `${
|
|
396
|
-
const
|
|
397
|
-
(
|
|
429
|
+
const u = (f = 1) => " ".repeat(f);
|
|
430
|
+
c(this, T).size > 0 && (l += `${e("Available sub-commands:")}
|
|
431
|
+
`, l += Array.from(c(this, T).entries()).sort(([f], [b]) => f.localeCompare(b)).map(([f, b]) => {
|
|
432
|
+
const h = b.parser;
|
|
433
|
+
if (!(h instanceof K))
|
|
434
|
+
return `${u()}${n(f.padEnd(20))} [Error: Subcommand '${f}' has an invalid parser configuration]`;
|
|
435
|
+
let v = `${u()}${n(f.padEnd(20))} ${s(c(h, L) || "")}`;
|
|
436
|
+
const C = ((h && c(h, M) ? c(h, M).flags : void 0) || []).filter(
|
|
437
|
+
(j) => j.name !== "help"
|
|
398
438
|
);
|
|
399
|
-
|
|
400
|
-
${
|
|
401
|
-
(
|
|
402
|
-
).forEach((
|
|
403
|
-
const
|
|
439
|
+
C.length > 0 ? (v += `
|
|
440
|
+
${u(2)}${a("Flags:")}`, C.sort(
|
|
441
|
+
(j, A) => j.name.localeCompare(A.name)
|
|
442
|
+
).forEach((j) => {
|
|
443
|
+
const A = j.options.map((N) => n(N)).join(", "), F = Array.isArray(j.description) ? j.description[0] : j.description;
|
|
404
444
|
v += `
|
|
405
|
-
${
|
|
445
|
+
${u(3)}${A} - ${a(F)}`;
|
|
406
446
|
})) : v += `
|
|
407
|
-
${
|
|
408
|
-
const
|
|
409
|
-
|
|
447
|
+
${u(2)}${a("Flags:")} none`;
|
|
448
|
+
const y = Array.from(
|
|
449
|
+
c(h, T).keys()
|
|
410
450
|
);
|
|
411
|
-
return
|
|
412
|
-
${
|
|
413
|
-
${
|
|
451
|
+
return y.length > 0 ? v += `
|
|
452
|
+
${u(2)}${a("Sub-commands:")} ${y.join(", ")}` : v += `
|
|
453
|
+
${u(2)}${a("Sub-commands:")} none`, v;
|
|
414
454
|
}).join(`
|
|
415
455
|
|
|
416
|
-
`),
|
|
417
|
-
`),
|
|
456
|
+
`), l += `
|
|
457
|
+
`), l += `
|
|
418
458
|
${e("Flags:")}
|
|
419
459
|
`;
|
|
420
|
-
const
|
|
421
|
-
return
|
|
422
|
-
const
|
|
423
|
-
let
|
|
424
|
-
typeof
|
|
425
|
-
`Allowed values: ${
|
|
460
|
+
const d = c(this, M).flags;
|
|
461
|
+
return d.length > 0 ? l += d.sort((f, b) => f.name.localeCompare(b.name)).map((f) => {
|
|
462
|
+
const b = f.options.slice().sort((F, N) => F.length - N.length).map((F) => n(F)).join(", "), h = typeof f.mandatory == "function" ? "dynamic" : f.mandatory, v = h === !0 ? ` ${r(c(this, z).mandatoryCharacter)}` : h === "dynamic" ? ` ${a("(conditionally mandatory)")}` : "", O = Array.isArray(f.description) ? f.description : [f.description], C = [];
|
|
463
|
+
let y = "unknown";
|
|
464
|
+
typeof f.type == "function" ? (y = f.type.name || "custom function", y === "Boolean" && (y = "boolean"), y === "String" && (y = "string"), y === "Number" && (y = "number"), y === "Array" && (y = "array"), y === "Object" && (y = "object")) : typeof f.type == "string" && (y = f.type), C.push(`Type: ${y}`), f.flagOnly && C.push("Flag only (no value expected)"), f.defaultValue !== void 0 && f.defaultValue !== null && C.push(`Default: ${JSON.stringify(f.defaultValue)}`), f.enum && f.enum.length > 0 && C.push(
|
|
465
|
+
`Allowed values: ${f.enum.map((F) => `'${F}'`).join(", ")}`
|
|
426
466
|
);
|
|
427
|
-
const
|
|
428
|
-
...
|
|
429
|
-
(
|
|
467
|
+
const j = Math.max(
|
|
468
|
+
...d.map(
|
|
469
|
+
(F) => F.options.join(", ").length
|
|
430
470
|
),
|
|
431
471
|
0
|
|
432
|
-
),
|
|
472
|
+
), A = b.padEnd(j + 5) + v;
|
|
433
473
|
return `
|
|
434
|
-
${
|
|
435
|
-
${
|
|
436
|
-
${
|
|
474
|
+
${u()}${A}
|
|
475
|
+
${u(2)}${s(O[0])}
|
|
476
|
+
${C.map((F) => `${u(3)}${a(F)}`).join(`
|
|
437
477
|
`)}
|
|
438
|
-
${
|
|
439
|
-
${
|
|
478
|
+
${O.slice(1).map((F) => `
|
|
479
|
+
${u(2)}${s(F)}`).join("")}
|
|
440
480
|
`.trim();
|
|
441
481
|
}).join(`
|
|
442
482
|
|
|
443
|
-
`) :
|
|
483
|
+
`) : l += `${u()}${a("none")}`, l;
|
|
444
484
|
}
|
|
445
485
|
getSubCommand(e) {
|
|
446
|
-
return
|
|
486
|
+
return c(this, T).get(e);
|
|
447
487
|
}
|
|
448
488
|
hasFlag(e) {
|
|
449
|
-
return
|
|
489
|
+
return c(this, M).hasFlag(e);
|
|
450
490
|
}
|
|
451
491
|
getCommandChain() {
|
|
452
492
|
const e = [];
|
|
453
493
|
let n = this;
|
|
454
|
-
for (; n &&
|
|
455
|
-
e.unshift(
|
|
494
|
+
for (; n && c(n, Q); )
|
|
495
|
+
e.unshift(c(n, R)), n = c(n, Q);
|
|
456
496
|
return e;
|
|
457
497
|
}
|
|
458
498
|
getLastParseResult() {
|
|
459
|
-
return
|
|
499
|
+
return c(this, ge);
|
|
460
500
|
}
|
|
461
501
|
};
|
|
462
|
-
|
|
463
|
-
let
|
|
464
|
-
for (let
|
|
465
|
-
const
|
|
466
|
-
if (
|
|
467
|
-
|
|
502
|
+
P = new WeakMap(), V = new WeakMap(), R = new WeakMap(), z = new WeakMap(), W = new WeakMap(), ee = new WeakMap(), L = new WeakMap(), q = new WeakMap(), Q = new WeakMap(), ge = new WeakMap(), U = new WeakMap(), T = new WeakMap(), M = new WeakMap(), ne = new WeakMap(), m = new WeakSet(), oe = function(e, n, s, r) {
|
|
503
|
+
let a = -1, t = null;
|
|
504
|
+
for (let f = 0; f < e.length; f++) {
|
|
505
|
+
const b = e[f];
|
|
506
|
+
if (c(n, T).has(b)) {
|
|
507
|
+
a = f, t = b;
|
|
468
508
|
break;
|
|
469
509
|
}
|
|
470
510
|
}
|
|
471
|
-
if (
|
|
511
|
+
if (a === -1 || t === null)
|
|
472
512
|
return {
|
|
473
513
|
finalParser: n,
|
|
474
|
-
commandChain:
|
|
475
|
-
parserChain:
|
|
514
|
+
commandChain: s,
|
|
515
|
+
parserChain: r,
|
|
476
516
|
remainingArgs: e
|
|
477
517
|
};
|
|
478
|
-
const
|
|
479
|
-
if (!
|
|
518
|
+
const i = c(n, T).get(t);
|
|
519
|
+
if (!i || !(i.parser instanceof K))
|
|
480
520
|
throw new Error(
|
|
481
|
-
`Internal error: Subcommand '${
|
|
521
|
+
`Internal error: Subcommand '${t}' configuration is invalid or parser is missing.`
|
|
482
522
|
);
|
|
483
|
-
const
|
|
484
|
-
return
|
|
485
|
-
},
|
|
486
|
-
var o,
|
|
487
|
-
|
|
523
|
+
const o = i.parser, l = e.slice(a + 1), u = [...s, t], d = [...r, o];
|
|
524
|
+
return $(this, m, oe).call(this, l, o, u, d);
|
|
525
|
+
}, Re = function(e, n) {
|
|
526
|
+
var o, l, u, d, f, b;
|
|
527
|
+
const s = !c(this, Q) && !!c(this, V);
|
|
528
|
+
if (e.length === 0 && s && !c(this, W))
|
|
488
529
|
return console.log(this.helpText()), typeof process == "object" && typeof process.exit == "function" && process.exit(0), !0;
|
|
489
|
-
if (e.includes("--
|
|
530
|
+
if (e.includes("--s-debug-print"))
|
|
490
531
|
return this.printAll("ArgParser.full.json"), typeof process == "object" && typeof process.exit == "function" && process.exit(0), !0;
|
|
491
|
-
|
|
492
|
-
|
|
532
|
+
if (e.includes("--s-enable-fuzzy")) {
|
|
533
|
+
$(this, m, ve).call(this);
|
|
534
|
+
const h = e.indexOf("--s-enable-fuzzy");
|
|
535
|
+
e.splice(h, 1);
|
|
536
|
+
}
|
|
537
|
+
const r = e.findIndex((h) => h === "--s-with-env");
|
|
538
|
+
if (r !== -1) {
|
|
539
|
+
if (r + 1 >= e.length)
|
|
540
|
+
return console.error(S.red("Error: --s-with-env requires a file path argument")), typeof process == "object" && typeof process.exit == "function" && process.exit(1), !0;
|
|
541
|
+
const h = e[r + 1];
|
|
542
|
+
if (!h || h.startsWith("-"))
|
|
543
|
+
return console.error(S.red("Error: --s-with-env requires a file path argument")), typeof process == "object" && typeof process.exit == "function" && process.exit(1), !0;
|
|
544
|
+
try {
|
|
545
|
+
const { finalParser: v, parserChain: O } = $(this, m, oe).call(this, e, this, [], [this]), C = $(o = v, m, Be).call(o, h, O);
|
|
546
|
+
if (C) {
|
|
547
|
+
const y = $(l = v, m, Qe).call(l, C, e);
|
|
548
|
+
e.length = 0, e.push(...y);
|
|
549
|
+
}
|
|
550
|
+
} catch (v) {
|
|
551
|
+
return console.error(S.red(`Error loading environment file: ${v instanceof Error ? v.message : String(v)}`)), typeof process == "object" && typeof process.exit == "function" && process.exit(1), !0;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
const { finalParser: a } = $(this, m, oe).call(this, e, this, [], [this]);
|
|
555
|
+
if (e.includes("--s-debug")) {
|
|
493
556
|
console.log(
|
|
494
|
-
|
|
495
|
-
--- ArgParser --
|
|
557
|
+
S.yellow.bold(`
|
|
558
|
+
--- ArgParser --s-debug Runtime Context ---`)
|
|
496
559
|
);
|
|
497
560
|
const {
|
|
498
|
-
commandChain:
|
|
499
|
-
parserChain:
|
|
500
|
-
} =
|
|
561
|
+
commandChain: h,
|
|
562
|
+
parserChain: v
|
|
563
|
+
} = $(this, m, oe).call(this, e, this, [], [this]);
|
|
501
564
|
console.log(
|
|
502
|
-
`Identified Command Chain: ${
|
|
565
|
+
`Identified Command Chain: ${S.cyan(h.join(" -> ") || "(root)")}`
|
|
503
566
|
), console.log(
|
|
504
|
-
`Identified Final Parser: ${
|
|
567
|
+
`Identified Final Parser: ${S.cyan(c(a, R) || c(a, P))}`
|
|
505
568
|
);
|
|
506
|
-
let
|
|
507
|
-
const
|
|
508
|
-
(
|
|
509
|
-
),
|
|
510
|
-
|
|
569
|
+
let O = this, C = [...e], y = {};
|
|
570
|
+
const j = [], A = C.findIndex(
|
|
571
|
+
(N) => c(O, T).has(N)
|
|
572
|
+
), F = A === -1 ? C : C.slice(0, A);
|
|
573
|
+
j.push({ level: "(root)", argsSlice: F });
|
|
511
574
|
try {
|
|
512
|
-
const { parsedArgs:
|
|
513
|
-
|
|
514
|
-
} catch (
|
|
515
|
-
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
for (let
|
|
519
|
-
const
|
|
520
|
-
if (!
|
|
521
|
-
|
|
575
|
+
const { parsedArgs: N } = $(u = O, m, ue).call(u, F, { skipHelpHandling: !0 });
|
|
576
|
+
j[0].parsed = N, y = { ...y, ...N };
|
|
577
|
+
} catch (N) {
|
|
578
|
+
j[0].error = N.message;
|
|
579
|
+
}
|
|
580
|
+
C = A === -1 ? [] : C.slice(A);
|
|
581
|
+
for (let N = 0; N < h.length; N++) {
|
|
582
|
+
const re = h[N];
|
|
583
|
+
if (!c(O, T).has(re)) {
|
|
584
|
+
j.push({
|
|
522
585
|
level: "Error",
|
|
523
586
|
argsSlice: [],
|
|
524
|
-
error: `Could not find sub-command parser for '${
|
|
587
|
+
error: `Could not find sub-command parser for '${re}'`
|
|
525
588
|
});
|
|
526
589
|
break;
|
|
527
590
|
}
|
|
528
|
-
|
|
529
|
-
const
|
|
530
|
-
(
|
|
531
|
-
),
|
|
532
|
-
level:
|
|
533
|
-
argsSlice:
|
|
591
|
+
O = (d = c(O, T).get(re)) == null ? void 0 : d.parser, C = C.slice(1);
|
|
592
|
+
const X = C.findIndex(
|
|
593
|
+
(E) => c(O, T).has(E)
|
|
594
|
+
), pe = X === -1 ? C : C.slice(0, X), k = {
|
|
595
|
+
level: re,
|
|
596
|
+
argsSlice: pe
|
|
534
597
|
};
|
|
535
|
-
|
|
598
|
+
j.push(k);
|
|
536
599
|
try {
|
|
537
|
-
const { parsedArgs:
|
|
600
|
+
const { parsedArgs: E } = $(f = O, m, ue).call(f, pe, {
|
|
538
601
|
skipHelpHandling: !0
|
|
539
602
|
});
|
|
540
|
-
|
|
541
|
-
} catch (
|
|
542
|
-
|
|
603
|
+
k.parsed = E, y = { ...y, ...E };
|
|
604
|
+
} catch (E) {
|
|
605
|
+
k.error = E.message;
|
|
543
606
|
}
|
|
544
|
-
|
|
545
|
-
}
|
|
546
|
-
return console.log(
|
|
547
|
-
Parsing Simulation Steps:`)),
|
|
548
|
-
console.log(` Level: ${
|
|
549
|
-
` Args Slice Considered: ${JSON.stringify(
|
|
550
|
-
),
|
|
551
|
-
` Parsed Args at this Level: ${JSON.stringify(
|
|
552
|
-
),
|
|
553
|
-
` ${
|
|
607
|
+
C = X === -1 ? [] : C.slice(X);
|
|
608
|
+
}
|
|
609
|
+
return console.log(S.yellow(`
|
|
610
|
+
Parsing Simulation Steps:`)), j.forEach((N) => {
|
|
611
|
+
console.log(` Level: ${S.cyan(N.level)}`), console.log(
|
|
612
|
+
` Args Slice Considered: ${JSON.stringify(N.argsSlice)}`
|
|
613
|
+
), N.parsed && console.log(
|
|
614
|
+
` Parsed Args at this Level: ${JSON.stringify(N.parsed)}`
|
|
615
|
+
), N.error && console.log(
|
|
616
|
+
` ${S.red("Error during parse simulation:")} ${N.error}`
|
|
554
617
|
);
|
|
555
618
|
}), console.log(
|
|
556
|
-
|
|
619
|
+
S.yellow(
|
|
557
620
|
`
|
|
558
621
|
Final Accumulated Args State (before final validation):`
|
|
559
622
|
)
|
|
560
|
-
), console.log(JSON.stringify(
|
|
561
|
-
Arguments Remaining After Simulation:`)), console.log(JSON.stringify(
|
|
562
|
-
|
|
623
|
+
), console.log(JSON.stringify(y, null, 2)), console.log(S.yellow(`
|
|
624
|
+
Arguments Remaining After Simulation:`)), console.log(JSON.stringify(C, null, 2)), console.log(
|
|
625
|
+
S.yellow.bold(
|
|
563
626
|
`
|
|
564
627
|
--- ArgParser Static Configuration (Final Parser) ---`
|
|
565
628
|
)
|
|
566
|
-
), a.printAll(), console.log(
|
|
629
|
+
), a.printAll(), console.log(S.yellow.bold("--- End ArgParser --s-debug ---")), typeof process == "object" && typeof process.exit == "function" && process.exit(0), !0;
|
|
567
630
|
}
|
|
568
|
-
let
|
|
569
|
-
if (a instanceof
|
|
631
|
+
let t = "undefined_parser";
|
|
632
|
+
if (a instanceof K ? t = a["#subCommandName"] || a["#appName"] : a && (t = a.name || a.appName || "unknown_type"), !(a instanceof K))
|
|
570
633
|
return console.error(
|
|
571
|
-
`[ArgParser #_handleGlobalChecks Critical Error] identifiedFinalParser is not an instance of ArgParser. Cannot process help. Name: ${
|
|
634
|
+
`[ArgParser #_handleGlobalChecks Critical Error] identifiedFinalParser is not an instance of ArgParser. Cannot process help. Name: ${t}, Constructor: ${a ? (b = a.constructor) == null ? void 0 : b.name : "undefined"}`
|
|
572
635
|
), !1;
|
|
573
|
-
const i =
|
|
636
|
+
const i = c(a, M).getFlag("help");
|
|
574
637
|
if (i && !(n != null && n.skipHelpHandling)) {
|
|
575
|
-
const
|
|
638
|
+
const h = i.options;
|
|
576
639
|
if (e.some(
|
|
577
|
-
(
|
|
640
|
+
(O) => h.includes(O)
|
|
578
641
|
))
|
|
579
642
|
return console.log(a.helpText()), typeof process == "object" && typeof process.exit == "function" && process.exit(0), !0;
|
|
580
643
|
}
|
|
581
644
|
return !1;
|
|
582
|
-
},
|
|
583
|
-
const
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
645
|
+
}, Le = function(e, n, s) {
|
|
646
|
+
const r = [], a = /* @__PURE__ */ new Set();
|
|
647
|
+
let t = [...n];
|
|
648
|
+
if (n.length > 1) {
|
|
649
|
+
const i = n[1];
|
|
650
|
+
c(i, U) || (t = t.slice(1));
|
|
651
|
+
}
|
|
652
|
+
for (let i = 0; i < t.length; i++) {
|
|
653
|
+
const o = t[i], l = o.getCommandChain();
|
|
654
|
+
for (const u of c(o, M).flags) {
|
|
655
|
+
if (u.name === "help" || a.has(u.name))
|
|
656
|
+
continue;
|
|
657
|
+
let d = !1;
|
|
658
|
+
if (i < t.length - 1) {
|
|
659
|
+
const v = t[i + 1];
|
|
660
|
+
c(v, U) && c(v, M).hasFlag(u.name) && (d = !0);
|
|
661
|
+
}
|
|
662
|
+
if (d || !(typeof u.mandatory == "function" ? u.mandatory(e) : u.mandatory)) continue;
|
|
663
|
+
const b = e[u.name];
|
|
664
|
+
let h = !1;
|
|
665
|
+
u.allowMultiple ? (b === void 0 || Array.isArray(b) && b.length === 0) && (h = !0) : b === void 0 && (h = !0), h && (a.has(u.name) || (r.push({
|
|
666
|
+
name: u.name,
|
|
667
|
+
parserName: c(o, R) || c(o, P),
|
|
668
|
+
commandChain: l
|
|
669
|
+
}), a.add(u.name)));
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
if (r.length > 0)
|
|
673
|
+
throw new Y(
|
|
674
|
+
`Missing mandatory flags: ${r.map((i) => S.yellow(i.name)).join(", ")}`,
|
|
675
|
+
s
|
|
601
676
|
);
|
|
602
|
-
},
|
|
603
|
-
for (const
|
|
604
|
-
const
|
|
605
|
-
e[
|
|
677
|
+
}, we = function(e, n) {
|
|
678
|
+
for (const s of c(n, M).flags) {
|
|
679
|
+
const r = s.name;
|
|
680
|
+
e[r] === void 0 && s.defaultValue !== void 0 && (s.allowMultiple ? e[r] = Array.isArray(s.defaultValue) ? s.defaultValue : [s.defaultValue] : e[r] = s.defaultValue);
|
|
606
681
|
}
|
|
607
|
-
},
|
|
608
|
-
if (
|
|
682
|
+
}, ze = function(e, n, s) {
|
|
683
|
+
if (s || !e)
|
|
609
684
|
return;
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
}
|
|
615
|
-
e.context.
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
685
|
+
if (c(this, ne)) {
|
|
686
|
+
const i = e.context.commandChain || [], o = e.context.args || {}, l = n._originalInputArgs || "unknown", u = Array.isArray(l) ? l.join(" ") : l;
|
|
687
|
+
console.log(`[--s-enable-fuzzy] handler() skipped for command chain: ${i.join(" ") || "(root)"}`), console.log(` Input args: [${u}]`), console.log(` Parsed args: ${JSON.stringify(o)}`);
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
const r = e.context.parser, a = c(r, M).flags, t = e.context.args;
|
|
691
|
+
for (const i of a) {
|
|
692
|
+
const o = i.name;
|
|
693
|
+
n.hasOwnProperty(o) ? t[o] = n[o] : i.allowMultiple && !t.hasOwnProperty(o) && (t[o] = []);
|
|
694
|
+
}
|
|
695
|
+
e.context.args = t;
|
|
696
|
+
try {
|
|
697
|
+
const i = e.handler(e.context);
|
|
698
|
+
if (i && typeof i.then == "function") {
|
|
699
|
+
n._asyncHandlerPromise = i, n._asyncHandlerInfo = e, i.catch(() => {
|
|
700
|
+
});
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
n.handlerResponse = i;
|
|
704
|
+
} catch (i) {
|
|
705
|
+
if (c(this, q))
|
|
706
|
+
$(this, m, Se).call(this, new Y(`Handler error: ${i}`, []));
|
|
707
|
+
else
|
|
708
|
+
throw i;
|
|
709
|
+
}
|
|
710
|
+
}, ue = function(e, n) {
|
|
711
|
+
var i, o;
|
|
712
|
+
const s = c(this, M).flags, r = Object.fromEntries(
|
|
713
|
+
s.map((l) => [
|
|
714
|
+
l.name,
|
|
715
|
+
l.allowMultiple ? [] : void 0
|
|
622
716
|
])
|
|
623
717
|
);
|
|
624
|
-
let
|
|
625
|
-
for (const
|
|
626
|
-
if (
|
|
627
|
-
const
|
|
628
|
-
|
|
629
|
-
...
|
|
718
|
+
let a = /* @__PURE__ */ new Set();
|
|
719
|
+
for (const l of s)
|
|
720
|
+
if (l.allowLigature && !l.flagOnly) {
|
|
721
|
+
const u = st(
|
|
722
|
+
at(
|
|
723
|
+
...l.options.map((d) => `${d}=`)
|
|
630
724
|
),
|
|
631
|
-
|
|
725
|
+
ot(it).groupedAs("arg")
|
|
632
726
|
);
|
|
633
|
-
for (let
|
|
634
|
-
if (
|
|
635
|
-
const
|
|
636
|
-
if ((
|
|
637
|
-
r,
|
|
638
|
-
(s = h == null ? void 0 : h.groups) == null ? void 0 : s.arg,
|
|
727
|
+
for (let d = 0; d < e.length; d++) {
|
|
728
|
+
if (a.has(d)) continue;
|
|
729
|
+
const f = e[d], b = u.exec(`${f}`);
|
|
730
|
+
if ((i = b == null ? void 0 : b.groups) != null && i.arg && (this._addToOutput(
|
|
639
731
|
l,
|
|
732
|
+
(o = b == null ? void 0 : b.groups) == null ? void 0 : o.arg,
|
|
733
|
+
r,
|
|
640
734
|
n
|
|
641
|
-
),
|
|
735
|
+
), a.add(d), !l.allowMultiple))
|
|
642
736
|
break;
|
|
643
737
|
}
|
|
644
738
|
}
|
|
645
|
-
for (const
|
|
646
|
-
for (let
|
|
647
|
-
if (
|
|
648
|
-
const
|
|
649
|
-
if (
|
|
739
|
+
for (const l of s)
|
|
740
|
+
for (let u = 0; u < e.length; u++) {
|
|
741
|
+
if (a.has(u)) continue;
|
|
742
|
+
const d = e[u], f = u + 1, b = f < e.length, h = b ? e[f] : void 0, v = typeof h == "string" && h.startsWith("-");
|
|
743
|
+
if (l.options.includes(d) && (a.add(u), l.flagOnly ? this._addToOutput(l, !0, r, n) : b && !v ? (this._addToOutput(l, h, r, n), a.add(f)) : l.type === Boolean && this._addToOutput(l, !0, r, n), !l.allowMultiple))
|
|
650
744
|
break;
|
|
651
745
|
}
|
|
652
|
-
let
|
|
653
|
-
for (let
|
|
654
|
-
if (!
|
|
655
|
-
|
|
746
|
+
let t = e.length;
|
|
747
|
+
for (let l = 0; l < e.length; l++)
|
|
748
|
+
if (!a.has(l)) {
|
|
749
|
+
t = l;
|
|
656
750
|
break;
|
|
657
751
|
}
|
|
658
|
-
return { parsedArgs:
|
|
659
|
-
},
|
|
752
|
+
return { parsedArgs: r, firstUnconsumedIndex: t };
|
|
753
|
+
}, /**
|
|
754
|
+
* Enables fuzzy testing mode by disabling error handling and making flags optional
|
|
755
|
+
*/
|
|
756
|
+
ve = function() {
|
|
757
|
+
var e;
|
|
758
|
+
H(this, ne, !0), H(this, q, !1);
|
|
759
|
+
for (const [, n] of c(this, T))
|
|
760
|
+
n.parser instanceof K && $(e = n.parser, m, ve).call(e);
|
|
761
|
+
}, Se = function(e) {
|
|
660
762
|
let n = "your-script";
|
|
661
|
-
if (
|
|
662
|
-
n =
|
|
663
|
-
else if (
|
|
664
|
-
n =
|
|
763
|
+
if (c(this, V))
|
|
764
|
+
n = c(this, V);
|
|
765
|
+
else if (c(this, P) && c(this, P) !== "Argument Parser")
|
|
766
|
+
n = c(this, P);
|
|
665
767
|
else if (typeof process < "u" && process.argv && process.argv[1])
|
|
666
768
|
try {
|
|
667
|
-
n =
|
|
769
|
+
n = ce.basename(process.argv[1]);
|
|
668
770
|
} catch {
|
|
669
771
|
}
|
|
670
|
-
const a = [
|
|
671
|
-
n,
|
|
672
|
-
...e.commandChain || []
|
|
673
|
-
].join(" ");
|
|
674
772
|
if (console.error(`
|
|
675
|
-
${
|
|
773
|
+
${S.red.bold("Error:")} ${e.message}`), console.error(
|
|
676
774
|
`
|
|
677
|
-
${
|
|
775
|
+
${S.dim(`Try '${n} --help' for usage details.`)}`
|
|
678
776
|
), typeof process == "object" && typeof process.exit == "function")
|
|
679
777
|
process.exit(1);
|
|
680
778
|
else
|
|
681
779
|
throw e;
|
|
682
|
-
},
|
|
683
|
-
const
|
|
780
|
+
}, Ce = function(e, n, s = /* @__PURE__ */ new Set()) {
|
|
781
|
+
const r = " ".repeat(n), a = " ".repeat(n + 1), t = " ".repeat(n + 2);
|
|
684
782
|
console.log(
|
|
685
|
-
`${
|
|
686
|
-
),
|
|
687
|
-
`${
|
|
688
|
-
), console.log(`${
|
|
689
|
-
`${
|
|
783
|
+
`${r}Parser: ${S.blueBright(c(e, R) || c(e, P))}`
|
|
784
|
+
), c(e, L) && console.log(`${a}Description: ${c(e, L)}`), console.log(`${a}Options:`), console.log(`${t}appName: ${c(e, P)}`), console.log(
|
|
785
|
+
`${t}appCommandName: ${c(e, V) ?? S.dim("undefined")}`
|
|
786
|
+
), console.log(`${t}handleErrors: ${c(e, q)}`), console.log(
|
|
787
|
+
`${t}throwForDuplicateFlags: ${c(e, ee)}`
|
|
690
788
|
), console.log(
|
|
691
|
-
`${
|
|
692
|
-
), console.log(`${
|
|
693
|
-
`${
|
|
789
|
+
`${t}inheritParentFlags: ${c(e, U)}`
|
|
790
|
+
), console.log(`${t}Handler Defined: ${!!c(e, W)}`), console.log(
|
|
791
|
+
`${a}Internal Params: ${JSON.stringify(c(e, z))}`
|
|
694
792
|
);
|
|
695
|
-
const
|
|
696
|
-
|
|
697
|
-
console.log(`${
|
|
698
|
-
`${
|
|
793
|
+
const i = c(e, M).flags;
|
|
794
|
+
i.length > 0 ? (console.log(`${a}Flags (${i.length}):`), i.forEach((l) => {
|
|
795
|
+
console.log(`${t}* ${S.green(l.name)}:`), console.log(`${t} Options: ${l.options.join(", ")}`), console.log(
|
|
796
|
+
`${t} Description: ${Array.isArray(l.description) ? l.description.join(" | ") : l.description}`
|
|
699
797
|
), console.log(
|
|
700
|
-
`${
|
|
798
|
+
`${t} Type: ${typeof l.type == "function" ? l.type.name || "custom function" : l.type}`
|
|
701
799
|
), console.log(
|
|
702
|
-
`${
|
|
800
|
+
`${t} Mandatory: ${typeof l.mandatory == "function" ? "dynamic" : l.mandatory ?? !1}`
|
|
703
801
|
), console.log(
|
|
704
|
-
`${
|
|
705
|
-
), console.log(`${
|
|
706
|
-
`${
|
|
707
|
-
), console.log(`${
|
|
708
|
-
})) : console.log(`${
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
})) : console.log(`${
|
|
713
|
-
},
|
|
714
|
-
if (
|
|
715
|
-
|
|
716
|
-
let
|
|
717
|
-
const
|
|
718
|
-
|
|
802
|
+
`${t} Default: ${JSON.stringify(l.defaultValue)}`
|
|
803
|
+
), console.log(`${t} Flag Only: ${l.flagOnly}`), console.log(`${t} Allow Multiple: ${l.allowMultiple}`), console.log(`${t} Allow Ligature: ${l.allowLigature}`), console.log(
|
|
804
|
+
`${t} Enum: ${l.enum && l.enum.length > 0 ? l.enum.join(", ") : "none"}`
|
|
805
|
+
), console.log(`${t} Validator Defined: ${!!l.validate}`);
|
|
806
|
+
})) : console.log(`${a}Flags: ${S.dim("none")}`);
|
|
807
|
+
const o = Array.from(c(e, T).values());
|
|
808
|
+
o.length > 0 ? (console.log(`${a}Sub-Commands (${o.length}):`), o.forEach((l) => {
|
|
809
|
+
$(this, m, Ce).call(this, l.parser, n + 1, s);
|
|
810
|
+
})) : console.log(`${a}Sub-Commands: ${S.dim("none")}`);
|
|
811
|
+
}, Ae = function(e, n, s = /* @__PURE__ */ new Set()) {
|
|
812
|
+
if (s.has(e)) return "";
|
|
813
|
+
s.add(e);
|
|
814
|
+
let r = "";
|
|
815
|
+
const a = " ".repeat(n), t = " ".repeat(n + 1), i = " ".repeat(n + 2), o = (d) => {
|
|
816
|
+
r += d + "\\n";
|
|
719
817
|
};
|
|
720
|
-
|
|
721
|
-
`${
|
|
818
|
+
o(
|
|
819
|
+
`${a}Parser: ${c(e, R) || c(e, P)}`
|
|
722
820
|
// #appName is guaranteed
|
|
723
|
-
),
|
|
724
|
-
`${
|
|
725
|
-
),
|
|
726
|
-
`${
|
|
727
|
-
),
|
|
728
|
-
`${
|
|
821
|
+
), c(e, L) && o(`${t}Description: ${c(e, L)}`), o(`${t}Options:`), o(`${i}appName: ${c(e, P)}`), o(
|
|
822
|
+
`${i}appCommandName: ${c(e, V) ?? "undefined"}`
|
|
823
|
+
), o(`${i}handleErrors: ${c(e, q)}`), o(
|
|
824
|
+
`${i}throwForDuplicateFlags: ${c(e, ee)}`
|
|
825
|
+
), o(`${i}inheritParentFlags: ${c(e, U)}`), o(`${i}Handler Defined: ${!!c(e, W)}`), o(
|
|
826
|
+
`${t}Internal Params: ${JSON.stringify(c(e, z))}`
|
|
729
827
|
);
|
|
730
|
-
const
|
|
731
|
-
|
|
732
|
-
var
|
|
733
|
-
|
|
734
|
-
`${
|
|
828
|
+
const l = c(e, M).flags;
|
|
829
|
+
l.length > 0 ? (o(`${t}Flags (${l.length}):`), l.forEach((d) => {
|
|
830
|
+
var b;
|
|
831
|
+
o(`${i}* ${d.name}:`), o(`${i} Options: ${d.options.join(", ")}`), o(
|
|
832
|
+
`${i} Description: ${Array.isArray(d.description) ? d.description.join(" | ") : d.description}`
|
|
735
833
|
);
|
|
736
|
-
let
|
|
737
|
-
if (typeof
|
|
738
|
-
|
|
739
|
-
else if (typeof
|
|
740
|
-
|
|
741
|
-
else if (typeof
|
|
834
|
+
let f = "unknown";
|
|
835
|
+
if (typeof d.type == "function")
|
|
836
|
+
f = d.type.name || "custom function";
|
|
837
|
+
else if (typeof d.type == "string")
|
|
838
|
+
f = d.type;
|
|
839
|
+
else if (typeof d.type == "object" && d.type)
|
|
742
840
|
try {
|
|
743
|
-
|
|
841
|
+
f = ((b = d.type.constructor) == null ? void 0 : b.name) || "object";
|
|
744
842
|
} catch {
|
|
745
|
-
|
|
843
|
+
f = "object";
|
|
746
844
|
}
|
|
747
|
-
|
|
748
|
-
`${
|
|
749
|
-
),
|
|
750
|
-
`${
|
|
751
|
-
),
|
|
752
|
-
`${
|
|
753
|
-
),
|
|
754
|
-
})) :
|
|
755
|
-
const
|
|
756
|
-
return
|
|
757
|
-
|
|
758
|
-
})) :
|
|
759
|
-
},
|
|
845
|
+
o(`${i} Type: ${f}`), o(
|
|
846
|
+
`${i} Mandatory: ${typeof d.mandatory == "function" ? "dynamic" : d.mandatory ?? !1}`
|
|
847
|
+
), o(
|
|
848
|
+
`${i} Default: ${JSON.stringify(d.defaultValue)}`
|
|
849
|
+
), o(`${i} Flag Only: ${d.flagOnly}`), o(`${i} Allow Multiple: ${d.allowMultiple}`), o(`${i} Allow Ligature: ${d.allowLigature}`), o(
|
|
850
|
+
`${i} Enum: ${d.enum && d.enum.length > 0 ? d.enum.join(", ") : "none"}`
|
|
851
|
+
), o(`${i} Validator Defined: ${!!d.validate}`);
|
|
852
|
+
})) : o(`${t}Flags: none`);
|
|
853
|
+
const u = Array.from(c(e, T).values());
|
|
854
|
+
return u.length > 0 ? (o(`${t}Sub-Commands (${u.length}):`), u.forEach((d) => {
|
|
855
|
+
r += $(this, m, Ae).call(this, d.parser, n + 1, s);
|
|
856
|
+
})) : o(`${t}Sub-Commands: none`), r;
|
|
857
|
+
}, Fe = function(e, n = /* @__PURE__ */ new Set()) {
|
|
760
858
|
if (n.has(e))
|
|
761
859
|
return {
|
|
762
|
-
note: `Reference to already processed parser: ${
|
|
860
|
+
note: `Reference to already processed parser: ${c(e, R) || c(e, P)}`
|
|
763
861
|
};
|
|
764
862
|
n.add(e);
|
|
765
|
-
const
|
|
766
|
-
parserName:
|
|
863
|
+
const s = {
|
|
864
|
+
parserName: c(e, R) || c(e, P),
|
|
767
865
|
// #appName is guaranteed
|
|
768
|
-
description:
|
|
866
|
+
description: c(e, L),
|
|
769
867
|
options: {
|
|
770
|
-
appName:
|
|
771
|
-
appCommandName:
|
|
772
|
-
handleErrors:
|
|
773
|
-
throwForDuplicateFlags:
|
|
774
|
-
inheritParentFlags:
|
|
868
|
+
appName: c(e, P),
|
|
869
|
+
appCommandName: c(e, V) ?? void 0,
|
|
870
|
+
handleErrors: c(e, q),
|
|
871
|
+
throwForDuplicateFlags: c(e, ee),
|
|
872
|
+
inheritParentFlags: c(e, U)
|
|
775
873
|
},
|
|
776
|
-
handlerDefined: !!
|
|
777
|
-
internalParams:
|
|
874
|
+
handlerDefined: !!c(e, W),
|
|
875
|
+
internalParams: c(e, z),
|
|
778
876
|
flags: [],
|
|
779
877
|
subCommands: {}
|
|
780
878
|
// Will be an object where keys are sub-command names
|
|
781
|
-
},
|
|
782
|
-
|
|
783
|
-
var
|
|
784
|
-
let
|
|
785
|
-
if (typeof
|
|
786
|
-
|
|
787
|
-
else if (typeof
|
|
788
|
-
|
|
789
|
-
else if (typeof
|
|
879
|
+
}, r = c(e, M).flags;
|
|
880
|
+
s.flags = r.map((t) => {
|
|
881
|
+
var o;
|
|
882
|
+
let i = "unknown";
|
|
883
|
+
if (typeof t.type == "function")
|
|
884
|
+
i = t.type.name || "custom function";
|
|
885
|
+
else if (typeof t.type == "string")
|
|
886
|
+
i = t.type;
|
|
887
|
+
else if (typeof t.type == "object" && t.type)
|
|
790
888
|
try {
|
|
791
|
-
|
|
889
|
+
i = ((o = t.type.constructor) == null ? void 0 : o.name) || "object";
|
|
792
890
|
} catch {
|
|
793
|
-
|
|
891
|
+
i = "object";
|
|
794
892
|
}
|
|
795
893
|
return {
|
|
796
|
-
name:
|
|
797
|
-
options:
|
|
798
|
-
description:
|
|
799
|
-
type:
|
|
800
|
-
mandatory: typeof
|
|
801
|
-
defaultValue:
|
|
802
|
-
flagOnly:
|
|
803
|
-
allowMultiple:
|
|
804
|
-
allowLigature:
|
|
805
|
-
enum:
|
|
806
|
-
validatorDefined: !!
|
|
894
|
+
name: t.name,
|
|
895
|
+
options: t.options,
|
|
896
|
+
description: t.description,
|
|
897
|
+
type: i,
|
|
898
|
+
mandatory: typeof t.mandatory == "function" ? "dynamic" : t.mandatory ?? !1,
|
|
899
|
+
defaultValue: t.defaultValue,
|
|
900
|
+
flagOnly: t.flagOnly,
|
|
901
|
+
allowMultiple: t.allowMultiple,
|
|
902
|
+
allowLigature: t.allowLigature,
|
|
903
|
+
enum: t.enum,
|
|
904
|
+
validatorDefined: !!t.validate
|
|
807
905
|
};
|
|
808
906
|
});
|
|
809
|
-
const
|
|
810
|
-
return
|
|
811
|
-
|
|
812
|
-
}),
|
|
907
|
+
const a = Array.from(c(e, T).values());
|
|
908
|
+
return a.length > 0 && a.forEach((t) => {
|
|
909
|
+
s.subCommands[t.name] = $(this, m, Fe).call(this, t.parser, n);
|
|
910
|
+
}), s;
|
|
911
|
+
}, /**
|
|
912
|
+
* Generates a default environment file name based on the app name
|
|
913
|
+
*/
|
|
914
|
+
Ne = function() {
|
|
915
|
+
let e = "config";
|
|
916
|
+
return c(this, V) ? e = c(this, V) : c(this, P) && c(this, P) !== "Argument Parser" && (e = c(this, P)), e = e.split(/[\s-_]+/).map((n) => n.charAt(0).toUpperCase() + n.slice(1).toLowerCase()).join(""), `${e}.env`;
|
|
917
|
+
}, /**
|
|
918
|
+
* Handles the --s-save-to-env system flag at the final parser level
|
|
919
|
+
*/
|
|
920
|
+
We = function(e, n) {
|
|
921
|
+
const s = e.findIndex((r) => r === "--s-save-to-env");
|
|
922
|
+
if (s !== -1) {
|
|
923
|
+
let r;
|
|
924
|
+
if (s + 1 < e.length) {
|
|
925
|
+
const a = e[s + 1];
|
|
926
|
+
a && !a.startsWith("-") ? r = a : r = $(this, m, Ne).call(this);
|
|
927
|
+
} else
|
|
928
|
+
r = $(this, m, Ne).call(this);
|
|
929
|
+
try {
|
|
930
|
+
return $(this, m, Je).call(this, r, e, n), console.log(S.green(`Environment configuration saved to: ${r}`)), typeof process == "object" && typeof process.exit == "function" && process.exit(0), !0;
|
|
931
|
+
} catch (a) {
|
|
932
|
+
return console.error(S.red(`Error saving environment file: ${a instanceof Error ? a.message : String(a)}`)), typeof process == "object" && typeof process.exit == "function" && process.exit(1), !0;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
return !1;
|
|
936
|
+
}, /**
|
|
937
|
+
* Saves the current parser's flags and their values to an environment file
|
|
938
|
+
* in the specified format based on file extension.
|
|
939
|
+
*/
|
|
940
|
+
Je = function(e, n, s) {
|
|
941
|
+
const r = ce.extname(e).toLowerCase();
|
|
942
|
+
let a;
|
|
943
|
+
r === ".yaml" || r === ".yml" ? a = "yaml" : r === ".json" || r === ".jsonc" ? a = "json" : r === ".toml" || r === ".tml" ? a = "toml" : a = "env";
|
|
944
|
+
const t = [], i = /* @__PURE__ */ new Set();
|
|
945
|
+
for (let f = s.length - 1; f >= 0; f--) {
|
|
946
|
+
const b = s[f];
|
|
947
|
+
for (const h of c(b, M).flags)
|
|
948
|
+
i.has(h.name) || (t.push(h), i.add(h.name));
|
|
949
|
+
}
|
|
950
|
+
const o = t, { parsedArgs: l } = $(this, m, ue).call(this, n.filter(
|
|
951
|
+
(f) => f !== "--s-save-to-env" && f !== e
|
|
952
|
+
));
|
|
953
|
+
let u;
|
|
954
|
+
switch (a) {
|
|
955
|
+
case "env":
|
|
956
|
+
u = $(this, m, Xe).call(this, o, l);
|
|
957
|
+
break;
|
|
958
|
+
case "yaml":
|
|
959
|
+
u = $(this, m, et).call(this, o, l);
|
|
960
|
+
break;
|
|
961
|
+
case "json":
|
|
962
|
+
u = $(this, m, tt).call(this, o, l);
|
|
963
|
+
break;
|
|
964
|
+
case "toml":
|
|
965
|
+
u = $(this, m, nt).call(this, o, l);
|
|
966
|
+
break;
|
|
967
|
+
}
|
|
968
|
+
const d = ce.dirname(e);
|
|
969
|
+
G.existsSync(d) || G.mkdirSync(d, { recursive: !0 }), G.writeFileSync(e, u, "utf8");
|
|
970
|
+
}, /**
|
|
971
|
+
* Loads configuration from an environment file in various formats
|
|
972
|
+
*/
|
|
973
|
+
Be = function(e, n) {
|
|
974
|
+
if (!G.existsSync(e))
|
|
975
|
+
throw new Error(`Configuration file not found: ${e}`);
|
|
976
|
+
const s = ce.extname(e).toLowerCase();
|
|
977
|
+
let r;
|
|
978
|
+
s === ".yaml" || s === ".yml" ? r = "yaml" : s === ".json" || s === ".jsonc" ? r = "json" : s === ".toml" || s === ".tml" ? r = "toml" : r = "env";
|
|
979
|
+
const a = G.readFileSync(e, "utf8");
|
|
980
|
+
let t;
|
|
981
|
+
switch (r) {
|
|
982
|
+
case "env":
|
|
983
|
+
t = $(this, m, qe).call(this, a);
|
|
984
|
+
break;
|
|
985
|
+
case "yaml":
|
|
986
|
+
t = $(this, m, Ue).call(this, a);
|
|
987
|
+
break;
|
|
988
|
+
case "json":
|
|
989
|
+
t = $(this, m, Ze).call(this, a);
|
|
990
|
+
break;
|
|
991
|
+
case "toml":
|
|
992
|
+
t = $(this, m, Ge).call(this, a);
|
|
993
|
+
break;
|
|
994
|
+
}
|
|
995
|
+
return $(this, m, Ye).call(this, t, n);
|
|
996
|
+
}, /**
|
|
997
|
+
* Parses .env file content using dotenv
|
|
998
|
+
*/
|
|
999
|
+
qe = function(e) {
|
|
1000
|
+
const n = lt.parse(e), s = {};
|
|
1001
|
+
for (const [r, a] of Object.entries(n)) {
|
|
1002
|
+
const t = r.toLowerCase().replace(/_/g, "-");
|
|
1003
|
+
a === "true" ? s[t] = !0 : a === "false" ? s[t] = !1 : /^-?\d+$/.test(a) ? s[t] = parseInt(a, 10) : /^-?\d*\.\d+$/.test(a) ? s[t] = parseFloat(a) : a.includes(",") ? s[t] = a.split(",").map((i) => i.trim()) : s[t] = a;
|
|
1004
|
+
}
|
|
1005
|
+
return s;
|
|
1006
|
+
}, /**
|
|
1007
|
+
* Parses YAML file content
|
|
1008
|
+
*/
|
|
1009
|
+
Ue = function(e) {
|
|
1010
|
+
const n = De.load(e);
|
|
1011
|
+
if (typeof n != "object" || n === null)
|
|
1012
|
+
throw new Error("YAML file must contain an object at the root level");
|
|
1013
|
+
const { _meta: s, ...r } = n;
|
|
1014
|
+
return r;
|
|
1015
|
+
}, /**
|
|
1016
|
+
* Parses JSON file content
|
|
1017
|
+
*/
|
|
1018
|
+
Ze = function(e) {
|
|
1019
|
+
const n = JSON.parse(e);
|
|
1020
|
+
if (typeof n != "object" || n === null)
|
|
1021
|
+
throw new Error("JSON file must contain an object at the root level");
|
|
1022
|
+
const { _meta: s, ...r } = n;
|
|
1023
|
+
return r;
|
|
1024
|
+
}, /**
|
|
1025
|
+
* Parses TOML file content
|
|
1026
|
+
*/
|
|
1027
|
+
Ge = function(e) {
|
|
1028
|
+
const n = He.parse(e);
|
|
1029
|
+
if (typeof n != "object" || n === null)
|
|
1030
|
+
throw new Error("TOML file must contain an object at the root level");
|
|
1031
|
+
return n;
|
|
1032
|
+
}, /**
|
|
1033
|
+
* Converts raw configuration values to match flag types and validates them
|
|
1034
|
+
*/
|
|
1035
|
+
Ye = function(e, n) {
|
|
1036
|
+
const s = {}, r = [], a = /* @__PURE__ */ new Set();
|
|
1037
|
+
for (let t = n.length - 1; t >= 0; t--) {
|
|
1038
|
+
const i = n[t];
|
|
1039
|
+
for (const o of c(i, M).flags)
|
|
1040
|
+
a.has(o.name) || (r.push(o), a.add(o.name));
|
|
1041
|
+
}
|
|
1042
|
+
for (const [t, i] of Object.entries(e)) {
|
|
1043
|
+
const o = r.find((l) => l.name === t);
|
|
1044
|
+
if (!o) {
|
|
1045
|
+
console.warn(S.yellow(`Warning: Configuration key '${t}' does not match any known flag. Ignoring.`));
|
|
1046
|
+
continue;
|
|
1047
|
+
}
|
|
1048
|
+
try {
|
|
1049
|
+
const l = $(this, m, Ke).call(this, i, o);
|
|
1050
|
+
s[t] = l;
|
|
1051
|
+
} catch (l) {
|
|
1052
|
+
console.error(S.red(`Error converting configuration value for '${t}': ${l instanceof Error ? l.message : String(l)}`)), typeof process == "object" && typeof process.exit == "function" && process.exit(1);
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
return s;
|
|
1056
|
+
}, /**
|
|
1057
|
+
* Converts a configuration value to match the expected flag type
|
|
1058
|
+
*/
|
|
1059
|
+
Ke = function(e, n) {
|
|
1060
|
+
const s = n.type;
|
|
1061
|
+
if (e == null)
|
|
1062
|
+
return e;
|
|
1063
|
+
if (s === Array || n.allowMultiple)
|
|
1064
|
+
return Array.isArray(e) ? e : typeof e == "string" ? e.split(",").map((r) => r.trim()) : [e];
|
|
1065
|
+
if (s === Boolean) {
|
|
1066
|
+
if (typeof e == "boolean") return e;
|
|
1067
|
+
if (typeof e == "string") {
|
|
1068
|
+
const r = e.toLowerCase();
|
|
1069
|
+
if (r === "true" || r === "1" || r === "yes") return !0;
|
|
1070
|
+
if (r === "false" || r === "0" || r === "no") return !1;
|
|
1071
|
+
}
|
|
1072
|
+
throw new Error(`Cannot convert '${e}' to boolean for flag '${n.name}'`);
|
|
1073
|
+
}
|
|
1074
|
+
if (s === String)
|
|
1075
|
+
return String(e);
|
|
1076
|
+
if (s === Number) {
|
|
1077
|
+
const r = Number(e);
|
|
1078
|
+
if (isNaN(r))
|
|
1079
|
+
throw new Error(`Cannot convert '${e}' to number for flag '${n.name}'`);
|
|
1080
|
+
return r;
|
|
1081
|
+
}
|
|
1082
|
+
if (n.enum && n.enum.length > 0 && !n.enum.includes(e))
|
|
1083
|
+
throw new Error(`Value '${e}' is not allowed for flag '${n.name}'. Allowed values: ${n.enum.join(", ")}`);
|
|
1084
|
+
return e;
|
|
1085
|
+
}, /**
|
|
1086
|
+
* Merges environment configuration with command line arguments
|
|
1087
|
+
* CLI arguments take precedence over file configuration
|
|
1088
|
+
*/
|
|
1089
|
+
Qe = function(e, n) {
|
|
1090
|
+
const s = [...n], r = s.findIndex((t) => t === "--s-with-env");
|
|
1091
|
+
r !== -1 && s.splice(r, 2);
|
|
1092
|
+
const a = /* @__PURE__ */ new Set();
|
|
1093
|
+
for (let t = 0; t < s.length; t++) {
|
|
1094
|
+
const i = s[t];
|
|
1095
|
+
if (i.startsWith("-") && (a.add(i), i.includes("="))) {
|
|
1096
|
+
const o = i.split("=")[0];
|
|
1097
|
+
a.add(o);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
for (const [t, i] of Object.entries(e)) {
|
|
1101
|
+
const o = `--${t}`;
|
|
1102
|
+
if (!a.has(o))
|
|
1103
|
+
if (typeof i == "boolean")
|
|
1104
|
+
i && s.push(o);
|
|
1105
|
+
else if (Array.isArray(i))
|
|
1106
|
+
for (const l of i)
|
|
1107
|
+
s.push(o, String(l));
|
|
1108
|
+
else
|
|
1109
|
+
s.push(o, String(i));
|
|
1110
|
+
}
|
|
1111
|
+
return s;
|
|
1112
|
+
}, /**
|
|
1113
|
+
* Generates environment file content in Bash .env format
|
|
1114
|
+
*/
|
|
1115
|
+
Xe = function(e, n) {
|
|
1116
|
+
const s = [];
|
|
1117
|
+
s.push("# Environment configuration generated by ArgParser"), s.push("# Format: Bash .env style"), s.push("");
|
|
1118
|
+
for (const r of e) {
|
|
1119
|
+
if (r.name === "help") continue;
|
|
1120
|
+
const a = n[r.name], t = a != null, i = typeof r.mandatory == "function" ? !1 : r.mandatory ?? !1;
|
|
1121
|
+
s.push(`# ${r.name}: ${Array.isArray(r.description) ? r.description.join(" | ") : r.description}`), s.push(`# Options: ${r.options.join(", ")}`), s.push(`# Type: ${$(this, m, de).call(this, r.type)}`), r.defaultValue !== void 0 && s.push(`# Default: ${JSON.stringify(r.defaultValue)}`), r.enum && r.enum.length > 0 && s.push(`# Allowed values: ${r.enum.join(", ")}`);
|
|
1122
|
+
const o = r.name.toUpperCase().replace(/[^A-Z0-9_]/g, "_");
|
|
1123
|
+
let l = "";
|
|
1124
|
+
if (t)
|
|
1125
|
+
Array.isArray(a) ? l = a.join(",") : typeof a == "boolean" ? l = a ? "true" : "false" : l = String(a), s.push(`${o}="${l}"`);
|
|
1126
|
+
else {
|
|
1127
|
+
const u = r.defaultValue !== void 0 ? String(r.defaultValue) : "", d = i ? "" : "# ";
|
|
1128
|
+
s.push(`${d}${o}="${u}"`);
|
|
1129
|
+
}
|
|
1130
|
+
s.push("");
|
|
1131
|
+
}
|
|
1132
|
+
return s.join(`
|
|
1133
|
+
`);
|
|
1134
|
+
}, /**
|
|
1135
|
+
* Generates environment file content in YAML format
|
|
1136
|
+
*/
|
|
1137
|
+
et = function(e, n) {
|
|
1138
|
+
const s = {}, r = [];
|
|
1139
|
+
r.push("# Environment configuration generated by ArgParser"), r.push("# Format: YAML"), r.push("");
|
|
1140
|
+
for (const t of e) {
|
|
1141
|
+
if (t.name === "help") continue;
|
|
1142
|
+
const i = n[t.name], o = i != null, l = typeof t.mandatory == "function" ? !1 : t.mandatory ?? !1;
|
|
1143
|
+
r.push(`# ${t.name}: ${Array.isArray(t.description) ? t.description.join(" | ") : t.description}`), r.push(`# Options: ${t.options.join(", ")}`), r.push(`# Type: ${$(this, m, de).call(this, t.type)}`), t.defaultValue !== void 0 && r.push(`# Default: ${JSON.stringify(t.defaultValue)}`), t.enum && t.enum.length > 0 && r.push(`# Allowed values: ${t.enum.join(", ")}`), o ? s[t.name] = i : l && (s[t.name] = t.defaultValue !== void 0 ? t.defaultValue : null), r.push("");
|
|
1144
|
+
}
|
|
1145
|
+
const a = De.dump(s, {
|
|
1146
|
+
indent: 2,
|
|
1147
|
+
lineWidth: -1,
|
|
1148
|
+
noRefs: !0,
|
|
1149
|
+
sortKeys: !0
|
|
1150
|
+
});
|
|
1151
|
+
return r.join(`
|
|
1152
|
+
`) + `
|
|
1153
|
+
` + a;
|
|
1154
|
+
}, /**
|
|
1155
|
+
* Generates environment file content in JSON format
|
|
1156
|
+
*/
|
|
1157
|
+
tt = function(e, n) {
|
|
1158
|
+
const s = {}, r = {
|
|
1159
|
+
_meta: {
|
|
1160
|
+
generated_by: "ArgParser",
|
|
1161
|
+
format: "JSON",
|
|
1162
|
+
flags_info: {}
|
|
1163
|
+
}
|
|
1164
|
+
};
|
|
1165
|
+
for (const t of e) {
|
|
1166
|
+
if (t.name === "help") continue;
|
|
1167
|
+
const i = n[t.name], o = i != null, l = typeof t.mandatory == "function" ? !1 : t.mandatory ?? !1;
|
|
1168
|
+
r._meta.flags_info[t.name] = {
|
|
1169
|
+
description: Array.isArray(t.description) ? t.description.join(" | ") : t.description,
|
|
1170
|
+
options: t.options,
|
|
1171
|
+
type: $(this, m, de).call(this, t.type),
|
|
1172
|
+
mandatory: l,
|
|
1173
|
+
defaultValue: t.defaultValue,
|
|
1174
|
+
enum: t.enum && t.enum.length > 0 ? t.enum : void 0
|
|
1175
|
+
}, o ? s[t.name] = i : l && (s[t.name] = t.defaultValue !== void 0 ? t.defaultValue : null);
|
|
1176
|
+
}
|
|
1177
|
+
const a = { ...r, ...s };
|
|
1178
|
+
return JSON.stringify(a, null, 2);
|
|
1179
|
+
}, /**
|
|
1180
|
+
* Generates environment file content in TOML format
|
|
1181
|
+
*/
|
|
1182
|
+
nt = function(e, n) {
|
|
1183
|
+
const s = {}, r = [];
|
|
1184
|
+
r.push("# Environment configuration generated by ArgParser"), r.push("# Format: TOML"), r.push("");
|
|
1185
|
+
for (const t of e) {
|
|
1186
|
+
if (t.name === "help") continue;
|
|
1187
|
+
const i = n[t.name], o = i != null, l = typeof t.mandatory == "function" ? !1 : t.mandatory ?? !1;
|
|
1188
|
+
r.push(`# ${t.name}: ${Array.isArray(t.description) ? t.description.join(" | ") : t.description}`), r.push(`# Options: ${t.options.join(", ")}`), r.push(`# Type: ${$(this, m, de).call(this, t.type)}`), t.defaultValue !== void 0 && r.push(`# Default: ${JSON.stringify(t.defaultValue)}`), t.enum && t.enum.length > 0 && r.push(`# Allowed values: ${t.enum.join(", ")}`), o ? s[t.name] = i : l && (s[t.name] = t.defaultValue !== void 0 ? t.defaultValue : null), r.push("");
|
|
1189
|
+
}
|
|
1190
|
+
const a = He.stringify(s);
|
|
1191
|
+
return r.join(`
|
|
1192
|
+
`) + `
|
|
1193
|
+
` + a;
|
|
1194
|
+
}, /**
|
|
1195
|
+
* Helper method to get a string representation of a flag's type
|
|
1196
|
+
*/
|
|
1197
|
+
de = function(e) {
|
|
1198
|
+
var n;
|
|
1199
|
+
if (typeof e == "function")
|
|
1200
|
+
return e.name || "custom function";
|
|
1201
|
+
if (typeof e == "string")
|
|
1202
|
+
return e;
|
|
1203
|
+
if (typeof e == "object" && e)
|
|
1204
|
+
try {
|
|
1205
|
+
return ((n = e.constructor) == null ? void 0 : n.name) || "object";
|
|
1206
|
+
} catch {
|
|
1207
|
+
return "object";
|
|
1208
|
+
}
|
|
1209
|
+
return "unknown";
|
|
1210
|
+
};
|
|
1211
|
+
let he = K;
|
|
1212
|
+
function ut(g) {
|
|
1213
|
+
let e;
|
|
1214
|
+
const n = g.type;
|
|
1215
|
+
let s;
|
|
1216
|
+
typeof n == "function" ? s = n.name.toLowerCase().replace("constructor", "") : s = String(n).toLowerCase();
|
|
1217
|
+
const r = g.enum;
|
|
1218
|
+
if (g.allowMultiple) {
|
|
1219
|
+
let o;
|
|
1220
|
+
if (r && Array.isArray(r) && r.length > 0)
|
|
1221
|
+
if (r.every((l) => typeof l == "string"))
|
|
1222
|
+
o = p.enum(r);
|
|
1223
|
+
else if (r.every((l) => typeof l == "number")) {
|
|
1224
|
+
const l = r.map((u) => p.literal(u));
|
|
1225
|
+
l.length === 1 ? o = l[0] : o = p.union(
|
|
1226
|
+
l
|
|
1227
|
+
);
|
|
1228
|
+
} else
|
|
1229
|
+
o = p.string();
|
|
1230
|
+
else
|
|
1231
|
+
switch (s) {
|
|
1232
|
+
case "number":
|
|
1233
|
+
o = p.number();
|
|
1234
|
+
break;
|
|
1235
|
+
case "boolean":
|
|
1236
|
+
o = p.boolean();
|
|
1237
|
+
break;
|
|
1238
|
+
default:
|
|
1239
|
+
o = p.string();
|
|
1240
|
+
break;
|
|
1241
|
+
}
|
|
1242
|
+
e = p.array(o);
|
|
1243
|
+
} else
|
|
1244
|
+
switch (s) {
|
|
1245
|
+
case "string":
|
|
1246
|
+
e = r && Array.isArray(r) && r.length > 0 && r.every((l) => typeof l == "string") ? p.enum(r) : p.string();
|
|
1247
|
+
break;
|
|
1248
|
+
case "number":
|
|
1249
|
+
if (r && Array.isArray(r) && r.length > 0 && r.every((l) => typeof l == "number")) {
|
|
1250
|
+
const l = r.map((u) => p.literal(u));
|
|
1251
|
+
l.length === 1 ? e = l[0] : l.length >= 2 ? e = p.union(
|
|
1252
|
+
l
|
|
1253
|
+
) : e = p.number();
|
|
1254
|
+
} else
|
|
1255
|
+
e = p.number();
|
|
1256
|
+
break;
|
|
1257
|
+
case "boolean":
|
|
1258
|
+
e = p.boolean();
|
|
1259
|
+
break;
|
|
1260
|
+
case "array":
|
|
1261
|
+
const o = r && Array.isArray(r) && r.length > 0 && r.every((l) => typeof l == "string") ? p.enum(r) : p.string();
|
|
1262
|
+
e = p.array(o);
|
|
1263
|
+
break;
|
|
1264
|
+
case "object":
|
|
1265
|
+
e = p.record(p.string(), p.any());
|
|
1266
|
+
break;
|
|
1267
|
+
default:
|
|
1268
|
+
console.warn(
|
|
1269
|
+
`[MCP Integration] Flag '${g.name}' has an unknown type '${s}'. Defaulting to z.string().`
|
|
1270
|
+
), e = p.string();
|
|
1271
|
+
break;
|
|
1272
|
+
}
|
|
1273
|
+
const t = g.description;
|
|
1274
|
+
t && (e = e.describe(
|
|
1275
|
+
Array.isArray(t) ? t.join(`
|
|
1276
|
+
`) : t
|
|
1277
|
+
));
|
|
1278
|
+
const i = g.defaultValue;
|
|
1279
|
+
return i !== void 0 ? e = e.default(i) : g.mandatory || (e = e.optional()), e;
|
|
1280
|
+
}
|
|
1281
|
+
function dt(g, e) {
|
|
1282
|
+
const n = [], s = /* @__PURE__ */ new Set();
|
|
1283
|
+
function r(a, t) {
|
|
1284
|
+
if (s.has(a)) return;
|
|
1285
|
+
s.add(a);
|
|
1286
|
+
const i = g, o = a, l = i.getAppName ? i.getAppName() : i["#appName"], u = o.getDescription ? o.getDescription() : o["#description"], d = o.getHandler ? o.getHandler() : o["#handler"], f = o.flags, b = o.getSubCommands ? o.getSubCommands() : o["#subCommands"], h = b ? Array.from(b.values()) : [];
|
|
1287
|
+
let v = o.getAppCommandName ? o.getAppCommandName() : o["#appCommandName"];
|
|
1288
|
+
!v && a !== g && (v = o.getSubCommandName ? o.getSubCommandName() : o["#subCommandName"]), a !== g && t.length > 0 && (v = t[t.length - 1]);
|
|
1289
|
+
const O = v || l, C = v || (t.length > 0 ? t[t.length - 1] : l);
|
|
1290
|
+
let y;
|
|
1291
|
+
if (e != null && e.generateToolName ? y = e.generateToolName(t, l) : (a === g ? y = O || l || "root_cmd" : y = C || "cmd", y = y.replace(/[^a-zA-Z0-9_-]/g, "_")), y || (y = a === g && l ? l : "cmd"), e != null && e.toolNamePrefix && (y = e.toolNamePrefix + y), e != null && e.toolNameSuffix && (y = y + e.toolNameSuffix), d) {
|
|
1292
|
+
const A = f, F = {}, N = A.some((k) => k.name === "help");
|
|
1293
|
+
for (const k of A) {
|
|
1294
|
+
let E = ut(k);
|
|
1295
|
+
N && k.name !== "help" && k.mandatory && (E = E.optional()), F[k.name] = E;
|
|
1296
|
+
}
|
|
1297
|
+
const re = p.object(F);
|
|
1298
|
+
let X;
|
|
1299
|
+
if (e != null && e.outputSchemaMap && e.outputSchemaMap[y]) {
|
|
1300
|
+
const k = e.outputSchemaMap[y];
|
|
1301
|
+
X = typeof k == "object" && !k._def ? p.object(k) : k;
|
|
1302
|
+
} else e != null && e.defaultOutputSchema && (X = e.defaultOutputSchema);
|
|
1303
|
+
const pe = {
|
|
1304
|
+
name: y,
|
|
1305
|
+
description: u || `Executes the ${y} command.`,
|
|
1306
|
+
inputSchema: re,
|
|
1307
|
+
outputSchema: X,
|
|
1308
|
+
async execute(k) {
|
|
1309
|
+
var je, Ee, Pe, Te, Oe, ke;
|
|
1310
|
+
if (k.help === !0) {
|
|
1311
|
+
let x = g;
|
|
1312
|
+
const I = [...t];
|
|
1313
|
+
for (const D of I) {
|
|
1314
|
+
const B = x.getSubCommand ? x.getSubCommand(D) : void 0;
|
|
1315
|
+
if (B && B.parser)
|
|
1316
|
+
x = B.parser;
|
|
1317
|
+
else
|
|
1318
|
+
break;
|
|
1319
|
+
}
|
|
1320
|
+
const w = x.helpText ? x.helpText() : "Help not available";
|
|
1321
|
+
return (je = e == null ? void 0 : e.outputSchemaMap) != null && je[y] ? {
|
|
1322
|
+
success: !0,
|
|
1323
|
+
help: w,
|
|
1324
|
+
files: [],
|
|
1325
|
+
commandExecuted: null,
|
|
1326
|
+
stderrOutput: null
|
|
1327
|
+
} : { success: !0, message: w };
|
|
1328
|
+
}
|
|
1329
|
+
const E = [...t], rt = f;
|
|
1330
|
+
for (const x of rt) {
|
|
1331
|
+
const I = x.name;
|
|
1332
|
+
if (k.hasOwnProperty(I)) {
|
|
1333
|
+
const w = k[I], D = x.type, B = x.options, se = x.flagOnly, ie = x.allowMultiple;
|
|
1334
|
+
let le = typeof D == "function" ? D.name.toLowerCase().replace("constructor", "") : String(D).toLowerCase();
|
|
1335
|
+
if (E.push(B[0]), le === "boolean")
|
|
1336
|
+
(w === !0 && se === !1 || w === !1 && se === !1) && E.push(String(w));
|
|
1337
|
+
else if (le === "array")
|
|
1338
|
+
if (Array.isArray(w))
|
|
1339
|
+
if (ie) {
|
|
1340
|
+
const Z = E.length;
|
|
1341
|
+
w.forEach((te) => {
|
|
1342
|
+
E.push(B[0]), E.push(String(te));
|
|
1343
|
+
}), w.length > 0 && E[Z - 1] === B[0] && E.splice(Z - 1, 1);
|
|
1344
|
+
} else
|
|
1345
|
+
E.push(w.join(","));
|
|
1346
|
+
else w != null && E.push(String(w));
|
|
1347
|
+
else w != null && E.push(String(w));
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
try {
|
|
1351
|
+
const x = await g.parse(
|
|
1352
|
+
E
|
|
1353
|
+
);
|
|
1354
|
+
if (x.$error) {
|
|
1355
|
+
const w = x.$error, D = {
|
|
1356
|
+
message: `Cmd error: ${w.type} - ${w.message}`,
|
|
1357
|
+
details: w.details
|
|
1358
|
+
};
|
|
1359
|
+
return (Ee = e == null ? void 0 : e.outputSchemaMap) != null && Ee[y] ? {
|
|
1360
|
+
error: D.message,
|
|
1361
|
+
files: [],
|
|
1362
|
+
commandExecuted: null,
|
|
1363
|
+
stderrOutput: ((Pe = D.details) == null ? void 0 : Pe.stderr) || null
|
|
1364
|
+
} : {
|
|
1365
|
+
success: !1,
|
|
1366
|
+
message: D.message,
|
|
1367
|
+
data: D.details
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
let I = x.handlerResponse;
|
|
1371
|
+
if (I === void 0 && x.$commandChain) {
|
|
1372
|
+
let w = g, D = { ...x }, B;
|
|
1373
|
+
const se = x.$commandChain;
|
|
1374
|
+
for (let Z = 0; Z < se.length; Z++) {
|
|
1375
|
+
const te = se[Z], ae = w != null && w.getSubCommand ? w.getSubCommand(te) : void 0;
|
|
1376
|
+
if (ae && ae.parser)
|
|
1377
|
+
B = { ...D }, D = D[te] || {}, w = ae.parser;
|
|
1378
|
+
else if (Z === 0 && w && te === (w.getAppCommandName ? w.getAppCommandName() : w["#appCommandName"] || (w.getAppName ? w.getAppName() : w["#appName"]))) {
|
|
1379
|
+
D = { ...x };
|
|
1380
|
+
break;
|
|
1381
|
+
} else {
|
|
1382
|
+
w = void 0;
|
|
1383
|
+
break;
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
const ie = w, le = ie.getHandler ? ie.getHandler() : ie["#handler"];
|
|
1387
|
+
if (w && le) {
|
|
1388
|
+
const Z = le, te = {
|
|
1389
|
+
args: D,
|
|
1390
|
+
commandChain: se,
|
|
1391
|
+
parser: w,
|
|
1392
|
+
parentArgs: B
|
|
1393
|
+
};
|
|
1394
|
+
try {
|
|
1395
|
+
I = await Z(te);
|
|
1396
|
+
} catch (ae) {
|
|
1397
|
+
const _e = `Handler error: ${ae.message || String(ae)}`;
|
|
1398
|
+
return (Te = e == null ? void 0 : e.outputSchemaMap) != null && Te[y] ? {
|
|
1399
|
+
error: _e,
|
|
1400
|
+
files: [],
|
|
1401
|
+
commandExecuted: null,
|
|
1402
|
+
stderrOutput: null
|
|
1403
|
+
} : { success: !1, message: _e };
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
return (Oe = e == null ? void 0 : e.outputSchemaMap) != null && Oe[y] ? I : { success: !0, data: I };
|
|
1408
|
+
} catch (x) {
|
|
1409
|
+
let I, w = x;
|
|
1410
|
+
return x instanceof Error && x.message ? (I = `Cmd error: handler_error - ${x.message}`, w = { details: x }) : I = `MCP tool exec failed: ${x.message || String(x)}`, (ke = e == null ? void 0 : e.outputSchemaMap) != null && ke[y] ? {
|
|
1411
|
+
error: I,
|
|
1412
|
+
files: [],
|
|
1413
|
+
commandExecuted: null,
|
|
1414
|
+
stderrOutput: (w == null ? void 0 : w.stderr) || null
|
|
1415
|
+
} : {
|
|
1416
|
+
success: !1,
|
|
1417
|
+
message: I,
|
|
1418
|
+
data: w
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
};
|
|
1423
|
+
n.push(pe);
|
|
1424
|
+
}
|
|
1425
|
+
const j = h;
|
|
1426
|
+
if (j && (e == null ? void 0 : e.includeSubCommands) !== !1)
|
|
1427
|
+
for (const A of j) {
|
|
1428
|
+
if (A.isMcp === !0)
|
|
1429
|
+
continue;
|
|
1430
|
+
const F = [...t, A.name];
|
|
1431
|
+
r(
|
|
1432
|
+
A.parser,
|
|
1433
|
+
F.filter((N) => N)
|
|
1434
|
+
);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
return r(g, []), n;
|
|
1438
|
+
}
|
|
1439
|
+
var fe, xe;
|
|
1440
|
+
const ye = class ye extends he {
|
|
1441
|
+
constructor() {
|
|
1442
|
+
super(...arguments);
|
|
1443
|
+
_(this, fe);
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Generate MCP tools from this ArgParser instance
|
|
1447
|
+
* @param options Optional configuration for MCP tool generation
|
|
1448
|
+
* @returns Array of MCP tool structures ready for server registration
|
|
1449
|
+
*/
|
|
1450
|
+
toMcpTools(n) {
|
|
1451
|
+
return dt(this, n);
|
|
1452
|
+
}
|
|
1453
|
+
/**
|
|
1454
|
+
* Create an MCP server with tools generated from this ArgParser
|
|
1455
|
+
* @param serverInfo Server configuration
|
|
1456
|
+
* @param toolOptions Optional MCP tool generation options
|
|
1457
|
+
* @returns Configured MCP server instance
|
|
1458
|
+
*/
|
|
1459
|
+
createMcpServer(n, s) {
|
|
1460
|
+
const { McpServer: r } = require("@modelcontextprotocol/sdk/server/mcp.js"), a = new r({
|
|
1461
|
+
id: n.name,
|
|
1462
|
+
version: n.version,
|
|
1463
|
+
name: n.name,
|
|
1464
|
+
description: n.description
|
|
1465
|
+
});
|
|
1466
|
+
return this.toMcpTools(s).reduce((o, l) => (o.find((u) => u.name === l.name) || o.push(l), o), []).forEach((o) => {
|
|
1467
|
+
const l = {
|
|
1468
|
+
description: o.description || "No description provided.",
|
|
1469
|
+
inputSchema: o.inputSchema.shape || o.inputSchema
|
|
1470
|
+
};
|
|
1471
|
+
o.outputSchema && (l.outputSchema = o.outputSchema.shape || o.outputSchema), a.registerTool(o.name, l, o.execute);
|
|
1472
|
+
}), a;
|
|
1473
|
+
}
|
|
1474
|
+
/**
|
|
1475
|
+
* Start an MCP server using stdio transport
|
|
1476
|
+
* @param serverInfo Server configuration
|
|
1477
|
+
* @param toolOptions Optional MCP tool generation options
|
|
1478
|
+
* @returns Promise that resolves when server is connected
|
|
1479
|
+
*/
|
|
1480
|
+
async startMcpServer(n, s) {
|
|
1481
|
+
return this.startMcpServerWithTransport(n, "stdio", {}, s);
|
|
1482
|
+
}
|
|
1483
|
+
/**
|
|
1484
|
+
* Start an MCP server with multiple transport types simultaneously
|
|
1485
|
+
* @param serverInfo Server configuration
|
|
1486
|
+
* @param transports Array of transport configurations
|
|
1487
|
+
* @param toolOptions Optional MCP tool generation options
|
|
1488
|
+
* @returns Promise that resolves when all servers are started
|
|
1489
|
+
*/
|
|
1490
|
+
async startMcpServerWithMultipleTransports(n, s, r) {
|
|
1491
|
+
const a = this.createMcpServer(n, r), t = [];
|
|
1492
|
+
for (const i of s) {
|
|
1493
|
+
const o = $(this, fe, xe).call(this, a, n, i);
|
|
1494
|
+
t.push(o);
|
|
1495
|
+
}
|
|
1496
|
+
await Promise.all(t), console.error(`[${n.name}] All MCP transports started successfully`);
|
|
1497
|
+
}
|
|
1498
|
+
/**
|
|
1499
|
+
* Start an MCP server with a specific transport type
|
|
1500
|
+
* @param serverInfo Server configuration
|
|
1501
|
+
* @param transportType Type of transport to use
|
|
1502
|
+
* @param transportOptions Transport-specific options
|
|
1503
|
+
* @param toolOptions Optional MCP tool generation options
|
|
1504
|
+
* @returns Promise that resolves when server is connected
|
|
1505
|
+
*/
|
|
1506
|
+
async startMcpServerWithTransport(n, s, r = {}, a) {
|
|
1507
|
+
const t = this.createMcpServer(n, a);
|
|
1508
|
+
await $(this, fe, xe).call(this, t, n, {
|
|
1509
|
+
type: s,
|
|
1510
|
+
...r
|
|
1511
|
+
});
|
|
1512
|
+
}
|
|
1513
|
+
/**
|
|
1514
|
+
* Override parse() to handle async handlers properly
|
|
1515
|
+
* This allows ArgParser to work with async handlers while keeping
|
|
1516
|
+
* backwards compatibility for synchronous usage
|
|
1517
|
+
*/
|
|
1518
|
+
parse(n, s) {
|
|
1519
|
+
const r = super.parse(n, s), a = r;
|
|
1520
|
+
return a._fuzzyModePreventedExecution ? r : a._asyncHandlerPromise ? this.parseAsync(n, s) : r;
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* Async version of parse for when async handlers are detected
|
|
1524
|
+
*/
|
|
1525
|
+
async parseAsync(n, s) {
|
|
1526
|
+
const r = super.parse(n, s), a = r;
|
|
1527
|
+
if (a._fuzzyModePreventedExecution)
|
|
1528
|
+
return r;
|
|
1529
|
+
if (a._asyncHandlerPromise) {
|
|
1530
|
+
try {
|
|
1531
|
+
const t = await a._asyncHandlerPromise;
|
|
1532
|
+
a.handlerResponse = t;
|
|
1533
|
+
} catch (t) {
|
|
1534
|
+
if (this["#handleErrors"])
|
|
1535
|
+
a.$error = {
|
|
1536
|
+
type: "handler_error",
|
|
1537
|
+
message: t instanceof Error ? t.message : String(t),
|
|
1538
|
+
details: t
|
|
1539
|
+
};
|
|
1540
|
+
else
|
|
1541
|
+
throw t;
|
|
1542
|
+
}
|
|
1543
|
+
delete a._asyncHandlerPromise, delete a._asyncHandlerInfo;
|
|
1544
|
+
}
|
|
1545
|
+
return r;
|
|
1546
|
+
}
|
|
1547
|
+
addMcpSubCommand(n = "mcp-server", s, r) {
|
|
1548
|
+
let a;
|
|
1549
|
+
r && typeof r == "object" && ("includeSubCommands" in r || "toolNamePrefix" in r || "toolNameSuffix" in r) ? a = { toolOptions: r } : a = r || {};
|
|
1550
|
+
const { defaultTransports: t, defaultTransport: i, toolOptions: o } = a, l = async (d) => {
|
|
1551
|
+
d.parentParser || (console.error(
|
|
1552
|
+
"[MCP Server] Critical: MCP server handler called without a parent parser context."
|
|
1553
|
+
), process.exit(1)), console.error(`[${s.name}] Starting MCP Server...`);
|
|
1554
|
+
const f = d.parentParser, b = d.args.transports;
|
|
1555
|
+
if (b)
|
|
1556
|
+
try {
|
|
1557
|
+
const h = JSON.parse(b);
|
|
1558
|
+
await f.startMcpServerWithMultipleTransports(s, h, o);
|
|
1559
|
+
} catch (h) {
|
|
1560
|
+
console.error("❌ Error parsing transports configuration:", h.message), console.error(`Expected JSON format: '[{"type":"stdio"},{"type":"sse","port":3001}]'`), process.exit(1);
|
|
1561
|
+
}
|
|
1562
|
+
else if (t && t.length > 0)
|
|
1563
|
+
await f.startMcpServerWithMultipleTransports(s, t, o);
|
|
1564
|
+
else if (i)
|
|
1565
|
+
await f.startMcpServerWithTransport(
|
|
1566
|
+
s,
|
|
1567
|
+
i.type,
|
|
1568
|
+
{
|
|
1569
|
+
port: i.port,
|
|
1570
|
+
host: i.host,
|
|
1571
|
+
path: i.path,
|
|
1572
|
+
sessionIdGenerator: i.sessionIdGenerator
|
|
1573
|
+
},
|
|
1574
|
+
o
|
|
1575
|
+
);
|
|
1576
|
+
else {
|
|
1577
|
+
const h = d.args.transport || "stdio", v = {
|
|
1578
|
+
port: d.args.port,
|
|
1579
|
+
host: d.args.host,
|
|
1580
|
+
path: d.args.path
|
|
1581
|
+
};
|
|
1582
|
+
await f.startMcpServerWithTransport(s, h, v, o);
|
|
1583
|
+
}
|
|
1584
|
+
return new Promise(() => {
|
|
1585
|
+
});
|
|
1586
|
+
}, u = new he({}, [
|
|
1587
|
+
{
|
|
1588
|
+
name: "transport",
|
|
1589
|
+
description: "Transport type for MCP server (single transport mode)",
|
|
1590
|
+
options: ["--transport", "-t"],
|
|
1591
|
+
type: "string",
|
|
1592
|
+
enum: ["stdio", "sse", "streamable-http"],
|
|
1593
|
+
defaultValue: "stdio"
|
|
1594
|
+
},
|
|
1595
|
+
{
|
|
1596
|
+
name: "transports",
|
|
1597
|
+
description: "Multiple transports configuration as JSON array (overrides single transport)",
|
|
1598
|
+
options: ["--transports"],
|
|
1599
|
+
type: "string"
|
|
1600
|
+
},
|
|
1601
|
+
{
|
|
1602
|
+
name: "port",
|
|
1603
|
+
description: "Port number for HTTP-based transports (single transport mode)",
|
|
1604
|
+
options: ["--port", "-p"],
|
|
1605
|
+
type: "number",
|
|
1606
|
+
defaultValue: 3e3
|
|
1607
|
+
},
|
|
1608
|
+
{
|
|
1609
|
+
name: "host",
|
|
1610
|
+
description: "Host address for HTTP-based transports (single transport mode)",
|
|
1611
|
+
options: ["--host"],
|
|
1612
|
+
type: "string",
|
|
1613
|
+
defaultValue: "localhost"
|
|
1614
|
+
},
|
|
1615
|
+
{
|
|
1616
|
+
name: "path",
|
|
1617
|
+
description: "Path for HTTP-based transports (single transport mode)",
|
|
1618
|
+
options: ["--path"],
|
|
1619
|
+
type: "string",
|
|
1620
|
+
defaultValue: "/mcp"
|
|
1621
|
+
}
|
|
1622
|
+
]);
|
|
1623
|
+
return this.addSubCommand({
|
|
1624
|
+
name: n,
|
|
1625
|
+
description: `Start ${s.name} as an MCP server`,
|
|
1626
|
+
handler: l,
|
|
1627
|
+
parser: u,
|
|
1628
|
+
isMcp: !0
|
|
1629
|
+
}), this;
|
|
1630
|
+
}
|
|
1631
|
+
/**
|
|
1632
|
+
* Factory method to create an ArgParser instance with MCP capabilities
|
|
1633
|
+
* This provides a clean API for users who want MCP functionality from the start
|
|
1634
|
+
*/
|
|
1635
|
+
static withMcp(n, s) {
|
|
1636
|
+
return new ye(n, s);
|
|
1637
|
+
}
|
|
1638
|
+
/**
|
|
1639
|
+
* Convert an existing ArgParserBase instance to ArgParser with MCP
|
|
1640
|
+
* This allows upgrading existing parsers to support MCP
|
|
1641
|
+
*/
|
|
1642
|
+
static fromArgParser(n) {
|
|
1643
|
+
const s = n, r = new ye({
|
|
1644
|
+
appName: s.getAppName(),
|
|
1645
|
+
appCommandName: s.getAppCommandName(),
|
|
1646
|
+
description: s.getDescription(),
|
|
1647
|
+
handler: s.getHandler(),
|
|
1648
|
+
handleErrors: s["#handleErrors"],
|
|
1649
|
+
throwForDuplicateFlags: s["#throwForDuplicateFlags"]
|
|
1650
|
+
}), a = s.flags.filter(
|
|
1651
|
+
(i) => i.name !== "help"
|
|
1652
|
+
);
|
|
1653
|
+
a.length > 0 && r.addFlags(a);
|
|
1654
|
+
const t = r;
|
|
1655
|
+
return t["#subCommandName"] = s["#subCommandName"], t["#parameters"] = s["#parameters"], t["#parentParser"] = s["#parentParser"], t["#lastParseResult"] = s["#lastParseResult"], t["#inheritParentFlags"] = s["#inheritParentFlags"], t["#subCommands"] = s["#subCommands"], r;
|
|
1656
|
+
}
|
|
1657
|
+
};
|
|
1658
|
+
fe = new WeakSet(), xe = async function(n, s, r) {
|
|
1659
|
+
switch (r.type) {
|
|
1660
|
+
case "stdio": {
|
|
1661
|
+
const { StdioServerTransport: a } = await import("./stdio-Cf19UQO7.js"), t = new a();
|
|
1662
|
+
await n.connect(t), console.error(`[${s.name}] MCP Server started with stdio transport`);
|
|
1663
|
+
break;
|
|
1664
|
+
}
|
|
1665
|
+
case "sse": {
|
|
1666
|
+
const { SSEServerTransport: a } = await import("./sse-DSjLfGFo.js"), t = (await import("express")).default, i = t();
|
|
1667
|
+
i.use(t.json());
|
|
1668
|
+
const o = r.port || 3e3, l = r.path || "/sse";
|
|
1669
|
+
i.get(l, (u, d) => {
|
|
1670
|
+
const f = new a(l, d);
|
|
1671
|
+
n.connect(f);
|
|
1672
|
+
}), await new Promise((u) => {
|
|
1673
|
+
i.listen(o, r.host || "localhost", () => {
|
|
1674
|
+
console.error(`[${s.name}] MCP Server listening on http://${r.host || "localhost"}:${o}${l} (SSE)`), u();
|
|
1675
|
+
});
|
|
1676
|
+
});
|
|
1677
|
+
break;
|
|
1678
|
+
}
|
|
1679
|
+
case "streamable-http": {
|
|
1680
|
+
const { StreamableHTTPServerTransport: a } = await import("./streamableHttp-DXIdDSbF.js"), t = (await import("express")).default, i = t();
|
|
1681
|
+
i.use(t.json());
|
|
1682
|
+
const o = r.port || 3e3, l = r.path || "/mcp", u = {};
|
|
1683
|
+
i.all(l, async (d, f) => {
|
|
1684
|
+
const b = d.headers["mcp-session-id"];
|
|
1685
|
+
let h;
|
|
1686
|
+
b && u[b] ? h = u[b] : (h = new a({
|
|
1687
|
+
sessionIdGenerator: r.sessionIdGenerator || (() => Math.random().toString(36).substring(7)),
|
|
1688
|
+
onsessioninitialized: (v) => {
|
|
1689
|
+
u[v] = h;
|
|
1690
|
+
}
|
|
1691
|
+
}), h.onclose = () => {
|
|
1692
|
+
h.sessionId && delete u[h.sessionId];
|
|
1693
|
+
}, await n.connect(h)), await h.handleRequest(d, f, d.body);
|
|
1694
|
+
}), await new Promise((d) => {
|
|
1695
|
+
i.listen(o, r.host || "localhost", () => {
|
|
1696
|
+
console.error(`[${s.name}] MCP Server listening on http://${r.host || "localhost"}:${o}${l} (HTTP)`), d();
|
|
1697
|
+
});
|
|
1698
|
+
});
|
|
1699
|
+
break;
|
|
1700
|
+
}
|
|
1701
|
+
default:
|
|
1702
|
+
throw new Error(`Unsupported transport type: ${r.type}`);
|
|
1703
|
+
}
|
|
813
1704
|
};
|
|
814
|
-
let
|
|
1705
|
+
let Ie = ye;
|
|
1706
|
+
class gt {
|
|
1707
|
+
constructor(e, n = {}) {
|
|
1708
|
+
this.parser = e, this.options = {
|
|
1709
|
+
maxDepth: n.maxDepth ?? 5,
|
|
1710
|
+
randomTestCases: n.randomTestCases ?? 10,
|
|
1711
|
+
includePerformance: n.includePerformance ?? !0,
|
|
1712
|
+
testErrorCases: n.testErrorCases ?? !0,
|
|
1713
|
+
verbose: n.verbose ?? !1
|
|
1714
|
+
};
|
|
1715
|
+
}
|
|
1716
|
+
/**
|
|
1717
|
+
* Run comprehensive fuzzy testing on the ArgParser instance
|
|
1718
|
+
*/
|
|
1719
|
+
async runFuzzyTest() {
|
|
1720
|
+
const e = this.discoverCommandPaths(), n = [];
|
|
1721
|
+
this.options.verbose && (console.log(`Discovered ${e.length} command paths:`), e.forEach((s) => console.log(` ${s.join(" ") || "(root)"}`)));
|
|
1722
|
+
for (const s of e) {
|
|
1723
|
+
const r = await this.testCommandPath(s);
|
|
1724
|
+
n.push(...r);
|
|
1725
|
+
}
|
|
1726
|
+
return this.generateReport(e, n);
|
|
1727
|
+
}
|
|
1728
|
+
/**
|
|
1729
|
+
* Discover all possible command paths in the parser
|
|
1730
|
+
*/
|
|
1731
|
+
discoverCommandPaths() {
|
|
1732
|
+
const e = [];
|
|
1733
|
+
return e.push([]), this.discoverSubCommandPaths(this.parser, [], e, 0), e;
|
|
1734
|
+
}
|
|
1735
|
+
/**
|
|
1736
|
+
* Recursively discover subcommand paths
|
|
1737
|
+
*/
|
|
1738
|
+
discoverSubCommandPaths(e, n, s, r) {
|
|
1739
|
+
if (r >= this.options.maxDepth) return;
|
|
1740
|
+
const a = this.getSubCommands(e);
|
|
1741
|
+
for (const [t, i] of a) {
|
|
1742
|
+
const o = [...n, t];
|
|
1743
|
+
s.push(o), this.discoverSubCommandPaths(i.parser, o, s, r + 1);
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
/**
|
|
1747
|
+
* Get subcommands from a parser instance
|
|
1748
|
+
*/
|
|
1749
|
+
getSubCommands(e) {
|
|
1750
|
+
return e.getSubCommands();
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Get flags from a parser instance
|
|
1754
|
+
*/
|
|
1755
|
+
getFlags(e) {
|
|
1756
|
+
return e.flags;
|
|
1757
|
+
}
|
|
1758
|
+
/**
|
|
1759
|
+
* Test a specific command path with various flag combinations
|
|
1760
|
+
*/
|
|
1761
|
+
async testCommandPath(e) {
|
|
1762
|
+
const n = [], s = this.getParserForPath(e), r = this.getFlags(s);
|
|
1763
|
+
this.options.verbose && console.log(`Testing command path: ${e.join(" ") || "(root)"}`);
|
|
1764
|
+
const a = this.generateValidFlagCombinations(r);
|
|
1765
|
+
for (const t of a) {
|
|
1766
|
+
const i = [...e, ...t], o = await this.executeTest(i, e);
|
|
1767
|
+
n.push(o);
|
|
1768
|
+
}
|
|
1769
|
+
for (let t = 0; t < this.options.randomTestCases; t++) {
|
|
1770
|
+
const i = this.generateRandomFlagCombination(r), o = [...e, ...i], l = await this.executeTest(o, e);
|
|
1771
|
+
n.push(l);
|
|
1772
|
+
}
|
|
1773
|
+
if (this.options.testErrorCases) {
|
|
1774
|
+
const t = this.generateErrorCases(r);
|
|
1775
|
+
for (const i of t) {
|
|
1776
|
+
const o = [...e, ...i], l = await this.executeTest(o, e, !0);
|
|
1777
|
+
n.push(l);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
return n;
|
|
1781
|
+
}
|
|
1782
|
+
/**
|
|
1783
|
+
* Get the parser instance for a specific command path
|
|
1784
|
+
*/
|
|
1785
|
+
getParserForPath(e) {
|
|
1786
|
+
let n = this.parser;
|
|
1787
|
+
for (const s of e) {
|
|
1788
|
+
const a = this.getSubCommands(n).get(s);
|
|
1789
|
+
if (!a)
|
|
1790
|
+
throw new Error(`Command path not found: ${e.join(" ")}`);
|
|
1791
|
+
n = a.parser;
|
|
1792
|
+
}
|
|
1793
|
+
return n;
|
|
1794
|
+
}
|
|
1795
|
+
/**
|
|
1796
|
+
* Generate valid flag combinations for testing
|
|
1797
|
+
*/
|
|
1798
|
+
generateValidFlagCombinations(e) {
|
|
1799
|
+
const n = [], s = e.filter(
|
|
1800
|
+
(t) => t.name !== "help" && (typeof t.mandatory == "boolean" ? t.mandatory : !1)
|
|
1801
|
+
), r = e.filter(
|
|
1802
|
+
(t) => t.name !== "help" && (typeof t.mandatory == "boolean" ? !t.mandatory : !0)
|
|
1803
|
+
), a = [];
|
|
1804
|
+
for (const t of s) {
|
|
1805
|
+
const i = this.generateFlagArgs(t, "valid");
|
|
1806
|
+
a.push(...i);
|
|
1807
|
+
}
|
|
1808
|
+
a.length > 0 ? n.push([...a]) : n.push([]);
|
|
1809
|
+
for (const t of r) {
|
|
1810
|
+
const i = this.generateFlagArgs(t, "valid");
|
|
1811
|
+
i.length > 0 && n.push([...a, ...i]);
|
|
1812
|
+
}
|
|
1813
|
+
if (r.length > 1)
|
|
1814
|
+
for (let t = 0; t < r.length - 1; t++)
|
|
1815
|
+
for (let i = t + 1; i < r.length; i++) {
|
|
1816
|
+
const o = this.generateFlagArgs(r[t], "valid"), l = this.generateFlagArgs(r[i], "valid");
|
|
1817
|
+
o.length > 0 && l.length > 0 && n.push([...a, ...o, ...l]);
|
|
1818
|
+
}
|
|
1819
|
+
return n;
|
|
1820
|
+
}
|
|
1821
|
+
/**
|
|
1822
|
+
* Generate random flag combination
|
|
1823
|
+
*/
|
|
1824
|
+
generateRandomFlagCombination(e) {
|
|
1825
|
+
const n = [], s = e.filter((r) => r.name !== "help");
|
|
1826
|
+
for (const r of s)
|
|
1827
|
+
if (Math.random() < 0.3) {
|
|
1828
|
+
const a = this.generateFlagArgs(r, "random");
|
|
1829
|
+
n.push(...a);
|
|
1830
|
+
}
|
|
1831
|
+
return n;
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Generate error test cases
|
|
1835
|
+
*/
|
|
1836
|
+
generateErrorCases(e) {
|
|
1837
|
+
const n = [];
|
|
1838
|
+
n.push(["--invalid-flag"]), n.push(["--nonexistent", "value"]);
|
|
1839
|
+
for (const s of e) {
|
|
1840
|
+
if (s.name === "help") continue;
|
|
1841
|
+
const r = this.generateFlagArgs(s, "invalid");
|
|
1842
|
+
r.length > 0 && n.push(r);
|
|
1843
|
+
}
|
|
1844
|
+
return n;
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* Generate arguments for a specific flag
|
|
1848
|
+
*/
|
|
1849
|
+
generateFlagArgs(e, n) {
|
|
1850
|
+
const s = e.options[0];
|
|
1851
|
+
if (!s) return [];
|
|
1852
|
+
if (e.flagOnly)
|
|
1853
|
+
return [s];
|
|
1854
|
+
const r = this.generateFlagValues(e, n), a = [];
|
|
1855
|
+
for (const t of r)
|
|
1856
|
+
e.allowLigature && Math.random() < 0.5 ? a.push(`${s}=${t}`) : a.push(s, t);
|
|
1857
|
+
return a;
|
|
1858
|
+
}
|
|
1859
|
+
/**
|
|
1860
|
+
* Generate values for a flag based on its type and constraints
|
|
1861
|
+
*/
|
|
1862
|
+
generateFlagValues(e, n) {
|
|
1863
|
+
const s = e.allowMultiple && n !== "invalid" ? Math.floor(Math.random() * 3) + 1 : 1, r = [];
|
|
1864
|
+
for (let a = 0; a < s; a++)
|
|
1865
|
+
r.push(this.generateSingleFlagValue(e, n));
|
|
1866
|
+
return r;
|
|
1867
|
+
}
|
|
1868
|
+
/**
|
|
1869
|
+
* Generate a single value for a flag
|
|
1870
|
+
*/
|
|
1871
|
+
generateSingleFlagValue(e, n) {
|
|
1872
|
+
if (n === "invalid")
|
|
1873
|
+
return e.type === Number ? "not-a-number" : e.enum && e.enum.length > 0 ? "invalid-enum-value" : e.type === Boolean ? "not-boolean" : "invalid-value";
|
|
1874
|
+
if (e.enum && e.enum.length > 0) {
|
|
1875
|
+
const r = Math.floor(Math.random() * e.enum.length);
|
|
1876
|
+
return String(e.enum[r]);
|
|
1877
|
+
}
|
|
1878
|
+
if (e.type === Number)
|
|
1879
|
+
return String(Math.floor(Math.random() * 1e3));
|
|
1880
|
+
if (e.type === Boolean)
|
|
1881
|
+
return Math.random() < 0.5 ? "true" : "false";
|
|
1882
|
+
const s = [
|
|
1883
|
+
"test-value",
|
|
1884
|
+
"hello-world",
|
|
1885
|
+
"file.txt",
|
|
1886
|
+
"/path/to/file",
|
|
1887
|
+
"user@example.com",
|
|
1888
|
+
"123",
|
|
1889
|
+
"special-chars-!@#"
|
|
1890
|
+
];
|
|
1891
|
+
return s[Math.floor(Math.random() * s.length)];
|
|
1892
|
+
}
|
|
1893
|
+
/**
|
|
1894
|
+
* Execute a single test case
|
|
1895
|
+
*/
|
|
1896
|
+
async executeTest(e, n, s = !1) {
|
|
1897
|
+
const r = this.options.includePerformance ? Date.now() : 0;
|
|
1898
|
+
try {
|
|
1899
|
+
const a = [...e], t = this.parser.parse(e, {
|
|
1900
|
+
skipHelpHandling: !0
|
|
1901
|
+
});
|
|
1902
|
+
t && typeof t == "object" && (t._originalInputArgs = a);
|
|
1903
|
+
const i = this.options.includePerformance ? Date.now() - r : void 0;
|
|
1904
|
+
return {
|
|
1905
|
+
commandPath: n,
|
|
1906
|
+
args: e,
|
|
1907
|
+
success: !s,
|
|
1908
|
+
parsedResult: t,
|
|
1909
|
+
executionTime: i
|
|
1910
|
+
};
|
|
1911
|
+
} catch (a) {
|
|
1912
|
+
const t = this.options.includePerformance ? Date.now() - r : void 0;
|
|
1913
|
+
return {
|
|
1914
|
+
commandPath: n,
|
|
1915
|
+
args: e,
|
|
1916
|
+
success: s,
|
|
1917
|
+
error: a instanceof Error ? a.message : String(a),
|
|
1918
|
+
executionTime: t
|
|
1919
|
+
};
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
/**
|
|
1923
|
+
* Generate comprehensive test report
|
|
1924
|
+
*/
|
|
1925
|
+
generateReport(e, n) {
|
|
1926
|
+
const s = n.length, r = n.filter((o) => o.success).length, a = s - r, t = {};
|
|
1927
|
+
for (const o of e) {
|
|
1928
|
+
const l = o.join(" ") || "(root)", u = n.filter(
|
|
1929
|
+
(d) => JSON.stringify(d.commandPath) === JSON.stringify(o)
|
|
1930
|
+
);
|
|
1931
|
+
t[l] = {
|
|
1932
|
+
total: u.length,
|
|
1933
|
+
passed: u.filter((d) => d.success).length
|
|
1934
|
+
};
|
|
1935
|
+
}
|
|
1936
|
+
const i = {};
|
|
1937
|
+
for (const o of n)
|
|
1938
|
+
if (o.error) {
|
|
1939
|
+
const l = o.error.split(":")[0] || "Unknown";
|
|
1940
|
+
i[l] = (i[l] || 0) + 1;
|
|
1941
|
+
}
|
|
1942
|
+
return {
|
|
1943
|
+
totalTests: s,
|
|
1944
|
+
successfulTests: r,
|
|
1945
|
+
failedTests: a,
|
|
1946
|
+
commandPaths: e,
|
|
1947
|
+
results: n,
|
|
1948
|
+
summary: {
|
|
1949
|
+
coverageByPath: t,
|
|
1950
|
+
errorTypes: i
|
|
1951
|
+
}
|
|
1952
|
+
};
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
815
1955
|
export {
|
|
816
|
-
|
|
817
|
-
|
|
1956
|
+
Ie as ArgParser,
|
|
1957
|
+
he as ArgParserBase,
|
|
1958
|
+
Y as ArgParserError,
|
|
1959
|
+
gt as ArgParserFuzzyTester,
|
|
1960
|
+
dt as generateMcpToolsFromArgParser,
|
|
1961
|
+
ct as zodFlagSchema
|
|
818
1962
|
};
|
|
819
1963
|
//# sourceMappingURL=index.min.mjs.map
|