Skip to content

Instantly share code, notes, and snippets.

@vranystepan
Created March 16, 2012 05:36
Show Gist options
  • Save vranystepan/2048689 to your computer and use it in GitHub Desktop.
Save vranystepan/2048689 to your computer and use it in GitHub Desktop.
require 'optparse'
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: ruby #{File.basename(__FILE__)} -t <target ip address> -i <input file name> [-o <output file name>]"
opts.on( '-t', '--target HOST', 'Target\'s IP address' ) do|h|
options[:host] = h
end
opts.on('-i', '--input FILE', 'Input file name') do |f|
options[:input] = f
end
opts.on('-o', '--output FILE', 'Input file name') do |f|
options[:output] = f
end
end
begin
optparse.parse!
mandatory = [:host, :input]
missing = mandatory.select{ |param| options[param].nil? }
if not missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts optparse
exit
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
puts optparse
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment