Tag: Ruby/OptionParser
require 'optparse' opt = OptionParser.new opt.banner = "Usage: #{opt.program_name} [-h|--help] <args>" opt.separator("#{opt.program_name} Available Options") opt.version = '2.3.4' opt.on_head('-h', '--help', 'Show this message') do |v| puts opt.help exit end opts = {} opt.on('-a VAL') {|v| opts[:a] = v } opt.on('-b') {|v| opts[:b] = v } opt.on('-c VAL', '--check-type=VAL') {|v| opts[:b] = v } opt.parse!(ARGV) p opts
$ ruby option.rb -a foo -b {:a=>"foo", :b=>true}
ruby option.rb --help Usage: option [-h|--help] <args> -h, --help Show this message option Available Options -a VAL -b -c, --check-type=VAL
opts = {} opt = OptionParser.new opt.on('-f VAL', '--file=VAL') {|v| opts[:file] = v } opt.parse!(ARGV) if opts[:file].nil? || !FileTest.file?(opts[:file]) puts opt # putsでヘルプが表示sあれる exit end