parsing - Ruby Options parser not reading command line options -
im trying use ruby builtin options parser
i have file
file parser.rb #!/usr/bin/env ruby require 'optparse' require 'pp'
class parser def initialize(args) @options = hash.new() @op = optionparser.new |opts| @options[:verbose] = false opts.on('-v', '--verbose', 'output more information') @options[:verbose] = true end @options[:quick] = false opts.on( '-q', '--quick', 'perform task quickly' ) @options[:quick] = true end @options[:logfile] = nil opts.on( '-l', '--logfile file', 'write log file' ) do|file| @options[:logfile] = file end opts.on( '-h', '--help', 'display screen' ) puts opts exit end @options[:sid] = "-1" opts.on('-sid', '--senderid', 'sender id used device') |sid| @options[:sid] = sid end @options[:rid] = "-1" opts.on('-rid', '--receiverid', 'receiver id used device') |rid| @options[:rid] = rid end @op.parse! @op end def getoptionshash @options end
then im trying use class in file below
#!/usr/bin/env ruby # setup bundler require 'rubygems' require 'bundler/setup' require_relative 'parser' #variables in options hash in parser.rb op = parser.new(argv) pp op.getoptionshash()
when run on command line without args uses default values: ./push_test.rb
i following output:
{:verbose=>false, :quick=>false, :logfile=>nil, :sid=>"-1", :rid=>"-1", }
when run on command line args: ./push_test.rb -sid "33"
i following output:
{:verbose=>false, :quick=>false, :logfile=>nil, :sid=>"id", :rid=>"-1", }
why sid not being set 33?
can please?ive tried figure out cant make headway
seems short switch has single character -s
./push_test.rb -sid "33"
outputs:
{:verbose=>false, :quick=>false, :logfile=>nil, :sid=>"id", :rid=>"-1" }
because after -s first white space assigned :sid, in case word "id" follows "-s", hence getting :sid =>"id"
./push_test.rb -s "33"
trick.
Comments
Post a Comment