Passed
Push — master ( f833b1...244bf6 )
by Timm
02:07
created

cli-app-subcommands.php$0 ➔ run()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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