Skip to content

Commit d864ed9

Browse files
committed
Add CLI::secret method
1 parent 088483a commit d864ed9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/CLI.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,25 @@ public static function prompt(string $question, array | string $options = null)
658658
return $answer;
659659
}
660660

661+
/**
662+
* Prompt a question with secret answer.
663+
*
664+
* @param string $question The question to prompt
665+
*
666+
* @see https://dev.to/mykeels/reading-passwords-from-stdin-in-php-1np9
667+
*
668+
* @return string The secret answer
669+
*/
670+
public static function secret(string $question) : string
671+
{
672+
$question .= ': ';
673+
\fwrite(\STDOUT, $question);
674+
\exec('stty -echo');
675+
$secret = \trim((string) \fgets(\STDIN));
676+
\exec('stty echo');
677+
return $secret;
678+
}
679+
661680
/**
662681
* Creates a well formatted table.
663682
*

0 commit comments

Comments
 (0)