我找到了解決辦法...最有可能不是最好的,但它的工作原理
解決 - 複製類命令聲明它是抽象的,並讓它擴展舊命令 - 複製ContainerAwareCommand並讓它擴展你的新命令 - 將代碼添加到新命令中
public function notIgnoreValidationErrors()
{
$this->ignoreValidationErrors = false;
}
protected function configureAdditionalInput(InputInterface $input, OutputInterface $output)
{
}
-change的run()來
public function run(InputInterface $input, OutputInterface $output)
{
// force the creation of the synopsis before the merge with the app definition
$this->getSynopsis(true);
$this->getSynopsis(false);
// add the application arguments and options
$this->mergeApplicationDefinition();
// bind the input against the command specific arguments/options
try {
$input->bind($this->definition);
} catch (ExceptionInterface $e) {
if (!$this->ignoreValidationErrors) {
throw $e;
}
}
$this->configureAdditionalInput($input, $output);
// bind the input against the command specific arguments/options
try {
$input->bind($this->definition);
} catch (ExceptionInterface $e) {
if (!$this->ignoreValidationErrors) {
throw $e;
}
}
$this->initialize($input, $output);
if (null !== $this->processTitle) {
if (function_exists('cli_set_process_title')) {
if (false === @cli_set_process_title($this->processTitle)) {
if ('Darwin' === PHP_OS) {
$output->writeln('<comment>Running "cli_get_process_title" as an unprivileged user is not supported on MacOS.</comment>');
} else {
$error = error_get_last();
trigger_error($error['message'], E_USER_WARNING);
}
}
} elseif (function_exists('setproctitle')) {
setproctitle($this->processTitle);
} elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) {
$output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>');
}
}
if ($input->isInteractive()) {
$this->interact($input, $output);
}
// The command name argument is often omitted when a command is executed directly with its run() method.
// It would fail the validation if we didn't make sure the command argument is present,
// since it's required by the application.
if ($input->hasArgument('command') && null === $input->getArgument('command')) {
$input->setArgument('command', $this->getName());
}
$input->validate();
if ($this->code) {
$statusCode = call_user_func($this->code, $input, $output);
} else {
$statusCode = $this->execute($input, $output);
}
return is_numeric($statusCode) ? (int) $statusCode : 0;
}
- 改變你的命令(ContainerAwareCommand是你的新類)
class ImportCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('app:import')
->addArgument('importKey', InputArgument::REQUIRED)
;
$this->ignoreValidationErrors();
}
protected function configureAdditionalInput(InputInterface $input, OutputInterface $output)
{
$key = $input->getArgument('importKey');
$this->notIgnoreValidationErrors();
// this will be handled in
// $importProvider = $this->getContainer()->get('app.import');
// $importer = $importProvider->getImport($key);
// $importer->configureCommand($input, $output, $this);
if($key == 'test')
{
$this->addOption('supplier', null,InputOption::VALUE_REQUIRED);
}
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$supplier = $input->getOption('supplier');
$output->writeln("your provided suplier id is '$supplier'");
}
}
現在運行命令
bin/console app:import test --supplier=5
您提供的uplier ID是「5」
因爲第一和第二線被定義配置不符合我的要求(),我們不知道進口關鍵。 –
你可以嘗試addArgument('supplier',InputArgument :: OPTIONAL) – Robert
不會更改任何內容,因爲interact()在輸入綁定後調用...我想出了一個解決方案並立即發佈它......但不管怎麼說,多謝拉 –