-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments.ts
34 lines (29 loc) · 934 Bytes
/
arguments.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
import { redBright } from "chalk";
import { cwd } from "process";
export interface ArgumentParserResult {
command: string;
flags: Array<string>;
}
export class ArgumentParser {
private readonly arguments: Array<string>;
constructor(args: Array<string>) {
this.arguments = args;
}
public createParserResults(): ArgumentParserResult {
let command: string = cwd();
let parameters: Array<string> = new Array<string>();
for (let index = 0; index < this.arguments.length; index++) {
if (index == 0) {
command = this.arguments[index];
continue;
}
const isValidParameter = this.arguments[index].startsWith("--");
if (!isValidParameter) {
console.log(redBright(`${this.arguments[index]}is not a valid flag`));
process.exit(1);
}
parameters.push(this.arguments[index].slice(2));
}
return { command: command, flags: parameters };
}
}