Ruby OptionParser Usage
OptionParser
這個東西在 python 應該是 setup script,而在 ruby 不知道叫什麼 google 了老半天終於被我找到了。用法上很簡單大概看 ruby doc 的範例就會使用了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| require 'optparse' require 'optparse/time'
options = {} OptionParser.new do |opts| opts.banner = "Usage: example.rb [options]"
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| options[:verbose] = v end
opts.on("-nNAME", "--name=NAME", "pass NAME arg with assignment") do |v| options[:verbose] = v end
opts.on("-r", "--require LIB", "pass LIB arg") do |v| options[:verbose] = v end
opts.on("-t", "--time [TIME]", Time, "pass TIME arg and check type") do |v| options[:verbose] = v end end.parse!
p options p ARGV
|
將 OptionParser 封裝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| require 'optparse'
Options = Struct.new(:name)
class Parser def self.parse(options) args = Options.new("world")
opt_parser = OptionParser.new do |opts| opts.banner = "Usage: example.rb [options]"
opts.on("-nNAME", "--name=NAME", "Name to say hello to") do |n| args.name = n end end
opt_parser.parse!(options) return args end end
options = Parser.parse %w[--name=akiicat] options = Parser.parse %w[-nakiicat] options.name
|
Ruby Doc 2.3.1 OptionParser