Class: Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/git_survey/arguments_parser.rb

Overview

Parses command line arguments

Class Method Summary collapse

Class Method Details

.parse(argv) ⇒ Object

[View source]

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/git_survey/arguments_parser.rb', line 23

def self.parse(argv)
  # If no arguments supplied, print help
  argv << '-h' if argv.empty?

  result = default_options

  options_parser = OptionParser.new do |o|
    o.banner = 'Usage: git-survey.rb [options] [input directory]'

    o.on('-a',
         '--anonymize',
         "Anonymizes the output (#{result.anonymize})") do |v|
      result.anonymize = v
    end

    o.on('-bBRANCH',
         '--branch=BRANCH',
         "Git branch (#{result.git_branch})") do |v|
      result.git_branch = v
    end

    o.on('-nNUMBER',
         '--number=NUMBER',
         "Number of hot files to display (#{result.number_of_hotfiles})") do |v|
      result.number_of_hotfiles = v
    end

    o.on('-tTODAY',
         '--today=TODAY',
         "Today's date for testing purposes (string)") do |v|
      result.scan_date = v
    end

    o.on('-h',
         '--help',
         'Prints this help') do
      puts options_parser
      exit 0
    end
  end

  begin
    options_parser.parse!(argv)
  rescue StandardError => exception
    puts exception
    puts options_parser
    exit 1
  end

  result.input_directory = argv.pop
  if result.input_directory.nil? || !Dir.exist?(result.input_directory)
    puts 'Can\'t find directory ' + result.input_directory
    Parser.parse %w[--help]
    exit 0
  end

  result
end