code_junkie

In Ruby, getoptlong destructively parses ARGV. Is there a way around this?

Nov 12th, 2011
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. require 'getoptlong'
  2.  
  3. storage = ARGV.clone
  4.  
  5. opts = GetoptLong.new(
  6. ['--help', '-h', GetoptLong::NO_ARGUMENT ],
  7. [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
  8. [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
  9. )
  10.  
  11. puts "Before: #{ARGV.inspect}"
  12. opts.each { |opt, arg| puts "Parsed #{opt} = #{arg}" }
  13. puts "After: #{ARGV.inspect}"
  14.  
  15. # Copy
  16. storage.each {|x| ARGV << x }
  17.  
  18. opts = GetoptLong.new(
  19. ['--help', '-h', GetoptLong::NO_ARGUMENT ],
  20. [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
  21. [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
  22. )
  23.  
  24. puts "Before 2: #{ARGV.inspect}"
  25. opts.each { |opt, arg| puts "Parsed #{opt} = #{arg}" }
  26. puts "After 2: #{ARGV.inspect}"
Add Comment
Please, Sign In to add comment