Passed
Push — master ( 3d076e...fba7a8 )
by Timm
02:10
created

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

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

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