Skip to content

Commit 8b0af92

Browse files
ziadoztaylorotwell
andauthored
[9.x] Add --name option to schedule:test command (#41439)
* Add --name option to schedule:test command. * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9684a04 commit 8b0af92

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Illuminate/Console/Scheduling/ScheduleTestCommand.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Console\Scheduling;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Str;
67

78
class ScheduleTestCommand extends Command
89
{
@@ -11,7 +12,7 @@ class ScheduleTestCommand extends Command
1112
*
1213
* @var string
1314
*/
14-
protected $name = 'schedule:test';
15+
protected $signature = 'schedule:test {--name= : The name of the scheduled command to run}';
1516

1617
/**
1718
* The name of the console command.
@@ -45,7 +46,21 @@ public function handle(Schedule $schedule)
4546
$commandNames[] = $command->command ?? $command->getSummaryForDisplay();
4647
}
4748

48-
$index = array_search($this->choice('Which command would you like to run?', $commandNames), $commandNames);
49+
if (empty($commandNames)) {
50+
return $this->comment('No scheduled commands have been defined.');
51+
}
52+
53+
if (! empty($name = $this->option('name'))) {
54+
$matches = array_filter($commandNames, fn ($commandName) => Str::endsWith($commandName, $name));
55+
56+
if (count($matches) !== 1) {
57+
return $this->error('No matching scheduled command found.');
58+
}
59+
60+
$index = key($matches);
61+
} else {
62+
$index = array_search($this->choice('Which command would you like to run?', $commandNames), $commandNames);
63+
}
4964

5065
$event = $commands[$index];
5166

0 commit comments

Comments
 (0)