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

cli-app-subcommands.php$1 ➔ init()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 6
rs 10
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::createRootCmd(
17
                new class extends CmdRunner {
18
19
                    public function run(): void {
20
                        $this->getCmd()->help();
21
                    }
22
                }
23
            )
24
                ->addSubCmd(
25
                    Cmd::createSubCmd('show', new class extends CmdRunner {
26
27
                        public function init(Cmd $cmd): void {
28
29
                            $cmd
30
                                ->setDescription('This command is used to show something. Take a look at the subcommands.');
31
32
                            parent::init($cmd);
33
                        }
34
35
                        public function run(): void {
36
                            $this->getCmd()->help();
37
                        }
38
39
                    })
40
                        ->addSubCmd(
41
                            Cmd::createSubCmd('hello', new class extends CmdRunner {
42
43
                                public function init(Cmd $cmd): void {
44
45
                                    $cmd
46
                                        ->setDescription('Displays hello world.')
47
                                        ->addOption('name:', [
48
                                            'description' => 'Name option. This option requires a value.',
49
                                            'isa' => 'string',
50
                                            'default' => 'World'
51
                                        ]);
52
53
                                    parent::init($cmd);
54
                                }
55
56
                                public function run(): void {
57
58
                                    $cmd = $this->getCmd();
59
60
                                    $name = $cmd->getProvidedOption('name');
61
62
                                    self::eol();
63
                                    self::echo(sprintf('Hello %s!', $name), Color::FOREGROUND_COLOR_CYAN);
64
                                    self::eol();
65
                                    self::eol();
66
                                }
67
                            })
68
                        )
69
                        ->addSubCmd(
70
                            Cmd::createSubCmd('phpversion', new class extends CmdRunner {
71
72
                                public function init(Cmd $cmd): void {
73
74
                                    $cmd
75
                                        ->setDescription('Displays the current php version of your cli.');
76
77
                                    parent::init($cmd);
78
                                }
79
80
                                public function run(): void {
81
                                    self::eol();
82
                                    self::echo('  Your PHP version is:', Color::FOREGROUND_COLOR_YELLOW);
83
                                    self::eol();
84
                                    self::echo('  ' . PHP_VERSION);
85
                                    self::eol();
86
                                    self::eol();
87
                                }
88
                            })
89
                        )
90
                );
91
        }
92
93
    }
94
);
95