44
55namespace Tempest \Console \Input ;
66
7+ use Tempest \Console \Exceptions \InvalidEnumArgument ;
8+
79final class ConsoleArgumentBag
810{
911 /** @var ConsoleInputArgument[] */
@@ -93,7 +95,7 @@ public function findFor(ConsoleArgumentDefinition $argumentDefinition): ?Console
9395 {
9496 foreach ($ this ->arguments as $ argument ) {
9597 if ($ argumentDefinition ->matchesArgument ($ argument )) {
96- return $ argument ;
98+ return $ this -> resolveArgumentValue ( $ argumentDefinition , $ argument) ;
9799 }
98100 }
99101
@@ -108,6 +110,31 @@ public function findFor(ConsoleArgumentDefinition $argumentDefinition): ?Console
108110 return null ;
109111 }
110112
113+ private function resolveArgumentValue (
114+ ConsoleArgumentDefinition $ argumentDefinition ,
115+ ConsoleInputArgument $ argument ,
116+ ): ConsoleInputArgument {
117+ if (! $ argumentDefinition ->isBackedEnum ()) {
118+ return $ argument ;
119+ }
120+
121+ $ resolved = $ argumentDefinition ->type ::tryFrom ($ argument ->value );
122+
123+ if ($ resolved === null ) {
124+ throw new InvalidEnumArgument (
125+ $ argumentDefinition ->name ,
126+ $ argumentDefinition ->type ,
127+ $ argument ->value ,
128+ );
129+ }
130+
131+ return new ConsoleInputArgument (
132+ name: $ argumentDefinition ->name ,
133+ position: $ argumentDefinition ->position ,
134+ value: $ resolved ,
135+ );
136+ }
137+
111138 public function findArrayFor (ConsoleArgumentDefinition $ argumentDefinition ): ?ConsoleInputArgument
112139 {
113140 $ values = [];
0 commit comments