class Sapristi::ArgumentsParser

Public Class Methods

build_parser(args) click to toggle source
# File lib/sapristi/arguments_parser.rb, line 17
def self.build_parser(args)
  OptionParser.new do |opts|
    ArgumentsParser.populate_options(opts, args)
  end
end
new() click to toggle source
# File lib/sapristi/arguments_parser.rb, line 8
def initialize
  @args = OpenStruct.new
end
parse_integer(value, min = nil, max = nil) click to toggle source

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength

# File lib/sapristi/arguments_parser.rb, line 44
def self.parse_integer(value, min = nil, max = nil)
  raise OptionParser::InvalidOption, "'#{value}' is not an integer" unless value.match(/^[0-9]+$/)

  integer = value.to_i
  raise OptionParser::InvalidOption, "requires a wait time > 0, provided=#{value}" unless min.nil? || integer >= min
  unless max.nil? || integer <= max
    raise OptionParser::InvalidOption, "requires a wait time <= 120 seconds, provided=#{value}"
  end

  integer
end
populate_options(opts, args) click to toggle source

This method smells of :reek:TooManyStatements rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/sapristi/arguments_parser.rb, line 26
def self.populate_options(opts, args)
  opts.banner = 'Usage: sapristi [options]'
  opts.on('-v', '--verbose', 'Verbose mode') { |value| args.verbose = value }
  opts.on('-g', '--group GROUP', 'Use named group definitions') { |value| args.group = value }
  opts.on('-w', '--wait-time NUMBER_OF_SECONDS (1-120), default=30', 'Wait time for detecting a window') do |value|
    args.wait_time = parse_integer(value, 1, 120)
  end
  opts.on('--dry-run', 'Dry run') { |value| args.dry = value }
  opts.on('-f', '--file FILE', 'Read configuration from FILE') { |file| args.file = file }
  opts.on('-h', '--help', 'Prints this help') do
    puts opts
    exit
  end
  opts.on('-m', '--monitors', 'Show monitor\'s info') { args.show_monitors = true }
end

Public Instance Methods

parse(options) click to toggle source
# File lib/sapristi/arguments_parser.rb, line 12
def parse(options)
  ArgumentsParser.build_parser(@args).parse!(options)
  @args
end