274
275
export function parseArgs<T>(args: string[], options: OptionDescriptions<T>, errorReporter: ErrorReporter = ignoringReporter): T {
276
>
// Find the first non-option arg, which also isn't the value for a previous `--flag`
argv.ts
277
>
const firstPossibleCommand = args.find((a, i) => a.length > 0 && a[0] !== '-' && options.hasOwnProperty(a) && options[a as T].type === 'subcommand');
278
>
279
>
const alias: { [key: string]: string } = {};
280
>
const stringOptions: string[] = ['_'];
281
>
const booleanOptions: string[] = [];
282
>
const globalOptions: Record<string, Option<'boolean'> | Option<'string'> | Option<'string[]'>> = {};
283
>
let command: Subcommand<Record<string, unknown>> | undefined = undefined;
284
>
for (const optionId in options) {
285
>
const o = options[optionId];
286
>
if (o.type === 'subcommand') {
287
>
if (optionId === firstPossibleCommand) {
288
command = o;
289
}
291
>
if (o.alias) {
292
alias[optionId] = o.alias;
293
}
295
>
if (o.type === 'string' || o.type === 'string[]') {
296
>
stringOptions.push(optionId);
297
>
if (o.deprecates) {
298
stringOptions.push(...o.deprecates);
299
}
300
>
} else if (o.type === 'boolean') {
argv.ts
301
>
booleanOptions.push(optionId);
302
>
if (o.deprecates) {
303
booleanOptions.push(...o.deprecates);
304
}
306
>
if (o.global) {
307
>
globalOptions[optionId] = o;
308
>
}
309
>
}
310
>
}
311
>
if (command && firstPossibleCommand) {
312
const options: Record<string, Option<'boolean'> | Option<'string'> | Option<'string[]'> | Subcommand<Record<string, unknown>>> = globalOptions;
313
for (const optionId in command.options) {