wjzijderveld/console-option-resolver

该包已被废弃且不再维护。作者建议使用wjzijderveld/console-input-resolver包。

用于询问缺失选项和参数的库

0.2.0 2014-11-30 14:38 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:42:07 UTC


README

Build Status

一个简单的用于 Symfony Console 的库。

它允许在未提供时向用户请求选项,这样您就可以将命令用作交互式命令或bash单行命令。

安装

composer require wjzijderveld/console-input-resolver 1.*@dev

使用方法

class GenerateCommand extends Command
{
    private $inputResolver;

    public function __construct(Resolver $inputResolver)
    {
        parent::__construct();

        $this->inputResolver = $inputResolver;
    }

    public function configure()
    {
        $this->setName('generate');

        $this
            ->addArgument('class', InputArgument::OPTIONAL, 'The name of the class to generate')
            ->addOption('namespace', null, InputOption::VALUE_REQUIRED, 'The namespace to generate the class in');
    }

    public function execut(InputInterface $input, OutputInterface $output)
    {
        // values will now contain values for namespace and class
        // for each option or argument that is not given when running this command
        // it will interactivily ask for a value
        $values = $this->inputResolver->resolveInputDefinition($this->getDefinition(), array('namespace', 'class'));

        var_dump($values);
    }
}

示例输出

$ ./console generate --namespace Acme
> The name of the class to generate: Foo
> array(2) {
  'namespace' =>
  string(4) "Acme"
  'class' =>
  string(3) "Foo"
}