diff --git a/src/Application.php b/src/Application.php index 99931b2..c2bb186 100644 --- a/src/Application.php +++ b/src/Application.php @@ -174,6 +174,37 @@ public function add(Command $command, string $alias = '', bool $default = false) return $this; } + /** + * Set the default command. + * + * @param string $commandName The name of the default command + * + * @return self The application + * + * @throws InvalidArgumentException If the specified command name does not exist + */ + public function defaultCommand(string $commandName): self + { + if (!isset($this->commands[$commandName])) { + throw new InvalidArgumentException(sprintf('Command "%s" does not exist', $commandName)); + } + + $this->default = $commandName; + + return $this; + } + + /** + * Get the default command. + * + * @return string|null The name of the default command, or null if not set + */ + public function getDefaultCommand(): ?string + { + return $this->default; + } + + /** * Groups commands set within the callable. * diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index 22bb2f9..0db09ff 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -305,4 +305,21 @@ protected function newApp(string $name, string $version = '') return $app->io(new Interactor(static::$in, static::$ou)); } + + public function testDefaultCommand() + { + $app = $this->newApp("test"); + + // Add some sample commands to the application + $app->command('command1'); + $app->command('command2'); + + // Test setting a valid default command + $app->defaultCommand('command1'); + $this->assertEquals('command1', $app->getDefaultCommand()); + + // Test setting an invalid default command + $this->expectException(InvalidArgumentException::class); + $app->defaultCommand('invalid_command'); + } }