2017 © Pedro Peláez
 

library symfony-console-completion

Automatic BASH completion for Symfony Console Component based applications.

image

stecman/symfony-console-completion

Automatic BASH completion for Symfony Console Component based applications.

  • Monday, April 2, 2018
  • by stecman
  • Repository
  • 15 Watchers
  • 344 Stars
  • 4,943,386 Installations
  • PHP
  • 36 Dependents
  • 4 Suggesters
  • 18 Forks
  • 12 Open issues
  • 20 Versions
  • 9 % Grown

The README.md

BASH/ZSH auto-complete for Symfony Console applications

Scrutinizer Code Quality, (*1)

Latest Stable Version Total Downloads License PHP Version Require, (*2)

This package provides automatic (tab) completion in BASH and ZSH for Symfony Console Component based applications. With zero configuration, this package allows completion of available command names and the options they provide. User code can define custom completion behaviour for argument and option values., (*3)

Example of zero-config use with Composer:, (*4)

Composer BASH completion, (*5)

Zero-config use

If you don't need any custom completion behaviour, you can simply add the completion command to your application:, (*6)

  1. Install stecman/symfony-console-completion using composer by running:, (*7)

    $ composer require stecman/symfony-console-completion
    
  2. For standalone Symfony Console applications, add an instance of CompletionCommand to your application's Application::getDefaultCommands() method:, (*8)

    protected function getDefaultCommands()
    {
      //...
       $commands[] = new \Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand();
      //...
    }
    

    For Symfony Framework applications, register the CompletionCommand as a service in app/config/services.yml:, (*9)

    services:
    #...
       console.completion_command:
         class: Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand
         tags:
             -  { name: console.command }
    #...
    
  3. Register completion for your application by running one of the following in a terminal, replacing [program] with the command you use to run your application (eg. 'composer'):, (*10)

    # BASH ~4.x, ZSH
    source <([program] _completion --generate-hook)
    
    # BASH ~3.x, ZSH
    [program] _completion --generate-hook | source /dev/stdin
    
    # BASH (any version)
    eval $([program] _completion --generate-hook)
    

    By default this registers completion for the absolute path to you application, which will work if the program is accessible on your PATH. You can specify a program name to complete for instead using the --program option, which is required if you're using an alias to run the program., (*11)

  4. If you want the completion to apply automatically for all new shell sessions, add the command from step 3 to your shell's profile (eg. ~/.bash_profile or ~/.zshrc), (*12)

Note: The type of shell (ZSH/BASH) is automatically detected using the SHELL environment variable at run time. In some circumstances, you may need to explicitly specify the shell type with the --shell-type option., (*13)

The current version supports Symfony 6 and PHP 8.x only, due to backwards compatibility breaks in Symfony 6. For older versions of Symfony and PHP, use version 0.11.0., (*14)

How it works

The --generate-hook option of CompletionCommand generates a small shell script that registers a function with your shell's completion system to act as a bridge between the shell and the completion command in your application. When you request completion for your program (by pressing tab with your program name as the first word on the command line), the bridge function is run; passing the current command line contents and cursor position to [program] _completion, and feeding the resulting output back to the shell., (*15)

Defining value completions

By default, no completion results will be returned for option and argument values. There are two ways of defining custom completion values for values: extend CompletionCommand, or implement CompletionAwareInterface., (*16)

Implementing CompletionAwareInterface

CompletionAwareInterface allows a command to be responsible for completing its own option and argument values. When completion is run with a command name specified (eg. myapp mycommand ...) and the named command implements this interface, the appropriate interface method is called automatically:, (*17)

class MyCommand extends Command implements CompletionAwareInterface
{
    ...

    public function completeOptionValues($optionName, CompletionContext $context)
    {
        if ($optionName == 'some-option') {
            return ['myvalue', 'other-value', 'word'];
        }
    }

    public function completeArgumentValues($argumentName, CompletionContext $context)
    {
        if ($argumentName == 'package') {
            return $this->getPackageNamesFromDatabase($context->getCurrentWord());
        }
    }
}

This method of generating completions doesn't support use of CompletionInterface implementations at the moment, which make it easy to share completion behaviour between commands. To use this functionality, you'll need write your value completions by extending CompletionCommand., (*18)

Extending CompletionCommand

Argument and option value completions can also be defined by extending CompletionCommand and overriding the configureCompletion method:, (*19)

class MyCompletionCommand extends CompletionCommand
{
    protected function configureCompletion(CompletionHandler $handler)
    {
        $handler->addHandlers([
            // Instances of Completion go here.
            // See below for examples.
        ]);
    }
}

The Completion class

The following snippets demonstrate how the Completion class works with CompletionHandler, and some possible configurations. The examples are for an application with the signature:, (*20)

`myapp (walk|run) [-w|--weather=""] direction`
Command-specific argument completion with an array
$handler->addHandler(
    new Completion(
        'walk',                    // match command name
        'direction',               // match argument/option name
        Completion::TYPE_ARGUMENT, // match definition type (option/argument)
        [                     // array or callback for results
            'north',
            'east',
            'south',
            'west'
        ]
    )
);

This will complete the direction argument for this:, (*21)

$ myapp walk [tab]

but not this:, (*22)

$ myapp run [tab]
Non-command-specific (global) argument completion with a function
$handler->addHandler(
    new Completion(
        Completion::ALL_COMMANDS,
        'direction',
        Completion::TYPE_ARGUMENT,
        function() {
            return range(1, 10);
        }
    )
);

This will complete the direction argument for both commands:, (*23)

$ myapp walk [tab]
$ myapp run [tab]
Option completion

Option handlers work the same way as argument handlers, except you use Completion::TYPE_OPTION for the type., (*24)

$handler->addHandler(
    new Completion(
        Completion::ALL_COMMANDS,
        'weather',
        Completion::TYPE_OPTION,
        [
            'raining',
            'sunny',
            'everything is on fire!'
        ]
    )
);
Completing the for both arguments and options

To have a completion run for both options and arguments matching the specified name, you can use the type Completion::ALL_TYPES. Combining this with Completion::ALL_COMMANDS and consistent option/argument naming throughout your application, it's easy to share completion behaviour between commands, options and arguments:, (*25)

$handler->addHandler(
    new Completion(
        Completion::ALL_COMMANDS,
        'package',
        Completion::ALL_TYPES,
        function() {
            // ...
        }
    )
);

Example completions

Completing references from a Git repository

new Completion(
    Completion::ALL_COMMANDS,
    'ref',
    Completion::TYPE_OPTION,
    function () {
        $raw = shell_exec('git show-ref --abbr');
        if (preg_match_all('/refs\/(?:heads|tags)?\/?(.*)/', $raw, $matches)) {
            return $matches[1];
        }
    }
)

Completing filesystem paths

This library provides the completion implementation ShellPathCompletion which defers path completion to the shell's built-in path completion behaviour rather than implementing it in PHP, so that users get the path completion behaviour they expect from their shell., (*26)

new Completion\ShellPathCompletion(
    Completion::ALL_COMMANDS,
    'path',
    Completion::TYPE_OPTION
)

Behaviour notes

  • Option shortcuts are not offered as completion options, however requesting completion (ie. pressing tab) on a valid option shortcut will complete.
  • Completion is not implemented for the --option="value" style of passing a value to an option, however --option value and --option "value" work and are functionally identical.
  • Value completion is always run for options marked as InputOption::VALUE_OPTIONAL since there is currently no way to determine the desired behaviour from the command line contents (ie. skip the optional value or complete for it)

The Versions

02/04 2018

dev-master

9999999-dev

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

12/02 2018

dev-string-arguments

dev-string-arguments

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

10/02 2018

0.8.0

0.8.0.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

24/02 2016

0.7.0

0.7.0.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

12/12 2015

0.6.2

0.6.2.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

30/09 2015

0.6.1

0.6.1.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

30/08 2015

0.6.0

0.6.0.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

07/05 2015

0.5.1

0.5.1.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

01/02 2015

0.5.0

0.5.0.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

18/11 2014

0.4.4

0.4.4.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

13/11 2014

0.4.3

0.4.3.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

10/11 2014

0.4.2

0.4.2.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

21/10 2014

0.4.1

0.4.1.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

21/10 2014

v0.4.0

0.4.0.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

13/07 2014

v0.3.0

0.3.0.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

20/09 2013

v0.2.2

0.2.2.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

21/08 2013

v0.2.1

0.2.1.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

29/05 2013

v0.2.0

0.2.0.0

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

27/05 2013

v0.1.1-beta

0.1.1.0-beta

Automatic BASH completion for Symfony Console Component based applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/05 2013

v0.1.0-beta

0.1.0.0-beta

Automatic BASH completion for the Symfony Console Component.

  Sources   Download

MIT

The Requires

 

The Development Requires