In Ruby, getoptlong analizza in modo distruttivo ARGV. C'è un modo per aggirare questo?
Domanda
Devo invocare getoptlong più volte, ma dopo la prima volta ARGV è vuoto.
Soluzione
Cattura gli arg prima della prima chiamata, rimettili al termine. Sembra che tu stia facendo qualcosa di strano, però.
Modifica : (espanso)
C'è un sacco di copia e incolla qui. Ritengo che aiutare con chiarezza:
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}"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow