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>.
41 public function getNativeDefinition()
43 return $this->createDefinition();
46 protected function execute(InputInterface $input, OutputInterface $output)
48 $this->handler = new CompletionHandler($this->getApplication());
49 $handler = $this->handler;
51 if ($input->getOption('generate-hook')) {
55 $factory = new HookFactory();
56 $alias = $input->getOption('program');
57 $multiple = (bool)$input->getOption('multiple');
59 // When completing for multiple apps having absolute path in the alias doesn't make sense.
60 if (!$alias && $multiple) {
61 $alias = basename($program);
64 $hook = $factory->generateHook(
65 $input->getOption('shell-type') ?: $this->getShellType(),
71 $output->write($hook, true);
73 $handler->setContext(new EnvironmentCompletionContext());
74 $output->write($this->runCompletion(), true);
79 * Run the completion handler and return a filtered list of results
81 * @deprecated - This will be removed in 1.0.0 in favour of CompletionCommand::configureCompletion
85 protected function runCompletion()
87 $this->configureCompletion($this->handler);
88 return $this->handler->runCompletion();
92 * Configure the CompletionHandler instance before it is run
94 * @param CompletionHandler $handler
96 protected function configureCompletion(CompletionHandler $handler)
98 // Override this method to configure custom value completions
102 * Determine the shell type for use with HookFactory
106 protected function getShellType()
108 if (!getenv('SHELL')) {
109 throw new \RuntimeException('Could not read SHELL environment variable. Please specify your shell type using the --shell-type option.');
112 return basename(getenv('SHELL'));
115 protected function createDefinition()
117 return new InputDefinition(array(
121 InputOption::VALUE_NONE,
122 'Generate BASH code that sets up completion for this application.'
127 InputOption::VALUE_REQUIRED,
128 "Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
133 InputOption::VALUE_NONE,
134 "Generated hook can be used for multiple applications."
139 InputOption::VALUE_OPTIONAL,
140 'Set the shell type (zsh or bash). Otherwise this is determined automatically.'