-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommand.php
More file actions
113 lines (88 loc) · 2.5 KB
/
Copy pathCommand.php
File metadata and controls
113 lines (88 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace Snicco\Component\BetterWPCLI;
use ReflectionClass;
use Snicco\Component\BetterWPCLI\Input\Input;
use Snicco\Component\BetterWPCLI\Output\Output;
use Snicco\Component\BetterWPCLI\Synopsis\InputFlag;
use Snicco\Component\BetterWPCLI\Synopsis\Synopsis;
use function str_replace;
use function strtolower;
abstract class Command
{
// see https://tldp.org/LDP/abs/html/exitcodes.html
/**
* @var int
*/
public const SUCCESS = 0;
/**
* @var int
*/
public const FAILURE = 1;
/**
* @var int
*/
public const INVALID = 2;
/**
* @psalm-readonly
*/
protected static string $short_description = '';
/**
* @psalm-readonly
*/
protected static string $long_description = '';
/**
* @psalm-readonly
*/
protected static string $name = '';
/**
* @psalm-readonly
*/
protected static string $when = 'after_wp_load';
abstract public function execute(Input $input, Output $output): int;
public static function synopsis(): Synopsis
{
$default = [...self::verbosityFlags(), ...[self::noInteractionFlag()], ...[self::ansiFlag()]];
return new Synopsis(...$default);
}
public static function name(): string
{
if (! empty(static::$name)) {
return static::$name;
}
$short_name = (new ReflectionClass(static::class))->getShortName();
return strtolower(str_replace('Command', '', $short_name));
}
public static function when(): string
{
return static::$when;
}
public static function shortDescription(): string
{
return static::$short_description;
}
public static function longDescription(): string
{
$long = static::$long_description;
return empty($long) ? static::shortDescription() : $long;
}
/**
* @return list<InputFlag>
*/
protected static function verbosityFlags(): array
{
return [
new InputFlag('v', 'Verbose output'),
new InputFlag('vv', 'More verbose output'),
new InputFlag('vvv', 'Maximum verbosity (equal to --debug)'),
];
}
protected static function ansiFlag(): InputFlag
{
return new InputFlag('ansi', 'Force (or disable --no-ansi) ANSI output.');
}
protected static function noInteractionFlag(): InputFlag
{
return new InputFlag('interaction', '(--no-interaction) Do not ask any interactive question.');
}
}