Skip to content

Commit 0945f82

Browse files
authored
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
1 parent a03fa80 commit 0945f82

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/Application.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function onException(callable $fn): self
308308
*/
309309
public function handle(array $argv): mixed
310310
{
311-
if (count($argv) < 2) {
311+
if ($this->default === '__default__' && count($argv) < 2) {
312312
return $this->showHelp();
313313
}
314314

tests/ApplicationTest.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,21 @@ public function testDefaultCommand()
312312
$app = $this->newApp('test');
313313

314314
// Add some sample commands to the application
315-
$app->command('command1');
315+
$app->command('command1')->action(function () {
316+
echo 'This should be the default command';
317+
});
316318
$app->command('command2');
317319

318320
// Test setting a valid default command
319321
$app->defaultCommand('command1');
320322
$this->assertEquals('command1', $app->getDefaultCommand());
321323

324+
// Test executing a default command
325+
ob_start();
326+
$app->handle(['test']);
327+
$buffer = ob_get_clean();
328+
$this->assertSame('This should be the default command', $buffer);
329+
322330
// Test setting an invalid default command
323331
$this->expectException(InvalidArgumentException::class);
324332
$app->defaultCommand('invalid_command');

0 commit comments

Comments
 (0)