-
Notifications
You must be signed in to change notification settings - Fork 6
/
baseArg.ts
136 lines (121 loc) · 3.34 KB
/
baseArg.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import type { InferredOptionType, Options, PositionalOptions } from "yargs";
import type { ArgumentOptions } from "./argument.js";
import type { OptionOptions } from "./option.js";
export interface BaseArgOptions {
prompt?: true | string;
requires?: string | string[];
excludes?: string | string[];
}
// prettier-ignore
export type InferArgType<O extends Options | PositionalOptions, F = unknown> =
// Default number
O extends { default: number } ? number :
// Optional number
O extends { type: 'number', optional: true } ? number | undefined :
// Variadic number
O extends { type: 'number', variadic: true } ? Array<number> :
// Number
O extends { type: 'number' } ? number :
// Default boolean
O extends { default: boolean } ? boolean :
// Optional boolean
O extends { type: 'boolean', optional: true } ? boolean | undefined :
// Variadic boolean
O extends { type: 'boolean', variadic: true } ? Array<boolean> :
// Boolean
O extends { type: 'boolean' } ? boolean :
// Choices with array type
O extends { choices: ReadonlyArray<infer C>; type: 'array' } ? C[] :
// Choices with array default
O extends { choices: ReadonlyArray<infer C>, default: ReadonlyArray<string> } ? C[] :
// Choices, variadic
O extends { choices: ReadonlyArray<infer C>; variadic: true } ? C[] :
// Choices, optional
O extends { choices: ReadonlyArray<infer C>, optional: true } ? C | undefined :
// Prefer choices over default
O extends { choices: ReadonlyArray<infer C> } ? C :
// Default string
O extends { default: string } ? string :
// Optional string
O extends { optional: true } ? string | undefined :
// Variadic string
O extends { variadic: true } ? Array<string> :
// Allow fallback type
unknown extends InferredOptionType<O> ? F :
// Base type from yargs
InferredOptionType<O>
export class BaseArg {
protected name: string;
protected options: ArgumentOptions | OptionOptions = {};
constructor(name: string) {
this.name = name;
}
/**
* Set the argument/option description.
*/
public description(description: string) {
this.options.description = description;
return this;
}
/**
* Whether this argument/option can be interactive.
*/
isPromptable() {
return !!this.options.prompt;
}
/**
* Returns the prompt line.
*/
getPrompt() {
return typeof this.options.prompt === "string"
? this.options.prompt
: this.options.description
? this.options.description
: this.name;
}
/**
* Get default value, if specified.
*/
getDefault() {
return this.options.default;
}
/**
* Get possible values, is specified.
* @todo See if we can add this to autocompleter
*/
getChoices() {
return this.options.choices;
}
/**
* Get type, is specified.
*/
getType() {
return this.options.type;
}
/**
* Returns the argument/option identifier.
*/
getName() {
return this.name;
}
/**
* Returns the argument/option description.
*/
getDescription() {
return this.options.description;
}
/**
* Returns the argument/option options.
*/
getOptions() {
return this.options;
}
protected static getYargsOptions<T extends BaseArgOptions>(options: T) {
const { requires, excludes, ...rest } = options;
return {
implies: requires,
conflicts: excludes,
...rest,
};
}
}