Passed
Push — master ( 244bf6...cc00b6 )
by Timm
01:59
created

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

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

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