@@ -280,25 +280,48 @@ public function choices(string $text, array $choices, $default = null, bool $cas
280
280
*/
281
281
public function prompt (string $ text , $ default = null , callable $ fn = null , int $ retry = 3 )
282
282
{
283
- $ error = 'Invalid value. Please try again! ' ;
283
+ $ error = 'Invalid value. Please try again! ' ;
284
+ $ hidden = \func_get_args ()[4 ] ?? false ;
285
+ $ readFn = $ hidden ? 'readHidden ' : 'read ' ;
284
286
285
287
$ this ->writer ->yellow ($ text )->comment (null !== $ default ? " [ $ default]: " : ': ' );
286
288
287
289
try {
288
- $ input = $ this ->reader ->read ($ default , $ fn );
290
+ $ input = $ this ->reader ->{ $ readFn } ($ default , $ fn );
289
291
} catch (\Exception $ e ) {
290
292
$ error = $ e ->getMessage ();
291
293
}
292
294
293
295
if ($ retry > 0 && (isset ($ e ) || \strlen ($ input ?? '' ) === 0 )) {
294
296
$ this ->writer ->bgRed ($ error , true );
295
297
296
- return $ this ->prompt ($ text , $ default , $ fn , $ retry - 1 );
298
+ return $ this ->prompt ($ text , $ default , $ fn , $ retry - 1 , $ hidden );
297
299
}
298
300
299
301
return $ input ?? $ default ;
300
302
}
301
303
304
+ /**
305
+ * Prompt user for secret input like password. Currently for unix only.
306
+ *
307
+ * @param string $text Prompt text.
308
+ * @param callable|null $fn The sanitizer/validator for user input
309
+ * Any exception message is printed as error.
310
+ * @param int $retry How many more times to retry on failure.
311
+ *
312
+ * @return mixed
313
+ */
314
+ public function promptHidden (string $ text , callable $ fn = null , int $ retry = 3 )
315
+ {
316
+ $ winOS = '\\' === \DIRECTORY_SEPARATOR ;
317
+
318
+ if ($ winOS ) {
319
+ $ this ->writer ->error ('Hidden input not supported, Press Ctrl+C if you would like to abort ' , true );
320
+ }
321
+
322
+ return $ this ->prompt ($ text , null , $ fn , $ retry , !$ winOS );
323
+ }
324
+
302
325
/**
303
326
* Show choices list.
304
327
*
0 commit comments