You can accomplish this using the Erlang :io.get_password() function, e.g.
IO.write("What is your password?")
password = :io.get_password()
|> List.to_string()
Note that IO.write/1 is preferable to using Mix.shell().info() for the prompt because the info function will add a newline, which is usually not what you want in a prompt. Also be advised that :io.get_password() returns input as a charlist, so you will probably want to convert it to a binary as demonstrated above. I wrote a package that utilizes the above technique: https://hex.pm/packages/cowrie
3条答案
按热度按时间zdwk9cvp1#
You can accomplish this using the Erlang
:io.get_password()
function, e.g.Note that
IO.write/1
is preferable to usingMix.shell().info()
for the prompt because theinfo
function will add a newline, which is usually not what you want in a prompt.Also be advised that
:io.get_password()
returns input as a charlist, so you will probably want to convert it to a binary as demonstrated above.I wrote a package that utilizes the above technique: https://hex.pm/packages/cowrie
iq0todco2#
显然这有一些问题。目前最好的解决方案似乎是重复清除循环中的输入,正如在Hex包管理器中实现的那样:
https://github.com/hexpm/hex/blob/1523f44e8966d77a2c71738629912ad59627b870/lib/mix/hex/utils.ex#L32-L58
smdncfj33#
只需编写如下代码,直接从
Mix.Tasks.Hex
借用该功能,如果您在任务中请求密码,并且不想一遍又一遍地请求密码,则可以将其保存在如下所示的环境变量中:
当然,还可以使用以下方法检索: