简体   繁体   English

symfony2控制台的“空白”命令

[英]a “blank” command for symfony2 console

I've created a little command line tool to help me launch sites. 我创建了一个命令行工具来帮助我启动网站。 It uses Symfony2 console to help create the commands and add some structure. 它使用Symfony2控制台来帮助创建命令并添加一些结构。 What I'm trying to figure out is if there is a way, I can crate a "blank" or a default command so if you don't put in a command it just defaults to this. 我试图找出的是,如果可以的话,我可以创建一个“空白”或默认命令,因此,如果您不输入命令,则默认为默认命令。 An example might help explain: 一个例子可能有助于解释:

A "normal" console command would look like this: 一个“普通”控制台命令如下所示:

php launch site foo

I want to make this do the exact same thing as above: 我想让它做和上面完全一样的事情:

php launch foo

The only thing I can think of is to sort-of short circuit the application->run process and check if "foo" is in my own command list, if it's not then force the console to run site foo. 我唯一能想到的就是对应用程序->运行进程进行短路,并检查“ foo”是否在我自己的命令列表中,如果不是,则强制控制台运行站点foo。 The crappy thing about that is if you just typo'ed a different command, the system would just try and run as a site and instead of an error message you'd get an error saying it can't launch that site (which is an error, but the wrong error and not a helpful one to a user). 糟糕的是,如果您仅键入了一个不同的命令,系统就会尝试作为站点运行,而不是一条错误消息,您会收到一条错误消息,指出它无法启动该站点(这是一个错误,但错误的错误对用户没有帮助)。

Maybe I missed something in the console docs, but is there a way to do what I'm trying here? 也许我错过了控制台文档中的某些内容,但是有没有办法做我在这里尝试的事情?

So what I ended up doing was just attempt my own match, if I can find the command, then I run the application as normal, if not, I try to launch the site: 因此,我最终要做的只是尝试自己的匹配,如果可以找到该命令,则可以正常运行该应用程序,否则,请尝试启动该站点:

    if (!array_key_exists($argv[1], $list))
    {
        $cmd = $this->application->find('launch');
        $args = array(
            'command' => 'launch',
            'alias' => $argv[1]
        );

        $input = new ArrayInput($args);
        $output = new ConsoleOutput();
        $cmd->run($input, $output);
    }
    else
    {
        $this->application->run();
    }

It works fine, it just feels a little meh, I'm open to other suggestions if anyone has them. 它工作正常,感觉有点me,如果有人有建议,我可以接受其他建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM