Help::displayArguments()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 0
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 4
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stefaminator\Cli;
5
6
7
class Help extends Cmd {
8
9
    /**
10
     * @var Cmd
11
     */
12
    public $runner;
13
14
    public static $header = <<<EOT
15
16
              o       
17
           ` /_\ '    
18
          - (o o) -   
19
--------ooO--(_)--Ooo--------
20
          Need help?
21
-----------------------------
22
EOT;
23
24 4
    public function __construct(Cmd $runner) {
25 4
        $this->runner = $runner;
26 4
        parent::__construct();
27 4
    }
28
29 4
    public function init(): void {
30 4
    }
31
32
33 4
    public function run(): void {
34 4
        $this->displayHeader();
35 4
        $this->displayUsage();
36 4
        $this->displayArguments();
37 4
        $this->displayOptions();
38 4
        $this->displaySubcommands();
39 4
        $this->displayHelp();
40 4
        self::eol();
41 4
    }
42
43 4
    public function displayHeader(): void {
44
45 4
        $help = self::$header;
46
47 4
        self::echo($help, Color::FOREGROUND_COLOR_YELLOW);
48
49 4
        self::eol();
50 4
    }
51
52 4
    public function displayUsage(): void {
53
54 4
        $runner = $this->runner;
55
56 4
        $oc = $runner->optionCollection();
57 4
        $has_options = !empty($oc->options);
58
59 4
        $arg_usage = $this->getArgumentUsage();
60
61 4
        $has_subcommands = !empty($runner->children);
62
63 4
        self::eol();
64 4
        self::echo('Usage: ', Color::FOREGROUND_COLOR_YELLOW);
65 4
        self::eol();
66
67 4
        self::echo(
68
            '  ' .
69 4
            ($runner->parent !== null ? $runner->cmd : 'command') .
70 4
            ($has_options ? ' [options]' : '') .
71 4
            (!empty($arg_usage) ? ' ' . $arg_usage : '') .
72 4
            ($has_subcommands ? ' [command]' : '')
73
        );
74
75 4
        self::eol();
76 4
    }
77
78 4
    private function getArgumentUsage() {
79
80 4
        $argSpecs = $this->runner->argSpecs();
81
82 4
        $arg_usage = [];
83 4
        foreach ($argSpecs as $k => $v) {
84 1
            $arg_usage[] = '[<' . $k . '>]' . (array_key_exists('multiple', $v) ? '...' : '');
85
        }
86
87 4
        return implode(' ', $arg_usage);
88
    }
89
90 4
    public function displayArguments(): void {
91
92 4
        $argSpecs = $this->runner->argSpecs();
93
94 4
        $has_arguments = !empty($argSpecs);
95
96 4
        if ($has_arguments) {
97
98 1
            self::eol();
99 1
            self::echo('Arguments: ', Color::FOREGROUND_COLOR_YELLOW);
100 1
            self::eol();
101
102 1
            foreach ($argSpecs as $spec => $config) {
103
104 1
                $s = $spec;
105 1
                $s = '  ' . str_pad($s, 20, ' ');
106 1
                self::echo($s, Color::FOREGROUND_COLOR_GREEN);
107
108 1
                $s = ' ' . (array_key_exists('description', $config) ? $config['description'] : '');
109 1
                self::echo($s);
110
111 1
                self::eol();
112
            }
113
114
        }
115
116 4
    }
117
118 4
    public function displayOptions(): void {
119
120 4
        $oc = $this->runner->optionCollection();
121 4
        $has_options = !empty($oc->options);
122
123 4
        if ($has_options) {
124
125 4
            self::eol();
126 4
            self::echo('Options: ', Color::FOREGROUND_COLOR_YELLOW);
127 4
            self::eol();
128
129 4
            foreach ($oc->options as $option) {
130
131 4
                $s = '    ';
132 4
                if (!empty($option->short)) {
133 4
                    $s = '-' . $option->short . ', ';
134
                }
135 4
                $s .= '--' . $option->long;
136
137 4
                $s = '  ' . str_pad($s, 20, ' ');
138 4
                self::echo($s, Color::FOREGROUND_COLOR_GREEN);
139
140 4
                $s = ' ' . $option->desc;
141 4
                self::echo($s);
142
143 4
                if ($option->defaultValue) {
144 1
                    $s = ' [default: ' . $option->defaultValue . ']';
145 1
                    self::echo($s, Color::FOREGROUND_COLOR_YELLOW);
146
                }
147
148 4
                self::eol();
149
            }
150
        }
151
152 4
    }
153
154 4
    public function displaySubcommands(): void {
155
156 4
        $subcommands = $this->runner->children;
157
158 4
        $has_subcommands = !empty($subcommands);
159
160 4
        if ($has_subcommands) {
161
162 2
            self::eol();
163 2
            self::echo('Available commands: ', Color::FOREGROUND_COLOR_YELLOW);
164 2
            self::eol();
165
166 2
            foreach ($subcommands as $_runner) {
167
168 2
                $s = '  ' . str_pad($_runner->cmd, 20, ' ');
169 2
                self::echo($s, Color::FOREGROUND_COLOR_GREEN);
170
171 2
                $s = ' ' . $_runner->description();
172 2
                self::echo($s);
173
174 2
                self::eol();
175
            }
176
        }
177 4
    }
178
179 4
    public function displayHelp(): void {
180
181 4
        ob_start();
182 4
        $this->runner->help();
183 4
        $help = ob_get_clean();
184
185 4
        if (empty($help)) {
186 3
            return;
187
        }
188
189 1
        self::eol();
190 1
        self::echo('Help: ', Color::FOREGROUND_COLOR_YELLOW);
191 1
        self::eol();
192
193 1
        echo $help;
194 1
    }
195
196
}