I am exploring the parsing option in ruby, referring to this and this . This is my test code:
require "optparse"
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Learning Option parsing in Ruby"
opts.on("-i", "--ipaddress", "IP address of the server") do |ipaddr|
options[:ipaddress] = ipaddr
end
opts.on("-u", "--username", "username to log in") do |user|
options[:username] = user
end
opts.on("-p", "--password", "password of the user") do |pass|
options[:password] = pass
end
end
optparse.parse!
puts "the IPaddress is #{options[:ipaddress]}" if options[:ipaddress]
puts "the username is #{options[:username]}" if options[:username]
puts "the password is #{options[:password]}" if options[:password]
My intention is to print the wholesale that I pass to the script. However, it does not print the option I am passing, instead it simply says true:
the IPaddress is true
the username is true
the password is true
Where am I mistaken? I also tried using short options, but the result is the same.
source
share