Im trying to use the Ruby builtin options parser
I have this file
File parser.rb #!/usr/bin/env ruby require 'optparse' require 'pp'
class parser
def initialize(args)
@options = Hash.new()
@op = OptionParser.new do |opts|
@options[:verbose] = false
opts.on('-v', '--verbose', 'Output more information') do
@options[:verbose] = true
end
@options[:quick] = false
opts.on( '-q', '--quick', 'Perform the task quickly' ) do
@options[:quick] = true
end
@options[:logfile] = nil
opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) do|file|
@options[:logfile] = file
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
@options[:sID] = "-1"
opts.on('-sID', '--senderID', 'Sender ID used by device') do |sID|
@options[:sID] = sID
end
@options[:rID] = "-1"
opts.on('-rID', '--receiverID', 'Receiver ID used by device') do |rID|
@options[:rID] = rID
end
@op.parse!
@op
end
def getOptionsHash
@options
end
then Im trying to use this class in the file below
#!/usr/bin/env ruby
# Setup Bundler
require 'rubygems'
require 'bundler/setup'
require_relative 'parser'
#Variables in the options hash in parser.rb
op = Parser.new(ARGV)
pp op.getOptionsHash()
when I run this on the command line without args it uses default values: ./push_test.rb
I get the following output:
{:verbose=>false,
:quick=>false,
:logfile=>nil,
:sID=>"-1",
:rID=>"-1",
}
when I run this on the command line with args: ./push_test.rb -sID "33"
I get the following output:
{:verbose=>false,
:quick=>false,
:logfile=>nil,
:sID=>"ID",
:rID=>"-1",
}
Why is the sID not being set to 33?
Can anyone help please?Ive tried to figure this out but cant make any headway