Skip to content

fix(command): move default command handling to parseCommand#860

Merged
c4spar merged 2 commits intomainfrom
command/default-command
Mar 10, 2026
Merged

fix(command): move default command handling to parseCommand#860
c4spar merged 2 commits intomainfrom
command/default-command

Conversation

@c4spar
Copy link
Owner

@c4spar c4spar commented Mar 10, 2026

No description provided.

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) {
@c4spar c4spar force-pushed the command/default-command branch from 10c821c to 86d52c6 Compare March 10, 2026 21:32
@c4spar c4spar enabled auto-merge (squash) March 10, 2026 21:38
@c4spar c4spar merged commit c518882 into main Mar 10, 2026
15 of 16 checks passed
@c4spar c4spar deleted the command/default-command branch March 10, 2026 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant