3
Je dois appeler getoptlong plusieurs fois, mais après la première fois ARGV est vide.Dans Ruby, getoptlong analyse de manière destructive ARGV. Y a-t-il un moyen de contourner ceci?
Je dois appeler getoptlong plusieurs fois, mais après la première fois ARGV est vide.Dans Ruby, getoptlong analyse de manière destructive ARGV. Y a-t-il un moyen de contourner ceci?
Capturez les arguments avant le premier appel, remettez-les lorsque vous avez terminé. On dirait que vous faites quelque chose de bizarre, cependant.
Modifier: (étendu)
Il y a beaucoup de copier-coller ici. Je considère que aider avec 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}"
Ne fonctionne pas tout à fait. J'appelle getoptlong dans un constructeur de classe. Je sous-classe cette classe (framework TestUnit de ruby) plusieurs fois dans un fichier. La redéfinition d'ARGV à partir d'une méthode génère une erreur dans Ruby –
Vous n'avez pas besoin de la redéfinir. Je vais développer. – Dustin
Fonctionne très bien, merci pour la clarification! –