Class: CheckFromFile::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/check_from_file/cli.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/check_from_file/cli.rb', line 7

def self.parse(args)
  options = OpenStruct.new
  options[:file_age_warning] = 150
  options[:file_age_critical] = 300

  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} -c <command> -o <stdout> -e <stderr> -r <ret>"

    opts.on('-c', '--command COMMAND', :REQUIRED, 'Command that was run to generate this output') do |command|
      options[:command] = command
    end

    opts.on('-o', '--stdout FILE', 'File that contains STDOUT') do |stdout|
      options[:stdout] = stdout
    end

    opts.on('-e', '--stderr FILE', 'File that contains STDERR') do |stderr|
      options[:stderr] = stderr
    end

    opts.on('-r', '--return FILE', 'File that contains return code') do |ret|
      options[:return] = ret
    end

    opts.on('-W', '--file-age-warn SECONDS', 'WARN when file is older than SECONDS. Default: 150') do |ret|
      options[:file_age_warning] = ret.to_i
    end

    opts.on('-C', '--file-age-crit SECONDS', 'CRIT when file is older than SECONDS. Default: 300') do |ret|
      options[:file_age_critical] = ret.to_i
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    opts.on_tail("--version", "Show version") do
      puts CheckFromFile::VERSION
      exit
    end
  end

  opt_parser.parse!(args)

  %w(command stdout stderr return).each do |opt|
    if options[opt.to_sym].nil?
      puts "Missing required option #{opt}"
      puts opt_parser
      exit 1
    end
  end
  options
end