Skip to content
This repository has been archived by the owner on Oct 12, 2018. It is now read-only.

Commit

Permalink
Add autocomplete for console command options.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Jan 21, 2011
1 parent 3137a6f commit f1a1eee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
24 changes: 18 additions & 6 deletions Command/AutocompleteCommand.php
Expand Up @@ -4,6 +4,7 @@

use Symfony\Bundle\FrameworkBundle\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -22,6 +23,7 @@ protected function configure()
{
$this
->setDefinition(array(
new InputArgument('command_name', InputArgument::OPTIONAL, 'A command name to generate autocomplete options for'),
))
->setName('console:autocomplete')
->setHelp(<<<EOT
Expand All @@ -30,18 +32,28 @@ protected function configure()
<info>php app/console console:autocomplete</info>
EOT
);
;
);
}

/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$commands = $this->application->all();
$commands = array_keys($commands);

$output->write(join(" ", $commands), false);
$commandName = $input->getArgument('command_name');
if ($commandName !== null && $this->application->has($commandName)) {
$options = array_merge(
$this->application->get($commandName)->getDefinition()->getOptions(),
$this->application->getDefinition()->getOptions()
);
$options = array_map(function($option) {
return '--' . $option->getName();
}, $options);
$output->write(join(" ", $options), false);
} else {
$commands = $this->application->all();
$commands = array_keys($commands);
$output->write(join(" ", $commands), false);
}
}
}
12 changes: 11 additions & 1 deletion Resources/Shells/symfony2-completion.bash
Expand Up @@ -26,7 +26,17 @@ _console()
return 0
;;
*)
;;
;;
esac
fi
if [[ ${COMP_CWORD} > 1 ]] ; then
case "${cur}" in
--*)
COMPREPLY=( $(compgen -W "$(${cmd} console:autocomplete ${COMP_WORDS[1]})" -- ${cur}) )
return 0
;;
*)
;;
esac
fi

Expand Down

0 comments on commit f1a1eee

Please sign in to comment.