-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli.ts
162 lines (134 loc) · 4.5 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
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
154
155
156
157
158
159
160
161
162
import {resolve} from 'path'
export type IValidation<T> = (value: string) => T
export interface IOption<T> {
shortCmd: string;
longCmd: string;
validator: IValidation<T>;
description?: string;
defaultValue?: any;
required?: boolean;
}
export interface IFlag {
shortCmd: string;
longCmd: string;
description?: string;
enabled?: boolean;
}
export interface SortedInput {
shortCommands: Array<Array<string>>;
longCommands: Array<Array<string>>
}
export const extendString = (str: string, maxLength: number) => {
for (let i = str.length; i <= maxLength; i++) {
str += ' ';
}
return str;
}
export const sortInputs = (inputs: Array<string>): SortedInput => {
const commands = [];
let command = [];
for (const input of inputs) {
if (input.startsWith('-')) {
commands.push(command);
command = [];
}
command.push(input);
}
commands.push(command);
const filteredCommands = commands.filter(c => c.length > 0);
const shortCommands = [];
const longCommands = [];
filteredCommands.forEach(v => {
if (v[0].startsWith('--')) {
longCommands.push(v);
} else {
shortCommands.push(v);
}
});
return {
longCommands,
shortCommands
}
}
export class CLI {
options: { [optionCmd: string]: IOption<any> } = {};
flags: { [flagCmd: string]: IFlag } = {};
_version: string = '';
_description: string = '';
constructor(private name: string) {
this.flag({shortCmd: 'h', longCmd: 'help', description: 'display help for command'})
}
version(version: string) {
this._version = version;
this.flag({shortCmd: 'v', longCmd: 'version', description: 'output the current version'})
return this;
}
description(description: string) {
this._description = description;
return this;
}
flag(flag: IFlag): CLI {
this.flags[flag.longCmd] = flag;
return this;
}
option<T>(option: IOption<T>): CLI {
this.options[option.longCmd] = option;
return this;
}
printHelp() {
console.log('Usage:', this.name, this._version, '\n')
if (!!this._description) {
console.log(this._description, '\n')
}
console.log('Options:')
const flags = Object.keys(this.flags).map(optionOrFlagKey => {
const option = this.flags[optionOrFlagKey];
return {
commands: ` -${option.shortCmd}, --${option.longCmd} `,
description: `${option.description} (default: ${typeof option.enabled === 'undefined' ? 'disabled' : 'enabled'})`
}
})
const options = Object.keys(this.options).map(optionOrFlagKey => {
const option = this.options[optionOrFlagKey];
return {
commands: ` -${option.shortCmd}, --${option.longCmd} `,
description: `${option.description} ${typeof option.defaultValue === 'undefined' ? '' : `(default: ${option.defaultValue})`}`
}
})
const commands = [...flags, ...options];
const length = commands.map(c => c.commands.length).reduce((previousValue, currentValue) => {
return previousValue > currentValue
? previousValue : currentValue
})
commands.forEach(({commands, description}) =>
console.log(`${extendString(commands, length)}${description}`)
)
}
action() {
if (process.argv.length <= 2) {
this.printHelp();
} else {
//parse input
const inputs = process.argv.slice(2);
const sortedInputs = sortInputs(inputs);
console.log('sorted inputs', sortedInputs);
const selectedFlags = Object.values(this.flags).filter((flag) =>
sortedInputs.shortCommands.map(v => v[0]).indexOf('-'+flag.shortCmd)> -1
|| sortedInputs.longCommands.map(v => v[0]).indexOf('-'+flag.longCmd)> -1
, [])
console.log(selectedFlags);
}
}
}
(() => {
const cli = new CLI('st-open-api')
.version('2.3.4') // TODO: use version from package.json
.description('the test project description')
.option<string>({
shortCmd: 't',
longCmd: 'test',
description: 'thats a test description',
validator: (value: string) => resolve(process.cwd(), value),
});
cli.action();
})()