Skip to content

Commit 8ca3c3d

Browse files
committed
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 <args...>', '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 <args...>', '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.
1 parent a80b984 commit 8ca3c3d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

typings/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,21 +523,21 @@ export class Command {
523523
*
524524
* @returns `this` command for chaining
525525
*/
526-
option(flags: string, description?: string, defaultValue?: string | boolean): this;
526+
option(flags: string, description?: string, defaultValue?: string | boolean | string[]): this;
527527
option<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this;
528528
/** @deprecated since v7, instead use choices or a custom function */
529-
option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
529+
option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean | string[]): this;
530530

531531
/**
532532
* Define a required option, which must have a value after parsing. This usually means
533533
* the option must be specified on the command line. (Otherwise the same as .option().)
534534
*
535535
* The `flags` string contains the short and/or long flags, separated by comma, a pipe or space.
536536
*/
537-
requiredOption(flags: string, description?: string, defaultValue?: string | boolean): this;
537+
requiredOption(flags: string, description?: string, defaultValue?: string | boolean | string[]): this;
538538
requiredOption<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this;
539539
/** @deprecated since v7, instead use choices or a custom function */
540-
requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
540+
requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean | string[]): this;
541541

542542
/**
543543
* Factory routine to create a new unattached option.

0 commit comments

Comments
 (0)