X-Git-Url: https://git.cweiske.de/bdrem.git/blobdiff_plain/21eb1d8202e756a2ccfd5ba4168f375ce995835e..d8ce92efb0f457051ba4cdaaa11fe938697282d8:/src/bdrem/Cli.php diff --git a/src/bdrem/Cli.php b/src/bdrem/Cli.php index 807f336..3ecd4ea 100644 --- a/src/bdrem/Cli.php +++ b/src/bdrem/Cli.php @@ -1,26 +1,110 @@ + * @copyright 2014 Christian Weiske + * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 + * @link http://cweiske.de/bdrem.htm + */ namespace bdrem; -class Cli +/** + * Command line user interface for the terminal/shell. + * Renders an ASCII table by default. + * + * @category Tools + * @package Bdrem + * @author Christian Weiske + * @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 { - public function run() + /** + * 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() { - $cfg = new Config(); - $cfg->load(); - setlocale(LC_TIME, $cfg->locale); - $source = $cfg->loadSource(); + $parser = parent::loadParameters(); + //set default renderer to console + $parser->options['renderer']->default = 'console'; - $arEvents = $source->getEvents( - date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter + $parser->addOption( + 'ansi', + array( + 'long_name' => '--no-color', + 'description' => 'Do not output ANSI color codes', + 'action' => 'StoreFalse', + 'default' => true + ) ); - usort($arEvents, '\\bdrem\\Event::compare'); - $this->render($arEvents); + + //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'); + } + } + + /** + * Handle the "readme" command and output the readme. + * + * @return void + */ + protected function showReadme() + { + readfile(__DIR__ . '/../../README.rst'); + exit(); } - public function render($arEvents) + /** + * 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(); } } ?>