본문 바로가기
조회 수 2869 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
#!/usr/bin/env ruby
require 'optparse'

options = {}
opt_parser = OptionParser.new do |opt|
    opt.banner = "Usage:"
    opt.separator "     alert_gen.rb -H host -P protocol [-p port] -f alerts_json_file"
    opt.separator ""
    opt.separator "Examples:"
    opt.separator "     alert_gen.rb -H 192.168.1.1 -P http -p 80 -f alert.txt"
    opt.separator "     alert_gen.rb -H 192.168.1.1 -P syslog -p 514 -f alert.txt"
    opt.separator ""
    opt.separator "Options"

    opt.on("-H", "--host host", "host to send json to") do |host|
        options[:host] = host
    end
    opt.on("-P", "--protocol protocol", "protocol to send json in") do |protocol|
        options[:protocol] = protocol.downcase
        options[:port] = case options[:protocol].downcase
                             when 'http' then
                                 80
                             when 'syslog' then
                                 514
                         end
    end
    opt.on("-p", "--port port", "port number for protocol") do |port|
        options[:port] = port
    end
    opt.on("-f", "--filename filename", "filename to save the script output as") do |filename|
        options[:filename] = filename
    end
    opt.on("-h", "--help", "help") do
        puts opt_parser
    end
end

begin
    opt_parser.parse!
rescue OptionParser::ParseError
    $stderr.puts "Error: #{$!}"
    exit
end

# debug
puts "options: #{options}"
puts "size of options: #{options.size}"
puts "-" * 20

# validation
abort "Error: there is one or more arguments missed" unless options.size == 4


Hojungs-MacBook-Air:_ruby_alert_generator hyun$ ./alert_gen.rb -P http

options: {:protocol=>"http", :port=>80}

size of options: 2

--------------------

Error: there is one or more arguments missed


Hojungs-MacBook-Air:_ruby_alert_generator hyun$ ./alert_gen.rb -P HttP

options: {:protocol=>"http", :port=>80}

size of options: 2

--------------------

Error: there is one or more arguments missed



Title
List of Articles
번호 제목 글쓴이 날짜 조회 수
31 Ruby Symbols Hojung 2014.04.05 6740
30 Best Ruby On Rails Content Management Systems (CMS) Hojung 2014.04.26 3121
29 Install Rbenv and Ruby on Mac OS X Mavericks 10.9 Hojung 2014.02.19 2872
» optparse: mandatory and optional arguments example Hojung 2014.03.14 2869
27 Refresh content automatically after some period time on Rails Hojung 2015.01.22 2854
26 Ruby Style Guide Hojung 2015.02.05 2286
25 Ruby map, each, collect, inject, reject, select quick reference Hojung 2014.06.10 2275
24 splat operator to get the tail and head of an array Hojung 2014.03.11 2228
23 How to run commands from Ruby (`backticks, %x, exec and system) file Hojung 2014.03.09 2182
22 deploying cron jobs with whenever gem Hojung 2014.06.01 2174
21 install Rbenv, ruby and rails Hojung 2014.03.22 2143
20 How to handle arguments in ruby command line tool Hojung 2014.03.01 2140
19 remove child association model when parent removed (rails-dependent-destroy) Hojung 2014.06.03 2139
18 HTTP Posts in Ruby Hojung 2014.03.14 2134
17 How to install Ruby 2.1.0 in CentOS using Rvm Hojung 2014.03.13 2118
16 remove nil from array (compact, compact!) Hojung 2014.06.10 2100
15 == , eql? , equal? 비교 Hojung 2014.02.19 2085
14 === vs. cover?, member?, include? Hojung 2014.03.11 2071
13 Install ruby-filemagic on Mac (brew, libmagic) Hojung 2014.12.03 2062
12 Ruby Basics Hojung 2014.03.08 2042
Board Pagination ‹ Prev 1 2 Next ›
/ 2

Designed by sketchbooks.co.kr / sketchbook5 board skin

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5