Symfony控制台-覆盖默认选项 [英] Symfony Console - Overwrite default options

查看:71
本文介绍了Symfony控制台-覆盖默认选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Symfony控制台为我的应用程序构建CLI应用程序。该应用程序需要执行一些简单的软件包管理操作来管理插件。因此,我需要一个名为-version 的命令选项,其快捷方式为 -v ,这似乎是不可能的,因为-version 保留用于应用程序版本,而 -v 保留用于默认详细程度设置。

如何禁用此子命令的默认选项或覆盖它们?

I'm building a CLI application for my app using Symfony Console. The application is required to do some simple package management operations to manage plugins. Therefore, I need a command option named --version with shortcut -v, which does not seem to be possible since --version is reserved for the application version and -v is reserved for the default verbosity setting.
How can I either disable the default options or overwrite them for this subcommand?

错误弹出状态


[Symfony\Component\Console \Exception\LogicException]已经存在一个名为版本的选项。

[Symfony\Component\Console\Exception\LogicException] An option named "version" already exists.


推荐答案

因此,这是一种可行的解决方案,但我不建议这样做。我建议您仅对命令使用-package-version 或其他选项,因为这需要大量额外的工作并且对于以后的Symfony效果不佳更新。

So, here is a solution that works, but I don't recommend it. I recommend you simply use --package-version or some other sort of option for your command, because this requires a lot of extra work and doesn't play well for future Symfony updates.

您可以做的第一件事是将控制台命令复制到 bin中/ 目录添加到新命令,然后将 use 语句更改为您自己的扩展应用程序:

The first thing you can do is copy the console command in your bin/ directory to a new command, and change the use statement to your own extended Application:

// use Symfony\Bundle\FrameworkBundle\Console\Application;
use AppBundle\Console\Application;

然后创建一个新的 Application 类,该类可以扩展现有的Symfony应用程序,如下所示:

Then create a new Application class that extends the existing Symfony Application, that looks like this:

namespace AppBundle\Console;

use AppBundle\Command\YourCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestApplication extends Application
{
    const NAME = 'Alternative Application';
    const VERSION = '1.0';

    // these are normally private, but must now be protected as they exist in doRun()
    protected $command;
    protected $defaultCommand;
    protected $dispatcher;
    protected $runningCommand;

    /**
     * {@inheritdoc}
     */
    public function __construct()
    {
        parent::__construct(static::NAME, static::VERSION);

        // manually add the commands you want to be handled by this
        $this->add(new YourCommand());
    }

    /**
     * {@inheritdoc}
     */
    public function doRun(InputInterface $input, OutputInterface $output)
    {
        /* remove these lines
        if (true === $input->hasParameterOption(array('--version', '-V'), true)) {
            $output->writeln($this->getLongVersion());

            return 0;
        }
        */

        // copy the rest of the doRun() function as it 
        // exists in the base Application class
        // ...
    }

    /**
     * {@inheritdoc}
     *
     * Return everything from the default input definition except the 'version' option
     */
    protected function getDefaultInputDefinition()
    {
        $definition  = [];

        $defaultDefinition = parent::getDefaultInputDefinition();

        foreach ($defaultDefinition->getOptions() as $option) {
            if ($option->getName() !== 'version') {
                $definition[] = $option;
            }
        }

        foreach ($defaultDefinition->getArguments() as $argument) {
            $definition[] = $argument;
        }

        return new InputDefinition($definition);
    }
}

现在使用自己的<$ c $添加您的应用程序c>-version 选项:

namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class YourCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('your:command')
            ->addOption('--version', '-V', InputOption::VALUE_NONE, 'okay')
        ;
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($input->hasOption('version')) {
            // do something
        }
    }
}

然后您可以通过以下方式调用命令:

Then you can call your command via:

php bin/console your:command
php bin/console your:comand --version

请注意,我不建议这样做。除了现在为您节省了一些使用其他选项的击键之外,这几乎是毫无收获的许多额外工作。另外,如果将来 bin / console Symfony\Component\Console\Application 发生更改,或者您因为您要覆盖这些文件,所以必须手动对其进行更新。

Note that I do NOT recommend this. This is a lot of extra work for almost no gain at all other than saving yourself a few keystrokes with a different option now. Plus if bin/console or Symfony\Component\Console\Application changes in the future, or you'll have to manually update those files since you overrode them.

这篇关于Symfony控制台-覆盖默认选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆