/**
  * @param InputOption $option
  *
  * @return \DOMDocument
  */
 private function getInputOptionDocument(InputOption $option)
 {
     $dom = new \DOMDocument('1.0', 'UTF-8');
     $dom->appendChild($objectXML = $dom->createElement('option'));
     $objectXML->setAttribute('name', '--' . $option->getName());
     $pos = strpos($option->getShortcut(), '|');
     if (false !== $pos) {
         $objectXML->setAttribute('shortcut', '-' . substr($option->getShortcut(), 0, $pos));
         $objectXML->setAttribute('shortcuts', '-' . implode('|-', explode('|', $option->getShortcut())));
     } else {
         $objectXML->setAttribute('shortcut', $option->getShortcut() ? '-' . $option->getShortcut() : '');
     }
     $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
     $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
     $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
     $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
     $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
     if ($option->acceptValue()) {
         $defaults = is_array($option->getDefault()) ? $option->getDefault() : (is_bool($option->getDefault()) ? array(var_export($option->getDefault(), true)) : ($option->getDefault() ? array($option->getDefault()) : array()));
         $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
         if (!empty($defaults)) {
             foreach ($defaults as $default) {
                 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
                 $defaultXML->appendChild($dom->createTextNode($default));
             }
         }
     }
     return $dom;
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 protected function describeInputOption(InputOption $option, array $options = array())
 {
     $name = '--' . $option->getName();
     if ($option->getShortcut()) {
         $name .= '|-' . implode('|-', explode('|', $option->getShortcut())) . '';
     }
     $this->write('#### `' . $name . '`' . "\n\n" . ($option->getDescription() ? preg_replace('/\\s*[\\r\\n]\\s*/', "\n", $option->getDescription()) . "\n\n" : '') . '* Accept value: ' . ($option->acceptValue() ? 'yes' : 'no') . "\n" . '* Is value required: ' . ($option->isValueRequired() ? 'yes' : 'no') . "\n" . '* Is multiple: ' . ($option->isArray() ? 'yes' : 'no') . "\n" . '* Default: `' . str_replace("\n", '', var_export($option->getDefault(), true)) . '`');
 }
 public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null, $question = null)
 {
     parent::__construct($name, $shortcut, $mode, $description, $default);
     $this->name = $name;
     $this->question = $question;
     $this->default = $default;
 }
예제 #4
0
 /**
  * @param InputOption $option
  * @return int
  */
 private function getOptionMode(InputOption $option)
 {
     $mode = 0;
     if ($option->isArray()) {
         $mode |= InputOption::VALUE_IS_ARRAY;
     }
     if ($option->isValueOptional()) {
         $mode |= InputOption::VALUE_OPTIONAL;
     }
     if ($option->isValueRequired()) {
         $mode |= InputOption::VALUE_REQUIRED;
     }
     if (!$mode) {
         $mode = InputOption::VALUE_NONE;
     }
     return $mode;
 }
예제 #5
0
파일: InputSwitch.php 프로젝트: kingsj/core
 /**
  * Constructor.
  *
  * @param string  $name        The option name
  * @param string  $description A description text
  *
  * @api
  */
 public function __construct($name, $description = '')
 {
     if ('--' === substr($name, 0, 2)) {
         $name = substr($name, 2);
     }
     $this->name = $name;
     $this->description = $description;
     parent::__construct($name, null, InputOption::VALUE_OPTIONAL, $description, null);
 }
예제 #6
0
 public function __construct($config_path, $name, $shortcut = null, $mode = null, $description = '', $default = null, $required = true, $callback = null)
 {
     parent::__construct($name, $shortcut, $mode, $description, $default);
     if (empty($config_path)) {
         throw new \InvalidArgumentException(sprintf('The config option "%s" is not mapped to a configuration parameter.', $name));
     }
     $this->config_path = $config_path;
     $this->required = $required;
     $this->callback = $callback;
 }
 /**
  * {@inheritdoc}
  */
 protected function describeInputOption(InputOption $option, array $options = [])
 {
     $this->write('* **`--' . $option->getName() . "`**");
     if ($shortcut = $option->getShortcut()) {
         $this->write(" (`-" . implode('|-', explode('|', $shortcut)) . "`)");
     }
     $notes = [];
     if ($option->isArray()) {
         $notes[] = 'multiple values allowed';
     } elseif ($option->acceptValue()) {
         $notes[] = 'expects a value';
     }
     if ($notes) {
         $this->write(' (' . implode('; ', $notes) . ')');
     }
     if ($description = $option->getDescription()) {
         $this->write("  \n  " . $description);
     }
     if ($option->acceptValue() && $option->getDefault()) {
         $default = var_export($option->getDefault(), true);
         $this->write("  \n  Default: `{$default}`");
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function describeInputOption(InputOption $option, array $options = array())
 {
     return $this->output(array('name' => '--' . $option->getName(), 'shortcut' => $option->getShortcut() ? '-' . implode('|-', explode('|', $option->getShortcut())) : '', 'accept_value' => $option->acceptValue(), 'is_value_required' => $option->isValueRequired(), 'is_multiple' => $option->isArray(), 'description' => $option->getDescription(), 'default' => $option->getDefault()), $options);
 }
예제 #9
0
 /**
  * @param InputOption $option
  *
  * @return array
  */
 private function getInputOptionData(InputOption $option)
 {
     return array('name' => '--' . $option->getName(), 'shortcut' => $option->getShortcut() ? '-' . implode('|-', explode('|', $option->getShortcut())) : '', 'accept_value' => $option->acceptValue(), 'is_value_required' => $option->isValueRequired(), 'is_multiple' => $option->isArray(), 'description' => preg_replace('/\\s*\\R\\s*/', ' ', $option->getDescription()), 'default' => $option->getDefault());
 }
예제 #10
0
 /**
  * Adds an InputOption object.
  *
  * @param InputOption $option An InputOption object
  *
  * @throws LogicException When option given already exist
  *
  * @api
  */
 public function addOption(InputOption $option)
 {
     if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
         throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
     }
     if ($option->getShortcut()) {
         foreach (explode('|', $option->getShortcut()) as $shortcut) {
             if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
                 throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
             }
         }
     }
     $this->options[$option->getName()] = $option;
     if ($option->getShortcut()) {
         foreach (explode('|', $option->getShortcut()) as $shortcut) {
             $this->shortcuts[$shortcut] = $option->getName();
         }
     }
 }
예제 #11
0
 public function testEquals()
 {
     $option = new InputOption('foo', 'f', null, 'Some description');
     $option2 = new InputOption('foo', 'f', null, 'Alternative description');
     $this->assertTrue($option->equals($option2));
     $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
     $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description', true);
     $this->assertFalse($option->equals($option2));
     $option = new InputOption('foo', 'f', null, 'Some description');
     $option2 = new InputOption('bar', 'f', null, 'Some description');
     $this->assertFalse($option->equals($option2));
     $option = new InputOption('foo', 'f', null, 'Some description');
     $option2 = new InputOption('foo', '', null, 'Some description');
     $this->assertFalse($option->equals($option2));
     $option = new InputOption('foo', 'f', null, 'Some description');
     $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
     $this->assertFalse($option->equals($option2));
 }
 public function testSetDefault()
 {
     $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
     $option->setDefault(null);
     $this->assertNull($option->getDefault(), '->setDefault() can reset the default value by passing null');
     $option->setDefault('another');
     $this->assertEquals('another', $option->getDefault(), '->setDefault() changes the default value');
     $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY);
     $option->setDefault(array(1, 2));
     $this->assertEquals(array(1, 2), $option->getDefault(), '->setDefault() changes the default value');
     $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
     try {
         $option->setDefault('default');
         $this->fail('->setDefault() throws an Exception if you give a default value for a VALUE_NONE option');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\Exception', $e, '->setDefault() throws an Exception if you give a default value for a VALUE_NONE option');
         $this->assertEquals('Cannot set a default value when using Option::VALUE_NONE mode.', $e->getMessage());
     }
     $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
     try {
         $option->setDefault('default');
         $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a VALUE_IS_ARRAY option');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\Exception', $e, '->setDefault() throws an Exception if you give a default value which is not an array for a VALUE_IS_ARRAY option');
         $this->assertEquals('A default value for an array option must be an array.', $e->getMessage());
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function describeInputOption(InputOption $option, array $options = array())
 {
     if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
         $default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($option->getDefault()));
     } else {
         $default = '';
     }
     $nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($option->getName());
     $nameWithShortcutWidth = $nameWidth - strlen($option->getName()) - 2;
     $this->writeText(sprintf(" <info>%s</info> %-{$nameWithShortcutWidth}s%s%s%s", '--' . $option->getName(), $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', str_replace("\n", "\n" . str_repeat(' ', $nameWidth + 2), $option->getDescription()), $default, $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''), $options);
 }
예제 #14
0
 /**
  * Take an option and return a question string
  * @param \Symfony\Component\Console\Input\InputOption $option
  * @param $default
  * @return string
  */
 private function getQuestionString(InputOption $option, $default)
 {
     if ($default) {
         return sprintf("%s? [Default: <options=bold>%s</>]: ", $option->getDescription(), $default);
     }
     return sprintf("%s?: ", $option->getDescription());
 }
 /**
  * Complete the value for the given option if a value completion is availble
  *
  * @param InputOption $option
  * @return array|false
  */
 protected function completeOption(InputOption $option)
 {
     if ($helper = $this->getCompletionHelper($option->getName(), Completion::TYPE_OPTION)) {
         return $helper->run();
     }
     if ($this->command instanceof CompletionAwareInterface) {
         return $this->command->completeOptionValues($option->getName(), $this->context);
     }
     return false;
 }
예제 #16
0
파일: InputOption.php 프로젝트: saj696/pipe
 /**
  * Checks whether the given option equals this one.
  *
  * @param InputOption $option option to compare
  *
  * @return bool
  */
 public function equals(InputOption $option)
 {
     return $option->getName() === $this->getName() && $option->getShortcut() === $this->getShortcut() && $option->getDefault() === $this->getDefault() && $option->isArray() === $this->isArray() && $option->isValueRequired() === $this->isValueRequired() && $option->isValueOptional() === $this->isValueOptional();
 }
 protected static function inputOptionSetDescription($inputOption, $description)
 {
     // Recover the 'mode' value, because Symfony is stubborn
     $mode = 0;
     if ($inputOption->isValueRequired()) {
         $mode |= InputOption::VALUE_REQUIRED;
     }
     if ($inputOption->isValueOptional()) {
         $mode |= InputOption::VALUE_OPTIONAL;
     }
     if ($inputOption->isArray()) {
         $mode |= InputOption::VALUE_IS_ARRAY;
     }
     if (!$mode) {
         $mode = InputOption::VALUE_NONE;
     }
     $inputOption = new InputOption($inputOption->getName(), $inputOption->getShortcut(), $mode, $description, $inputOption->getDefault());
     return $inputOption;
 }
 /**
  * Constructor
  *
  * @param string $name
  * @param string $frontendType
  * @param int $mode
  * @param string $configPath
  * @param string $description
  * @param string|array|null $defaultValue
  * @param string|array|null $shortcut
  */
 public function __construct($name, $frontendType, $mode, $configPath, $description = '', $defaultValue = null, $shortcut = null)
 {
     $this->frontendType = $frontendType;
     $this->configPath = $configPath;
     parent::__construct($name, $shortcut, $mode, $description, $defaultValue);
 }
예제 #19
0
 private function createQuestionForOption(InputOption $option)
 {
     $question = new Question($option->getDescription() . ': ');
     return $question;
 }
예제 #20
0
 /**
  * {@inheritdoc}
  */
 protected function describeInputOption(InputOption $option, array $options = array())
 {
     if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
         $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault()));
     } else {
         $default = '';
     }
     $value = '';
     if ($option->acceptValue()) {
         $value = '=' . strtoupper($option->getName());
         if ($option->isValueOptional()) {
             $value = '[' . $value . ']';
         }
     }
     $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions(array($option));
     $synopsis = sprintf('%s%s', $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : '    ', sprintf('--%s%s', $option->getName(), $value));
     $spacingWidth = $totalWidth - strlen($synopsis) + 2;
     $this->writeText(sprintf('  <info>%s</info>%s%s%s%s', $synopsis, str_repeat(' ', $spacingWidth), preg_replace('/\\s*[\\r\\n]\\s*/', "\n" . str_repeat(' ', $totalWidth + 17), $option->getDescription()), $default, $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''), $options);
 }
예제 #21
0
 /**
  * {@inheritdoc}
  */
 protected function describeInputOption(InputOption $option, array $options = array())
 {
     $this->write('**' . $option->getName() . ':**' . "\n\n" . '* Name: `--' . $option->getName() . '`' . "\n" . '* Shortcut: ' . ($option->getShortcut() ? '`-' . implode('|-', explode('|', $option->getShortcut())) . '`' : '<none>') . "\n" . '* Accept value: ' . ($option->acceptValue() ? 'yes' : 'no') . "\n" . '* Is value required: ' . ($option->isValueRequired() ? 'yes' : 'no') . "\n" . '* Is multiple: ' . ($option->isArray() ? 'yes' : 'no') . "\n" . '* Description: ' . preg_replace('/\\s*\\R\\s*/', PHP_EOL . '  ', $option->getDescription() ?: '<none>') . "\n" . '* Default: `' . str_replace("\n", '', var_export($option->getDefault(), true)) . '`');
 }
예제 #22
0
 /**
  * {@inheritdoc}
  *
  * @codeCoverageIgnore
  */
 public function getDescription()
 {
     return $this->description ?: parent::getDescription();
 }