@@ -9,9 +9,20 @@ final class CommandArgument
99 private $ defaultValue ;
1010 private ?string $ description ;
1111
12- public function __construct (string $ name , bool $ isRequired = false , $ defaultValue = null , ?string $ description = null )
12+ public function __construct (string $ name , bool $ isRequired = false , $ defaultValue = null , ?string $ description = null )
1313 {
14- $ this ->name = $ name ;
14+ if ($ name === '' ) {
15+ throw new \InvalidArgumentException ("Option name cannot be empty. " );
16+ }
17+ if (!ctype_alpha ($ name )) {
18+ throw new \InvalidArgumentException ("Option name must contain only letters. ' $ name' is invalid. " );
19+ }
20+
21+ if ($ isRequired && $ defaultValue !== null ) {
22+ throw new \LogicException ("Argument ' $ name' cannot be required and have a default value. " );
23+ }
24+
25+ $ this ->name = strtolower ($ name );
1526 $ this ->isRequired = $ isRequired ;
1627 $ this ->defaultValue = $ defaultValue ;
1728 $ this ->description = $ description ;
@@ -47,4 +58,13 @@ public function getDescription(): ?string
4758 return $ this ->description ;
4859 }
4960
61+ public static function required (string $ name , ?string $ description = null ): self
62+ {
63+ return new self ($ name , true , null , $ description );
64+ }
65+
66+ public static function optional (string $ name , $ default = null , ?string $ description = null ): self
67+ {
68+ return new self ($ name , false , $ default , $ description );
69+ }
5070}
0 commit comments