array type

This commit is contained in:
2020-05-09 21:30:37 +02:00
parent 469afeb777
commit 7a13de1d03
11 changed files with 138 additions and 56 deletions

View File

@ -1,11 +1,11 @@
/* eslint-disable no-console */
/* eslint-disable no-process-exit */
import yargs, { Options } from 'yargs';
import { OptionProcess } from '../Types';
import { OptionProcess } from '../Option';
import { OptionSource } from './OptionSource';
export class ArgSource extends OptionSource {
public async parse (options: OptionProcess[]): Promise<void> {
private create_config (options: OptionProcess[]): Record<string, Options> {
const yargs_config: Record<string, Options> = {
quiet: {
alias: 'q',
@ -24,10 +24,15 @@ export class ArgSource extends OptionSource {
yargs_config[opt.name] = {
alias: opt.alias,
default: opt.default,
type: opt.type_validation.string_type,
type: opt.type_validation.persistent_type,
describe: opt.description
};
}
return yargs_config;
}
public async parse (options: OptionProcess[]): Promise<void> {
const yargs_config = this.create_config (options);
const argv = yargs.options (yargs_config)
.parse ();
if (argv.help) {
@ -37,9 +42,15 @@ export class ArgSource extends OptionSource {
}
await Promise.all (options.map ((opt) => {
if (typeof argv[opt.name] !== 'undefined')
return this.assign_arg (opt, argv[opt.name]);
return Promise.resolve ();
if (argv[opt.name] === 'undefined')
return Promise.resolve ();
if (
opt.type === 'array'
&& (argv[opt.name] as Array<unknown>)
.filter ((v) => typeof v !== 'undefined').length <= 0
)
return Promise.resolve ();
return this.assign_arg (opt, argv[opt.name]);
}));
if (argv.quiet) {

View File

@ -1,5 +1,5 @@
/* eslint-disable no-process-env */
import { OptionProcess } from '../Types';
import { OptionProcess } from '../Option';
import { OptionSource } from './OptionSource';
export class EnvSource extends OptionSource {

View File

@ -1,13 +1,20 @@
/* eslint-disable no-console */
/* eslint-disable no-process-exit */
import { Confirm, Input } from 'enquirer';
import { OptionProcess } from '../Types';
import { Confirm, Input, List } from 'enquirer';
import { OptionProcess, Option } from '../Option';
import { OptionSource } from './OptionSource';
export class InteractiveSource extends OptionSource {
private get_message (opt: Option): string {
return typeof opt.message === 'undefined'
? `input ${opt.name}`
: opt.message;
}
private async prompt (opt: OptionProcess): Promise<void> {
if (opt.filled)
return;
let value = null;
if (
opt.type === 'string'
|| opt.type === 'file'
@ -15,26 +22,32 @@ export class InteractiveSource extends OptionSource {
|| opt.type === 'path'
|| opt.type === 'number'
) {
const value = await new Input ({
message: typeof opt.message === 'undefined'
? `input ${opt.name}`
: opt.message,
value = await new Input ({
message: this.get_message (opt),
default: opt.default
})
.run ();
await this.assign_arg (opt, value);
return;
}
if (
opt.type === 'boolean'
) {
const value = await new Confirm ({
message: opt.message,
value = await new Confirm ({
message: this.get_message (opt),
default: opt.default
})
.run ();
await this.assign_arg (opt, value);
}
if (opt.type === 'array') {
value = await new List ({
message: this.get_message (opt),
default: opt.default
})
.run ();
}
if (value === null)
return;
await this.assign_arg (opt, value);
}
public async parse (options: OptionProcess[]): Promise<void> {

View File

@ -1,4 +1,4 @@
import { OptionProcess } from '../Types';
import { OptionProcess } from '../Option';
export abstract class OptionSource {
public abstract async parse(opt: OptionProcess[]): Promise<void>;