aboutsummaryrefslogtreecommitdiff
path: root/src/bdrem/Cli.php
blob: 3ecd4ea488b83a4bb003189d1c04e1168a53d8cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?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
{
    /**
     * 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()
    {
        $parser = parent::loadParameters();
        //set default renderer to console
        $parser->options['renderer']->default = 'console';

        $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');
        }
    }

    /**
     * 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()
    {
        readfile(__DIR__ . '/../../data/bdrem.config.php.dist');
        exit();
    }
}
?>