如何在 Racket 中获取命令行密码输入
How to get command-line password input in Racket
我正在尝试在命令行上输入密码而不在输入时显示密码。下面这段代码不合适,因为它会在用户在命令行中键入密码时显示密码:
(display "Password: ")
(define password (read-line))
如何使用 Racket 在命令行输入密码?
这不是真正的 Racket 问题,而是终端的能力问题。这是使用 stty
的一种解决方案。根据您希望程序的复杂程度,还有其他解决方案。
#lang racket
(define (read-password)
(system "stty -echo")
(begin0 (read-line)
(system "stty echo")
(newline)))
(display "Password: ")
(define password (read-password))
(display "Username: ")
(define user (read-line))
(printf "User ~a's secret is: ~a\n" user password)
Password:
Username: Randall
User Randall's secret is: correcthorsebatterystaple
附录:还有一个包 https://pkgs.racket-lang.org/package/get-pass 提供此功能,请使用 raco pkg install get-pass
安装它。
我正在尝试在命令行上输入密码而不在输入时显示密码。下面这段代码不合适,因为它会在用户在命令行中键入密码时显示密码:
(display "Password: ")
(define password (read-line))
如何使用 Racket 在命令行输入密码?
这不是真正的 Racket 问题,而是终端的能力问题。这是使用 stty
的一种解决方案。根据您希望程序的复杂程度,还有其他解决方案。
#lang racket
(define (read-password)
(system "stty -echo")
(begin0 (read-line)
(system "stty echo")
(newline)))
(display "Password: ")
(define password (read-password))
(display "Username: ")
(define user (read-line))
(printf "User ~a's secret is: ~a\n" user password)
Password:
Username: Randall
User Randall's secret is: correcthorsebatterystaple
附录:还有一个包 https://pkgs.racket-lang.org/package/get-pass 提供此功能,请使用 raco pkg install get-pass
安装它。