Skip to content

Commit 865a2da

Browse files
committed
set & get default cmd
1 parent b557798 commit 865a2da

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Application.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,37 @@ public function add(Command $command, string $alias = '', bool $default = false)
174174
return $this;
175175
}
176176

177+
/**
178+
* Set the default command.
179+
*
180+
* @param string $commandName The name of the default command
181+
*
182+
* @return self The application
183+
*
184+
* @throws InvalidArgumentException If the specified command name does not exist
185+
*/
186+
public function defaultCommand(string $commandName): self
187+
{
188+
if (!isset($this->commands[$commandName])) {
189+
throw new InvalidArgumentException(sprintf('Command "%s" does not exist', $commandName));
190+
}
191+
192+
$this->default = $commandName;
193+
194+
return $this;
195+
}
196+
197+
/**
198+
* Get the default command.
199+
*
200+
* @return string|null The name of the default command, or null if not set
201+
*/
202+
public function getDefaultCommand(): ?string
203+
{
204+
return $this->default;
205+
}
206+
207+
177208
/**
178209
* Groups commands set within the callable.
179210
*

tests/ApplicationTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,21 @@ protected function newApp(string $name, string $version = '')
305305

306306
return $app->io(new Interactor(static::$in, static::$ou));
307307
}
308+
309+
public function testDefaultCommand()
310+
{
311+
$app = $this->newApp("test");
312+
313+
// Add some sample commands to the application
314+
$app->command('command1');
315+
$app->command('command2');
316+
317+
// Test setting a valid default command
318+
$app->defaultCommand('command1');
319+
$this->assertEquals('command1', $app->getDefaultCommand());
320+
321+
// Test setting an invalid default command
322+
$this->expectException(InvalidArgumentException::class);
323+
$app->defaultCommand('invalid_command');
324+
}
308325
}

0 commit comments

Comments
 (0)