Skip to content

Commit 10c821c

Browse files
committed
fix(command): move default command handling to parseCommand
diff --git c/command/command.ts i/command/command.ts index 92c9dfb..0eef8c2 100644 --- c/command/command.ts +++ i/command/command.ts @@ -1273,8 +1273,8 @@ export class Command< /** * Set default command. * - * The default command is executed when the program was called without any - * arguments. + * The default command is executed when the command was called without any + * additional arguments. * * @param name Name of the default command. */ @@ -2068,6 +2068,20 @@ export class Command< this.registerDefaults(); this.props.rawArgs = ctx.unknown.slice(); + if (!ctx.unknown.length && this.settings.defaultCommand) { + const defaultCommand = this.getCommand(this.settings.defaultCommand, true); + + if (!defaultCommand) { + throw new DefaultCommandNotFoundError( + this.settings.defaultCommand, + this.getCommands(), + ); + } + defaultCommand.props.globalParent = this; + + return defaultCommand.parseCommand(ctx); + } + if (this.settings.useRawArgs) { await this.parseEnvVars(ctx, this.builder.envVars); return await this.execute(ctx.env, ctx.unknown); @@ -2288,23 +2302,6 @@ export class Command< options: Record<string, unknown>, args: Array<unknown>, ): Promise<CommandResult> { - if ( - this.settings.defaultCommand && !args.length && - !Object.keys(options).length - ) { - const cmd = this.getCommand(this.settings.defaultCommand, true); - - if (!cmd) { - throw new DefaultCommandNotFoundError( - this.settings.defaultCommand, - this.getCommands(), - ); - } - cmd.props.globalParent = this; - - return cmd.execute(options, args); - } - await this.executeGlobalAction(options, args); if (this.settings.actionHandler) {
1 parent 929f30b commit 10c821c

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

command/command.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,8 +1273,8 @@ export class Command<
12731273
/**
12741274
* Set default command.
12751275
*
1276-
* The default command is executed when the program was called without any
1277-
* arguments.
1276+
* The default command is executed when the command was called without any
1277+
* additional arguments.
12781278
*
12791279
* @param name Name of the default command.
12801280
*/
@@ -2067,6 +2067,20 @@ export class Command<
20672067
this.registerDefaults();
20682068
this.props.rawArgs = ctx.unknown.slice();
20692069

2070+
if (!ctx.unknown.length && this.settings.defaultCommand) {
2071+
const defaultCommand = this.getCommand(this.settings.defaultCommand, true);
2072+
2073+
if (!defaultCommand) {
2074+
throw new DefaultCommandNotFoundError(
2075+
this.settings.defaultCommand,
2076+
this.getCommands(),
2077+
);
2078+
}
2079+
defaultCommand.props.globalParent = this;
2080+
2081+
return defaultCommand.parseCommand(ctx);
2082+
}
2083+
20702084
if (this.settings.useRawArgs) {
20712085
await this.parseEnvVars(ctx, this.builder.envVars);
20722086
return await this.execute(ctx.env, ctx.unknown);
@@ -2287,23 +2301,6 @@ export class Command<
22872301
options: Record<string, unknown>,
22882302
args: Array<unknown>,
22892303
): Promise<CommandResult> {
2290-
if (
2291-
this.settings.defaultCommand && !args.length &&
2292-
!Object.keys(options).length
2293-
) {
2294-
const cmd = this.getCommand(this.settings.defaultCommand, true);
2295-
2296-
if (!cmd) {
2297-
throw new DefaultCommandNotFoundError(
2298-
this.settings.defaultCommand,
2299-
this.getCommands(),
2300-
);
2301-
}
2302-
cmd.props.globalParent = this;
2303-
2304-
return cmd.execute(options, args);
2305-
}
2306-
23072304
await this.executeGlobalAction(options, args);
23082305

23092306
if (this.settings.actionHandler) {

0 commit comments

Comments
 (0)