Skip to content

Instantly share code, notes, and snippets.

@dgrijalva
Created September 1, 2011 23:22
Show Gist options
  • Save dgrijalva/1187559 to your computer and use it in GitHub Desktop.
Save dgrijalva/1187559 to your computer and use it in GitHub Desktop.
Option Parsing in rb
#!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
# OPTIONS PARSING
options = OpenStruct.new
parser = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [options] [files]"
opts.on('-v', '--verbose', 'Verbose') do
# TODO: add some verbosity
options.verbose = true
end
opts.on('-f', '--file="file"', "The file to use") do |file|
options.filename = filename
end
opts.on('-h', '--help', 'Show this message') do
puts opts
exit
end
end
parser.parse!(ARGV)
# Anything unparsed remains in ARGV
# in the above example files = ARGV after parsing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment