Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow _completion command to auto-complete it's arguments or their values #47

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions src/CompletionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Stecman\Component\Symfony\Console\BashCompletion;

use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -19,6 +20,7 @@ protected function configure()
{
$this
->setName('_completion')
->setDefinition($this->createDefinition())
->setDescription('BASH completion hook.')
->setHelp(<<<END
To enable BASH completion, run:
Expand All @@ -30,27 +32,17 @@ protected function configure()
<comment>eval `[program] _completion -g -p [alias]`</comment>.

END
)
->addOption(
'generate-hook',
'g',
InputOption::VALUE_NONE,
'Generate BASH code that sets up completion for this application.'
)
->addOption(
'program',
'p',
InputOption::VALUE_REQUIRED,
"Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
)
->addOption(
'shell-type',
null,
InputOption::VALUE_OPTIONAL,
'Set the shell type (zsh or bash). Otherwise this is determined automatically.'
);
}

/**
* {@inheritdoc}
*/
public function getNativeDefinition()
{
return $this->createDefinition();
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->handler = new CompletionHandler($this->getApplication());
Expand Down Expand Up @@ -110,4 +102,28 @@ protected function getShellType()

return basename(getenv('SHELL'));
}

protected function createDefinition()
{
return new InputDefinition(array(
new InputOption(
'generate-hook',
'g',
InputOption::VALUE_NONE,
'Generate BASH code that sets up completion for this application.'
),
new InputOption(
'program',
'p',
InputOption::VALUE_REQUIRED,
"Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
),
new InputOption(
'shell-type',
null,
InputOption::VALUE_OPTIONAL,
'Set the shell type (zsh or bash). Otherwise this is determined automatically.'
),
));
}
}
10 changes: 5 additions & 5 deletions src/CompletionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected function completeForOptionShortcuts()
$word = $this->context->getCurrentWord();

if (strpos($word, '-') === 0 && strlen($word) == 2) {
$definition = $this->command ? $this->command->getDefinition() : $this->application->getDefinition();
$definition = $this->command ? $this->command->getNativeDefinition() : $this->application->getDefinition();

if ($definition->hasShortcut(substr($word, 1))) {
return array($word);
Expand All @@ -202,7 +202,7 @@ protected function completeForOptionShortcutValues()
// Complete short options
if ($left[0] == '-' && strlen($left) == 2) {
$shortcut = substr($left, 1);
$def = $this->command->getDefinition();
$def = $this->command->getNativeDefinition();

if (!$def->hasShortcut($shortcut)) {
return false;
Expand Down Expand Up @@ -232,7 +232,7 @@ protected function completeForOptionValues()

if (strpos($left, '--') === 0) {
$name = substr($left, 2);
$def = $this->command->getDefinition();
$def = $this->command->getNativeDefinition();

if (!$def->hasOption($name)) {
return false;
Expand Down Expand Up @@ -279,7 +279,7 @@ protected function completeForCommandArguments()
{
if (strpos($this->context->getCurrentWord(), '-') !== 0) {
if ($this->command) {
$argWords = $this->mapArgumentsToWords($this->command->getDefinition()->getArguments());
$argWords = $this->mapArgumentsToWords($this->command->getNativeDefinition()->getArguments());
$wordIndex = $this->context->getWordIndex();

if (isset($argWords[$wordIndex])) {
Expand Down Expand Up @@ -433,7 +433,7 @@ protected function getAllOptions()
}

return array_merge(
$this->command->getDefinition()->getOptions(),
$this->command->getNativeDefinition()->getOptions(),
$this->application->getDefinition()->getOptions()
);
}
Expand Down