|
2 | 2 |
|
3 | 3 | <?php |
4 | 4 |
|
5 | | -use Cerbero\Enum\Enums; |
6 | | -use Cerbero\Enum\Enums\Backed; |
7 | | -use Cerbero\Enum\Services\Annotator; |
8 | | -use Cerbero\Enum\Services\Generator; |
9 | | - |
10 | | -use function Cerbero\Enum\enumOutcome; |
11 | 5 | use function Cerbero\Enum\fail; |
12 | | -use function Cerbero\Enum\normalizeEnums; |
13 | | -use function Cerbero\Enum\option; |
14 | | -use function Cerbero\Enum\runAnnotate; |
| 6 | +use function Cerbero\Enum\path; |
15 | 7 | use function Cerbero\Enum\setPathsByOptions; |
16 | 8 | use function Cerbero\Enum\splitArgv; |
17 | | -use function Cerbero\Enum\succeed; |
18 | 9 |
|
19 | 10 | is_file($autoload = dirname(__DIR__, 1) . '/vendor/autoload.php') && require $autoload; |
20 | 11 | is_file($autoload = dirname(__DIR__, 4) . '/vendor/autoload.php') && require $autoload; |
21 | 12 |
|
22 | | -$allowedCommands = ['annotate', 'make']; |
23 | | -[$arguments, $options] = splitArgv($argv); |
24 | | - |
25 | | -if (in_array($command = $argv[1] ?? null, $allowedCommands)) { |
| 13 | +if (is_file($command = path(__DIR__ . '/../cli/' . ($argv[1] ?? null) . '.php'))) { |
26 | 14 | try { |
| 15 | + [$arguments, $options] = splitArgv($argv); |
27 | 16 | setPathsByOptions($options); |
28 | 17 |
|
29 | | - $outcome = $command($arguments, $options); |
| 18 | + $outcome = require $command; |
30 | 19 | } catch (Throwable $e) { |
31 | 20 | $outcome = fail($e->getMessage()); |
32 | 21 | } |
33 | 22 |
|
34 | 23 | exit($outcome ? 0 : 1); |
35 | 24 | } |
36 | 25 |
|
37 | | -/** |
38 | | - * Annotate enums to ease IDE autocompletion. |
39 | | - * |
40 | | - * @param string[] $arguments |
41 | | - * @param string[] $options |
42 | | - */ |
43 | | -function annotate(array $arguments, array $options): bool |
44 | | -{ |
45 | | - $enums = array_intersect(['--all', '-a'], $options) ? [...Enums::namespaces()] : normalizeEnums($arguments); |
46 | | - |
47 | | - if (empty($enums)) { |
48 | | - return succeed('No enums to annotate.'); |
49 | | - } |
50 | | - |
51 | | - $succeeded = true; |
52 | | - $force = !! array_intersect(['--force', '-f'], $options); |
53 | | - |
54 | | - foreach ($enums as $enum) { |
55 | | - $succeeded = enumOutcome($enum, fn() => (new Annotator($enum))->annotate($force)) && $succeeded; |
56 | | - } |
57 | | - |
58 | | - return $succeeded; |
59 | | -} |
60 | | - |
61 | | -/** |
62 | | - * Create a new enum. |
63 | | - * |
64 | | - * @param string[] $arguments |
65 | | - * @param string[] $options |
66 | | - */ |
67 | | -function make(array $arguments, array $options): bool |
68 | | -{ |
69 | | - if (! $enum = strtr($arguments[0] ?? '', '/', '\\')) { |
70 | | - return fail('The name of the enum is missing.'); |
71 | | - } |
72 | | - |
73 | | - $force = !! array_intersect(['--force', '-f'], $options); |
74 | | - |
75 | | - if (enum_exists($enum) && ! $force) { |
76 | | - return succeed("The enum {$enum} already exists."); |
77 | | - } |
78 | | - |
79 | | - if (! $cases = array_slice($arguments, 1)) { |
80 | | - return fail('The cases of the enum are missing.'); |
81 | | - } |
82 | | - |
83 | | - try { |
84 | | - $generator = new Generator($enum, $cases, option('backed', $options)); |
85 | | - } catch (ValueError) { |
86 | | - return fail('The option --backed supports only ' . implode(', ', Backed::names())); |
87 | | - } |
88 | | - |
89 | | - return enumOutcome($enum, fn() => $generator->generate($force) && runAnnotate($enum, $force)); |
90 | | -} |
| 26 | +require path(__DIR__ . '/../cli/help'); |
91 | 27 |
|
92 | 28 | ?> |
93 | | -Annotate enums to ease IDE autocompletion. |
94 | | - |
95 | | -Usage: enum annotate enum1 [enum2 ...] |
96 | | - |
97 | | -Available options: |
98 | | - |
99 | | - -a, --all Whether all enums should be annotated |
100 | | - -f, --force Whether existing annotations should be overwritten |
101 | | - |
102 | | -Examples: |
103 | | - enum annotate App/Enums/MyEnum |
104 | | - enum annotate "App\Enums\MyEnum" |
105 | | - enum annotate App/Enums/MyEnum1 App/Enums/MyEnum2 |
106 | | - enum annotate App/Enums/MyEnum --force |
107 | | - enum annotate --all |
108 | | - enum annotate --all --force |
109 | | - |
110 | | -―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― |
111 | | - |
112 | | -Create a new enum. |
113 | | - |
114 | | -Usage: enum make enum case1 case2 |
115 | | - |
116 | | -Available options: |
117 | | - |
118 | | - --backed=VALUE How cases should be backed. VALUE is either: |
119 | | - snake|camel|kebab|upper|lower|int0|int1|bitwise |
120 | | - -f, --force Whether the existing enum should be overwritten |
121 | | - |
122 | | -Examples: |
123 | | - enum make App/Enums/MyEnum Case1 Case2 |
124 | | - enum make "App\Enums\MyEnum" Case1 Case2 |
125 | | - enum make App/Enums/MyEnum Case1=value1 Case2=value2 |
126 | | - enum make App/Enums/MyEnum Case1 Case2 --backed=int1 |
127 | | - enum make App/Enums/MyEnum Case1 Case2 --force |
128 | | - enum make App/Enums/MyEnum Case1 Case2 --backed=bitwise --force |
0 commit comments