/
index.ts
34 lines (27 loc) · 1023 Bytes
/
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
import { Cfg, Format, Mock, Opts, parseAll, printHelp, Rest, Vals } from "./common"
class ParserOptions<O extends Opts> {
/** @ignore */
constructor(private readonly cfg: Cfg, private readonly opts: O) {}
/**
* Load options from config file, environment variables and command line arguments
* @return Object of options with their values
*/
parse(mock: Mock = {}): Vals<O> & Rest {
return parseAll(this.cfg, this.opts, mock)
}
/** Prints help message to STDOUT. It's generated automatically based on the option definitions */
help(format: Format = "text"): void {
printHelp(this.cfg, this.opts, format)
}
}
class ParserConfig {
/** @ignore */
constructor(private readonly cfg: Cfg = {}) {}
/** Provide option definitions to the parser */
options<O extends Opts>(opts: O): ParserOptions<O> {
return new ParserOptions(this.cfg, opts)
}
}
/** Initialize the config parser */
export const config = (cfg: Cfg = {}): ParserConfig => new ParserConfig(cfg)
export { Cfg, Opts }