console-app/lib/Sources/OptionSource.ts

35 lines
935 B
TypeScript
Raw Normal View History

2020-05-15 16:53:45 +02:00
/*
* 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
*/
2020-06-09 13:13:27 +02:00
import { ErrorCallback } from '../ErrorCallback';
import { OptionValue, Option } from '../Option';
2020-05-09 19:51:43 +02:00
export abstract class OptionSource {
2020-06-09 13:13:27 +02:00
public abstract async parse(opt: Option, value: OptionValue): Promise<void>;
2020-05-09 19:51:43 +02:00
protected error_callback?: ErrorCallback;
public constructor (error_callback?: ErrorCallback) {
this.error_callback = error_callback;
}
2020-05-09 19:51:43 +02:00
protected async assign_arg (
2020-06-09 21:03:10 +02:00
opt: Option,
val: OptionValue,
2020-05-09 19:51:43 +02:00
value: unknown
): Promise<void> {
try {
2020-06-09 21:03:10 +02:00
val.value = await val.type_validation.to_type (value);
val.filled = true;
2020-05-09 19:51:43 +02:00
}
catch (e) {
if (typeof this.error_callback !== 'undefined')
this.error_callback (opt.name, value, e);
2020-05-09 19:51:43 +02:00
}
}
}