From ad379d8e2f3baee7a7a1345e771e83135773713a Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 15 Dec 2015 17:14:59 +0100 Subject: [PATCH] support reading from stdin --- src/phpsqllint/Cli.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/phpsqllint/Cli.php b/src/phpsqllint/Cli.php index a417e23..5e4f638 100644 --- a/src/phpsqllint/Cli.php +++ b/src/phpsqllint/Cli.php @@ -36,7 +36,7 @@ class Cli public function run() { try { - $parser = $this->loadParameters(); + $parser = $this->loadOptionParser(); $files = $this->parseParameters($parser); $allfine = true; @@ -66,7 +66,11 @@ class Cli { $this->renderer->startRendering($filename); - $sql = file_get_contents($filename); + if ($filename == '-') { + $sql = file_get_contents('php://stdin'); + } else { + $sql = file_get_contents($filename); + } if (trim($sql) == '') { $this->renderer->displayError('SQL file empty', '', 0, 0); return false; @@ -111,11 +115,12 @@ class Cli * * @return \Console_CommandLine CLI option parser */ - protected function loadParameters() + protected function loadOptionParser() { $parser = new \Console_CommandLine(); $parser->description = 'php-sqllint'; $parser->version = '0.0.2'; + $parser->avoid_reading_stdin = true; $parser->addOption( 'renderer', @@ -136,7 +141,8 @@ class Cli $parser->addArgument( 'sql_files', array( - 'multiple' => true + 'description' => 'SQL files, "-" for stdin', + 'multiple' => true ) ); @@ -160,6 +166,9 @@ class Cli $this->renderer = new $rendClass(); foreach ($result->args['sql_files'] as $filename) { + if ($filename == '-') { + continue; + } if (!file_exists($filename)) { throw new \Exception('File does not exist: ' . $filename); } -- 2.30.2