Skip to content

Commit 86d400d

Browse files
committed
Factor in option arguments, show application options as well as command options
1 parent 7b5a2a2 commit 86d400d

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/Stecman/Component/Symfony/Console/BashCompletion/CompletionHandler.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected function completeForOptionShortcuts()
155155
protected function completeForOptionValues()
156156
{
157157

158-
fwrite(STDERR, "\n".print_r($this->words, true)."\n\nIndex: $this->wordIndex\n\n");
158+
// fwrite(STDERR, "\n".print_r($this->words, true)."\n\nIndex: $this->wordIndex\n\n");
159159

160160
// if ($this->command && $this->wordIndex > 1) {
161161
// $left = $this->words[$this->wordIndex-1];
@@ -253,13 +253,12 @@ public function getInput()
253253
}
254254

255255
/**
256-
* @param BaseCommand $cmd
257256
* @return array
258257
*/
259-
protected function formatOptions(BaseCommand $cmd)
258+
protected function formatOptions()
260259
{
261260
$options = array();
262-
foreach ($cmd->getDefinition()->getOptions() as $opt) {
261+
foreach ($this->getAllOptions() as $opt) {
263262
$string = '--'.$opt->getName();
264263

265264
if ($opt->isValueRequired()) {
@@ -341,19 +340,32 @@ protected function mapArgumentsToWords($arguments)
341340

342341
$argsArray = array_keys($arguments);
343342

343+
$optionsWithArgs = array();
344+
345+
foreach ($this->getAllOptions() as $option) {
346+
if ($option->isValueRequired() && $option->getShortcut()) {
347+
$optionsWithArgs[] = '-'.$option->getShortcut();
348+
}
349+
}
350+
344351
foreach ($this->words as $word) {
345352
$wordNum++;
346353

347-
// Skip program name, command name and options
348-
if ($wordNum < 2 || ($word && '-' === $word[0])) {
354+
// Skip program name, command name, options, and option values
355+
if ($wordNum < 2
356+
|| ($word && '-' === $word[0])
357+
|| in_array($prevWord, $optionsWithArgs))
358+
{
359+
$prevWord = $word;
349360
continue;
361+
} else {
362+
$prevWord = $word;
350363
}
351364

352365
if (isset($argsArray[$argIndex])) {
353366
$argPositions[$argsArray[$argIndex]] = $wordNum;
354367
}
355368
$argIndex++;
356-
$prevWord = $word;
357369
}
358370

359371
return $argPositions;
@@ -412,6 +424,13 @@ function $funcName {
412424
END;
413425
}
414426

427+
protected function getAllOptions(){
428+
return array_merge(
429+
$this->command->getDefinition()->getOptions(),
430+
$this->application->getDefinition()->getOptions()
431+
);
432+
}
433+
415434
/**
416435
* @return array|string
417436
*/

0 commit comments

Comments
 (0)