3 namespace Stecman\Component\Symfony\Console\BashCompletion;
5 use Symfony\Component\Console\Command\Command as SymfonyCommand;
6 use Symfony\Component\Console\Input\InputDefinition;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Input\InputOption;
9 use Symfony\Component\Console\Output\OutputInterface;
11 class CompletionCommand extends SymfonyCommand
15 * @var CompletionHandler
19 protected function configure()
22 ->setName('_completion')
23 ->setDefinition($this->createDefinition())
24 ->setDescription('BASH completion hook.')
26 To enable BASH completion, run:
28 <comment>eval `[program] _completion -g`</comment>.
32 <comment>eval `[program] _completion -g -p [alias]`</comment>.
37 // Hide this command from listing if supported
38 // Command::setHidden() was not available before Symfony 3.2.0
39 if (method_exists($this, 'setHidden')) {
40 $this->setHidden(true);
47 public function getNativeDefinition()
49 return $this->createDefinition();
52 protected function execute(InputInterface $input, OutputInterface $output)
54 $this->handler = new CompletionHandler($this->getApplication());
55 $handler = $this->handler;
57 if ($input->getOption('generate-hook')) {
61 $factory = new HookFactory();
62 $alias = $input->getOption('program');
63 $multiple = (bool)$input->getOption('multiple');
66 $alias = basename($program);
69 $hook = $factory->generateHook(
70 $input->getOption('shell-type') ?: $this->getShellType(),
76 $output->write($hook, true);
78 $handler->setContext(new EnvironmentCompletionContext());
79 $output->write($this->runCompletion(), true);
84 * Run the completion handler and return a filtered list of results
86 * @deprecated - This will be removed in 1.0.0 in favour of CompletionCommand::configureCompletion
90 protected function runCompletion()
92 $this->configureCompletion($this->handler);
93 return $this->handler->runCompletion();
97 * Configure the CompletionHandler instance before it is run
99 * @param CompletionHandler $handler
101 protected function configureCompletion(CompletionHandler $handler)
103 // Override this method to configure custom value completions
107 * Determine the shell type for use with HookFactory
111 protected function getShellType()
113 if (!getenv('SHELL')) {
114 throw new \RuntimeException('Could not read SHELL environment variable. Please specify your shell type using the --shell-type option.');
117 return basename(getenv('SHELL'));
120 protected function createDefinition()
122 return new InputDefinition(array(
126 InputOption::VALUE_NONE,
127 'Generate BASH code that sets up completion for this application.'
132 InputOption::VALUE_REQUIRED,
133 "Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
138 InputOption::VALUE_NONE,
139 "Generated hook can be used for multiple applications."
144 InputOption::VALUE_OPTIONAL,
145 'Set the shell type (zsh or bash). Otherwise this is determined automatically.'