#!/usr/bin/env php * * SPDX-License-Identifier: GPL-3.0-or-later * * FILE INFORMATION * DEFGROUP: moko-platform.CLI * INGROUP: moko-platform * REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform * PATH: /cli/completion.php * BRIEF: Generate bash/zsh tab completion scripts for bin/moko */ declare(strict_types=1); require_once __DIR__ . '/../lib/Enterprise/CliFramework.php'; use MokoEnterprise\CliFramework; class CompletionCli extends CliFramework { protected function configure(): void { $this->setDescription('Generate bash/zsh tab completion scripts for bin/moko'); $this->addArgument('--shell', 'Shell type: bash or zsh', 'bash'); } protected function run(): int { $shell = $this->getArgument('--shell'); // Also accept positional-style: check raw argv for bash/zsh global $argv; foreach ($argv as $arg) { if (in_array($arg, ['bash', 'zsh'], true)) { $shell = $arg; break; } } // Extract command names from bin/moko COMMAND_MAP using regex (no eval). $mokoFile = dirname(__DIR__) . '/bin/moko'; $content = file_get_contents($mokoFile); // Isolate the COMMAND_MAP block, then extract keys. if (!preg_match('/const COMMAND_MAP\s*=\s*\[(.+?)\];/s', $content, $block)) { $this->log('ERROR', 'Could not find COMMAND_MAP in bin/moko'); return 1; } // Match 'command-name' => 'path' entries within the block. if (!preg_match_all("/'([a-z][a-z0-9:_-]*)'\s*=>/m", $block[1], $matches)) { $this->log('ERROR', 'Could not parse command names from COMMAND_MAP'); return 1; } $commandNames = array_unique($matches[1]); sort($commandNames); // Common flags supported by CliFramework. $commonFlags = ['--help', '--verbose', '--quiet', '--dry-run', '--json', '--no-color', '--path']; if ($shell === 'zsh') { $this->generateZsh($commandNames, $commonFlags); } else { $this->generateBash($commandNames, $commonFlags); } return 0; } // -- Generators -- private function generateBash(array $commands, array $flags): void { $cmdList = implode(' ', $commands); $flagList = implode(' ', $flags); echo << 'Show help for the command', '--verbose' => 'Show detailed output', '--quiet' => 'Suppress non-error output', '--dry-run' => 'Preview changes without writing', '--json' => 'Machine-readable JSON output', '--no-color' => 'Disable ANSI colour output', '--path' => 'Repository root path', default => $flag, }; $flagLines .= " '{$flag}[{$desc}]'\n"; } echo <<execute());