加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby Option解析器

发布时间:2020-12-17 03:48:32 所属栏目:百科 来源:网络整理
导读:我正在通过引用 this和 this来学习ruby中的选项解析.这是我的测试代码: #!/usr/bin/rubyrequire "optparse"options = {}optparse = OptionParser.new do |opts| opts.banner = "Learning Option parsing in Ruby" opts.on("-i","--ipaddress","IP address o
我正在通过引用 this和 this来学习ruby中的选项解析.这是我的测试代码:

#!/usr/bin/ruby
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]

我的目的是打印我传递给脚本的opton.但是,它不打印我通过的选项,而只是说是真的:

# ruby getops.rb  --ipaddress 1.1.1.1
the IPaddress is true
# ruby getops.rb  --username user1
the username is true
# ruby getops.rb  --password secret
the password is true

我哪里错了?我尝试了短期选项,但结果是一样的.

解决方法

# Mandatory argument.
  opts.on("-r","--require LIBRARY","Require the LIBRARY before executing your script") do |lib|
    options.library << lib
  end

我发现你的第一个链接.注意“–require LIBRARY”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读