Search code examples
rubypasswordsoptionparser

how can ruby optionparser hidden password when user type


When I use optionparser, I always type like this

ruby xxx.rb -u username -p password

but it's very dangerous everyone can see the password, I check the ruby api but can not find about hidden password, help me please

Thank you all the same


Solution

  • To hide your input when asking from input in a ruby program you can use the IO built in class.

    require 'io/console'
    
    STDIN.noecho(&:gets
    

    This is directly from the ruby standard library. Use ruby docs IO class for more information. Here is the link

    If I had time I would implement it into option parse for you but you can most likely figure it out pretty easily. If not, I will help you tomorrow.

    Happy coding!