“X”参数不存在

rkm*_*max 3 php console-application symfony

我有一个简单的 symfony 控制台应用程序,具有以下入口点

#!/usr/bin/env php
<?php

include __DIR__ . '/vendor/autoload.php';

use Rkmax\Application;

$app = new Application();
$app->run();
Run Code Online (Sandbox Code Playgroud)

我的应用程序类

class Application extends BaseApplication
{
    public function __construct($name = 'myapp', $version = '0.1')
    {
        parent::__construct($name, $version);

        $this->add(new Command\SingleCommand());
    }
}
Run Code Online (Sandbox Code Playgroud)

和我的命令

class SingleCommand extends Command
{
    const KEYWORDS = 'keywords';

    protected function configure()
    {
        $this
            ->setName("single")
            ->setDescription("Sample")
            ->addArgument(self::KEYWORDS, InputArgument::OPTIONAL, "Keywords for the search")
        ;
    }

    public function run(InputInterface $input, OutputInterface $output)
    {
        $keywords = $input->getArgument(self::KEYWORDS);
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白问题出在哪里,总是出现错误

 [InvalidArgumentException]               
 The "keywords" argument does not exist.
Run Code Online (Sandbox Code Playgroud)

Tho*_*ist 7

您可能应该重写exe​​cute方法而不是run

public function execute(InputInterface $input, OutputInterface $output)
{
    $keywords = $input->getArgument(self::KEYWORDS);
    // ...
}
Run Code Online (Sandbox Code Playgroud)

run初始化$inputoutput,然后验证输入并稍后调用execute($input, $output) 。

https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Console/Command/Command.php#L215-L257