@oclif/core 2.0.5 → 2.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/interfaces/parser.d.ts +15 -9
- package/lib/parser/parse.js +8 -8
- package/package.json +1 -1
|
@@ -58,8 +58,8 @@ export type DefaultContext<T> = {
|
|
|
58
58
|
options: T;
|
|
59
59
|
flags: Record<string, string>;
|
|
60
60
|
};
|
|
61
|
-
export type FlagDefault<T, P = CustomOptions> = T | ((context: DefaultContext<OptionFlag<T, P
|
|
62
|
-
export type FlagDefaultHelp<T, P = CustomOptions> = T | ((context: DefaultContext<OptionFlag<T, P
|
|
61
|
+
export type FlagDefault<T, P = CustomOptions> = T | ((context: DefaultContext<P & OptionFlag<T, P>>) => Promise<T>);
|
|
62
|
+
export type FlagDefaultHelp<T, P = CustomOptions> = T | ((context: DefaultContext<P & OptionFlag<T, P>>) => Promise<string | undefined>);
|
|
63
63
|
export type ArgDefault<T, P = CustomOptions> = T | ((context: DefaultContext<Arg<T, P>>) => Promise<T>);
|
|
64
64
|
export type ArgDefaultHelp<T, P = CustomOptions> = T | ((context: DefaultContext<Arg<T, P>>) => Promise<string | undefined>);
|
|
65
65
|
export type FlagRelationship = string | {
|
|
@@ -174,8 +174,14 @@ export type OptionFlagProps = FlagProps & {
|
|
|
174
174
|
options?: string[];
|
|
175
175
|
multiple?: boolean;
|
|
176
176
|
};
|
|
177
|
-
export type
|
|
178
|
-
|
|
177
|
+
export type FlagParserContext = Command & {
|
|
178
|
+
token: FlagToken;
|
|
179
|
+
};
|
|
180
|
+
export type FlagParser<T, I extends string | boolean, P = CustomOptions> = (input: I, context: FlagParserContext, opts: P & OptionFlag<T, P>) => Promise<T>;
|
|
181
|
+
export type ArgParserContext = Command & {
|
|
182
|
+
token: ArgToken;
|
|
183
|
+
};
|
|
184
|
+
export type ArgParser<T, P = CustomOptions> = (input: string, context: ArgParserContext, opts: P & Arg<T, P>) => Promise<T>;
|
|
179
185
|
export type Arg<T, P = CustomOptions> = ArgProps & {
|
|
180
186
|
options?: T[];
|
|
181
187
|
defaultHelp?: ArgDefaultHelp<T>;
|
|
@@ -199,13 +205,13 @@ export type BooleanFlag<T> = FlagProps & BooleanFlagProps & {
|
|
|
199
205
|
parse: (input: boolean, context: Command, opts: FlagProps & BooleanFlagProps) => Promise<T>;
|
|
200
206
|
};
|
|
201
207
|
export type OptionFlagDefaults<T, P = CustomOptions, M = false> = FlagProps & OptionFlagProps & {
|
|
202
|
-
parse: FlagParser<T, string, P
|
|
208
|
+
parse: FlagParser<T, string, P>;
|
|
203
209
|
defaultHelp?: FlagDefaultHelp<T>;
|
|
204
210
|
input: string[];
|
|
205
211
|
default?: M extends true ? FlagDefault<T[] | undefined, P> : FlagDefault<T | undefined, P>;
|
|
206
212
|
};
|
|
207
213
|
export type OptionFlag<T, P = CustomOptions> = FlagProps & OptionFlagProps & {
|
|
208
|
-
parse: FlagParser<T, string, P
|
|
214
|
+
parse: FlagParser<T, string, P>;
|
|
209
215
|
defaultHelp?: FlagDefaultHelp<T, P>;
|
|
210
216
|
input: string[];
|
|
211
217
|
} & ({
|
|
@@ -225,13 +231,13 @@ export type FlagDefinition<T, P = CustomOptions> = {
|
|
|
225
231
|
}) & Partial<OptionFlag<T, P>>): OptionFlag<T[]>;
|
|
226
232
|
(options: P & {
|
|
227
233
|
multiple: true;
|
|
228
|
-
} & Partial<OptionFlag<T>>): OptionFlag<T[] | undefined
|
|
234
|
+
} & Partial<OptionFlag<T>>): OptionFlag<T[] | undefined>;
|
|
229
235
|
(options: P & ({
|
|
230
236
|
required: true;
|
|
231
237
|
} | {
|
|
232
238
|
default: FlagDefault<T>;
|
|
233
|
-
}) & Partial<OptionFlag<T>>): OptionFlag<T
|
|
234
|
-
(options?: P & Partial<OptionFlag<T>>): OptionFlag<T | undefined
|
|
239
|
+
}) & Partial<OptionFlag<T>>): OptionFlag<T>;
|
|
240
|
+
(options?: P & Partial<OptionFlag<T>>): OptionFlag<T | undefined>;
|
|
235
241
|
};
|
|
236
242
|
export type Flag<T> = BooleanFlag<T> | OptionFlag<T>;
|
|
237
243
|
export type Input<TFlags extends FlagOutput, BFlags extends FlagOutput, AFlags extends ArgOutput> = {
|
package/lib/parser/parse.js
CHANGED
|
@@ -185,18 +185,18 @@ class Parser {
|
|
|
185
185
|
else {
|
|
186
186
|
flags[token.flag] = true;
|
|
187
187
|
}
|
|
188
|
-
flags[token.flag] = await this._parseFlag(flags[token.flag], flag);
|
|
188
|
+
flags[token.flag] = await this._parseFlag(flags[token.flag], flag, token);
|
|
189
189
|
}
|
|
190
190
|
else {
|
|
191
191
|
const input = token.input;
|
|
192
192
|
this._validateOptions(flag, input);
|
|
193
193
|
if (flag.delimiter && flag.multiple) {
|
|
194
|
-
const values = await Promise.all(input.split(flag.delimiter).map(async (v) => this._parseFlag(v.trim(), flag)));
|
|
194
|
+
const values = await Promise.all(input.split(flag.delimiter).map(async (v) => this._parseFlag(v.trim(), flag, token)));
|
|
195
195
|
flags[token.flag] = flags[token.flag] || [];
|
|
196
196
|
flags[token.flag].push(...values);
|
|
197
197
|
}
|
|
198
198
|
else {
|
|
199
|
-
const value = await this._parseFlag(input, flag);
|
|
199
|
+
const value = await this._parseFlag(input, flag, token);
|
|
200
200
|
if (flag.multiple) {
|
|
201
201
|
flags[token.flag] = flags[token.flag] || [];
|
|
202
202
|
flags[token.flag].push(value);
|
|
@@ -232,14 +232,14 @@ class Parser {
|
|
|
232
232
|
}
|
|
233
233
|
return flags;
|
|
234
234
|
}
|
|
235
|
-
async _parseFlag(input, flag) {
|
|
235
|
+
async _parseFlag(input, flag, token) {
|
|
236
236
|
if (!flag.parse)
|
|
237
237
|
return input;
|
|
238
238
|
try {
|
|
239
239
|
if (flag.type === 'boolean') {
|
|
240
|
-
return await flag.parse(input, this.context, flag);
|
|
240
|
+
return await flag.parse(input, { ...this.context, token }, flag);
|
|
241
241
|
}
|
|
242
|
-
return flag.parse ? await flag.parse(input, this.context, flag) : input;
|
|
242
|
+
return flag.parse ? await flag.parse(input, { ...this.context, token }, flag) : input;
|
|
243
243
|
}
|
|
244
244
|
catch (error) {
|
|
245
245
|
error.message = `Parsing --${flag.name} \n\t${error.message}\nSee more help with --help`;
|
|
@@ -261,7 +261,7 @@ class Parser {
|
|
|
261
261
|
if (arg.options && !arg.options.includes(token.input)) {
|
|
262
262
|
throw new errors_1.ArgInvalidOptionError(arg, token.input);
|
|
263
263
|
}
|
|
264
|
-
const parsed = await arg.parse(token.input, this.context, arg);
|
|
264
|
+
const parsed = await arg.parse(token.input, { ...this.context, token }, arg);
|
|
265
265
|
argv.push(parsed);
|
|
266
266
|
args[token.arg] = parsed;
|
|
267
267
|
}
|
|
@@ -269,7 +269,7 @@ class Parser {
|
|
|
269
269
|
let stdin = await readStdin();
|
|
270
270
|
if (stdin) {
|
|
271
271
|
stdin = stdin.trim();
|
|
272
|
-
const parsed = await arg.parse(stdin, this.context, arg);
|
|
272
|
+
const parsed = await arg.parse(stdin, { ...this.context, token }, arg);
|
|
273
273
|
argv.push(parsed);
|
|
274
274
|
args[name] = parsed;
|
|
275
275
|
}
|