Created
November 13, 2012 15:52
-
-
Save mikelikesbikes/4066486 to your computer and use it in GitHub Desktop.
ARGV/OptionParser destruction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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