console-app/lib/Option.ts

54 lines
1.2 KiB
TypeScript
Raw Permalink 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:58:29 +02:00
import { TypeValidation } from './TypeValidation/TypeValidation';
import { ErrorCallback } from './ErrorCallback';
2020-06-09 13:58:29 +02:00
2020-06-09 13:13:27 +02:00
interface SourceConfig {
console?: boolean,
configs?: string[],
interactive?: boolean,
}
2020-05-09 19:51:43 +02:00
interface Option {
name: string;
default?: unknown;
2020-06-09 13:13:27 +02:00
sources?: SourceConfig;
2020-06-09 13:58:29 +02:00
alias?: string;
env?: string;
message?: string;
error?: string;
error_callback?: ErrorCallback;
exit_on_interrupt?: boolean;
2020-05-09 19:51:43 +02:00
}
2020-06-09 13:58:29 +02:00
class OptionValue {
public filled = false;
public value?: unknown;
public readonly type_validation: TypeValidation;
public constructor (type_validation: TypeValidation) {
this.type_validation = type_validation;
}
2020-06-15 11:56:33 +02:00
public async assign_arg (
opt: Option,
value: unknown
): Promise<void> {
try {
this.value = await this.type_validation.to_type (value);
this.filled = true;
}
catch (e) {
if (typeof opt.error_callback !== 'undefined')
opt.error_callback (opt.name, value, e);
}
}
2020-05-09 19:51:43 +02:00
}
2020-06-09 13:13:27 +02:00
export { Option, OptionValue };