1616class CommandLine
1717{
1818 public const
19- ARGUMENT = 'argument ' ,
20- OPTIONAL = 'optional ' ,
21- REPEATABLE = 'repeatable ' ,
22- ENUM = 'enum ' ,
23- REALPATH = 'realpath ' ,
24- NORMALIZER = 'normalizer ' ,
25- VALUE = 'default ' ;
19+ Argument = 'argument ' ,
20+ Optional = 'optional ' ,
21+ Repeatable = 'repeatable ' ,
22+ Enum = 'enum ' ,
23+ Realpath = 'realpath ' ,
24+ Normalizer = 'normalizer ' ,
25+ Value = 'default ' ;
2626
2727 /** @var array[] */
2828 private $ options = [];
@@ -52,11 +52,11 @@ public function __construct(string $help, array $defaults = [])
5252 $ name = end ($ m [1 ]);
5353 $ opts = $ this ->options [$ name ] ?? [];
5454 $ this ->options [$ name ] = $ opts + [
55- self ::ARGUMENT => (bool ) end ($ m [2 ]),
56- self ::OPTIONAL => isset ($ line [2 ]) || (substr (end ($ m [2 ]), 0 , 1 ) === '[ ' ) || isset ($ opts [self ::VALUE ]),
57- self ::REPEATABLE => (bool ) end ($ m [3 ]),
58- self ::ENUM => count ($ enums = explode ('| ' , trim (end ($ m [2 ]), '<[]> ' ))) > 1 ? $ enums : null ,
59- self ::VALUE => $ line [2 ] ?? null ,
55+ self ::Argument => (bool ) end ($ m [2 ]),
56+ self ::Optional => isset ($ line [2 ]) || (substr (end ($ m [2 ]), 0 , 1 ) === '[ ' ) || isset ($ opts [self ::Value ]),
57+ self ::Repeatable => (bool ) end ($ m [3 ]),
58+ self ::Enum => count ($ enums = explode ('| ' , trim (end ($ m [2 ]), '<[]> ' ))) > 1 ? $ enums : null ,
59+ self ::Value => $ line [2 ] ?? null ,
6060 ];
6161 if ($ name !== $ m [1 ][0 ]) {
6262 $ this ->aliases [$ m [1 ][0 ]] = $ name ;
@@ -89,7 +89,7 @@ public function parse(?array $args = null): array
8989
9090 $ name = current ($ this ->positional );
9191 $ this ->checkArg ($ this ->options [$ name ], $ arg );
92- if (empty ($ this ->options [$ name ][self ::REPEATABLE ])) {
92+ if (empty ($ this ->options [$ name ][self ::Repeatable ])) {
9393 $ params [$ name ] = $ arg ;
9494 next ($ this ->positional );
9595 } else {
@@ -110,31 +110,31 @@ public function parse(?array $args = null): array
110110
111111 $ opt = $ this ->options [$ name ];
112112
113- if ($ arg !== true && empty ($ opt [self ::ARGUMENT ])) {
113+ if ($ arg !== true && empty ($ opt [self ::Argument ])) {
114114 throw new \Exception ("Option $ name has not argument. " );
115115
116- } elseif ($ arg === true && !empty ($ opt [self ::ARGUMENT ])) {
116+ } elseif ($ arg === true && !empty ($ opt [self ::Argument ])) {
117117 if (isset ($ args [$ i ]) && $ args [$ i ][0 ] !== '- ' ) {
118118 $ arg = $ args [$ i ++];
119- } elseif (empty ($ opt [self ::OPTIONAL ])) {
119+ } elseif (empty ($ opt [self ::Optional ])) {
120120 throw new \Exception ("Option $ name requires argument. " );
121121 }
122122 }
123123
124124 $ this ->checkArg ($ opt , $ arg );
125125
126126 if (
127- !empty ($ opt [self ::ENUM ])
128- && !in_array (is_array ($ arg ) ? reset ($ arg ) : $ arg , $ opt [self ::ENUM ], true )
127+ !empty ($ opt [self ::Enum ])
128+ && !in_array (is_array ($ arg ) ? reset ($ arg ) : $ arg , $ opt [self ::Enum ], true )
129129 && !(
130- $ opt [self ::OPTIONAL ]
130+ $ opt [self ::Optional ]
131131 && $ arg === true
132132 )
133133 ) {
134- throw new \Exception ("Value of option $ name must be " . implode (', or ' , $ opt [self ::ENUM ]) . '. ' );
134+ throw new \Exception ("Value of option $ name must be " . implode (', or ' , $ opt [self ::Enum ]) . '. ' );
135135 }
136136
137- if (empty ($ opt [self ::REPEATABLE ])) {
137+ if (empty ($ opt [self ::Repeatable ])) {
138138 $ params [$ name ] = $ arg ;
139139 } else {
140140 $ params [$ name ][] = $ arg ;
@@ -144,15 +144,15 @@ public function parse(?array $args = null): array
144144 foreach ($ this ->options as $ name => $ opt ) {
145145 if (isset ($ params [$ name ])) {
146146 continue ;
147- } elseif (isset ($ opt [self ::VALUE ])) {
148- $ params [$ name ] = $ opt [self ::VALUE ];
149- } elseif ($ name [0 ] !== '- ' && empty ($ opt [self ::OPTIONAL ])) {
147+ } elseif (isset ($ opt [self ::Value ])) {
148+ $ params [$ name ] = $ opt [self ::Value ];
149+ } elseif ($ name [0 ] !== '- ' && empty ($ opt [self ::Optional ])) {
150150 throw new \Exception ("Missing required argument < $ name>. " );
151151 } else {
152152 $ params [$ name ] = null ;
153153 }
154154
155- if (!empty ($ opt [self ::REPEATABLE ])) {
155+ if (!empty ($ opt [self ::Repeatable ])) {
156156 $ params [$ name ] = (array ) $ params [$ name ];
157157 }
158158 }
@@ -169,11 +169,11 @@ public function help(): void
169169
170170 public function checkArg (array $ opt , &$ arg ): void
171171 {
172- if (!empty ($ opt [self ::NORMALIZER ])) {
173- $ arg = call_user_func ($ opt [self ::NORMALIZER ], $ arg );
172+ if (!empty ($ opt [self ::Normalizer ])) {
173+ $ arg = call_user_func ($ opt [self ::Normalizer ], $ arg );
174174 }
175175
176- if (!empty ($ opt [self ::REALPATH ])) {
176+ if (!empty ($ opt [self ::Realpath ])) {
177177 $ path = realpath ($ arg );
178178 if ($ path === false ) {
179179 throw new \Exception ("File path ' $ arg' not found. " );
0 commit comments