forked from org-formation/org-formation-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
47 lines (43 loc) · 1.24 KB
/
cli.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
#!/usr/bin/env node
import { CliProgram } from './cli-program';
const joinParameterArguments = (argv: string[]): string[] => {
const result = [];
let passedFirstArg = false;
let currentOption = '';
for (const arg of argv) {
const isOption = arg.startsWith('--');
if (!passedFirstArg && !isOption) {
result.push(arg);
continue;
}
if (!passedFirstArg && isOption) {
result.push(arg);
passedFirstArg = true;
continue;
}
if (passedFirstArg && !isOption) {
currentOption += arg + ' ';
continue;
}
if (passedFirstArg && isOption) {
if (currentOption !== '') {
result.push(currentOption.trimRight());
currentOption = '';
}
result.push(arg);
continue;
}
}
if (currentOption !== '') {
result.push(currentOption.trimRight());
}
return result;
}
const program = CliProgram.Create();
const args = joinParameterArguments(process.argv);
program.parse(args);
if (process.argv.lastIndexOf('help') === process.argv.length - 1) {
program.help();
} else if (process.argv.length < 3) {
program.help();
}