getOptions() public method

Returns all options of the format.
public getOptions ( boolean $includeBase = true ) : Option[]
$includeBase boolean Whether to include options of the base format in the result.
return Option[] The options.
Esempio n. 1
0
 /**
  * Returns all options added to the builder.
  *
  * @param bool $includeBase Whether to include options of the base format
  *                          in the result.
  *
  * @return Option[] The options.
  */
 public function getOptions($includeBase = true)
 {
     Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s');
     $options = $this->options;
     if ($includeBase && $this->baseFormat) {
         // append base options
         $options = array_replace($options, $this->baseFormat->getOptions());
     }
     return $options;
 }
Esempio n. 2
0
 /**
  * Returns all options.
  *
  * By default, this method also includes the default values set for options
  * with values. You can disable this behavior by passing `false` for
  * `$includeDefaults`.
  *
  * @param bool $includeDefaults Whether to return the default values for
  *                              options that were not set.
  *
  * @return array The option values and `true`/`false` for options without
  *               values.
  *
  * @see getOption()
  */
 public function getOptions($includeDefaults = true)
 {
     $options = $this->options;
     if ($includeDefaults) {
         foreach ($this->format->getOptions() as $option) {
             $name = $option->getLongName();
             if (!array_key_exists($name, $options)) {
                 $options[$name] = $option->acceptsValue() ? $option->getDefaultValue() : false;
             }
         }
     }
     return $options;
 }
 /**
  * Creates a new adapter.
  *
  * @param ArgsFormat $format The adapted format.
  */
 public function __construct(ArgsFormat $format)
 {
     parent::__construct();
     $i = 1;
     foreach ($format->getCommandNames() as $commandName) {
         do {
             $argName = 'cmd' . $i++;
         } while ($format->hasArgument($argName));
         $this->addArgument($argument = $this->adaptCommandName($commandName, $argName));
         $this->commandNames[$argument->getName()] = $commandName;
     }
     foreach ($format->getCommandOptions() as $commandOption) {
         $this->addOption($this->adaptCommandOption($commandOption));
     }
     foreach ($format->getOptions() as $option) {
         $this->addOption($this->adaptOption($option));
     }
     foreach ($format->getArguments() as $argument) {
         $this->addArgument($this->adaptArgument($argument));
     }
 }
Esempio n. 4
0
 /**
  * Renders the synopsis of a console command.
  *
  * @param BlockLayout $layout       The layout.
  * @param ArgsFormat  $argsFormat   The console arguments to render.
  * @param string      $appName      The name of the application binary.
  * @param string      $prefix       The prefix to insert.
  * @param bool        $lastOptional Set to `true` if the last command of the
  *                                  console arguments is optional. This
  *                                  command will be enclosed in brackets in
  *                                  the output.
  */
 protected function renderSynopsis(BlockLayout $layout, ArgsFormat $argsFormat, $appName, $prefix = '', $lastOptional = false)
 {
     $nameParts = array();
     $argumentParts = array();
     $nameParts[] = '<u>' . ($appName ?: 'console') . '</u>';
     foreach ($argsFormat->getCommandNames() as $commandName) {
         $nameParts[] = '<u>' . $commandName->toString() . '</u>';
     }
     foreach ($argsFormat->getCommandOptions() as $commandOption) {
         $nameParts[] = $commandOption->isLongNamePreferred() ? '--' . $commandOption->getLongName() : '-' . $commandOption->getShortName();
     }
     if ($lastOptional) {
         $lastIndex = count($nameParts) - 1;
         $nameParts[$lastIndex] = '[' . $nameParts[$lastIndex] . ']';
     }
     foreach ($argsFormat->getOptions(false) as $option) {
         // \xC2\xA0 is a non-breaking space
         if ($option->isValueRequired()) {
             $format = "%s <%s>";
         } elseif ($option->isValueOptional()) {
             $format = "%s [<%s>]";
         } else {
             $format = '%s';
         }
         $optionName = $option->isLongNamePreferred() ? '--' . $option->getLongName() : '-' . $option->getShortName();
         $argumentParts[] = sprintf('[' . $format . ']', $optionName, $option->getValueName());
     }
     foreach ($argsFormat->getArguments() as $argument) {
         $argName = $argument->getName();
         $argumentParts[] = sprintf($argument->isRequired() ? '<%s>' : '[<%s>]', $argName . ($argument->isMultiValued() ? '1' : ''));
         if ($argument->isMultiValued()) {
             $argumentParts[] = sprintf('... [<%sN>]', $argName);
         }
     }
     $argsOpts = implode(' ', $argumentParts);
     $name = implode(' ', $nameParts);
     $layout->add(new LabeledParagraph($prefix . $name, $argsOpts, 1, false));
 }
Esempio n. 5
0
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testGetOptionsFailsIfIncludeBaseNoBoolean()
 {
     $format = new ArgsFormat();
     $format->getOptions(1234);
 }