简体   繁体   English

Ruby Options解析器不读取命令行选项

[英]Ruby Options parser not reading command line options

Im trying to use the Ruby builtin options parser 我正在尝试使用Ruby内置选项解析器

I have this file 我有这个档案

File parser.rb #!/usr/bin/env ruby require 'optparse' require 'pp' 文件parser.rb#!/ usr / bin / env ruby​​需要'optparse'需要'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 当我在不使用args的命令行上运行它时,它使用默认值:./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" 当我在带有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? 为什么未将sID设置为33?

Can anyone help please?Ive tried to figure this out but cant make any headway 任何人都可以帮忙吗?我试图解决这个问题,但没有取得任何进展

Seems the short switch has to be a single character -s 似乎短开关必须是单个字符-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" 因为-s到第一个空格的所有内容都将分配给:sID,在您的情况下,它的单词“ ID”紧跟在“ -s”之后,因此您将得到:sID =>"ID"

./push_test.rb -s "33" will do the trick. ./push_test.rb -s "33"将解决问题。

From the OptParser docs : 从OptParser 文档

 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. 因此,在指定switch -sID您将使用名称为ID参数定义switch -s与您可能期望的有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM