Skip to content

Commit

Permalink
Allow for custom default command execution without arguments (#109)
Browse files Browse the repository at this point in the history
* Allow for custom default command execution without arguments

* Add a test for default command execution

* Fix testDefaultCommand command1 action indentation
  • Loading branch information
kodie authored Nov 20, 2024
1 parent a03fa80 commit 0945f82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
10 changes: 9 additions & 1 deletion tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 0945f82

Please sign in to comment.