Module: GithubContributions::OptionHandler

Defined in:
lib/github_contributions/option_handler.rb

Class Method Summary collapse

Class Method Details

.parse_options(args) ⇒ Object



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
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/github_contributions/option_handler.rb', line 3

def self.parse_options(args)
  options = OpenStruct.new(
    author: nil,
    org: nil,
    start_date: Time.current.at_beginning_of_day,
    end_date: Time.current.at_end_of_day
  )

  OptionParser.new do |opts|
    opts.banner = "      Usage: github_contributions [options]\n      You need a GitHub Personal Access Token in your ENVs as GITHUB_ACCESS_TOKEN for this to work.\n      Dates are specified using natual language (via chronic gem).\n      The GitHub's Events API only returns the last 300 events, and the filtering is client-side.\n    TEXT\n    opts.version = VERSION\n    opts.on \"-a\", \"--author STRING\", String, \"The author of the contributions\" do |value|\n      options.author = value\n    end\n    opts.on \"-o\", \"--org STRING\", String, \"Filter contributions by the organization they belong in (optional)\" do |value|\n      options.org = value\n    end\n    opts.on \"-s\", \"--start_date STRING\", String, \"Filter contributions to be after the beginning of the specified day (defaults to today)\" do |value|\n      options.start_date = Chronic.parse(value)&.at_beginning_of_day\n    end\n    opts.on \"-e\", \"--end_date STRING\", String, \"Filter contributions to be before the end of the specified day (defaults to today)\" do |value|\n      options.end_date = Chronic.parse(value)&.at_end_of_day\n    end\n  end.parse!\n\n  raise(TimeParseError, \"The end date could not be properly parsed\") if options.end_date.nil?\n  raise(TimeParseError, \"The start date could not be properly parsed\") if options.start_date.nil?\n  raise(StartDateOutOfRange, \"The start date must be within 90 day from today\") unless options.start_date.between?(90.days.ago, Time.current.at_end_of_day)\n  raise(MissingAuthor, \"No author was supplied in arguments (use -a <author>)\") if options.author.blank?\n  raise(MissingGithubAcessToken, \"GitHub Personal Access Token was not found (should be an ENV as GITHUB_ACCESS_TOKEN\") if ENV[\"GITHUB_ACCESS_TOKEN\"].nil?\n\n  options\nrescue GithubContributions::Exception => e\n  puts e.message\n  exit\nend\n"