getWords() public method

Get the contents of the command line, exploded into words based on the configured word break characters
public getWords ( ) : array
return array
 public function testWordBreakingWithSmallInputs()
 {
     $context = new CompletionContext();
     // Cursor at the end of a word and not in the following space has no effect
     $context->setCommandLine('cmd a');
     $context->setCharIndex(5);
     $this->assertEquals(array('cmd', 'a'), $context->getWords());
     $this->assertEquals(1, $context->getWordIndex());
     $this->assertEquals('a', $context->getCurrentWord());
     // As above, but in the middle of the command line string
     $context->setCommandLine('cmd a');
     $context->setCharIndex(3);
     $this->assertEquals(array('cmd', 'a'), $context->getWords());
     $this->assertEquals(0, $context->getWordIndex());
     $this->assertEquals('cmd', $context->getCurrentWord());
     // Cursor at the end of the command line with a space appends an empty word
     $context->setCommandLine('cmd   a ');
     $context->setCharIndex(8);
     $this->assertEquals(array('cmd', 'a', ''), $context->getWords());
     $this->assertEquals(2, $context->getWordIndex());
     $this->assertEquals('', $context->getCurrentWord());
     // Cursor in break space before a word appends an empty word in that position
     $context->setCommandLine('cmd a');
     $context->setCharIndex(4);
     $this->assertEquals(array('cmd', '', 'a'), $context->getWords());
     $this->assertEquals(1, $context->getWordIndex());
     $this->assertEquals('', $context->getCurrentWord());
 }
Beispiel #2
0
 protected function createInputFromContext(CompletionContext $context)
 {
     $words = $context->getWords();
     $words = array_filter($words);
     $this->mergeApplicationDefinition();
     return new ArgvInput($words, $this->getDefinition());
 }
 public function testWordBreakSplit()
 {
     $context = new CompletionContext();
     $context->setCommandLine('console  config:application --direction="west" --with-bruce --repeat 3');
     // Cursor at the end of the first word
     $context->setCharIndex(7);
     $words = $context->getWords();
     $this->assertEquals(array('console', 'config:application', '--direction', 'west', '--with-bruce', '--repeat', '3'), $words);
 }
 /**
  * Step through the command line to determine which word positions represent which argument values
  *
  * The word indexes of argument values are found by eliminating words that are known to not be arguments (options,
  * option values, and command names). Any word that doesn't match for elimination is assumed to be an argument value,
  *
  * @param InputArgument[] $argumentDefinitions
  * @return array as [argument name => word index on command line]
  */
 protected function mapArgumentsToWords($argumentDefinitions)
 {
     $argumentPositions = array();
     $argumentNumber = 0;
     $previousWord = null;
     $argumentNames = array_keys($argumentDefinitions);
     // Build a list of option values to filter out
     $optionsWithArgs = $this->getOptionWordsWithValues();
     foreach ($this->context->getWords() as $wordIndex => $word) {
         // Skip program name, command name, options, and option values
         if ($wordIndex < 2 || $word && '-' === $word[0] || in_array($previousWord, $optionsWithArgs)) {
             $previousWord = $word;
             continue;
         } else {
             $previousWord = $word;
         }
         // If argument n exists, pair that argument's name with the current word
         if (isset($argumentNames[$argumentNumber])) {
             $argumentPositions[$wordIndex] = $argumentNames[$argumentNumber];
         }
         $argumentNumber++;
     }
     return $argumentPositions;
 }