Class: Calrom::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/calrom/option_parser.rb

Overview

Parses command line options, produces Config.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(argv) ⇒ Object



9
10
11
# File 'lib/calrom/option_parser.rb', line 9

def self.call(argv)
  self.new.call(argv)
end

Instance Method Details

#call(argv) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/calrom/option_parser.rb', line 13

def call(argv)
  config = Config.new

  today = config.today
  year = today.year
  month = today.month
  day = nil
  another_day = nil

  range_type = nil

  opt_parser = ::OptionParser.new do |opts|
    opts.on('-l', '--list', 'list mode') do
      config.formatter = :list
    end

    # cal
    opts.on('-e', '--easter', 'display date of Easter') do
      config.formatter = :easter
    end

    # cal
    opts.on('-m MONTH', '--month=MONTH', 'display the specified month. \'f\' or \'p\' can be appended to display the same month of the following or previous year respectively') do |value|
      range_type = :month
      if value =~ /^(\d+)([pf])$/
        month = $1
        year = validate_year(year) + ($2 == 'f' ? 1 : -1)
      else
        month = value
      end
    end

    # cal
    opts.on('-y', '--year', 'display specified (or current) year') do |value|
      range_type = :year
    end

    # cal
    opts.on('-d YM', '--current-month=YM', 'use given month (YYYY-MM) as the current month (for debugging of date range selection)') do |value|
      year, month = value.split '-'
    end

    # cal
    opts.on('-H DATE', '--highlight-date=DATE', 'use given date as the current date (for debugging of highlighting') do |value|
      config.today = validate_day value
    end

    opts.on(nil, '--[no-]color', 'enable/disable colours (enabled by default)') do |value|
      config.colours = value
    end

    opts.on_tail(nil, '--version', 'display calrom version') do
      puts 'calrom v' + Calrom::VERSION
      exit
    end

    # Normally optparse defines this option by default, but once -H option is added,
    # for some reason -h (if not defined explicitly) is treated as -H.
    opts.on_tail('-h', '--help', 'display this help') do
      puts opts
      exit
    end
  end

  arguments = opt_parser.parse argv

  iso_date_regexp = /^(\d{4}-\d{2}-\d{2})$/
  match(arguments) do
    with(_[iso_date_regexp.(date)]) do
      range_type = :day
      day = date
    end

    with(_[iso_date_regexp.(date), iso_date_regexp.(another_date)]) do
      range_type = :free
      day = date
      another_day = another_date
    end

    with(_[y]) do
      range_type ||= :year
      year = y
    end

    with(_[m, y]) do
      range_type = :month
      month, year = m, y
    end

    with([]) {}

    with(_) do
      raise InputError.new('too many arguments')
    end
  end

  config.date_range =
    build_date_range(
      range_type,
      validate_year(year),
      validate_month(month),
      day && validate_day(day),
      another_day && validate_day(another_day)
    )

  config.freeze
end