|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Commands; |
15 | 15 |
|
16 | | -use CodeIgniter\CLI\BaseCommand; |
| 16 | +use CodeIgniter\CLI\AbstractCommand; |
| 17 | +use CodeIgniter\CLI\Attributes\Command; |
| 18 | +use CodeIgniter\CLI\CLI; |
| 19 | +use CodeIgniter\CLI\Input\Argument; |
17 | 20 |
|
18 | 21 | /** |
19 | | - * CI Help command for the spark script. |
20 | | - * |
21 | | - * Lists the basic usage information for the spark script, |
22 | | - * and provides a way to list help for other commands. |
| 22 | + * Displays the basic usage information for a given command. |
23 | 23 | */ |
24 | | -class Help extends BaseCommand |
| 24 | +#[Command(name: 'help', description: 'Displays basic usage information.', group: 'CodeIgniter')] |
| 25 | +class Help extends AbstractCommand |
25 | 26 | { |
26 | | - /** |
27 | | - * The group the command is lumped under |
28 | | - * when listing commands. |
29 | | - * |
30 | | - * @var string |
31 | | - */ |
32 | | - protected $group = 'CodeIgniter'; |
| 27 | + protected function configure(): void |
| 28 | + { |
| 29 | + $this->addArgument(new Argument( |
| 30 | + name: 'command_name', |
| 31 | + description: 'The command name.', |
| 32 | + default: $this->getName(), |
| 33 | + )); |
| 34 | + } |
33 | 35 |
|
34 | | - /** |
35 | | - * The Command's name |
36 | | - * |
37 | | - * @var string |
38 | | - */ |
39 | | - protected $name = 'help'; |
| 36 | + protected function execute(array $arguments, array $options): int |
| 37 | + { |
| 38 | + $command = $arguments['command_name']; |
| 39 | + assert(is_string($command)); |
40 | 40 |
|
41 | | - /** |
42 | | - * the Command's short description |
43 | | - * |
44 | | - * @var string |
45 | | - */ |
46 | | - protected $description = 'Displays basic usage information.'; |
| 41 | + $commands = $this->getCommandRunner(); |
47 | 42 |
|
48 | | - /** |
49 | | - * the Command's usage |
50 | | - * |
51 | | - * @var string |
52 | | - */ |
53 | | - protected $usage = 'help [<command_name>]'; |
| 43 | + if (array_key_exists($command, $commands->getCommands())) { |
| 44 | + $commands->getCommand($command)->showHelp(); |
54 | 45 |
|
55 | | - /** |
56 | | - * the Command's Arguments |
57 | | - * |
58 | | - * @var array<string, string> |
59 | | - */ |
60 | | - protected $arguments = [ |
61 | | - 'command_name' => 'The command name [default: "help"]', |
62 | | - ]; |
| 46 | + return EXIT_SUCCESS; |
| 47 | + } |
63 | 48 |
|
64 | | - /** |
65 | | - * the Command's Options |
66 | | - * |
67 | | - * @var array<string, string> |
68 | | - */ |
69 | | - protected $options = []; |
| 49 | + if (! $commands->verifyCommand($command, legacy: false)) { |
| 50 | + return EXIT_ERROR; |
| 51 | + } |
70 | 52 |
|
71 | | - /** |
72 | | - * Displays the help for spark commands. |
73 | | - */ |
74 | | - public function run(array $params) |
| 53 | + $this->describeHelp($commands->getCommand($command, legacy: false)); |
| 54 | + |
| 55 | + return EXIT_SUCCESS; |
| 56 | + } |
| 57 | + |
| 58 | + private function describeHelp(AbstractCommand $command): void |
75 | 59 | { |
76 | | - $command = array_shift($params); |
77 | | - $command ??= 'help'; |
78 | | - $commands = $this->commands->getCommands(); |
| 60 | + CLI::write(lang('CLI.helpUsage'), 'yellow'); |
79 | 61 |
|
80 | | - if (! $this->commands->verifyCommand($command, $commands)) { |
81 | | - return EXIT_ERROR; |
| 62 | + foreach ($command->getUsages() as $usage) { |
| 63 | + CLI::write($this->addPadding($usage)); |
82 | 64 | } |
83 | 65 |
|
84 | | - $class = new $commands[$command]['class']($this->logger, $this->commands); |
85 | | - $class->showHelp(); |
| 66 | + if ($command->getDescription() !== '') { |
| 67 | + CLI::newLine(); |
| 68 | + CLI::write(lang('CLI.helpDescription'), 'yellow'); |
| 69 | + CLI::write($this->addPadding($command->getDescription())); |
| 70 | + } |
86 | 71 |
|
87 | | - return EXIT_SUCCESS; |
| 72 | + $maxPadding = $this->getMaxPadding($command); |
| 73 | + |
| 74 | + if ($command->getArgumentsDefinition() !== []) { |
| 75 | + CLI::newLine(); |
| 76 | + CLI::write(lang('CLI.helpArguments'), 'yellow'); |
| 77 | + |
| 78 | + foreach ($command->getArgumentsDefinition() as $argument => $definition) { |
| 79 | + $default = ''; |
| 80 | + |
| 81 | + if (! $definition->required) { |
| 82 | + $default = sprintf(' [default: %s]', $this->formatDefaultValue($definition->default)); |
| 83 | + } |
| 84 | + |
| 85 | + CLI::write(sprintf( |
| 86 | + '%s%s%s', |
| 87 | + CLI::color($this->addPadding($argument, 2, $maxPadding), 'green'), |
| 88 | + $definition->description, |
| 89 | + CLI::color($default, 'yellow'), |
| 90 | + )); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if ($command->getOptionsDefinition() !== []) { |
| 95 | + CLI::newLine(); |
| 96 | + CLI::write(lang('CLI.helpOptions'), 'yellow'); |
| 97 | + |
| 98 | + $hasShortcuts = $command->getShortcuts() !== []; |
| 99 | + |
| 100 | + foreach ($command->getOptionsDefinition() as $option => $definition) { |
| 101 | + $value = ''; |
| 102 | + |
| 103 | + if ($definition->acceptsValue) { |
| 104 | + $value = sprintf('=%s', strtoupper($definition->valueLabel ?? '')); |
| 105 | + |
| 106 | + if (! $definition->requiresValue) { |
| 107 | + $value = sprintf('[%s]', $value); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + $optionString = sprintf( |
| 112 | + '%s--%s%s%s', |
| 113 | + $definition->shortcut !== null |
| 114 | + ? sprintf('-%s, ', $definition->shortcut) |
| 115 | + : ($hasShortcuts ? ' ' : ''), |
| 116 | + $option, |
| 117 | + $value, |
| 118 | + $definition->negation !== null ? sprintf('|--%s', $definition->negation) : '', |
| 119 | + ); |
| 120 | + |
| 121 | + CLI::write(sprintf( |
| 122 | + '%s%s%s', |
| 123 | + CLI::color($this->addPadding($optionString, 2, $maxPadding), 'green'), |
| 124 | + $definition->description, |
| 125 | + $definition->isArray ? CLI::color(' (multiple values allowed)', 'yellow') : '', |
| 126 | + )); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private function addPadding(string $item, int $before = 2, ?int $max = null): string |
| 132 | + { |
| 133 | + return str_pad(str_repeat(' ', $before) . $item, $max ?? (strlen($item) + $before)); |
| 134 | + } |
| 135 | + |
| 136 | + private function getMaxPadding(AbstractCommand $command): int |
| 137 | + { |
| 138 | + $max = 0; |
| 139 | + |
| 140 | + foreach (array_keys($command->getArgumentsDefinition()) as $argument) { |
| 141 | + $max = max($max, strlen($argument)); |
| 142 | + } |
| 143 | + |
| 144 | + $hasShortcuts = $command->getShortcuts() !== []; |
| 145 | + |
| 146 | + foreach ($command->getOptionsDefinition() as $option => $definition) { |
| 147 | + $optionLength = strlen($option) + 2 // Account for the "--" prefix on options. |
| 148 | + + ($definition->acceptsValue ? strlen($definition->valueLabel ?? '') + ($definition->requiresValue ? 1 : 3) : 0) // Account for the "=%s" value notation if the option accepts a value. |
| 149 | + + ($hasShortcuts ? 4 : 0) // Account for the "-%s, " shortcut notation if shortcuts are present. |
| 150 | + + ($definition->negation !== null ? 3 + strlen($definition->negation) : 0); // Account for the "|--no-%s" negation notation if a negation exists for this option. |
| 151 | + |
| 152 | + $max = max($max, $optionLength); |
| 153 | + } |
| 154 | + |
| 155 | + return $max + 4; // Account for the extra padding around the option/argument. |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @param list<string>|string $value |
| 160 | + */ |
| 161 | + private function formatDefaultValue(array|string $value): string |
| 162 | + { |
| 163 | + if (is_array($value)) { |
| 164 | + return sprintf('[%s]', implode(', ', array_map($this->formatDefaultValue(...), $value))); |
| 165 | + } |
| 166 | + |
| 167 | + return sprintf('"%s"', $value); |
88 | 168 | } |
89 | 169 | } |
0 commit comments