-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams.ts
More file actions
199 lines (167 loc) Β· 4.41 KB
/
params.ts
File metadata and controls
199 lines (167 loc) Β· 4.41 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { ParamError } from './exceptions';
import { nil, Value } from './value';
export abstract class Param {
public aliases: string[];
constructor(
public name: string,
aliases?: string[],
) {
this.aliases = aliases ? aliases : [];
}
}
export class Arg extends Param {
constructor(name: string) {
super(name);
}
}
export class OptionalArg extends Arg {}
export class Argv extends Arg {}
export class Flag extends Param {}
export class Opt extends Param {}
export type ParamSpec = Param[];
export type Parameters = Record<string, Value>;
// export type Value = number | boolean | string | BaseException | Nil;
export class Params {
args: Arg[];
argv: string[];
opts: Record<string, Opt>;
aliases: Record<string, string>;
constructor(spec: ParamSpec) {
this.args = [];
this.argv = [];
this.opts = {};
this.aliases = {};
for (const param of spec) {
if (param instanceof Argv) {
this.argv.push(param.name);
} else if (param instanceof Arg) {
this.args.push(param);
} else {
this.opts[param.name] = param;
}
for (const alias of param.aliases) {
this.aliases[alias] = param.name;
}
}
}
parse(params: Value[]): Record<string, any> {
const argv: Value[] = [];
// Result can be either a value or, in the case of argv, an array of
// values. Rather than forcing the user to check the type, we assume they
// are adults - lol
const parsed: Record<string, any> = {};
for (const name of this.argv) {
parsed[name] = [];
}
for (const arg of this.args) {
parsed[arg.name] = nil;
}
for (const name of Object.keys(this.opts)) {
parsed[name] = nil;
}
let i = 0;
const advance = (n: number = 1) => {
i += n;
};
const getOpt = (name: string): Param | null => {
name = this.aliases[name] ? this.aliases[name] : name;
return this.opts[name] || null;
};
const getValue = (n: number = 1): Value => {
return params[i + n];
};
const setOpt = (name: string, flagValue: boolean = true): void => {
const p = getOpt(name);
if (p) {
if (p instanceof Flag) {
parsed[p.name] = flagValue;
advance();
return;
}
parsed[p.name] = getValue();
advance(2);
return;
}
throw new ParamError(`Unknown flag or option --${name}`);
};
const setNoOpt = (name: string): void => {
try {
setOpt(name, true);
} catch (err) {
if (!(err instanceof ParamError)) {
throw err;
}
setOpt(name.slice(3), false);
}
};
const setShortOpts = (opts: string) => {
let n = 1;
for (let i = 0; i < opts.length; i++) {
const name = opts[i];
const p = getOpt(name);
if (p) {
if (p instanceof Flag) {
parsed[p.name] = true;
continue;
}
if (i !== opts.length - 1) {
throw new ParamError(
`Option flag -${name} must be followed by a value`,
);
}
parsed[p.name] = getValue();
n = 2;
}
}
advance(n);
};
const collectArg = () => {
argv.push(getValue(0));
advance();
};
while (i < params.length) {
const param = params[i];
if (typeof param === 'string') {
if (param.match(/^--no-/)) {
setNoOpt(param.slice(2));
continue;
}
if (param.match(/^--/)) {
setOpt(param.slice(2), true);
continue;
}
if (param.match(/^-/)) {
setShortOpts(param.slice(1));
continue;
}
}
collectArg();
}
let j = 0;
for (const arg of this.args) {
// Additional arguments were not provided
if (j >= argv.length) {
if (arg instanceof OptionalArg) {
parsed[arg.name] = nil;
j++;
continue;
}
throw new ParamError(`Expected argument ${arg.name}`);
}
// Absorb additional argv
if (arg instanceof Argv) {
parsed[arg.name] = argv.slice(j);
j = argv.length;
continue;
}
// Save the arg as normal and continue
parsed[arg.name] = argv[j];
j++;
}
// Unconsumed arguments
if (j < argv.length) {
throw new ParamError(`Unexpected argument ${argv[j]}`);
}
return parsed;
}
}