1 |
|
2 | var pushOrSet = (obj, key, value) => {
|
3 | if (Array.isArray(obj[key])) {
|
4 | obj[key].push(value);
|
5 | } else {
|
6 | obj[key] = value;
|
7 | }
|
8 | };
|
9 | var parseFlag = (parsed, args, currentIndex, opt, flag) => {
|
10 | if (opt.type === Boolean) {
|
11 | pushOrSet(parsed, opt.name, true);
|
12 | } else {
|
13 | const nextValue = args[++currentIndex];
|
14 | if (nextValue === void 0) {
|
15 | if (opt.optionalValue) {
|
16 | pushOrSet(parsed, opt.name, true);
|
17 | } else {
|
18 | throw new Error(`missing value for ${flag}`);
|
19 | }
|
20 | } else {
|
21 | pushOrSet(parsed, opt.name, opt.type(nextValue));
|
22 | }
|
23 | }
|
24 | return currentIndex;
|
25 | };
|
26 | var parsePositional = (parsed, args, currentIndex, opt) => {
|
27 | if (!opt.multiple) {
|
28 | parsed[opt.name] = opt.type(args[currentIndex]);
|
29 | return currentIndex;
|
30 | }
|
31 | const values = [opt.type(args[currentIndex])];
|
32 | for (let i = currentIndex + 1; i < args.length; i++) {
|
33 | const value = args[i];
|
34 | if (value && value[0] === "-" && opt.multiple !== "include-flags") {
|
35 | break;
|
36 | } else if (value) {
|
37 | currentIndex += 1;
|
38 | values.push(opt.type(value));
|
39 | }
|
40 | }
|
41 | parsed[opt.name] = values;
|
42 | return currentIndex;
|
43 | };
|
44 | var splitShortFlags = (arg) => {
|
45 | if (/^-[a-zA-Z]/.test(arg)) {
|
46 | return arg.slice(1).split("").map((flag) => `-${flag}`);
|
47 | }
|
48 | return [arg];
|
49 | };
|
50 | var parse = (args, options) => {
|
51 | const parsed = { _: [] };
|
52 | let stopped = false;
|
53 | const skippedPositionalArgs = new Set();
|
54 | args = args.reduce((res, arg) => {
|
55 | if (arg[0] === "-") {
|
56 | let equalSignIndex = arg.indexOf("=");
|
57 | if (equalSignIndex > 0) {
|
58 | res.push(...splitShortFlags(arg.slice(0, equalSignIndex)), arg.slice(equalSignIndex + 1));
|
59 | } else {
|
60 | res.push(...splitShortFlags(arg));
|
61 | }
|
62 | } else {
|
63 | res.push(arg);
|
64 | }
|
65 | return res;
|
66 | }, []);
|
67 | for (let i = 0; i < args.length; i++) {
|
68 | const flag = args[i];
|
69 | const flagName = flag.replace(/^-{1,2}/, "");
|
70 | if (stopped) {
|
71 | parsed._.push(flag);
|
72 | continue;
|
73 | }
|
74 | if (flag.startsWith("-")) {
|
75 | const opt = options.find((o) => {
|
76 | var _a;
|
77 | return !o.positional && (o.name === flagName || ((_a = o.flags) == null ? void 0 : _a.includes(flagName))) && (!o.when || o.when(parsed));
|
78 | });
|
79 | if (opt) {
|
80 | if (opt.multiple) {
|
81 | parsed[opt.name] = parsed[opt.name] || [];
|
82 | }
|
83 | i = parseFlag(parsed, args, i, opt, flag);
|
84 | if (opt.stop) {
|
85 | stopped = true;
|
86 | }
|
87 | } else {
|
88 | throw new Error(`unknown flag: ${flag}`);
|
89 | }
|
90 | } else {
|
91 | const opt = options.find((o) => {
|
92 | return o.positional && parsed[o.name] === void 0 && (!o.when || o.when(parsed) || !skippedPositionalArgs.add(o.name));
|
93 | });
|
94 | if (opt) {
|
95 | i = parsePositional(parsed, args, i, opt);
|
96 | if (opt.stop) {
|
97 | stopped = true;
|
98 | }
|
99 | } else {
|
100 | throw new Error(`unknown positional argument: ${flag}`);
|
101 | }
|
102 | }
|
103 | }
|
104 | for (const opt of options) {
|
105 | if (opt.positional && !opt.optionalValue && parsed[opt.name] === void 0 && !skippedPositionalArgs.has(opt.name)) {
|
106 | if (opt.when && !opt.when(parsed)) {
|
107 | continue;
|
108 | }
|
109 | throw new Error(`missing positional argument: ${opt.name}`);
|
110 | }
|
111 | }
|
112 | return parsed;
|
113 | };
|
114 | export {
|
115 | parse
|
116 | };
|