【问题标题】:How to pass the output of command as the input of another?如何将命令的输出作为另一个的输入传递?
【发布时间】:2020-09-13 14:16:30
【问题描述】:

我正在使用 Symfony 控制台应用程序并尝试将 phpstan 的输出作为控制台命令的输入:

vendor/bin/phpstan analyse | bin/wte analyse

目前命令按预期运行,但如何将 phpstan 的输出传递给bin/wte analyse 命令?

// AnalyseCommand.php 
final class AnalyseCommand extends Command
{
    public const NAME = 'analyse';

    /**
     * @var SymfonyStyle
     */
    private $symfonyStyle;

    public function __construct(SymfonyStyle $symfonyStyle)
    {
        $this->symfonyStyle = $symfonyStyle;
        parent::__construct();
    }

    public function execute(InputInterface $input, OutputInterface $output): int
    {
        // Get phpstan output in here. 

        return ShellCode::SUCCESS;
    }

    protected function configure(): void
    {
        $this->setName(self::NAME);
        $this->setDescription('Find an error');

    }
}

【问题讨论】:

    标签: php symfony stdin symfony-console


    【解决方案1】:

    Symfony 控制台没有任何处理标准输入的功能。但这不是必需的,因为您仍在使用 PHP,并且可以使用任何 native features

    public function execute(InputInterface $input, OutputInterface $output): int
    {
        $stdin = '';
    
        while (!feof(STDIN)) {
           $stdin .= fread(STDIN, 1024);
        } 
        
        // do what you need with $stdin
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-30
      • 2016-08-22
      • 2021-04-19
      • 2013-11-08
      • 2014-12-03
      • 1970-01-01
      相关资源
      最近更新 更多