From 8ca3c3d5a6e21debcf5ba0047e68b573fc4f8433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Caleb=20=E3=83=84=20Everett?= Date: Wed, 20 Apr 2022 14:54:07 -0700 Subject: [PATCH] Add string[] to options defaultValue type option and requiredOption's defaultValue parameter was `string | boolean` but the default for a variadic option should be an array. ``` program.option('--var ', 'variadic arguments', ['1']) program.parse([]) > { var: ['1'] } program.parse(['--var', '1', '2']) > { var: ['1', '2'] } ``` If you use a string default you have to handle opts that are strings ``` // with a string arg the default value is a string. program.option('--var ', 'variadic arguments', '1') program.parse([]) > { var: '1' } program.parse(['--var', '1', '2']) > { var: ['1', '2'] } ``` `unknown` matches the jsdoc comment and the typings for argument(name: string, description?: string, defaultValue?: unknown): this` but conflicts with other `option` overloads. commander will pass thru any defaultValue to parse opts so `any` or `unknown` are good choices, but `string | boolean | string[]` reflects the values that commander will return when it parses arguments without a coerce function. --- typings/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 34a51202f..98f460742 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -523,10 +523,10 @@ export class Command { * * @returns `this` command for chaining */ - option(flags: string, description?: string, defaultValue?: string | boolean): this; + option(flags: string, description?: string, defaultValue?: string | boolean | string[]): this; option(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this; /** @deprecated since v7, instead use choices or a custom function */ - option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this; + option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean | string[]): this; /** * Define a required option, which must have a value after parsing. This usually means @@ -534,10 +534,10 @@ export class Command { * * The `flags` string contains the short and/or long flags, separated by comma, a pipe or space. */ - requiredOption(flags: string, description?: string, defaultValue?: string | boolean): this; + requiredOption(flags: string, description?: string, defaultValue?: string | boolean | string[]): this; requiredOption(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this; /** @deprecated since v7, instead use choices or a custom function */ - requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this; + requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean | string[]): this; /** * Factory routine to create a new unattached option.