Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:OptionParser:String Arg&;散列赋值_Ruby_Variables_Split_Variable Assignment_Optionparser - Fatal编程技术网

Ruby:OptionParser:String Arg&;散列赋值

Ruby:OptionParser:String Arg&;散列赋值,ruby,variables,split,variable-assignment,optionparser,Ruby,Variables,Split,Variable Assignment,Optionparser,使用OptionParser进行字符串参数输入和哈希赋值。为一个参数读入多个变量的最佳方法是什么?然后如何将它们分配给要引用的哈希?以下是我到目前为止的情况: large_skus = Hash.new small_skus = Hash.new OptionParser.new do |opts| opts.on("-b", "--brands bName1,bName2,bNameN", String, "Check specific brands by name") do |b| o

使用OptionParser进行字符串参数输入和哈希赋值。为一个参数读入多个变量的最佳方法是什么?然后如何将它们分配给要引用的哈希?以下是我到目前为止的情况:

large_skus = Hash.new
small_skus = Hash.new

OptionParser.new do |opts|

opts.on("-b", "--brands bName1,bName2,bNameN", String, "Check specific brands by name") do |b|
 options[:brands] = b.split(",")
end

opts.on("-l", "--large lSku1,lSku2,lSkuN", String, "Large SKUs - List CSVs") do |l|
 options[:large_skus] = l.split(",")
 ##For each sku given
 brandName = options[:brands]
 large_skus[brandName] = l[$sku].to_i
 ##
end

opts.on("-s", "--small sSku1,sSku2,sSkuN", String, "Small SKUs - List CSVs") do |s|
 options[:small_skus] = s.split(",")
 ##For each sku given
 brandName = options[:brands]
 small_skus[brandName] = s[$sku].to_i
 ##
end

end.parse!(ARGV)
如果输入:

 ruby test.rb --brands bName1,bName2,bNameN --large lSku1,lSku2,lSkuN --small wQueue1,wQueue2,wQueueN
此代码

#!/usr/bin/env ruby

require 'ap'
require 'optparse'

options = {}
OptionParser.new do |opts|

  opts.on("-b", "--brands bName1,bName2,bNameN", Array, "Check specific brands by name") do |b|
    options[:brands] = b
  end

  opts.on("-l", "--large lSku1,lSku2,lSkuN", Array, "Large SKUs - List CSVs") do |l|
    options[:large_skus] = l
  end

  opts.on("-s", "--small wQueue1,wQueue2,wQueueN", Array, "Small SKUs - List CSVs") do |s|
    options[:small_skus] = s
  end

end.parse!(ARGV)

ap options
生成此输出:

{
        :brands => [
        [0] "bName1",
        [1] "bName2",
        [2] "bNameN"
    ],
    :large_skus => [
        [0] "lSku1",
        [1] "lSku2",
        [2] "lSkuN"
    ],
    :small_skus => [
        [0] "wQueue1",
        [1] "wQueue2",
        [2] "wQueueN"
    ]
}

请注意,我使用的不是每个选项的字符串类型,而是数组。这让OptionParser能够完成将元素解析为数组的繁重工作。从这一点上看,如何处理数组元素取决于您。

我认为您的做法是错误的。您希望您的用户必须跟踪他们输入的参数的顺序,但您不希望自己在代码中这样做

你不要求任何人跟踪什么与什么相关,并使其明确:

ruby test.rb --input bName1,lSku1,wQueue1 --input bName2,lSku2,wQueue2 --input bNameN,lSkuN,wQueueN
代码:


要知道,阵列是一个很好的选择。散列赋值呢?我更新了我原来的帖子,我注意到一些打字错误(队列与sku)。我的想法是,我不必按索引号迭代大小sku阵列以找到它们的匹配/关联品牌。根据示例代码,您试图做的事情并不明显。您的代码缺少变量($sku),并且您没有解释使用大sku和小sku散列的意图。
opts.on("--input <brand,Large_Skus,Small_Skus>", "input description",
        "NOTE: Can be used more than once.") do |opt|
  list = opt.split(',')
  unless list.lenght == 3
    raise "some error because you didn't place all arguments"
  end
  options[:input].push list
end 
[ [ 'bName1', 'lSku1', 'wQueue1' ],
  [ 'bName2', 'lSku2', 'wQueue2' ],
  [ 'bNameN', 'lSkuN', 'wQueueN' ] ]