Skip to content

Commit 820837f

Browse files
committed
Improve readme and completion command help text
Also adds a GIF demonstrating completion on a Symfony Console application
1 parent b1b9d31 commit 820837f

File tree

2 files changed

+63
-39
lines changed

2 files changed

+63
-39
lines changed

README.md

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
# Symfony Console completion
1+
# BASH completion for Symfony Console applications
22

3-
This package provides automatic BASH completion for Symfony Console Component based applications. With minimal configuration, this package allows completion of available command names and the options they provide. Custom completion behaviour can be added for option and argument values by name.
3+
This package provides automatic BASH completion for Symfony Console Component based applications. With zero configuration, this package allows completion of available command names and the options they provide. Custom completion behaviour can be added for option and argument values by name.
44

5-
## Basic use
5+
Example of zero-config use with Composer:
66

7-
If you don't need any custom completion behaviour:
7+
![Composer BASH completion](https://i.imgur.com/MoDWkby.gif)
8+
9+
## Zero-config use
10+
11+
If you don't need any custom completion behaviour, all you need to do is add the completion command to your application's available commands:
812

913
1. Install `stecman/symfony-console-completion` through composer
10-
2. Add an instance of `CompletionCommand` to your application's `Application::getDefaultCommands()`:
14+
2. Add an instance of `CompletionCommand` to your application's `Application::getDefaultCommands()` method:
1115
```
1216
protected function getDefaultCommands()
1317
{
@@ -17,12 +21,15 @@ If you don't need any custom completion behaviour:
1721
}
1822
```
1923

20-
3. Run `eval $([program] _completion -g)` in a terminal.
21-
4. Add the above command to your bash profile if you want the completion to apply automatically for new terminal sessions.
24+
3. Run `eval $([program] _completion --generate-hook)` in a terminal, replacing `[program]` with the command you use to run your application (eg. 'composer'). By default this registers completion for the absolute path to you application; you can specify a command name to complete for instead using the `-p` option.
25+
4. Add the command from step 3 to your bash profile if you want the completion to apply automatically for all new terminal sessions.
26+
27+
Note: If `[program]` is an alias you will need to specify the aliased name with the `-p|--program` option, as completion may not work otherwise: `_completion --generate-hook -p [myalias]`.
28+
29+
### How it works
2230

23-
The `-g` option generates a few lines of bash that, when run, register your application as a completion handler for your program name. Completion is handled by running the completion command on your application with no arguments: `[program] _completion`.
31+
The `--generate-hook` option of `CompletionCommand` generates a few lines of BASH that, when executed, register your application as a completion handler for your itself in the current shell session. When you request completion for your program (by pressing tab), the completion command on your application is run with no arguments: `[program] _completion`. This uses environment variables set by BASH to get the current command line contents and cursor position, then completes from your console command definitions.
2432

25-
If you need to use completion with an alias, specify the program name to complete for with the `-p` option: `_completion -g -p [alias-name]`.
2633

2734
## Custom completion
2835

@@ -34,25 +41,27 @@ The following examples are for an application with this signature: `myapp (walk|
3441

3542
protected function runCompletion()
3643
{
37-
$handler = $this->handler;
38-
$handler->addHandlers(array(
44+
$this->handler->addHandlers(array(
3945

40-
... // See below for what goes in here
46+
// Instances of Completion go here.
47+
// See below for examples.
4148

4249
));
4350

44-
return $handler->runCompletion();
51+
return $this->handler->runCompletion();
4552
}
4653

4754
}
4855

4956

5057
**Command-specific argument completion with an array:**
51-
52-
new Completion(
53-
'walk', 'direction', Completion::TYPE_ARGUMENT,
54-
array('north', 'east', 'south', 'west')
55-
)
58+
59+
$this->handler->addHandler(
60+
new Completion(
61+
'walk', 'direction', Completion::TYPE_ARGUMENT,
62+
array('north', 'east', 'south', 'west')
63+
)
64+
);
5665

5766
This will complete for this:
5867

@@ -65,16 +74,18 @@ but not this:
6574

6675
**Non-command-specific (global) argument completion with a function**
6776

68-
Completion::makeGlobalHandler(
69-
'direction', Completion::TYPE_ARGUMENT,
70-
function(){
71-
$values = array();
77+
$this->handler->addHandler(
78+
Completion::makeGlobalHandler(
79+
'direction', Completion::TYPE_ARGUMENT,
80+
function(){
81+
$values = array();
7282

73-
// Fill the array up with stuff
83+
// Fill the array up with stuff
7484

75-
return $values;
76-
}
77-
)
85+
return $values;
86+
}
87+
)
88+
);
7889

7990
This will complete for both commands:
8091

@@ -86,16 +97,29 @@ This will complete for both commands:
8697

8798
Option handlers work the same way as argument handlers, except you use `Completion::TYPE_OPTION` for the type..
8899

89-
Completion::makeGlobalHandler(
90-
'weather', Completion::TYPE_OPTION,
91-
array('raining', 'sunny', 'everything is on fire!')
92-
)
100+
$this->handler->addHandler(
101+
Completion::makeGlobalHandler(
102+
'weather', Completion::TYPE_OPTION,
103+
array('raining', 'sunny', 'everything is on fire!')
104+
)
105+
);
106+
93107

94-
Option completion is only supported for non-quoted values currently:
108+
### Example: completing references from a Git repository
95109

96-
myapp walk -w [tab]
97-
myapp walk --weather [tab]
110+
Completion::makeGlobalHandler(
111+
'ref',
112+
Completion::TYPE_OPTION,
113+
function () {
114+
$raw = shell_exec('git show-ref --abbr');
115+
if (preg_match_all('/refs\/(?:heads|tags)?\/?(.*)/', $raw, $matches)) {
116+
return $matches[1];
117+
}
118+
}
119+
)
98120

99-
## Notes
121+
## Behaviour notes
100122

101-
* Option shorcuts are not offered as completion options, however requesting completion (ie. pressing tab) on a valid option shortcut will complete.
123+
* Option shortcuts are not offered as completion options, however requesting completion (ie. pressing tab) on a valid option shortcut will complete.
124+
* 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.
125+
* There is currently no way to hand-off to BASH to complete folder/file names.

src/CompletionCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ protected function configure()
3232
END
3333
)
3434
->addOption(
35-
'genhook',
35+
'generate-hook',
3636
'g',
3737
InputOption::VALUE_NONE,
38-
'Generate BASH script to use completion with this application.'
38+
'Generate BASH code that sets up completion for this application.'
3939
)
4040
->addOption(
4141
'program',
4242
'p',
4343
InputOption::VALUE_REQUIRED,
44-
'Program name to add completion for.'
44+
"Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
4545
);
4646
}
4747

@@ -50,7 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5050
$this->handler = new CompletionHandler( $this->getApplication() );
5151
$handler = $this->handler;
5252

53-
if ( $input->getOption('genhook') ) {
53+
if ( $input->getOption('generate-hook') ) {
5454
$output->write( $handler->generateBashCompletionHook($input->getOption('program')), true );
5555
} else {
5656
$handler->setContext(new EnvironmentCompletionContext());

0 commit comments

Comments
 (0)