php - "X"参数不存在

标签 php symfony console-application

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

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

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

use Rkmax\Application;

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

我的应用类

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 方法而不是run

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

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

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

关于php - "X"参数不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27931283/

相关文章:

php - JEdi​​table 未保存在 Mysql 中

php - Symfony2 FOSRESTBundle REST API 返回 PDF

c++ - 确保单个实例并为 Windows C++ 控制台应用程序传递参数的正确方法

c# - 如何在 C# 中以编程方式关闭 Windows XP Print Spooler 服务

javascript - 如何通过一键删除按钮删除选中的项目

php - smarty if empty else smarty 值

javascript - jQuery Ajax 登录中没有 'Access-Control-Allow-Origin' header

symfony - ("Parameter "“对于路由 ""必须匹配 "[^/]++"(""给定)使用pagerfanta

php - Symfony 表单生成器多行相同类型

f# - 如何从 F# 控制台应用程序写入 appSettings?