【问题标题】:Read raw arguments passed to ruby script读取传递给 ruby​​ 脚本的原始参数
【发布时间】:2014-12-25 10:20:57
【问题描述】:

我想读取 ruby​​ 脚本的原始输入并进一步传递它。

例如:

script.rb -t test -a argument three -b test

在这个脚本中,我需要一个带有字符串-t test -a argument three -b test 的变量,稍后其他函数将对其进行解析。

标准OptPartserARGV 功能从流中删除-flags,或者需要大量的硬编码...

标志之前没有定义,它应该被视为一个。

【问题讨论】:

    标签: ruby scripting console arguments parameter-passing


    【解决方案1】:

    查看 Ruby 的酷炫OptionParser 库。

    它提供对标志/开关、具有可选或必需值的参数的解析,可以将参数列表解析为单个选项,并可以为您生成帮助。

    这里有一些可以玩的示例:

    require 'optparse'
    require 'yaml'
    
    options = {}
    OptionParser.new do |opts|
      opts.banner = "Usage: example.rb [options]"
    
      opts.on('-t', '--sourcename test', 'test') { |v| options[:test] = v }
      opts.on('-a', '--sourcehost argument', 'args') { |v| options[:args] = v }
      opts.on('-b', '--sourceport something', 'Something') { |v| options[:something] = v }
    
    end.parse!
    

    【讨论】:

      猜你喜欢
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      相关资源
      最近更新 更多