console-app/lib/Sources/OptionSource.ts
2020-06-09 21:07:55 +02:00

35 lines
935 B
TypeScript

/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of console-app which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
import { ErrorCallback } from '../ErrorCallback';
import { OptionValue, Option } from '../Option';
export abstract class OptionSource {
public abstract async parse(opt: Option, value: OptionValue): Promise<void>;
protected error_callback?: ErrorCallback;
public constructor (error_callback?: ErrorCallback) {
this.error_callback = error_callback;
}
protected async assign_arg (
opt: Option,
val: OptionValue,
value: unknown
): Promise<void> {
try {
val.value = await val.type_validation.to_type (value);
val.filled = true;
}
catch (e) {
if (typeof this.error_callback !== 'undefined')
this.error_callback (opt.name, value, e);
}
}
}