Gets the InputDefinition attached to this Command.
public getDefinition ( ) : Symfony\Component\Console\Input\InputDefinition | ||
리턴 | Symfony\Component\Console\Input\InputDefinition | An InputDefinition instance |
/** * @param Command $command * @param InputInterface $input */ protected function addOptionsToCommand(Command $command, InputInterface $input) { $inputDefinition = $command->getApplication()->getDefinition(); $inputDefinition->addOption(new InputOption(self::DISABLE_OPTIONAL_LISTENERS, null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, sprintf('Disable optional listeners, "%s" to disable all listeners, ' . 'command "%s" shows all listeners', self::ALL_OPTIONAL_LISTENERS_VALUE, OptionalListenersCommand::NAME))); $command->mergeApplicationDefinition(); $input->bind($command->getDefinition()); }
/** * Strip a command's options from an argv array. * * @param string[] $args * @param Command $command * * @return string[] */ protected function stripOptions(array $args, Command $command) { $definition = $command->getDefinition(); foreach ($args as $key => $arg) { // Only consider options. if ($arg[0] !== '-') { continue; } // Look up the option. If it exists in the command definition, // remove it from the $args array. $argAsOption = preg_replace('/^\\-+([^=]+).*/', '$1', $arg); if ($definition->hasOption($argAsOption)) { $option = $definition->getOption($argAsOption); } else { try { $option = $definition->getOptionForShortcut($argAsOption); } catch (\InvalidArgumentException $e) { continue; } } // Unset the option. unset($args[$key]); // Unset the option's value too. if ($option->acceptValue() && isset($args[$key + 1]) && !strpos($arg, '=') && $args[$key + 1][0] !== '-') { unset($args[$key + 1]); } } return $args; }
private function updateCommandDefinition(Command $command, OutputInterface $output) { $eventDispatcher = $this->getApplication()->getDispatcher(); $input = new ArrayInput(['command' => $command->getName()]); $event = new ConsoleCommandEvent($command, $input, $output); $eventDispatcher->dispatch(GushEvents::DECORATE_DEFINITION, $event); $command->getSynopsis(true); $command->getSynopsis(false); $command->mergeApplicationDefinition(); try { $input->bind($command->getDefinition()); } catch (\Exception $e) { $output->writeln('<error>Something went wrong: </error>' . $e->getMessage()); return; } $eventDispatcher->dispatch(GushEvents::INITIALIZE, $event); // The options were set on the input but now we need to set them on the Command definition if ($options = $input->getOptions()) { foreach ($options as $name => $value) { $option = $command->getDefinition()->getOption($name); if ($option->acceptValue()) { $option->setDefault($value); } } } }
/** * {@inheritDoc} */ protected function configure() { if ($this->isVersionCompatible()) { $this->command = $this->createCommand(); $this->setHelp($this->command->getHelp()); $this->setDefinition($this->command->getDefinition()); $this->setDescription($this->command->getDescription()); } $this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command'); }
/** * Get the combined options of the application and entered command * * @return InputOption[] */ protected function getAllOptions() { if (!$this->command) { return $this->application->getDefinition()->getOptions(); } return array_merge($this->command->getDefinition()->getOptions(), $this->application->getDefinition()->getOptions()); }
/** * Applies parameters that are defined in the extra section of composer.json. * * If the Input object already has a value for the argument or parameter then the value in composer is ignored. * * @param Command $command * @param InputInterface $input * * @return $this * @throws MysqlVersionControlException */ public function applyComposerParams(Command $command, InputInterface $input) { $params = $this->getComposerParams($input->getArgument("env")); $definition = $command->getDefinition(); foreach ($this->filterComposerParams($params, $definition) as $param => $value) { if (0 === strpos($param, "--")) { // option $option = substr($param, 2); $Option = $definition->getOption($option); if (!$Option->acceptValue() && false === $input->getOption($option)) { $input->setOption($option, null); } elseif ($Option->acceptValue() && $Option->getDefault() === $input->getOption($option)) { if ($Option->isArray()) { $input->setOption($option, is_array($value) ? $value : [$value]); } elseif (is_array($value)) { throw new MysqlVersionControlException("The '{$option}' option does not accept arrays. Check your composer.json"); } else { $input->setOption($option, $value); } } } else { // argument $argument = $definition->getArgument($param); if ($argument->getDefault() === $input->getArgument($param)) { $input->setArgument($param, $value); } } } return $this; }
function let(CommandCollection $collection, Command $command1, Command $command2, InputDefinition $inputDefinition, InputDefinition $inputDefinition2, InputOption $inputOption, InputOption $inputOption2) { $inputDefinition->getOptions()->willReturn(['option1' => $inputOption, 'option2' => $inputOption2]); $command1->getName()->willReturn('test:command'); $command1->getDefinition()->willReturn($inputDefinition); $inputDefinition2->getOptions()->willReturn([]); $command2->getName()->willReturn('other-command'); $command2->getDefinition()->willReturn($inputDefinition2); $collection->getItems()->willReturn(['test:command' => $command1, 'other-command' => $command2]); $this->beConstructedWith($collection); }
public function integrate(Command $cmd) { $cmd->setName("metro:" . $cmd->getName()); $definition = $cmd->getDefinition(); $options = $definition->getOptions(); if (isset($options['short'])) { // remove -s shortcut (already used by Symfony's --shell option) $shortOption = $options['short']; $options['short'] = new InputOption('short', null, InputOption::VALUE_NONE, $shortOption->getDescription()); $definition->setOptions($options); } }
protected function getInput(Command $command, array $arguments = [], array $options = []) { $input = new ArrayInput([]); $input->bind($command->getDefinition()); foreach ($arguments as $key => $value) { $input->setArgument($key, $value); } foreach ($options as $key => $value) { $input->setOption($key, $value); } return $input; }
private function getCommandParams(Command $command) { $elements = array(); $definition = $command->getDefinition(); foreach ($definition->getArguments() as $argument) { /** @var $option \Symfony\Component\Console\Input\Inputargument */ $elements[] = sprintf($argument->isRequired() ? '%s' : '[%s[="%s"]]', $argument->getName(), $argument->getDefault()); } foreach ($definition->getOptions() as $option) { /** @var $option \Symfony\Component\Console\Input\Inputoption */ $shortcut = $option->getshortcut() ? sprintf('-%s|', $option->getshortcut()) : ''; $elements[] = sprintf('[%s--%s[="%s"]]', $shortcut, $option->getname(), $option->getdefault()); } return implode(' ', $elements); }
/** * Gets the InputDefinition attached to this Command. * * @return InputDefinition An InputDefinition instance * * @api */ public function getDefinition() { $definition = parent::getDefinition(); if (!$definition) { $definition = new InputDefinition(); if ($this->default_generator) { $generator = $this->getDefaultGenerator(); $generator->addDefinition($definition); } else { foreach ($this->generator_list as $generator) { $generator->addDefinition($definition); } } $this->setDefinition($definition); } return $definition; }
/** * @see Behat\Behat\Console\Configuration\ProcessorInterface::configure() */ public function configure(Command $command) { $defaultFormatters = FormatManager::getDefaultFormatterClasses(); $defaultLanguage = null; if (($locale = getenv('LANG')) && preg_match('/^([a-z]{2})/', $locale, $matches)) { $defaultLanguage = $matches[1]; } $command->addOption('--format', '-f', InputOption::VALUE_REQUIRED, "How to format features. <comment>pretty</comment> is default.\n" . "Default formatters are:\n" . implode("\n", array_map(function ($name) use($defaultFormatters) { $class = $defaultFormatters[$name]; return "- <comment>{$name}</comment>: " . $class::getDescription(); }, array_keys($defaultFormatters))) . "\n" . "Can use multiple formats at once (splitted with \"<comment>,</comment>\")")->addOption('--out', null, InputOption::VALUE_REQUIRED, "Write formatter output to a file/directory\n" . "instead of STDOUT <comment>(output_path)</comment>.")->addOption('--lang', null, InputOption::VALUE_REQUIRED, 'Print formatter output in particular language.', $defaultLanguage); $definition = $command->getDefinition(); $definition->addOption(new InputSwitch('--[no-]ansi', "Whether or not to use ANSI color in the output.\n" . "Behat decides based on your platform and the output\n" . "destination if not specified.")); $definition->addOption(new InputSwitch('--[no-]time', "Whether or not to show timer in output.")); $definition->addOption(new InputSwitch('--[no-]paths', "Whether or not to print sources paths.")); $definition->addOption(new InputSwitch('--[no-]snippets', "Whether or not to print snippets for undefined steps.")); $definition->addOption(new InputSwitch('--[no-]snippets-paths', "Whether or not to print details about undefined steps\n" . "in their snippets.")); $definition->addOption(new InputSwitch('--[no-]multiline', "Whether or not to print multiline arguments for steps.")); $definition->addOption(new InputSwitch('--[no-]expand', "Whether or not to expand scenario outline examples\n" . "tables.\n")); }
public function getDefinition() { return $this->innerCommand->getDefinition(); }
/** * Run a Symfony command. * * @param Command $command The command to run. * @param array $parameters An array of parameters to give to the command. * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * * @return int The command return code. */ protected function runCommand(Command $command, array $parameters, InputInterface $input, OutputInterface $output) { // add the command's name to the parameters array_unshift($parameters, $this->getName()); // merge the default parameters $extraParameters = ['--verbose' => $input->getOption('verbose')]; if ($command->getDefinition()->hasOption('schema-dir')) { $extraParameters['--schema-dir'] = $this->cacheDir; } if ($command->getDefinition()->hasOption('config-dir')) { $extraParameters['--config-dir'] = $this->cacheDir; } $parameters = array_merge($extraParameters, $parameters); if ($input->hasOption('platform')) { if ($platform = $input->getOption('platform') ?: $this->getPlatform()) { $parameters['--platform'] = $platform; } } $command->setApplication($this->getApplication()); // and run the sub-command return $command->run(new ArrayInput($parameters), $output); }
/** * {@inheritdoc} */ public function configure(SymfonyCommand $command) { $command->addOption(self::OPTION_PARALLEL_PROCESS, null, InputOption::VALUE_OPTIONAL, 'Max parallel processes amount', 1); $this->inputDefinition = $command->getDefinition(); }
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { $definition = $command->getDefinition(); $definition->addOption(new InputOption('gruver_file', 'g', InputOption::VALUE_OPTIONAL, 'Location of gruver.yml file')); return parent::doRunCommand($command, $input, $output); }
public function getCommandString(Command $command, $args = [], $options = []) { return $this->getConsole() . ' ' . $command->getName() . ' ' . $this->parseArgs($command->getDefinition()->getArguments(), $args) . ' ' . $this->parseOptions($command->getDefinition()->getOptions(), $options) . ' &> /dev/null &'; }
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { foreach ($command->getHelperSet() as $helper) { if ($helper instanceof InputAwareInterface) { $helper->setInput($input); } } if (null === $this->dispatcher) { return $command->run($input, $output); } try { $command->mergeApplicationDefinition(); $input->bind($command->getDefinition()); } catch (ExceptionInterface $e) { } $event = new ConsoleCommandEvent($command, $input, $output); $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event); if ($event->commandShouldRun()) { try { $exitCode = $command->run($input, $output); } catch (\Exception $e) { $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event); $e = $event->getException(); $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); throw $e; } } else { $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED; } $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); return $event->getExitCode(); }
/** * {@inheritdoc} */ public function getDefinition() { return $this->decoratedCommand->getDefinition(); }
/** * Runs the current command. * * If an event dispatcher has been attached to the application, * events are also dispatched during the life-cycle of the command. * * @param Command $command A Command instance * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * * @return int 0 if everything went fine, or an error code * * @throws \Exception when the command being run threw an exception */ protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { foreach ($command->getHelperSet() as $helper) { if ($helper instanceof InputAwareInterface) { $helper->setInput($input); } } if (null === $this->dispatcher) { return $command->run($input, $output); } // bind before the console.command event, so the listeners have access to input options/arguments try { $command->mergeApplicationDefinition(); $input->bind($command->getDefinition()); } catch (ExceptionInterface $e) { // ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition } $event = new ConsoleCommandEvent($command, $input, $output); $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event); if ($event->commandShouldRun()) { try { $exitCode = $command->run($input, $output); } catch (\Exception $e) { $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event); $e = $event->getException(); $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); throw $e; } } else { $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED; } $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); return $event->getExitCode(); }
/** * Create Command. * * @param string $expression * @param callable $callable * * @return \Symfony\Component\Console\Command\Command */ protected function createCommand(string $expression, callable $callable) : SymfonyCommand { $result = $this->expressionParser->parse($expression); $command = new SymfonyCommand($result['name']); $command->getDefinition()->addArguments($result['arguments']); $command->getDefinition()->addOptions($result['options']); $command->setCode($callable); return $command; }
/** * Adds a command object. * * If a command with the same name already exists, it will be overridden. * * @param Command $command A Command object * * @return Command The registered command * * @api */ public function add(Command $command) { $command->setApplication($this); if (!$command->isEnabled()) { $command->setApplication(null); return; } if (null === $command->getDefinition()) { throw new \LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command))); } $this->commands[$command->getName()] = $command; foreach ($command->getAliases() as $alias) { $this->commands[$alias] = $command; } return $command; }
protected function configure() { $this->setName($this->vagrant_ssh . ':' . $this->command->getName())->setDescription('Vagrant SSH ' . $this->command->getDescription())->setDefinition($this->command->getDefinition()); parent::configure(); }