-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathGlobalOption.php
205 lines (180 loc) · 5.39 KB
/
GlobalOption.php
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php declare(strict_types=1);
/**
* The file is part of inhere/console
*
* @author https://github.com/inhere
* @homepage https://github.com/inhere/php-console
* @license https://github.com/inhere/php-console/blob/master/LICENSE
*/
namespace Inhere\Console;
use Toolkit\PFlag\FlagType;
use function array_merge;
/**
* Class GlobalOption
*
* @package Inhere\Console
*/
class GlobalOption
{
public const HELP = 'help';
public const DEBUG = 'debug';
public const ISHELL = 'ishell';
public const VERSION = 'version';
public const PROFILE = 'profile';
public const NO_COLOR = 'no-color';
public const NO_INTERACTIVE = 'no-interactive';
public const KEY_MAP = [
'debug' => 1,
'ishell' => 1,
'profile' => 1,
'no-color' => 1,
// 'h' => 1,
'help' => 1,
// 'V' => 1,
'version' => 1,
'no-interactive' => 1,
];
/**
* Global options config
*
* @var array
* @psalm-var array<string, string>
*/
private static array $globalOptions = [
'debug' => [
'type' => FlagType::INT,
'desc' => 'Setting the runtime log debug level, quiet 0 - crazy 5',
'envVar' => Console::DEBUG_ENV_KEY,
'default' => Console::VERB_ERROR,
],
'ishell' => 'bool;Run application an interactive shell environment',
'profile' => 'bool;Display timing and memory usage information',
'no-color' => 'bool;Disable color/ANSI for message output',
'help' => 'bool;Display application help message;;;h',
'version' => 'bool;Show application version information;;;V',
'no-interactive' => 'bool;Run commands in a non-interactive environment',
// - hidden options
'auto-completion' => [
'type' => FlagType::BOOL,
'hidden' => true,
'desc' => 'Open generate auto completion script',
// 'envVar' => Console::DEBUG_ENV_KEY,
],
'shell-env' => [
'type' => FlagType::STRING,
'hidden' => true,
'desc' => 'The shell env name for generate auto completion script',
// 'envVar' => Console::DEBUG_ENV_KEY,
],
'gen-file' => [
'type' => FlagType::STRING,
'hidden' => true,
'desc' => 'The output file for generate auto completion script',
// 'envVar' => Console::DEBUG_ENV_KEY,
'default' => 'stdout',
],
'tpl-file' => [
'type' => FlagType::STRING,
'hidden' => true,
'desc' => 'custom tpl file for generate completion script',
// 'default' => 'stdout',
],
];
/**
* @var array built-in options for the alone command
*/
protected static array $aloneOptions = [
// '--help' => 'bool;Display this help message;;;h',
// '--show-disabled' => 'string;Whether display disabled commands',
];
public const SHOW_DISABLED = 'show-disabled';
/**
* @var array built-in options for the group command
*/
protected static array $groupOptions = [
// '--help' => 'bool;Display this help message;;;h',
self::SHOW_DISABLED => [
'hidden' => true,
'desc' => 'Whether display disabled commands'
],
];
/**
* @var array common options for the group/command
*/
protected static array $commonOptions = [
self::HELP => 'bool;Display command help message;;;h',
];
/**
* @param string $name
*
* @return bool
*/
public static function isExists(string $name): bool
{
return isset(self::$globalOptions[$name]);
}
/**
* @param string $name
* @param array $rule
*/
public static function setOption(string $name, array $rule): void
{
self::$globalOptions[$name] = $rule;
}
/**
* @param array $globalOptions
*/
public static function setOptions(array $globalOptions): void
{
if ($globalOptions) {
self::$globalOptions = $globalOptions;
}
}
/**
* @param array $globalOptions
*/
public static function addOptions(array $globalOptions): void
{
if ($globalOptions) {
self::$globalOptions = array_merge(self::$globalOptions, $globalOptions);
}
}
/**
* @return array
*/
public static function getOptions(): array
{
return self::$globalOptions;
}
/**
* @return array
*/
public static function getCommonOptions(): array
{
return self::$commonOptions;
}
/**
* @param bool $withCommon
*
* @return array
*/
public static function getAloneOptions(bool $withCommon = true): array
{
if ($withCommon) {
return array_merge(self::$commonOptions, self::$aloneOptions);
}
return self::$aloneOptions;
}
/**
* @param bool $withCommon
*
* @return array
*/
public static function getGroupOptions(bool $withCommon = true): array
{
if ($withCommon) {
return array_merge(self::$commonOptions, self::$groupOptions);
}
return self::$groupOptions;
}
}