-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
153 lines (134 loc) · 3.56 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import readline from "readline";
interface Is {
[key: string]: string | boolean
}
interface Ic {
[key: string]: string | boolean
}
type StringNumberTuple = [string, number]
interface Icli {
s: Is,
c: Ic,
o: StringNumberTuple[],
p: string | false,
e: number[],
noArgs: boolean,
argc: number
}
const CLI: Icli = {
s: {}, // single
c: {}, // couple
o: [], // other
p: false, // pipped
e: [], // end
noArgs: false,
argc: process.argv.length - 2
};
const getPippedInput = async(): Promise<string> => {
return new Promise( (resolve, reject) => {
if (!process.stdin.isTTY) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
let lines = "";
let linesCounter = 0;
rl.on("line", line => {
lines += line + "\n";
++linesCounter;
});
rl.on("close", () => {
if (linesCounter > 1) {
if (lines[lines.length-1] === "\n") {
lines = lines.substring(0, lines.length-1);
}
} else if (linesCounter === 1) {
if (lines[lines.length-1] === "\n") {
lines = lines.substring(0, lines.length-1);
}
}
resolve(lines);
});
} else {
reject("Not pipped input");
}
})
}
const numberOfArgs = process.argv.length - 2;
const parseCLI = async () => {
try {
CLI.p = await getPippedInput();
} catch(err) {
CLI.p = false;
}
let previousIsArg = false;
process.argv.splice(2).forEach( (current, index, arr) => {
if (previousIsArg) {
previousIsArg = false;
return;
}
const next = arr[index + 1];
const [fc, sc, tc] = current.split("").splice(0, 3);
switch(fc) {
case "-":
switch(sc) {
case "-":
if (tc) {
const nameOfArg = current.substring(2, current.length);
//if (/^[a-zA-Z]+$/g.test(nameOfArg)) { // allow guions between words
if (/^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$/g.test(nameOfArg)) {
if (next && next[0] === "-" || next?.substring(0, 2) === "--") {
// next arg is an argument too
CLI.c[nameOfArg] = true;
return
}
CLI.c[nameOfArg] = next ? next : true;
previousIsArg = true;
return;
} else { // not valid --a to Z word
CLI.o.push([current, index + 1]);
}
} else {
CLI.e.push(index + 1);
// separator found. Ex: cat hello.txt -- echo "abc"
}
break;
default: // parse -v -h ...
if (sc) {
const nameOfArg = current.substring(1, current.length);
if (/^[a-zA-Z]+$/g.test(nameOfArg)) {
if (next && next[0] === "-" || next?.substring(0, 2) === "--") {
// next arg is an argument too
CLI.s[nameOfArg] = true;
return;
}
// parse multiple args like -lvk
if (nameOfArg.length > 1 && /^[a-zA-Z]+$/g.test(nameOfArg)) {
nameOfArg.split("").forEach( arg => CLI.s[arg] = true );
return;
}
CLI.s[nameOfArg] = next ? next : true;
previousIsArg = true;
return
} else {
//console.log(`WHAT IS THIS??? (${current})`);
}
} else {
//console.log(`WHAT IS THIS2??? (${current})`);
// is not -word && not --word
}
}
break;
default:
CLI.o.push([current, index + 1]);
return;
}
});
if (!CLI.p && !numberOfArgs) {
CLI.noArgs = true
}
return Object.freeze(CLI);
}
module.exports = parseCLI; // node require export
export default parseCLI; // typescript export