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

ruby – 如何生成OptionParser需要参数

发布时间:2020-12-16 21:35:10 所属栏目:百科 来源:网络整理
导读:下面的代码可以工作,但是当我想要为必需的参数构建必需参数到本机的OptionParser sytax时,我手动提取使用fetch所需参数的参数错误: # ocra script.rb -- --type=valueoptions = {}OptionParser.new do |opts| opts.banner = "Usage: example.rb [options]"
下面的代码可以工作,但是当我想要为必需的参数构建必需参数到本机的OptionParser sytax时,我手动提取使用fetch所需参数的参数错误:
# ocra script.rb -- --type=value
options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: example.rb [options]"

  opts.on("--type [TYPE]",String,[:gl,:time],"Select Exception file type (gl,time)") do |t|
    options["type"] = t
  end

  opts.on("--company [TYPE]",[:jaxon,:doric],"Select Company (jaxon,doric)") do |t|
    options["company"] = t
  end

end.parse!

opts = {}
opts['type'] = options.fetch('type') do
  raise ArgumentError,"no 'type' option specified  as a parameter (gl or time)"
end

opts['company'] = options.fetch('company') do
  raise ArgumentError,"no 'company' option specified  as a parameter (doric or jaxon)"
end

解决方法

有一个类似的问题,答案可能会帮助你:
“ How do you specify a required switch (not argument) with Ruby OptionParser?”

简而言之:似乎没有办法选择需要的(毕竟它们被称为选项).

有一个OptionParser :: MissingArgument异常,您可以加注,而不是您当前抛出的ArgumentError.

(编辑:李大同)

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

    推荐文章
      热点阅读