@snowtop/ent 0.2.8 → 0.2.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -0,0 +1,3 @@
1
+ export function parseArgs(argv: any): {
2
+ _: never[];
3
+ };
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ function coerceValue(value) {
3
+ if (value === "true") {
4
+ return true;
5
+ }
6
+ if (value === "false") {
7
+ return false;
8
+ }
9
+ return value;
10
+ }
11
+ function normalizeKey(key) {
12
+ return key.replace(/-/g, "_");
13
+ }
14
+ function setOption(result, key, value) {
15
+ result[key] = value;
16
+ const normalized = normalizeKey(key);
17
+ if (normalized !== key) {
18
+ result[normalized] = value;
19
+ }
20
+ }
21
+ function parseArgs(argv) {
22
+ const result = { _: [] };
23
+ const args = argv || process.argv.slice(2);
24
+ for (let i = 0; i < args.length; i++) {
25
+ const arg = args[i];
26
+ if (arg === "--") {
27
+ result._.push(...args.slice(i + 1));
28
+ break;
29
+ }
30
+ if (!arg.startsWith("-") || arg === "-") {
31
+ result._.push(arg);
32
+ continue;
33
+ }
34
+ if (arg.startsWith("--no-")) {
35
+ setOption(result, normalizeKey(arg.slice(5)), false);
36
+ continue;
37
+ }
38
+ if (arg.startsWith("--")) {
39
+ const body = arg.slice(2);
40
+ const idx = body.indexOf("=");
41
+ if (idx !== -1) {
42
+ setOption(result, normalizeKey(body.slice(0, idx)), coerceValue(body.slice(idx + 1)));
43
+ continue;
44
+ }
45
+ const key = normalizeKey(body);
46
+ const next = args[i + 1];
47
+ if (next !== undefined && !next.startsWith("-")) {
48
+ setOption(result, key, coerceValue(next));
49
+ i++;
50
+ }
51
+ else {
52
+ setOption(result, key, true);
53
+ }
54
+ continue;
55
+ }
56
+ const shorts = arg.slice(1);
57
+ if (shorts.length === 1) {
58
+ const next = args[i + 1];
59
+ if (next !== undefined && !next.startsWith("-")) {
60
+ setOption(result, shorts, coerceValue(next));
61
+ i++;
62
+ }
63
+ else {
64
+ setOption(result, shorts, true);
65
+ }
66
+ continue;
67
+ }
68
+ for (const ch of shorts) {
69
+ setOption(result, ch, true);
70
+ }
71
+ }
72
+ return result;
73
+ }
74
+ module.exports = { parseArgs };