Comment detail

起動オプションの解析 (Nested Flatten)

 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
25
26
27
28
29
require "optparse"

conf = Hash.new
opts = OptionParser.new
opts.on("-o","--output","set output") { |v| conf[:o] = v }
opts.on("-q","--quote","set quote") { |v| conf[:q] = v }
opts.on("-d DEBUG","--debug=DEBUG",["0","1","2"],"set debug level") { |v| conf[:d] = v }

begin
    opts.parse!
    if ARGV.size == 0 || !conf.key?(:o)
        raise OptionParser::InvalidArgument.new
    else
        puts "[オプション情報]"
        puts "o(output):\t#{conf[:o] ? "ON" : "OFF"}"
        puts "q(quote):\t#{(conf[:q] || false) ? "ON" : "OFF"}"
        puts "d(debug):\t#{conf[:d] || 0}"
        puts ""
        puts "[パラメータ情報]"
        puts "指定数:\t#{ARGV.size}"
        ARGV.zip((1..ARGV.size).to_a) do |a,i|
            puts "#{i}:\t#{a}"
        end
    end
rescue OptionParser::InvalidArgument => ex
    puts opts.help
rescue => ex
    puts ex.message
end

Index

Feed

Other

Link

Pathtraq

loading...