add LICENSE file
[bdrem.git] / src / bdrem / Cli.php
1 <?php
2 /**
3  * Part of bdrem
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Bdrem
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/bdrem.htm
13  */
14 namespace bdrem;
15
16 /**
17  * Command line user interface for the terminal/shell.
18  * Renders an ASCII table by default.
19  *
20  * @category  Tools
21  * @package   Bdrem
22  * @author    Christian Weiske <cweiske@cweiske.de>
23  * @copyright 2014 Christian Weiske
24  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
25  * @version   Release: @package_version@
26  * @link      http://cweiske.de/bdrem.htm
27  */
28 class Cli extends UserInterface
29 {
30     /**
31      * Load parameters for the CLI option parser.
32      * Set the default renderer to "console" and adds some CLI-only commands
33      * like "readme" and "config".
34      *
35      * @return \Console_CommandLine CLI option parser
36      */
37     protected function loadParameters()
38     {
39         $parser = parent::loadParameters();
40         //set default renderer to console
41         $parser->options['renderer']->default = 'console';
42
43         $parser->addOption(
44             'ansi',
45             array(
46                 'long_name'   => '--no-color',
47                 'description' => 'Do not output ANSI color codes',
48                 'action'      => 'StoreFalse',
49                 'default'     => true
50             )
51         );
52
53         //only on CLI
54         $parser->addCommand(
55             'readme', array(
56                 'description' => 'Show README.rst file'
57             )
58         );
59         $parser->addCommand(
60             'config', array(
61                 'description' => 'Extract configuration file'
62             )
63         );
64
65         return $parser;
66     }
67
68     /**
69      * Handle any commands given on the CLI
70      *
71      * @param object $res Command line parameters and options
72      *
73      * @return void
74      */
75     protected function handleCommands($res)
76     {
77         if ($res->command_name == '') {
78             return;
79         } else if ($res->command_name == 'readme') {
80             $this->showReadme();
81         } else if ($res->command_name == 'config') {
82             $this->extractConfig();
83         } else {
84             throw new \Exception('Unknown command');
85         }
86     }
87
88     /**
89      * Handle the "readme" command and output the readme.
90      *
91      * @return void
92      */
93     protected function showReadme()
94     {
95         readfile(__DIR__ . '/../../README.rst');
96         exit();
97     }
98
99     /**
100      * Handle the "config" command and output the default configuration
101      *
102      * @return void
103      */
104     protected function extractConfig()
105     {
106         readfile(__DIR__ . '/../../data/bdrem.config.php.dist');
107         exit();
108     }
109 }
110 ?>