Passed
Push — master ( cc00b6...3d076e )
by Timm
02:00
created

cli-app-options.php$0 ➔ verbose()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
#!/usr/bin/php
2
<?php
3
4
require __DIR__ . '/../vendor/autoload.php';
5
6
use Stefaminator\Cli\App;
7
use Stefaminator\Cli\AppParser;
8
use Stefaminator\Cli\Cmd;
9
use Stefaminator\Cli\CmdRunner;
10
use Stefaminator\Cli\Color;
11
12
AppParser::run(
13
    (new class extends App {
14
15
        public function setup(): Cmd {
16
            return Cmd::root()
17
                ->addOption('h|help', [
18
                    'description' => 'Displays the command help.'
19
                ])
20
                ->addOption('v|verbose', [
21
                    'description' => 'Flag to enable verbose output.'
22
                ])
23
                ->addOption('name:', [
24
                    'description' => 'Name option. This option requires a value.',
25
                    'isa' => 'string',
26
                    'default' => 'World'
27
                ])
28
                ->setRunner(
29
                    (new class extends CmdRunner {
30
31
                        public function run(): void {
32
33
                            $cmd = $this->getCmd();
34
35
                            if ($cmd->hasProvidedOption('help')) {
36
                                $cmd->help();
37
                                return;
38
                            }
39
40
                            $name = $cmd->getProvidedOption('name');
41
42
                            self::eol();
43
                            self::echo(sprintf('Hello %s', $name), Color::FOREGROUND_COLOR_YELLOW);
44
                            self::eol();
45
                            self::eol();
46
47
                            if ($cmd->hasProvidedOption('verbose')) {
48
                                $this->verbose();
49
                            }
50
51
                        }
52
53
                        public function help(): void {
54
55
                            echo '' .
56
                                ' This is the custom help for ' . Color::green('cli-app-options') . ' command. ' . self::EOL .
57
                                ' Please use the --name option to pass your name to this command and you will be greeted personally. ' . self::EOL .
58
                                ' ' . self::EOL .
59
                                ' ' . Color::green('php cli-app-options.php --name="great Stefaminator"') . self::EOL;
60
61
62
                        }
63
64
                        private function verbose(): void {
65
66
                            self::echo('--- VERBOSE OUTPUT ---', Color::FOREGROUND_COLOR_GREEN);
67
                            self::eol();
68
                            self::eol();
69
70
                            $this->outputProvidedOptions();
71
72
                            $this->outputProvidedArguments();
73
                        }
74
75
                        private function outputProvidedOptions(): void {
76
77
                            $cmd = $this->getCmd();
78
79
                            self::echo('  All current options...', Color::FOREGROUND_COLOR_GREEN);
80
                            self::eol();
81
82
                            $pOptions = $cmd->getAllProvidedOptions();
83
                            foreach ($pOptions as $k => $v) {
84
                                self::echo('    ' . $k . ': ' . json_encode($v), Color::FOREGROUND_COLOR_GREEN);
85
                                self::eol();
86
                            }
87
                            self::eol();
88
                        }
89
90
                        private function outputProvidedArguments(): void {
91
92
                            $cmd = $this->getCmd();
93
94
                            self::echo('  All current arguments...', Color::FOREGROUND_COLOR_GREEN);
95
                            self::eol();
96
97
                            $args = $cmd->getAllProvidedArguments();
98
                            foreach ($args as $a) {
99
                                self::echo('    ' . $a, Color::FOREGROUND_COLOR_GREEN);
100
                                self::eol();
101
                            }
102
                            self::eol();
103
104
                        }
105
                    })
106
                );
107
        }
108
109
    })
110
);
111