RubyのOptionParserのopt.onが引数を無限に取れることをはじめて知った
以下のポストでOptParserの使い方を書きましたが、どうやらopts.onメソッドは引数を無限に取れるらしい
[Ruby]オプションの使い方・取得の仕方(OptionParser) - 脳汁portal
コード(上のポストのを流用)
require 'optparse' opt = OptionParser.new options = {} #opt.on("-f" ,"--foo", "引数なしだよ"){|v| options[:f] = v} opt.on("-f" ,"--foo", "引数なしだよ", "ヒャッハー!", "ヒ-ハー!!", "ya-ha-!!!!!!!"){|v| options[:f] = v} opt.on("-b", "--bar <param>", "引数ありだよ") {|v| options[:bar] = v} opt.banner="usage:#{File.basename($0)} -f --bar [value]" opt.on("-h", "--help", "Show this message") { puts opt; exit } opt.parse!(ARGV) puts options
実行結果
$ ruby opt.rb -h usage:opt.rb -f --bar [value] -f, --foo 引数なしだよ ヒャッハー! ヒ-ハー!! ya-ha-!!!!!!! -b, --bar <param> 引数ありだよ -h, --help Show this message
上の例は適当だが、オプションの例とか入れるにはいいかもしれない
$ ruby opt.rb -h usage:opt.rb -f --bar [value] -h, --help Show this message --storage <dir_path> Specify the Storage directory Ex.)/usr/local/storage/10001/ds --library <dir_path> Specify the library directory Ex.)/lib/libexec_A