|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace imperazim\command; |
| 6 | + |
| 7 | +use imperazim\command\Command; |
| 8 | +use imperazim\command\SubCommand; |
| 9 | +use imperazim\command\argument\Argument; |
| 10 | +use imperazim\command\constraint\Constraint; |
| 11 | + |
| 12 | +/** |
| 13 | + * Generates automatic help text for commands. |
| 14 | + */ |
| 15 | +final class HelpGenerator { |
| 16 | + |
| 17 | + /** |
| 18 | + * Generates help text for a command. |
| 19 | + * |
| 20 | + * @param Command $command The command |
| 21 | + * @param string $format The format style (detailed, compact, usage) |
| 22 | + * @return string The help text |
| 23 | + */ |
| 24 | + public static function generate(Command $command, string $format = "detailed"): string { |
| 25 | + return match ($format) { |
| 26 | + "compact" => self::generateCompact($command), |
| 27 | + "usage" => self::generateUsage($command), |
| 28 | + default => self::generateDetailed($command) |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Generates detailed help text. |
| 34 | + * |
| 35 | + * @param Command $command The command |
| 36 | + * @return string The help text |
| 37 | + */ |
| 38 | + private static function generateDetailed(Command $command): string { |
| 39 | + $lines = []; |
| 40 | + |
| 41 | + // Header |
| 42 | + $lines[] = "§e§l" . strtoupper($command->getName()) . " COMMAND"; |
| 43 | + $lines[] = "§r§7" . $command->getDescription(); |
| 44 | + $lines[] = ""; |
| 45 | + |
| 46 | + // Usage |
| 47 | + $lines[] = "§eUsage:"; |
| 48 | + $lines[] = " §7/" . self::buildUsageLine($command); |
| 49 | + $lines[] = ""; |
| 50 | + |
| 51 | + // Arguments |
| 52 | + if (!empty($command->getArguments())) { |
| 53 | + $lines[] = "§eArguments:"; |
| 54 | + foreach ($command->getArguments() as $arg) { |
| 55 | + $name = $arg->getName(); |
| 56 | + $type = $arg->getTypeName(); |
| 57 | + $desc = $arg->getDescription(); |
| 58 | + $optional = $arg->isOptional(); |
| 59 | + $default = $arg->getDefault(); |
| 60 | + |
| 61 | + $prefix = $optional ? " §7[" : " §7<"; |
| 62 | + $suffix = $optional ? "]" : ">"; |
| 63 | + $line = $prefix . $name . "§8:" . $type . $suffix; |
| 64 | + |
| 65 | + if ($desc !== "") { |
| 66 | + $line .= " §f- " . $desc; |
| 67 | + } |
| 68 | + |
| 69 | + if ($optional && $default !== null) { |
| 70 | + $line .= " §8(default: §7" . self::formatValue($default) . "§8)"; |
| 71 | + } |
| 72 | + |
| 73 | + $lines[] = $line; |
| 74 | + } |
| 75 | + $lines[] = ""; |
| 76 | + } |
| 77 | + |
| 78 | + // Subcommands |
| 79 | + if (!empty($command->getSubCommands())) { |
| 80 | + $lines[] = "§eSubcommands:"; |
| 81 | + foreach ($command->getSubCommands() as $sub) { |
| 82 | + $name = $sub->getName(); |
| 83 | + $desc = $sub->getDescription(); |
| 84 | + $aliases = $sub->getAliases(); |
| 85 | + |
| 86 | + $line = " §7" . $name; |
| 87 | + |
| 88 | + if (!empty($aliases)) { |
| 89 | + $line .= " §8(" . implode(", ", $aliases) . ")"; |
| 90 | + } |
| 91 | + |
| 92 | + if ($desc !== "") { |
| 93 | + $line .= " §f- " . $desc; |
| 94 | + } |
| 95 | + |
| 96 | + $lines[] = $line; |
| 97 | + } |
| 98 | + $lines[] = ""; |
| 99 | + } |
| 100 | + |
| 101 | + // Constraints |
| 102 | + if (!empty($command->getConstraints())) { |
| 103 | + $lines[] = "§eRequirements:"; |
| 104 | + foreach ($command->getConstraints() as $constraint) { |
| 105 | + $desc = $constraint->getDescription(); |
| 106 | + if ($desc !== "") { |
| 107 | + $lines[] = " §7• " . $desc; |
| 108 | + } |
| 109 | + } |
| 110 | + $lines[] = ""; |
| 111 | + } |
| 112 | + |
| 113 | + return implode("\n", $lines); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Generates compact help text. |
| 118 | + * |
| 119 | + * @param Command $command The command |
| 120 | + * @return string The help text |
| 121 | + */ |
| 122 | + private static function generateCompact(Command $command): string { |
| 123 | + $lines = []; |
| 124 | + |
| 125 | + $lines[] = "§e/" . self::buildUsageLine($command); |
| 126 | + $lines[] = "§7" . $command->getDescription(); |
| 127 | + |
| 128 | + if (!empty($command->getSubCommands())) { |
| 129 | + $subNames = array_map(fn($s) => $s->getName(), $command->getSubCommands()); |
| 130 | + $lines[] = "§8Subcommands: §7" . implode("§8, §7", $subNames); |
| 131 | + } |
| 132 | + |
| 133 | + return implode("\n", $lines); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Generates usage-only help text. |
| 138 | + * |
| 139 | + * @param Command $command The command |
| 140 | + * @return string The usage line |
| 141 | + */ |
| 142 | + private static function generateUsage(Command $command): string { |
| 143 | + return "§7/" . self::buildUsageLine($command); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Builds the usage line for a command. |
| 148 | + * |
| 149 | + * @param Command $command The command |
| 150 | + * @return string The usage line |
| 151 | + */ |
| 152 | + private static function buildUsageLine(Command $command): string { |
| 153 | + $parts = [$command->getName()]; |
| 154 | + |
| 155 | + if (!empty($command->getSubCommands())) { |
| 156 | + $subNames = array_map(fn($s) => $s->getName(), $command->getSubCommands()); |
| 157 | + $parts[] = "<" . implode("|", $subNames) . ">"; |
| 158 | + } |
| 159 | + |
| 160 | + foreach ($command->getArguments() as $arg) { |
| 161 | + $name = $arg->getName(); |
| 162 | + $aliases = $arg->getAliases(); |
| 163 | + |
| 164 | + if (!empty($aliases)) { |
| 165 | + $name .= "|" . implode("|", $aliases); |
| 166 | + } |
| 167 | + |
| 168 | + if ($arg->isOptional()) { |
| 169 | + $parts[] = "[" . $name . "]"; |
| 170 | + } else { |
| 171 | + $parts[] = "<" . $name . ">"; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return implode(" ", $parts); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Generates help text for a subcommand. |
| 180 | + * |
| 181 | + * @param SubCommand $subcommand The subcommand |
| 182 | + * @param string $parentName The parent command name |
| 183 | + * @param string $format The format style |
| 184 | + * @return string The help text |
| 185 | + */ |
| 186 | + public static function generateSubCommand( |
| 187 | + SubCommand $subcommand, |
| 188 | + string $parentName, |
| 189 | + string $format = "detailed" |
| 190 | + ): string { |
| 191 | + $lines = []; |
| 192 | + |
| 193 | + if ($format === "detailed") { |
| 194 | + // Header |
| 195 | + $lines[] = "§e§l" . strtoupper($parentName . " " . $subcommand->getName()); |
| 196 | + $lines[] = "§r§7" . $subcommand->getDescription(); |
| 197 | + $lines[] = ""; |
| 198 | + |
| 199 | + // Usage |
| 200 | + $lines[] = "§eUsage:"; |
| 201 | + $lines[] = " §7/" . self::buildSubCommandUsageLine($subcommand, $parentName); |
| 202 | + $lines[] = ""; |
| 203 | + |
| 204 | + // Arguments |
| 205 | + if (!empty($subcommand->getArguments())) { |
| 206 | + $lines[] = "§eArguments:"; |
| 207 | + foreach ($subcommand->getArguments() as $arg) { |
| 208 | + $name = $arg->getName(); |
| 209 | + $type = $arg->getTypeName(); |
| 210 | + $desc = $arg->getDescription(); |
| 211 | + $optional = $arg->isOptional(); |
| 212 | + |
| 213 | + $prefix = $optional ? " §7[" : " §7<"; |
| 214 | + $suffix = $optional ? "]" : ">"; |
| 215 | + $line = $prefix . $name . "§8:" . $type . $suffix; |
| 216 | + |
| 217 | + if ($desc !== "") { |
| 218 | + $line .= " §f- " . $desc; |
| 219 | + } |
| 220 | + |
| 221 | + $lines[] = $line; |
| 222 | + } |
| 223 | + $lines[] = ""; |
| 224 | + } |
| 225 | + } else { |
| 226 | + $lines[] = "§7/" . self::buildSubCommandUsageLine($subcommand, $parentName); |
| 227 | + $lines[] = "§7" . $subcommand->getDescription(); |
| 228 | + } |
| 229 | + |
| 230 | + return implode("\n", $lines); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Builds the usage line for a subcommand. |
| 235 | + * |
| 236 | + * @param SubCommand $subcommand The subcommand |
| 237 | + * @param string $parentName The parent command name |
| 238 | + * @return string The usage line |
| 239 | + */ |
| 240 | + private static function buildSubCommandUsageLine( |
| 241 | + SubCommand $subcommand, |
| 242 | + string $parentName |
| 243 | + ): string { |
| 244 | + $parts = [$parentName, $subcommand->getName()]; |
| 245 | + |
| 246 | + foreach ($subcommand->getArguments() as $arg) { |
| 247 | + $name = $arg->getName(); |
| 248 | + |
| 249 | + if ($arg->isOptional()) { |
| 250 | + $parts[] = "[" . $name . "]"; |
| 251 | + } else { |
| 252 | + $parts[] = "<" . $name . ">"; |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + return implode(" ", $parts); |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Formats a value for display. |
| 261 | + * |
| 262 | + * @param mixed $value The value |
| 263 | + * @return string The formatted value |
| 264 | + */ |
| 265 | + private static function formatValue($value): string { |
| 266 | + if (is_bool($value)) { |
| 267 | + return $value ? "true" : "false"; |
| 268 | + } |
| 269 | + |
| 270 | + if (is_array($value)) { |
| 271 | + return "[" . implode(", ", array_map([self::class, "formatValue"], $value)) . "]"; |
| 272 | + } |
| 273 | + |
| 274 | + if (is_string($value)) { |
| 275 | + return '"' . $value . '"'; |
| 276 | + } |
| 277 | + |
| 278 | + return (string) $value; |
| 279 | + } |
| 280 | +} |
0 commit comments