From 0945f82f564fb8296a358bec6f922c2dc473906c Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Wed, 20 Nov 2024 01:38:11 -0600 Subject: [PATCH] Allow for custom default command execution without arguments (#109) * Allow for custom default command execution without arguments * Add a test for default command execution * Fix testDefaultCommand command1 action indentation --- src/Application.php | 2 +- tests/ApplicationTest.php | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Application.php b/src/Application.php index 4e15610..3b41793 100644 --- a/src/Application.php +++ b/src/Application.php @@ -308,7 +308,7 @@ public function onException(callable $fn): self */ public function handle(array $argv): mixed { - if (count($argv) < 2) { + if ($this->default === '__default__' && count($argv) < 2) { return $this->showHelp(); } diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index e5f7dee..b6fce4f 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -312,13 +312,21 @@ public function testDefaultCommand() $app = $this->newApp('test'); // Add some sample commands to the application - $app->command('command1'); + $app->command('command1')->action(function () { + echo 'This should be the default command'; + }); $app->command('command2'); // Test setting a valid default command $app->defaultCommand('command1'); $this->assertEquals('command1', $app->getDefaultCommand()); + // Test executing a default command + ob_start(); + $app->handle(['test']); + $buffer = ob_get_clean(); + $this->assertSame('This should be the default command', $buffer); + // Test setting an invalid default command $this->expectException(InvalidArgumentException::class); $app->defaultCommand('invalid_command');