Dans Ruby, getoptlong analyse de manière destructive le virus ARGV. Y a-t-il un moyen de contourner ceci?

StackOverflow https://stackoverflow.com/questions/330008

  •  11-07-2019
  •  | 
  •  

Question

Je dois invoquer getoptlong plusieurs fois, mais après la première fois, ARGV est vide.

Était-ce utile?

La solution

Capturez les arguments avant le premier appel, remettez-les lorsque vous avez terminé. On dirait que vous faites quelque chose de bizarre, cependant.

Modifier : (développé)

Il y a beaucoup de copier-coller ici. Je considère que cela aide à la clarté:

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}"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top