@@ -63,7 +63,7 @@ public function validate(): void
63
63
$ definition = $ this ->definition ;
64
64
$ givenArguments = $ this ->arguments ;
65
65
66
- $ missingArguments = array_filter (array_keys ($ definition ->getArguments ()), fn ($ argument ) => !\array_key_exists ($ argument , $ givenArguments ) && $ definition ->getArgument ($ argument )->isRequired ());
66
+ $ missingArguments = array_filter (\ array_keys ($ definition ->getArguments ()), fn ($ argument ) => !\array_key_exists ($ argument , $ givenArguments ) && $ definition ->getArgument ($ argument )->isRequired ());
67
67
68
68
if (\count ($ missingArguments ) > 0 ) {
69
69
throw new RuntimeException (\sprintf ('Not enough arguments (missing: "%s"). ' , implode (', ' , $ missingArguments )));
@@ -85,6 +85,35 @@ public function getArguments(): array
85
85
return array_merge ($ this ->definition ->getArgumentDefaults (), $ this ->arguments );
86
86
}
87
87
88
+ /**
89
+ * Returns all the given arguments NOT merged with the default values.
90
+ *
91
+ * @param bool $strip Whether to return the raw parameters (false) or the values after the command name (true)
92
+ *
93
+ * @return array<string|bool|int|float|null|array<string|bool|int|float|null>>
94
+ */
95
+ public function getRawArguments (bool $ strip = false ): array
96
+ {
97
+ if (!$ strip ) {
98
+ return $ this ->arguments ;
99
+ }
100
+
101
+ $ arguments = [];
102
+ $ keep = false ;
103
+ foreach ($ this ->arguments as $ argument ) {
104
+ if (!$ keep && $ argument === $ this ->getFirstArgument ()) {
105
+ $ keep = true ;
106
+
107
+ continue ;
108
+ }
109
+ if ($ keep ) {
110
+ $ arguments [] = $ argument ;
111
+ }
112
+ }
113
+
114
+ return $ arguments ;
115
+ }
116
+
88
117
public function getArgument (string $ name ): mixed
89
118
{
90
119
if (!$ this ->definition ->hasArgument ($ name )) {
@@ -113,6 +142,16 @@ public function getOptions(): array
113
142
return array_merge ($ this ->definition ->getOptionDefaults (), $ this ->options );
114
143
}
115
144
145
+ /**
146
+ * Returns all the given options NOT merged with the default values.
147
+ *
148
+ * @return array<string|bool|int|float|null|array<string|bool|int|float|null>>
149
+ */
150
+ public function getRawOptions (): array
151
+ {
152
+ return $ this ->options ;
153
+ }
154
+
116
155
public function getOption (string $ name ): mixed
117
156
{
118
157
if ($ this ->definition ->hasNegation ($ name )) {
@@ -171,4 +210,57 @@ public function getStream()
171
210
{
172
211
return $ this ->stream ;
173
212
}
213
+
214
+ /**
215
+ * Returns a stringified representation of the options passed to the command.
216
+ *
217
+ * InputArguments MUST be escaped as well as the InputOption values passed to the command.
218
+ *
219
+ * @param string[] $optionNames Name of the options returned. If empty, all options are returned and non-passed or non-existent are ignored.
220
+ *
221
+ * @return list<string>
222
+ */
223
+ public function unparse (array $ optionNames = []): array
224
+ {
225
+ $ rawOptions = $ this ->getRawOptions ();
226
+
227
+ $ filteredRawOptions = count ($ optionNames ) === 0
228
+ ? $ rawOptions
229
+ : array_intersect_key ($ rawOptions , array_fill_keys ($ optionNames , '' ),
230
+ );
231
+
232
+ return array_map (
233
+ fn (string $ optionName ) => $ this ->unparseOption (
234
+ $ this ->definition ->getOption ($ optionName ),
235
+ $ optionName ,
236
+ $ filteredRawOptions [$ optionName ],
237
+ ),
238
+ array_keys ($ filteredRawOptions ),
239
+ );
240
+ }
241
+
242
+ /**
243
+ * @param string|bool|int|float|null|array<string|bool|int|float|null> $value
244
+ */
245
+ private function unparseOption (
246
+ InputOption $ option ,
247
+ string $ name ,
248
+ array |bool |float |int |string |null $ value ,
249
+ ): string
250
+ {
251
+ return match (true ) {
252
+ $ option ->isNegatable () => sprintf ('--%s%s ' , $ value ? '' : 'no- ' , $ name ),
253
+ !$ option ->acceptValue () => sprintf ('--%s ' , $ name ),
254
+ $ option ->isArray () => implode ('' , array_map (fn ($ item ) => $ this ->unparseOptionWithValue ($ name , $ item ), $ value ,)),
255
+ default => $ this ->unparseOptionWithValue ($ name , $ value ),
256
+ };
257
+ }
258
+
259
+ private function unparseOptionWithValue (
260
+ string $ name ,
261
+ bool |float |int |string |null $ value ,
262
+ ): string
263
+ {
264
+ return sprintf ('--%s=%s ' , $ name , $ this ->escapeToken ($ value ));
265
+ }
174
266
}
0 commit comments