12 namespace Symfony\Component\Console\Input;
57 $argv = $_SERVER[
'argv'];
63 $this->tokens = $argv;
80 while (null !== $token = array_shift($this->parsed)) {
81 if ($parseOptions &&
'' == $token) {
83 }
elseif ($parseOptions &&
'--' == $token) {
84 $parseOptions =
false;
85 }
elseif ($parseOptions && 0 === strpos($token,
'--')) {
87 }
elseif ($parseOptions &&
'-' === $token[0] &&
'-' !== $token) {
102 $name = substr($token, 1);
104 if (strlen($name) > 1) {
105 if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
125 $len = strlen($name);
126 for ($i = 0; $i < $len; ++$i) {
127 if (!$this->definition->hasShortcut($name[$i])) {
128 throw new \RuntimeException(sprintf(
'The "-%s" option does not exist.', $name[$i]));
131 $option = $this->definition->getOptionForShortcut($name[$i]);
132 if ($option->acceptValue()) {
133 $this->
addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
149 $name = substr($token, 2);
151 if (
false !== $pos = strpos($name,
'=')) {
152 $this->
addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
167 $c = count($this->arguments);
170 if ($this->definition->hasArgument($c)) {
171 $arg = $this->definition->getArgument($c);
172 $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
175 }
elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
176 $arg = $this->definition->getArgument($c - 1);
177 $this->arguments[$arg->getName()][] = $token;
181 throw new \RuntimeException(
'Too many arguments.');
195 if (!$this->definition->hasShortcut($shortcut)) {
196 throw new \RuntimeException(sprintf(
'The "-%s" option does not exist.', $shortcut));
199 $this->
addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
212 if (!$this->definition->hasOption($name)) {
213 throw new \RuntimeException(sprintf(
'The "--%s" option does not exist.', $name));
216 $option = $this->definition->getOption($name);
219 if (!isset($value[0])) {
223 if (null !== $value && !$option->acceptValue()) {
224 throw new \RuntimeException(sprintf(
'The "--%s" option does not accept a value.', $name));
227 if (null === $value && $option->acceptValue() && count($this->parsed)) {
230 $next = array_shift($this->parsed);
231 if (isset($next[0]) &&
'-' !== $next[0]) {
236 array_unshift($this->parsed, $next);
240 if (null === $value) {
241 if ($option->isValueRequired()) {
242 throw new \RuntimeException(sprintf(
'The "--%s" option requires a value.', $name));
245 if (!$option->isArray()) {
246 $value = $option->isValueOptional() ? $option->getDefault() :
true;
250 if ($option->isArray()) {
251 $this->options[$name][] = $value;
253 $this->options[$name] = $value;
264 foreach ($this->tokens as $token) {
265 if ($token &&
'-' === $token[0]) {
285 $values = (array) $values;
287 foreach ($this->tokens as $token) {
288 foreach ($values as $value) {
289 if ($token === $value || 0 === strpos($token, $value.
'=')) {
311 $values = (array) $values;
317 foreach ($values as $value) {
318 if ($token === $value || 0 === strpos($token, $value.
'=')) {
319 if (
false !== $pos = strpos($token,
'=')) {
320 return substr($token, $pos + 1);
339 $tokens = array_map(
function ($token) use ($self) {
340 if (preg_match(
'{^(-[^=]+=)(.+)}', $token, $match)) {
341 return $match[1].$self->escapeToken($match[2]);
344 if ($token && $token[0] !==
'-') {
345 return $self->escapeToken($token);