console-app/lib/Sources/ArgSource.ts

51 lines
1.3 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-05-09 19:51:43 +02:00
/* eslint-disable no-console */
/* eslint-disable no-process-exit */
import yargs, { Options } from 'yargs';
2020-06-09 21:03:10 +02:00
import { Option, OptionValue } from '../Option';
2020-05-09 19:51:43 +02:00
import { OptionSource } from './OptionSource';
export class ArgSource extends OptionSource {
2020-06-09 21:03:10 +02:00
private create_config (
config: Option,
persistent_type: string
): Record<string, Options> {
return {
[config.name]: {
alias: config.alias,
2020-06-05 13:22:54 +02:00
// eslint-disable-next-line no-undefined
2020-06-09 21:03:10 +02:00
default: undefined,
type: persistent_type
}
} as Record<string, Options>;
2020-05-09 21:30:37 +02:00
}
2020-06-09 21:03:10 +02:00
public async parse (opt: Option, val: OptionValue): Promise<void> {
const yargs_config = this.create_config (
opt,
val.type_validation.persistent_type
);
2020-05-09 19:51:43 +02:00
const argv = yargs.options (yargs_config)
.parse ();
2020-06-09 21:03:10 +02:00
if (typeof argv[opt.name] === 'undefined')
return;
if (
val.type_validation.option_type === 'array'
2020-05-09 21:30:37 +02:00
&& (argv[opt.name] as Array<unknown>)
.filter ((v) => typeof v !== 'undefined').length <= 0
2020-06-09 21:03:10 +02:00
)
return;
2020-05-09 19:51:43 +02:00
2020-06-15 11:56:33 +02:00
await val.assign_arg (opt, argv[opt.name]);
2020-05-09 19:51:43 +02:00
}
}