如何从 Ruby 的命令行读取密码? [英] How can I read a password from the command line in Ruby?

查看:47
本文介绍了如何从 Ruby 的命令行读取密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 机器上运行 Ruby 和 MySQL.

I am running Ruby and MySQL on a Windows box.

我有一些 Ruby 代码需要连接到 MySQL 数据库并执行选择.要连接到数据库,我需要提供密码等.

I have some Ruby code that needs to connect to a MySQL database a perform a select. To connect to the database I need to provide the password among other things.

Ruby 代码可以显示要求输入密码的提示,用户输入密码并按 Enter 键.我需要的是输入的密码显示为一行星号.

The Ruby code can display a prompt requesting the password, the user types in the password and hits the Enter key. What I need is for the password, as it is typed, to be displayed as a line of asterisks.

如何让 Ruby 将键入的密码显示为dos 框"中的一行星号?

How can I get Ruby to display the typed password as a line of asterisks in the 'dos box'?

推荐答案

为了回答我自己的问题,并为了其他想知道的人的利益,有一个名为 HighLine 你需要的.

To answer my own question, and for the benefit of anyone else who would like to know, there is a Ruby gem called HighLine that you need.

require 'rubygems'
require 'highline/import'

def get_password(prompt="Enter Password")
   ask(prompt) {|q| q.echo = false}
end

thePassword = get_password()

这篇关于如何从 Ruby 的命令行读取密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆