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