gpt4 book ai didi

ruby - 在 Ruby 中,getoptlong 破坏性地解析 ARGV。有没有解决的办法?

转载 作者:数据小太阳 更新时间:2023-10-29 07:58:10 25 4
gpt4 key购买 nike

我需要多次调用 getoptlong,但在第一次之后 ARGV 为空。

最佳答案

在第一次调用之前捕获参数,完成后将它们放回去。不过,听起来你在做一些奇怪的事情。

编辑:(展开)

这里有很多复制和粘贴。我认为这有助于清晰:

require 'getoptlong'

storage = ARGV.clone

opts = GetoptLong.new(
['--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
[ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
)

puts "Before: #{ARGV.inspect}"
opts.each { |opt, arg| puts "Parsed #{opt} = #{arg}" }
puts "After: #{ARGV.inspect}"

# Copy
storage.each {|x| ARGV << x }

opts = GetoptLong.new(
['--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
[ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
)

puts "Before 2: #{ARGV.inspect}"
opts.each { |opt, arg| puts "Parsed #{opt} = #{arg}" }
puts "After 2: #{ARGV.inspect}"

关于ruby - 在 Ruby 中,getoptlong 破坏性地解析 ARGV。有没有解决的办法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/330008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com