Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
39 / 42 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Help | |
92.86% |
39 / 42 |
|
75.00% |
3 / 4 |
11.04 | |
0.00% |
0 / 1 |
| run | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| showCommand | |
90.91% |
30 / 33 |
|
0.00% |
0 / 1 |
7.04 | |||
| renderOption | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getDescription | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework CLI Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\CLI\Commands; |
| 11 | |
| 12 | use Framework\CLI\CLI; |
| 13 | use Framework\CLI\Command; |
| 14 | |
| 15 | /** |
| 16 | * Class Help. |
| 17 | * |
| 18 | * @package cli |
| 19 | */ |
| 20 | class Help extends Command |
| 21 | { |
| 22 | protected string $name = 'help'; |
| 23 | protected string $usage = 'help [command_name]'; |
| 24 | |
| 25 | public function run() : void |
| 26 | { |
| 27 | $command = $this->console->getArgument(0) ?? 'help'; |
| 28 | $this->showCommand($command); |
| 29 | } |
| 30 | |
| 31 | protected function showCommand(string $commandName) : void |
| 32 | { |
| 33 | $command = $this->console->getCommand($commandName); |
| 34 | if ($command === null) { |
| 35 | CLI::error( |
| 36 | $this->console->getLanguage()->render('cli', 'commandNotFound', [$commandName]) |
| 37 | ); |
| 38 | } |
| 39 | CLI::write(CLI::style( |
| 40 | $this->console->getLanguage()->render('cli', 'command') . ': ', |
| 41 | CLI::FG_GREEN |
| 42 | ) . $command->getName()); |
| 43 | $value = $command->getDescription(); |
| 44 | if ($value !== '') { |
| 45 | CLI::write(CLI::style( |
| 46 | $this->console->getLanguage()->render('cli', 'description') . ': ', |
| 47 | CLI::FG_GREEN |
| 48 | ) . $value); |
| 49 | } |
| 50 | $value = $command->getUsage(); |
| 51 | if ($value !== '') { |
| 52 | CLI::write(CLI::style( |
| 53 | $this->console->getLanguage()->render('cli', 'usage') . ': ', |
| 54 | CLI::FG_GREEN |
| 55 | ) . $value); |
| 56 | } |
| 57 | $value = $command->getOptions(); |
| 58 | if ($value) { |
| 59 | CLI::write( |
| 60 | $this->console->getLanguage()->render('cli', 'options') . ': ', |
| 61 | CLI::FG_GREEN |
| 62 | ); |
| 63 | $lastKey = \array_key_last($value); |
| 64 | foreach ($value as $option => $description) { |
| 65 | CLI::write(' ' . $this->renderOption($option)); |
| 66 | CLI::write(' ' . $description); |
| 67 | if ($option !== $lastKey) { |
| 68 | CLI::newLine(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | protected function renderOption(string $text) : string |
| 75 | { |
| 76 | $text = \trim(\preg_replace('/\s+/', '', $text)); |
| 77 | $text = \explode(',', $text); |
| 78 | \sort($text); |
| 79 | foreach ($text as &$item) { |
| 80 | $item = CLI::style($item, CLI::FG_YELLOW); |
| 81 | } |
| 82 | return \implode(', ', $text); |
| 83 | } |
| 84 | |
| 85 | public function getDescription() : string |
| 86 | { |
| 87 | return $this->console->getLanguage()->render('cli', 'help.description'); |
| 88 | } |
| 89 | } |