Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve option typings when adding arguments to Command #49

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,21 +549,21 @@ export class CommanderError extends Error {
* @returns `this` command for chaining
*/
argument<S extends string, T>(
flags: S, description: string, fn: (value: string, previous: T) => T): Command<[...Args, InferArgument<S, undefined, T>]>;
flags: S, description: string, fn: (value: string, previous: T) => T): Command<[...Args, InferArgument<S, undefined, T>], Opts>;
argument<S extends string, T>(
flags: S, description: string, fn: (value: string, previous: T) => T, defaultValue: T): Command<[...Args, InferArgument<S, T, T>]>;
flags: S, description: string, fn: (value: string, previous: T) => T, defaultValue: T): Command<[...Args, InferArgument<S, T, T>], Opts>;
argument<S extends string>(
usage: S, description?: string): Command<[...Args, InferArgument<S, undefined>]>;
usage: S, description?: string): Command<[...Args, InferArgument<S, undefined>], Opts>;
argument<S extends string, DefaultT>(
usage: S, description: string, defaultValue: DefaultT): Command<[...Args, InferArgument<S, DefaultT>]>;
usage: S, description: string, defaultValue: DefaultT): Command<[...Args, InferArgument<S, DefaultT>], Opts>;

/**
* Define argument syntax for command, adding a prepared argument.
*
* @returns `this` command for chaining
*/
addArgument<Usage extends string, DefaultT, CoerceT, ArgRequired extends boolean|undefined, ChoicesT>(
arg: Argument<Usage, DefaultT, CoerceT, ArgRequired, ChoicesT>): Command<[...Args, InferArgument<Usage, DefaultT, CoerceT, ArgRequired, ChoicesT>]>;
arg: Argument<Usage, DefaultT, CoerceT, ArgRequired, ChoicesT>): Command<[...Args, InferArgument<Usage, DefaultT, CoerceT, ArgRequired, ChoicesT>], Opts>;


/**
Expand All @@ -578,7 +578,7 @@ export class CommanderError extends Error {
*
* @returns `this` command for chaining
*/
arguments<Names extends string>(args: Names): Command<[...Args, ...InferArguments<Names>]>;
arguments<Names extends string>(args: Names): Command<[...Args, ...InferArguments<Names>], Opts>;


/**
Expand Down
25 changes: 25 additions & 0 deletions tests/arguments.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,28 @@ expectType<('C')>(
.parse()
.processedArgs[0]
)

// adding argument preserves options
expectType<({ example?: true })>(
program
.option('--example')
.argument('<arg>', 'arg description')
.parse()
.opts()
)

expectType<({ example?: true })>(
program
.option('--example')
.arguments('<arg1> [arg2]')
.parse()
.opts()
)

expectType<({ example?: true })>(
program
.option('--example')
.addArgument(new Argument('<arg>'))
.parse()
.opts()
)