use ANSI color codes on shell by default
[bdrem.git] / src / bdrem / Cli.php
index c38e1735f06721b8fac1944e500d48294e8aedc7..3ecd4ea488b83a4bb003189d1c04e1168a53d8cf 100644 (file)
 <?php
+/**
+ * Part of bdrem
+ *
+ * PHP version 5
+ *
+ * @category  Tools
+ * @package   Bdrem
+ * @author    Christian Weiske <cweiske@cweiske.de>
+ * @copyright 2014 Christian Weiske
+ * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link      http://cweiske.de/bdrem.htm
+ */
 namespace bdrem;
 
+/**
+ * Command line user interface for the terminal/shell.
+ * Renders an ASCII table by default.
+ *
+ * @category  Tools
+ * @package   Bdrem
+ * @author    Christian Weiske <cweiske@cweiske.de>
+ * @copyright 2014 Christian Weiske
+ * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @version   Release: @package_version@
+ * @link      http://cweiske.de/bdrem.htm
+ */
 class Cli extends UserInterface
 {
-    protected function loadParameters($cfg)
+    /**
+     * Load parameters for the CLI option parser.
+     * Set the default renderer to "console" and adds some CLI-only commands
+     * like "readme" and "config".
+     *
+     * @return \Console_CommandLine CLI option parser
+     */
+    protected function loadParameters()
     {
-        $params = $GLOBALS['argv'];
-        array_shift($params);
-        $storeInto = null;
-        foreach ($params as $param) {
-            if ($storeInto !== null) {
-                $cfg->$storeInto = (int)$param;
-                $storeInto = null;
-                continue;
-            }
+        $parser = parent::loadParameters();
+        //set default renderer to console
+        $parser->options['renderer']->default = 'console';
 
-            if ($param == '--days-after' || $param == '-a') {
-                $storeInto = 'daysAfter';
-                continue;
-            } else if ($param == '--days-before' || $param == '-b') {
-                $storeInto = 'daysBefore';
-                continue;
-            }
-            $storeInto = null;
+        $parser->addOption(
+            'ansi',
+            array(
+                'long_name'   => '--no-color',
+                'description' => 'Do not output ANSI color codes',
+                'action'      => 'StoreFalse',
+                'default'     => true
+            )
+        );
+
+        //only on CLI
+        $parser->addCommand(
+            'readme', array(
+                'description' => 'Show README.rst file'
+            )
+        );
+        $parser->addCommand(
+            'config', array(
+                'description' => 'Extract configuration file'
+            )
+        );
+
+        return $parser;
+    }
+
+    /**
+     * Handle any commands given on the CLI
+     *
+     * @param object $res Command line parameters and options
+     *
+     * @return void
+     */
+    protected function handleCommands($res)
+    {
+        if ($res->command_name == '') {
+            return;
+        } else if ($res->command_name == 'readme') {
+            $this->showReadme();
+        } else if ($res->command_name == 'config') {
+            $this->extractConfig();
+        } else {
+            throw new \Exception('Unknown command');
         }
     }
 
-    protected function render($arEvents)
+    /**
+     * Handle the "readme" command and output the readme.
+     *
+     * @return void
+     */
+    protected function showReadme()
+    {
+        readfile(__DIR__ . '/../../README.rst');
+        exit();
+    }
+
+    /**
+     * Handle the "config" command and output the default configuration
+     *
+     * @return void
+     */
+    protected function extractConfig()
     {
-        $r = new Renderer_Console();
-        echo $r->render($arEvents);
+        readfile(__DIR__ . '/../../data/bdrem.config.php.dist');
+        exit();
     }
 }
 ?>