From 7ede30d4fb99a9928c4d4e33b487c0273e2be2ed Mon Sep 17 00:00:00 2001 From: Cameron Hunter Date: Fri, 31 Jan 2020 15:15:24 -0800 Subject: [PATCH] yargs: Add array and demand types for positional arguments (#41721) * yargs: Add array and demand types for positional arguments * Fix formatting --- types/yargs/index.d.ts | 4 ++++ types/yargs/v10/index.d.ts | 1 + types/yargs/v10/yargs-tests.ts | 11 +++++++++++ types/yargs/v11/index.d.ts | 1 + types/yargs/v11/yargs-tests.ts | 11 +++++++++++ types/yargs/v12/index.d.ts | 2 ++ types/yargs/v12/yargs-tests.ts | 16 ++++++++++++++++ types/yargs/v13/index.d.ts | 4 ++++ types/yargs/v13/yargs-tests.ts | 16 ++++++++++++++++ types/yargs/yargs-tests.ts | 16 ++++++++++++++++ 10 files changed, 82 insertions(+) diff --git a/types/yargs/index.d.ts b/types/yargs/index.d.ts index 9adf019902..29c67dac36 100644 --- a/types/yargs/index.d.ts +++ b/types/yargs/index.d.ts @@ -682,6 +682,8 @@ declare namespace yargs { interface PositionalOptions { /** string or array of strings, see `alias()` */ alias?: string | ReadonlyArray; + /** boolean, interpret option as an array, see `array()` */ + array?: boolean; /** value or array of values, limit valid option arguments to a predefined set, see `choices()` */ choices?: Choices; /** function, coerce or transform parsed command line values into another value, see `coerce()` */ @@ -690,6 +692,8 @@ declare namespace yargs { conflicts?: string | ReadonlyArray | { [key: string]: string | ReadonlyArray }; /** value, set a default value for the option, see `default()` */ default?: any; + /** boolean or string, demand the option be given, with optional error message, see `demandOption()` */ + demandOption?: boolean | string; /** string, the option description for help content, see `describe()` */ desc?: string; /** string, the option description for help content, see `describe()` */ diff --git a/types/yargs/v10/index.d.ts b/types/yargs/v10/index.d.ts index f02b81bc25..e6d7611aa2 100644 --- a/types/yargs/v10/index.d.ts +++ b/types/yargs/v10/index.d.ts @@ -284,6 +284,7 @@ declare namespace yargs { interface PositionalOptions { alias?: string | string[]; + array?: boolean; choices?: Choices; coerce?: (arg: any) => any; conflicts?: string | string[] | { [key: string]: string | string[] }; diff --git a/types/yargs/v10/yargs-tests.ts b/types/yargs/v10/yargs-tests.ts index dd4b1a4cec..c95d629468 100644 --- a/types/yargs/v10/yargs-tests.ts +++ b/types/yargs/v10/yargs-tests.ts @@ -298,6 +298,17 @@ function Argv$command() { .argv; } +function Argv$positional() { + yargs + .command('test [paths...]', 'run tests', yargs => { + yargs.positional('paths', { + type: 'string', + array: true, + }); + }) + .help().argv; +} + function Argv$completion_sync() { const argv = yargs .completion('completion', (current, argv) => { diff --git a/types/yargs/v11/index.d.ts b/types/yargs/v11/index.d.ts index fe03a76702..a81f76767f 100644 --- a/types/yargs/v11/index.d.ts +++ b/types/yargs/v11/index.d.ts @@ -299,6 +299,7 @@ declare namespace yargs { interface PositionalOptions { alias?: string | string[]; + array?: boolean; choices?: Choices; coerce?: (arg: any) => any; conflicts?: string | string[] | { [key: string]: string | string[] }; diff --git a/types/yargs/v11/yargs-tests.ts b/types/yargs/v11/yargs-tests.ts index 288ac13b5e..d9af719cf4 100644 --- a/types/yargs/v11/yargs-tests.ts +++ b/types/yargs/v11/yargs-tests.ts @@ -298,6 +298,17 @@ function Argv$command() { .argv; } +function Argv$positional() { + yargs + .command('test [paths...]', 'run tests', yargs => { + yargs.positional('paths', { + type: 'string', + array: true, + }); + }) + .help().argv; +} + function Argv$completion_sync() { const argv = yargs .completion('completion', (current, argv) => { diff --git a/types/yargs/v12/index.d.ts b/types/yargs/v12/index.d.ts index 7239f21f78..43659780cb 100644 --- a/types/yargs/v12/index.d.ts +++ b/types/yargs/v12/index.d.ts @@ -359,10 +359,12 @@ declare namespace yargs { interface PositionalOptions { alias?: string | ReadonlyArray; + array?: boolean; choices?: Choices; coerce?: (arg: any) => any; conflicts?: string | ReadonlyArray | { [key: string]: string | ReadonlyArray }; default?: any; + demandOption?: boolean | string; desc?: string; describe?: string; description?: string; diff --git a/types/yargs/v12/yargs-tests.ts b/types/yargs/v12/yargs-tests.ts index 9346d24642..2f905c0b82 100644 --- a/types/yargs/v12/yargs-tests.ts +++ b/types/yargs/v12/yargs-tests.ts @@ -321,6 +321,22 @@ function Argv$command() { .argv; } +function Argv$positional() { + const module: yargs.CommandModule<{}, { paths: string[] }> = { + command: 'test ', + builder(yargs) { + return yargs.positional('paths', { + type: 'string', + array: true, + demandOption: true + }); + }, + handler(argv) { + argv.paths.map((path) => path); + } + }; +} + function Argv$commandModule() { class CommandOne implements yargs.CommandModule { handler(args: yargs.Arguments): void { diff --git a/types/yargs/v13/index.d.ts b/types/yargs/v13/index.d.ts index ada306cb82..8e062d40bd 100644 --- a/types/yargs/v13/index.d.ts +++ b/types/yargs/v13/index.d.ts @@ -676,6 +676,8 @@ declare namespace yargs { interface PositionalOptions { /** string or array of strings, see `alias()` */ alias?: string | ReadonlyArray; + /** boolean, interpret option as an array, see `array()` */ + array?: boolean; /** value or array of values, limit valid option arguments to a predefined set, see `choices()` */ choices?: Choices; /** function, coerce or transform parsed command line values into another value, see `coerce()` */ @@ -684,6 +686,8 @@ declare namespace yargs { conflicts?: string | ReadonlyArray | { [key: string]: string | ReadonlyArray }; /** value, set a default value for the option, see `default()` */ default?: any; + /** boolean or string, demand the option be given, with optional error message, see `demandOption()` */ + demandOption?: boolean | string; /** string, the option description for help content, see `describe()` */ desc?: string; /** string, the option description for help content, see `describe()` */ diff --git a/types/yargs/v13/yargs-tests.ts b/types/yargs/v13/yargs-tests.ts index 609a05be27..3f8ec41635 100644 --- a/types/yargs/v13/yargs-tests.ts +++ b/types/yargs/v13/yargs-tests.ts @@ -322,6 +322,22 @@ function Argv$command() { .argv; } +function Argv$positional() { + const module: yargs.CommandModule<{}, { paths: string[] }> = { + command: 'test ', + builder(yargs) { + return yargs.positional('paths', { + type: 'string', + array: true, + demandOption: true + }); + }, + handler(argv) { + argv.paths.map((path) => path); + } + }; +} + function Argv$commandModule() { class CommandOne implements yargs.CommandModule { handler(args: yargs.Arguments): void { diff --git a/types/yargs/yargs-tests.ts b/types/yargs/yargs-tests.ts index 609a05be27..3f8ec41635 100644 --- a/types/yargs/yargs-tests.ts +++ b/types/yargs/yargs-tests.ts @@ -322,6 +322,22 @@ function Argv$command() { .argv; } +function Argv$positional() { + const module: yargs.CommandModule<{}, { paths: string[] }> = { + command: 'test ', + builder(yargs) { + return yargs.positional('paths', { + type: 'string', + array: true, + demandOption: true + }); + }, + handler(argv) { + argv.paths.map((path) => path); + } + }; +} + function Argv$commandModule() { class CommandOne implements yargs.CommandModule { handler(args: yargs.Arguments): void {