Skip to content

Instantly share code, notes, and snippets.

@mikelikesbikes
Created November 13, 2012 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikelikesbikes/4066486 to your computer and use it in GitHub Desktop.
Save mikelikesbikes/4066486 to your computer and use it in GitHub Desktop.
ARGV/OptionParser destruction
require 'optparse'
require 'ostruct'
options = OpenStruct.new
parser = OptionParser.new do |opts|
opts.on("-f", "--force", "Force something to happen") do |force|
options.force = force
end
end
# let's see what ARGV is before it's parsed
puts ARGV.inspect
# by default, OptionParser consumes the argument (default ARGV), so
# dup it before parsing will preserve ARGV.
argv_copy = ARGV.dup
parser.parse!(argv_copy)
puts options.inspect
# look... ARGV is preserved, while argv_copy is consumed.
puts ARGV.inspect
puts argv_copy.inspect
# run with:
# ruby argv.rb -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment