Module: GitEvolution::OptionHandler

Defined in:
lib/git_evolution/option_handler.rb

Class Method Summary collapse

Class Method Details

.parse_options(args) ⇒ Object

Raises:



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
30
# File 'lib/git_evolution/option_handler.rb', line 3

def self.parse_options(args)
  options = OpenStruct.new(
    range: nil,
    since: nil,
    start_line: nil,
    end_line: nil
  )

  OptionParser.new do |opts|
    opts.banner = 'Usage: git_evolution [options] <file>'
    opts.version = VERSION
    opts.on '-r', '--range N:N', String, 'The specified range of lines to consider within the file (optional)' do |value|
      options.range = value
    end
    opts.on '-s', '--since STRING', String, 'Consider the commits which are more recent than the specified time (optional)' do |value|
      options.since = value
    end
  end.parse!

  raise(FileMissingError, 'Missing required file argument') if args.empty?

  options[:file] = File.expand_path(args[0])
  options[:start_line], options[:end_line] = parse_range(options[:range])

  validate_options!(options)

  options
end

.parse_range(range) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/git_evolution/option_handler.rb', line 32

def self.parse_range(range)
  return if range.nil?

  regex_matches = range.match(/^(\d+):(\d+)/)
  raise(InvalidRangeFormatError, 'The --range option was not in the valid format (N:N)') if regex_matches.nil?

  start_line = regex_matches[1].to_i
  end_line = regex_matches[2].to_i

  return start_line, end_line
end

.validate_options!(options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/git_evolution/option_handler.rb', line 44

def self.validate_options!(options)
  if options.file.nil?
    raise(FileMissingError, 'Missing required file argument')
  elsif !File.exist?(options.file)
    raise(FileDoesNotExistError, "File #{options.file} does not exist")
  end

  if !options.range.nil?
    raise(RangeOutOfBoundsError, 'Start line cannot be greater than the end line') if options.start_line > options.end_line

    file_length = File.new(options.file).readlines.size
    raise(RangeOutOfBoundsError, "End line cannot be larger than the length of the file (#{file_length})") if options.end_line > file_length
  end

  if !options.since.nil?
    options.since = Chronic.parse(options.since)
    raise(TimeParseError, 'The since time could not be properly parsed') if options.since.nil?
  end
end