本文整理汇总了PHP中Symfony\Component\Console\Input\InputOption类的典型用法代码示例。如果您正苦于以下问题:PHP InputOption类的具体用法?PHP InputOption怎么用?PHP InputOption使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了InputOption类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getInputOptionDocument
/**
* @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: describeInputOption
/**
* {@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)) . '`');
}
示例3: getOptionMode
/**
* @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;
}
示例4: __construct
/**
* 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);
}
示例5: describeInputOption
/**
* {@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}`");
}
}
示例6: describeInputOption
/**
* {@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);
}
示例7: getInputOptionData
/**
* @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());
}
示例8: addOption
/**
* 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();
}
}
}
示例9: testEquals
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));
}
示例10: testSetDefault
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());
}
}
示例11: describeInputOption
/**
* {@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);
}
示例12: getQuestionString
/**
* 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());
}
示例13: completeOption
/**
* 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;
}
示例14: equals
/**
* 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();
}
示例15: inputOptionSetDescription
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;
}