Example #1
0
 protected function parseInput($line, InputInterface $input)
 {
     $input->reset();
     for ($size = strlen($line), $i = 0; $i < $size; $i++) {
         if (trim($line[$i]) === '') {
             continue;
         }
         if ($line[$i] == '-') {
             $i++;
             if ($i >= $size) {
                 throw new \InvalidArgumentException(sprintf('Invalid command: "%s"', $line));
             }
             if ($line[$i] != '-') {
                 if (preg_match("'[a-z]+'i", substr($line, $i), $m)) {
                     foreach (str_split($m[0], 1) as $flag) {
                         $input->setOption($flag, true);
                         $i++;
                     }
                 } else {
                     throw new \InvalidArgumentException('');
                 }
             } elseif (preg_match("'^([^-=\\s]+)(\\s*=\\s*)?'", substr($line, ++$i), $match)) {
                 $hasValue = isset($match[2]) && trim($match[2]) !== '';
                 $i += strlen($match[1]);
                 if ($hasValue) {
                     $i++;
                     if ($line[$i] == '"') {
                         if (preg_match("'\"((?:[^\"]|\\\")*)\"'", substr($line, $i), $m)) {
                             $input->setOption($match[1], $m[1]);
                             $i += strlen($m[0]);
                         } else {
                             throw new \InvalidArgumentException('');
                         }
                     } elseif (preg_match("'\\S+'", substr($line, $i), $m)) {
                         $input->setOption($match[1], $m[0]);
                         $i += strlen($m[0]);
                     } else {
                         $input->setOption($match[1], '');
                     }
                 } else {
                     $input->setOption($match[1], true);
                 }
             }
             continue;
         }
         if ($line[$i] == '"') {
             if (preg_match("'\"((?:[^\"]|\\\")*)\"'", substr($line, $i), $m)) {
                 $input->addArgument($m[1]);
                 $i += strlen($m[0]);
             } else {
                 throw new \InvalidArgumentException('');
             }
         } elseif (preg_match("'\\S+'", substr($line, $i), $m)) {
             $input->addArgument($m[0]);
             $i += strlen($m[0]);
         }
     }
 }