Skip to content

Commit

Permalink
Use this instead of Command as a return type in TypeScript (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam authored Feb 10, 2020
1 parent a8a14c9 commit 1704022
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ class Command extends EventEmitter {

parseAsync(argv, parseOptions) {
this.parse(argv, parseOptions);
return Promise.all(this._actionResults);
return Promise.all(this._actionResults).then(() => this);
};

/**
Expand Down
46 changes: 23 additions & 23 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ declare namespace commander {
*
* You can optionally supply the flags and description to override the defaults.
*/
version(str: string, flags?: string, description?: string): Command;
version(str: string, flags?: string, description?: string): this;

/**
* Define a command, implemented using an action handler.
Expand Down Expand Up @@ -81,26 +81,26 @@ declare namespace commander {
* @param opts - configuration options
* @returns top level command for chaining more command definitions
*/
command(nameAndArgs: string, description: string, opts?: commander.CommandOptions): Command;
command(nameAndArgs: string, description: string, opts?: commander.CommandOptions): this;

/**
* Add a prepared subcommand.
*
* @returns parent command for chaining
*/
addCommand(cmd: Command): Command;
addCommand(cmd: Command): this;

/**
* Define argument syntax for the top-level command.
*
* @returns Command for chaining
*/
arguments(desc: string): Command;
arguments(desc: string): this;

/**
* Register callback to use as replacement for calling process.exit.
*/
exitOverride(callback?: (err: CommanderError) => never|void): Command;
exitOverride(callback?: (err: CommanderError) => never|void): this;

/**
* Register callback `fn` for the command.
Expand All @@ -115,7 +115,7 @@ declare namespace commander {
*
* @returns Command for chaining
*/
action(fn: (...args: any[]) => void | Promise<void>): Command;
action(fn: (...args: any[]) => void | Promise<void>): this;

/**
* Define option with `flags`, `description` and optional
Expand Down Expand Up @@ -159,19 +159,19 @@ declare namespace commander {
*
* @returns Command for chaining
*/
option(flags: string, description?: string, defaultValue?: string | boolean): Command;
option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): Command;
option<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command;
option(flags: string, description?: string, defaultValue?: string | boolean): this;
option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
option<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): 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 should contain both the short and long flags, separated by comma, a pipe or space.
*/
requiredOption(flags: string, description?: string, defaultValue?: string | boolean): Command;
requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): Command;
requiredOption<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command;
requiredOption(flags: string, description?: string, defaultValue?: string | boolean): this;
requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
requiredOption<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this;


/**
Expand All @@ -180,23 +180,23 @@ declare namespace commander {
*
* @return Command for chaining
*/
storeOptionsAsProperties(value?: boolean): Command;
storeOptionsAsProperties(value?: boolean): this;

/**
* Whether to pass command to action handler,
* or just the options (specify false).
*
* @return Command for chaining
*/
passCommandToAction(value?: boolean): Command;
passCommandToAction(value?: boolean): this;

/**
* Allow unknown options on the command line.
*
* @param [arg] if `true` or omitted, no error will be thrown for unknown options.
* @returns Command for chaining
*/
allowUnknownOption(arg?: boolean): Command;
allowUnknownOption(arg?: boolean): this;

/**
* Parse `argv`, setting options and invoking commands when defined.
Expand All @@ -212,7 +212,7 @@ declare namespace commander {
*
* @returns Command for chaining
*/
parse(argv?: string[], options?: ParseOptions): Command;
parse(argv?: string[], options?: ParseOptions): this;

/**
* Parse `argv`, setting options and invoking commands when defined.
Expand All @@ -230,7 +230,7 @@ declare namespace commander {
*
* @returns Promise
*/
parseAsync(argv?: string[], options?: ParseOptions): Promise<any>;
parseAsync(argv?: string[], options?: ParseOptions): Promise<this>;

/**
* Parse options from `argv` removing known options,
Expand All @@ -255,7 +255,7 @@ declare namespace commander {
*
* @returns Command for chaining
*/
description(str: string, argsDescription?: {[argName: string]: string}): Command;
description(str: string, argsDescription?: {[argName: string]: string}): this;
/**
* Get the description.
*/
Expand All @@ -266,7 +266,7 @@ declare namespace commander {
*
* @returns Command for chaining
*/
alias(alias: string): Command;
alias(alias: string): this;
/**
* Get alias for the command.
*/
Expand All @@ -277,7 +277,7 @@ declare namespace commander {
*
* @returns Command for chaining
*/
usage(str: string): Command;
usage(str: string): this;
/**
* Get the command usage.
*/
Expand All @@ -288,7 +288,7 @@ declare namespace commander {
*
* @returns Command for chaining
*/
name(str: string): Command;
name(str: string): this;
/**
* Get the name of the command.
*/
Expand All @@ -311,7 +311,7 @@ declare namespace commander {
* You can pass in flags and a description to override the help
* flags and help description for your command.
*/
helpOption(flags?: string, description?: string): Command;
helpOption(flags?: string, description?: string): this;

/**
* Output help information and exit.
Expand All @@ -327,7 +327,7 @@ declare namespace commander {
* console.log('See web site for more information.');
* });
*/
on(event: string | symbol, listener: (...args: any[]) => void): Command;
on(event: string | symbol, listener: (...args: any[]) => void): this;
}
type CommandConstructor = { new (name?: string): Command };

Expand Down

0 comments on commit 1704022

Please sign in to comment.