"X" 参数不存在

The "X" argument does not exist

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

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

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

use Rkmax\Application;

$app = new Application();
$app->run();

我的申请class

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

        $this->add(new Command\SingleCommand());
    }
}

和我的命令

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);
        // ...
    }
}

我不明白问题出在哪里总是我得到错误

 [InvalidArgumentException]               
 The "keywords" argument does not exist.

您应该覆盖 execute 方法而不是 运行.

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

运行 初始化 $inputoutput,然后验证 input 并稍后调用 execute($input, $output)

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