anonymous//examples/cli-app-options.php$0   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

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