【问题标题】:Slop parsing command line arguments with multiple inputs per argument option in ruby在ruby中使用每个参数选项的多个输入来解析命令行参数
【发布时间】:2020-09-12 14:38:28
【问题描述】:

我在 ruby​​ 中使用 Slop 来解析输入参数:

slop_opts = Slop.parse(ARGV.map(&:strip)) do |o|                                
  o.string '--test1', 'explain test1'                             
  o.string '--test2', 'explain test2'                                      
  o.on '--help' do                                                              
    puts o                                                                      
    exit                                                                        
  end                                                                           
end                                                                             
                                                                                
slop_opts.to_hash      

我需要test2 可以包含几个选项: 例如

ruby this_script.rb --test1 one_arg --test2 first_arg second_arg

我的一个限制是我需要 first_arg 和 second_arg 是 2 个不同的输入,所以我不能只在,(或类似的)输入字符串(如first_arg,second_arg)上拆分它们。

感谢您的帮助!

【问题讨论】:

    标签: ruby parsing command-line-interface


    【解决方案1】:

    --test2 设为Array argument。将分隔符设置为nil 以禁用拆分输入。

    slop_opts = Slop.parse(ARGV.map(&:strip)) do |o|                                
      o.string '--test1', 'explain test1'                             
      o.array '--test2', 'explain test2', delimiter: nil                                  
      o.on '--help' do                                                              
        puts o                                                                      
        exit                                                                        
      end                                                                           
    end  
    

    然后每个输入都有自己的--test2

    ruby this_script.rb --test1 one_arg --test2 first_arg --test2 second_arg
    

    【讨论】:

      猜你喜欢
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 2022-01-26
      • 2017-08-31
      • 2019-07-10
      • 2021-12-07
      • 2014-02-19
      相关资源
      最近更新 更多