/** * 添加一个option * * @param string $name * @param string $value * @throws RuntimeException */ protected function addNewOption($name, $value) { $option = $this->definition->getOption($name); if (is_null($value)) { if ($option->isValueRequired()) { $value = $this->getNextToken(); if (!empty($value)) { array_shift($this->tokens); } else { throw new RuntimeException(sprintf('The "%s" option requires a value.', $option->isShort() ? "-{$name}" : "--{$name}")); } } elseif ($option->isValueOptional()) { $value = $option->getDefault(); } elseif ($option->isValueNone()) { $value = true; } } else { if ($option->isValueNone()) { throw new RuntimeException(sprintf('The "%s" option does not accept a value.', $option->isShort() ? "-{$name}" : "--{$name}")); } } $this->options[$name] = $value; }
/** * 合并另外一个Definition * * @param Definition $definition * @return \Slince\Console\Context\Definition */ function merge(Definition $definition) { foreach ($definition->getArguments() as $argument) { $this->addArgument($argument); } foreach ($definition->getOptions() as $option) { $this->addOption($option); } return $this; }
/** * 给command添加argument * * @param string $name * @param int $valueMode * @param string $description * @param string $default * @return \Slince\Console\Command */ function addArgument($name, $valueMode, $description = null, $default = null) { $this->definition->addArgument(new Argument($name, $valueMode, $description, $default)); return $this; }