console-app/lib/Options/BaseOption.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-06-09 13:13:27 +02:00
import { OptionSource } from '../Sources/OptionSource';
import { Option } from '../Option';
import { EnvSource } from '../Sources/EnvSource';
import { ErrorCallback } from '../ErrorCallback';
import { ArgSource } from '../Sources/ArgSource';
import { ConfigSource } from '../Sources/ConfigSource';
import { TypeValidation } from '../TypeValidation/TypeValidation';
export abstract class BaseOption<T> {
protected readonly sources: OptionSource[] = [];
private _config: Option;
public constructor (
config: Option,
error_callback?: ErrorCallback,
exit_on_interrupt = true
) {
this._config = config;
const sources = config.sources || {};
if (typeof sources.configs !== 'undefined')
this.sources.push (new ConfigSource (sources.configs, error_callback));
if (sources.env !== false)
this.sources.push (new EnvSource (error_callback));
if (sources.console !== false)
this.sources.push (new ArgSource (error_callback));
if (sources.interactive !== false) {
this.sources.push (new InteractiveSource (
exit_on_interrupt,
error_callback
));
}
}
protected abstract get validation(): TypeValidation;
public async parse(): Promise<T> {
for (let source of this.sources) {
source.parse(this._config);
}
};
}