You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+80-63Lines changed: 80 additions & 63 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,13 +12,13 @@ If you don't need any custom completion behaviour, all you need to do is add the
12
12
13
13
1. Install `stecman/symfony-console-completion` through composer
14
14
2. Add an instance of `CompletionCommand` to your application's `Application::getDefaultCommands()` method:
15
-
```
16
-
protected function getDefaultCommands()
17
-
{
18
-
...
19
-
$commands[] = new \Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand();
20
-
...
21
-
}
15
+
```php
16
+
protected function getDefaultCommands()
17
+
{
18
+
//...
19
+
$commands[] = new \Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand();
20
+
//...
21
+
}
22
22
```
23
23
24
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.
@@ -37,89 +37,106 @@ Custom completion behaviour for argument and option values can be added by sub-c
37
37
38
38
The following examples are for an application with this signature: `myapp (walk|run) [-w|--weather=""] direction`
39
39
40
-
class MyCompletionCommand extends CompletionCommand{
41
-
42
-
protected function runCompletion()
43
-
{
44
-
$this->handler->addHandlers(array(
45
-
46
-
// Instances of Completion go here.
47
-
// See below for examples.
48
-
49
-
));
50
-
51
-
return $this->handler->runCompletion();
52
-
}
40
+
```php
41
+
class MyCompletionCommand extends CompletionCommand {
53
42
43
+
protected function runCompletion()
44
+
{
45
+
$this->handler->addHandlers(array(
46
+
// Instances of Completion go here.
47
+
// See below for examples.
48
+
));
49
+
return $this->handler->runCompletion();
54
50
}
55
-
51
+
}
52
+
```
56
53
57
54
**Command-specific argument completion with an array:**
58
-
59
-
$this->handler->addHandler(
60
-
new Completion(
61
-
'walk', 'direction', Completion::TYPE_ARGUMENT,
62
-
array('north', 'east', 'south', 'west')
55
+
56
+
```php
57
+
$this->handler->addHandler(
58
+
new Completion(
59
+
'walk',
60
+
'direction',
61
+
Completion::TYPE_ARGUMENT,
62
+
array(
63
+
'north',
64
+
'east',
65
+
'south',
66
+
'west'
63
67
)
64
-
);
68
+
)
69
+
);
70
+
```
65
71
66
72
This will complete for this:
67
-
68
-
myapp walk [tab]
73
+
```bash
74
+
$ myapp walk [tab]
75
+
```
69
76
70
77
but not this:
71
-
72
-
myapp run [tab]
73
-
78
+
```bash
79
+
$ myapp run [tab]
80
+
```
74
81
75
82
**Non-command-specific (global) argument completion with a function**
76
83
77
-
$this->handler->addHandler(
78
-
Completion::makeGlobalHandler(
79
-
'direction', Completion::TYPE_ARGUMENT,
80
-
function(){
81
-
$values = array();
82
-
83
-
// Fill the array up with stuff
84
+
```php
85
+
$this->handler->addHandler(
86
+
Completion::makeGlobalHandler(
87
+
'direction',
88
+
Completion::TYPE_ARGUMENT,
89
+
function() {
90
+
$values = array();
84
91
85
-
return $values;
86
-
}
87
-
)
88
-
);
92
+
// Fill the array up with stuff
93
+
return $values;
94
+
}
95
+
)
96
+
);
97
+
```
89
98
90
99
This will complete for both commands:
91
-
92
-
myapp walk [tab]
93
-
myapp run [tab]
94
-
100
+
```bash
101
+
$ myapp walk [tab]
102
+
$ myapp run [tab]
103
+
```
95
104
96
105
**Option completion**
97
106
98
107
Option handlers work the same way as argument handlers, except you use `Completion::TYPE_OPTION` for the type..
99
108
100
-
$this->handler->addHandler(
101
-
Completion::makeGlobalHandler(
102
-
'weather', Completion::TYPE_OPTION,
103
-
array('raining', 'sunny', 'everything is on fire!')
109
+
```php
110
+
$this->handler->addHandler(
111
+
Completion::makeGlobalHandler(
112
+
'weather',
113
+
Completion::TYPE_OPTION,
114
+
array(
115
+
'raining',
116
+
'sunny',
117
+
'everything is on fire!'
104
118
)
105
-
);
106
-
119
+
)
120
+
);
121
+
```
107
122
108
123
### Example: completing references from a Git repository
109
124
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
-
}
125
+
```php
126
+
Completion::makeGlobalHandler(
127
+
'ref',
128
+
Completion::TYPE_OPTION,
129
+
function () {
130
+
$raw = shell_exec('git show-ref --abbr');
131
+
if (preg_match_all('/refs\/(?:heads|tags)?\/?(.*)/', $raw, $matches)) {
132
+
return $matches[1];
118
133
}
119
-
)
134
+
}
135
+
)
136
+
```
120
137
121
138
## Behaviour notes
122
139
123
140
* Option shortcuts are not offered as completion options, however requesting completion (ie. pressing tab) on a valid option shortcut will complete.
124
141
* 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.
142
+
* There is currently no way to hand-off to BASH to complete folder/file names.
0 commit comments