Skip to content

Commit

Permalink
Add string[] to options defaultValue type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Caleb ツ Everett committed Apr 20, 2022
1 parent a80b984 commit 8ca3c3d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions typings/index.d.ts
Expand Up @@ -523,21 +523,21 @@ 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<T>(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
* the option must be specified on the command line. (Otherwise the same as .option().)
*
* 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<T>(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.
Expand Down

0 comments on commit 8ca3c3d

Please sign in to comment.