Load version number from file
[php-sqllint.git] / src / phpsqllint / Cli.php
index 2f2473fa7cf7867b2a9a2ffeb447d6c7e1407725..4edcf666c777db965c544c1840aa0967f28150b8 100644 (file)
@@ -29,7 +29,14 @@ class Cli
     protected $renderer;
 
     protected $format = false;
-    protected $colors = false;
+
+    /**
+     * What syntax highlighting mode should be used
+     *
+     * none, ansi, html
+     */
+    protected $highlight = 'none';
+
 
     /**
      * Start processing.
@@ -122,9 +129,13 @@ class Cli
             return false;
         }
 
+        $typeMap = array(
+            'none' => 'text',
+            'ansi' => 'cli',
+            'html' => 'html',
+        );
         $options = array(
-            //FIXME: automatically detect if the shell/tool supports colors
-            'type' => $this->colors ? 'cli' : 'text'
+            'type' => $typeMap[$this->highlight],
         );
         echo \SqlParser\Utils\Formatter::format($sql, $options) . "\n";
     }
@@ -152,9 +163,14 @@ class Cli
     {
         $parser = new \Console_CommandLine();
         $parser->description = 'php-sqllint';
-        $parser->version = '0.0.2';
+        $parser->version = 'dev';
         $parser->avoid_reading_stdin = true;
 
+        $versionFile = __DIR__ . '/../../VERSION';
+        if (file_exists($versionFile)) {
+            $parser->version = trim(file_get_contents($versionFile));
+        }
+
         $parser->addOption(
             'format',
             array(
@@ -166,19 +182,20 @@ class Cli
             )
         );
         $parser->addOption(
-            'colors',
-            array(
-                'long_name'   => '--color',
-                'description' => 'Use colors in formatting output',
-                'action'      => 'StoreTrue',
-            )
-        );
-        $parser->addOption(
-            'nocolors',
+            'highlight',
             array(
-                'long_name'   => '--nocolor',
-                'description' => 'Disable colors in formatting output',
-                'action'      => 'StoreTrue',
+                'short_name'  => '-h',
+                'long_name'   => '--highlight',
+                'description' => 'Highlighting mode (when using --format)',
+                'action'      => 'StoreString',
+                'choices'     => array(
+                    'none',
+                    'ansi',
+                    'html',
+                    'auto',
+                ),
+                'default' => 'auto',
+                'add_list_option' => true,
             )
         );
         $parser->addOption(
@@ -226,18 +243,20 @@ class Cli
 
             $this->format = $result->options['format'];
 
-            if ($result->options['colors'] !== null) {
-                $this->colors = $result->options['colors'];
-            } else if ($result->options['nocolors'] !== null) {
-                $this->colors = !$result->options['nocolors'];
-            } else {
-                //default coloring to enabled, except
-                // when piping | to another tool
-                $this->colors = true;
-                if (function_exists('posix_isatty')
-                    && !posix_isatty(STDOUT)
-                ) {
-                    $this->colors = false;
+            $this->highlight = $result->options['highlight'];
+            if ($this->highlight == 'auto') {
+                if (php_sapi_name() == 'cli') {
+                    //default coloring to enabled, except
+                    // when piping | to another tool
+                    $this->highlight = 'ansi';
+                    if (function_exists('posix_isatty')
+                        && !posix_isatty(STDOUT)
+                    ) {
+                        $this->highlight = 'none';
+                    }
+                } else {
+                    //no idea where we are, so do not highlight
+                    $this->highlight = 'none';
                 }
             }