1

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
fishyMac
  • 23
  • 4
  • possible duplicate of [Strange behavior of Ruby's OptionParser lib?](http://stackoverflow.com/questions/9577478/strange-behavior-of-rubys-optionparser-lib) – mu is too short May 21 '13 at 17:25
  • Thanks for this tip, I tried to change my script to follow this answer but that is not working for me either. This is the line that modified in my script: opts.on('-mID mID', '--msgID=mID', 'Message ID used by device') do |mID| But Im still getting the same output on my terminal msgID is still not being set to the number passed in to the command line args – fishyMac May 21 '13 at 17:41
  • You need not to specify argument at both switch versions, one is enough. On the other hand by OptParser nature you cann't define short switch name longer then just *one* character (after the dash). – Torimus May 21 '13 at 18:25

2 Answers2

1

Seems the short switch has to be a single character -s

./push_test.rb -sID "33"

outputs:

{:verbose=>false, :quick=>false, :logfile=>nil, :sID=>"ID", :rID=>"-1" }

because everything after -s to the first white space will be assigned to :sID, in your case its the word "ID" that follows "-s", hence you are getting :sID =>"ID"

./push_test.rb -s "33" will do the trick.

Anand Shah
  • 14,575
  • 16
  • 72
  • 110
0

From the OptParser docs:

Short style switch::   Specifies short style switch which takes a
mandatory, optional or no   argument. It's a string of the following
form:
  "-xMANDATORY"
  "-x[OPTIONAL]"
  "-x"

So at specifying switch -sID you define switch -s with argument named ID - something different than you were probably expecting.

Torimus
  • 379
  • 2
  • 10