|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Console\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Input\InputDefinition; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | +use Symfony\Component\Process\Process; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Wouter de Jong <[email protected]> |
| 22 | + */ |
| 23 | +class AutocompleteCommand extends Command |
| 24 | +{ |
| 25 | + public function __construct() |
| 26 | + { |
| 27 | + parent::__construct('_autocomplete'); |
| 28 | + } |
| 29 | + |
| 30 | + protected function configure() |
| 31 | + { |
| 32 | + $this |
| 33 | + ->addOption('global-args', '', InputOption::VALUE_NONE) |
| 34 | + ->addOption('command', '', InputOption::VALUE_OPTIONAL, '', false) |
| 35 | + ; |
| 36 | + } |
| 37 | + |
| 38 | + public function execute(InputInterface $input, OutputInterface $output) |
| 39 | + { |
| 40 | + try { |
| 41 | + $p = Process::fromShellCommandline('echo $SHELL'); |
| 42 | + $shell = basename(rtrim($p->mustRun()->getOutput())); |
| 43 | + if ('zsh' !== $shell) { |
| 44 | + throw new \RuntimeException(sprintf('Unsupported shell: "%s"', $shell)); |
| 45 | + } |
| 46 | + |
| 47 | + $output->writeln($this->autocompleteZsh($input)); |
| 48 | + } catch (\Throwable $e) { |
| 49 | + $output->writeln($e->getMessage()); |
| 50 | + } |
| 51 | + |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + private function autocompleteZsh(InputInterface $input): array |
| 56 | + { |
| 57 | + if ($input->getOption('global-args')) { |
| 58 | + return $this->autocompleteZshInputDefinition($this->getApplication()->getDefinition()); |
| 59 | + } |
| 60 | + |
| 61 | + if (false !== ($cmd = $input->getOption('command'))) { |
| 62 | + if (!$cmd) { |
| 63 | + $zshDescribe = []; |
| 64 | + foreach ($this->getApplication()->all() as $command) { |
| 65 | + $zshDescribe[] = sprintf('%s:%s', str_replace(':', '\:', $command->getName()), $command->getDescription()); |
| 66 | + } |
| 67 | + |
| 68 | + return $zshDescribe; |
| 69 | + } |
| 70 | + |
| 71 | + return $this->autocompleteZshInputDefinition($this->getApplication()->find($cmd)->getDefinition()); |
| 72 | + } |
| 73 | + |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
| 77 | + private function autocompleteZshInputDefinition(InputDefinition $inputDefinition): array |
| 78 | + { |
| 79 | + $zshArguments = []; |
| 80 | + foreach ($inputDefinition->getOptions() as $inputOption) { |
| 81 | + $name = $inputOption->getName(); |
| 82 | + if ('verbose' === $name) { |
| 83 | + $zshArguments[] = '(-q --quiet --verbose)*-v[increase output verbosity]'; |
| 84 | + $zshArguments[] = '(-q --quiet --verbose)--verbose[increase output verbosity]'; |
| 85 | + continue; |
| 86 | + } elseif ('quiet' === $name) { |
| 87 | + $zshArguments[] = '(-q -v --quiet --verbose)-q[reduce output verbosity]'; |
| 88 | + $zshArguments[] = '(-q -v --quiet --verbose)--quiet[reduce output verbosity]'; |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + $alternative = $inputOption->isNegatable() ? '-no-'.$name : $inputOption->getShortcut(); |
| 93 | + $description = $inputOption->getDescription(); |
| 94 | + if ($alternative) { |
| 95 | + $zshArguments[] = sprintf('(--%s -%s)--%1$s[%s]', $name, $alternative, $description); |
| 96 | + $zshArguments[] = sprintf('(--%s -%s)-%2$s[%s]', $name, $alternative, $description); |
| 97 | + } else { |
| 98 | + $zshArguments[] = sprintf('--%1s[%s]', $name, $description); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return $zshArguments; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/* |
| 107 | +[ |
| 108 | + '(-h --help)-h[display help information]', |
| 109 | + '(-h --help)--help[display help information]', |
| 110 | +
|
| 111 | + '(: * -)-V[display version information]', |
| 112 | + '(: * -)--version[display version information]', |
| 113 | +
|
| 114 | + '(-q -v --quiet --verbose)-q[reduce output verbosity]', |
| 115 | + '(-q -v --quiet --verbose)--quiet}[reduce output verbosity]', |
| 116 | + '(-q --quiet --verbose)*-v[increase output verbosity]', |
| 117 | + '(-q --quiet --verbose)--verbose[increase output verbosity]', |
| 118 | +
|
| 119 | + '(--no-ansi)--ansi[force ANSI (color) output]', |
| 120 | + '(--ansi)--no-ansi[disable ANSI (color) output]', |
| 121 | +
|
| 122 | + '(-n --no-interaction)-n[run non-interactively]', |
| 123 | + '(-n --no-interaction)--no-interaction[run non-interactively]', |
| 124 | + ] |
| 125 | + */ |
0 commit comments