Class: Calrom::OptionParser

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

Overview

Parses command line options, produces Config.

Defined Under Namespace

Classes: CustomizedOptionParser

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(argv) ⇒ Object



15
16
17
# File 'lib/calrom/option_parser.rb', line 15

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

Instance Method Details

#call(argv) ⇒ Object



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/calrom/option_parser.rb', line 19

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 = CustomizedOptionParser.new do |opts|
    opts.banner = "    Usage: calrom [options] [arg1 [arg2]]\n\n    Specifying date range (cal/ncal-compatible):\n\n      calrom             - current month\n      calrom -m 5        - May of the current year\n      calrom -m 5p       - May of the previous year\n      calrom -m 5f       - May of the following year\n      calrom -m 5 2000   - May 2000\n      calrom 5 2000      - also May 2000\n      calrom 2000        - whole year 2000\n      calrom -y 2000     - also whole year 2000\n      calrom -y          - whole current year\n\n    Specifying date range (not cal-compatible):\n\n      calrom 2000-05-31              - specified day (only)\n      calrom 2000-05-31 2000-07-01   - arbitrary date range\n      calrom (--yesterday|--today|--tomorrow)\n\n    EOS\n\n    opts.separator 'Configuration files'\n\n    opts.on('--config=CONFIG', 'load configuration from file (may be used multiple times, all specified files will be loaded)') do |value|\n      config.configs << value\n    end\n\n    opts.separator 'Options selecting date range'\n\n    # cal\n    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|\n      range_type = :month\n      if value =~ /^(\\d+)([pf])$/\n        month = $1\n        year = validate_year(year) + ($2 == 'f' ? 1 : -1)\n      else\n        month = value\n      end\n    end\n\n    # cal\n    opts.on('-y', '--year', 'display specified (or current) year') do |value|\n      range_type = :year\n    end\n\n    opts.on('--yesterday', 'display previous day') do |value|\n      day = Date.today - 1\n      range_type = :day\n    end\n\n    opts.on('--today', 'display current day') do |value|\n      day = Date.today\n      range_type = :day\n    end\n\n    opts.on('--tomorrow', 'display following day') do |value|\n      day = Date.today + 1\n      range_type = :day\n    end\n\n    opts.separator \"Options configuring liturgical calendar\"\n\n    opts.on('-c CAL', '--calendar=CAL', 'specify (sanctorale) calendar to use. If repeated, layers all specified calendars one over another') do |value|\n      config.sanctorale << value\n    end\n\n    locales_help = I18n.available_locales.join(', ')\n    opts.on('--locale=LOCALE', \"override language in which temporale celebration titles are rendered (supported: \#{locales_help})\") do |value|\n      config.locale = value.to_sym\n    end\n\n    opts.separator 'Options affecting presentation'\n\n    opts.on('-l', '--list', 'display detailed listing of days and celebrations (synonym to --format=list)') do\n      config.formatter = :list\n    end\n\n    supported_formats = %i(overview list csv json)\n    formats_help = supported_formats.join(', ')\n    opts.on('--format=FORMAT', supported_formats, \"specify output format (supported: \#{formats_help})\") do |value|\n      config.formatter = value\n    end\n\n    # cal\n    opts.on('-e', '--easter', 'display date of Easter (only)') do\n      config.formatter = :easter\n    end\n\n    opts.on('--calendars', 'list bundled calendars') do |value|\n      config.formatter = :calendars\n    end\n\n    opts.on('--[no-]color', 'enable/disable colours (enabled by default)') do |value|\n      config.colours = value\n    end\n\n    opts.separator 'Debugging options'\n\n    # cal\n    opts.on('-d YM', '--current-month=YM', 'use given month (YYYY-MM) as the current month (for debugging of date range selection)') do |value|\n      year, month = value.split '-'\n    end\n\n    # cal\n    opts.on('-H DATE', '--highlight-date=DATE', 'use given date as the current date (for debugging of highlighting)') do |value|\n      config.today = validate_day value\n    end\n\n    opts.separator 'Information regarding calrom'\n\n    opts.on('-V', '--version', 'display calrom version') do\n      puts 'calrom v' + Calrom::VERSION\n      exit\n    end\n\n    # Normally optparse defines this option by default, but once -H option is added,\n    # for some reason -h (if not defined explicitly) is treated as -H.\n    opts.on('-h', '--help', 'display this help') do\n      puts opts\n      exit\n    end\n  end\n\n  arguments = opt_parser.parse argv\n\n  iso_date_regexp = /^(\\d{4}-\\d{2}-\\d{2})$/\n  match(arguments) do\n    with(_[iso_date_regexp.(date)]) do\n      range_type = :day\n      day = date\n    end\n\n    with(_[iso_date_regexp.(date), iso_date_regexp.(another_date)]) do\n      range_type = :free\n      day = date\n      another_day = another_date\n    end\n\n    with(_[y]) do\n      range_type ||= :year\n      year = y\n    end\n\n    with(_[m, y]) do\n      range_type = :month\n      month, year = m, y\n    end\n\n    with([]) {}\n\n    with(_) do\n      raise InputError.new('too many arguments')\n    end\n  end\n\n  config.date_range =\n    build_date_range(\n      range_type,\n      validate_year(year),\n      validate_month(month),\n      day && validate_day(day),\n      another_day && validate_day(another_day)\n    )\n\n  config.freeze\nend\n"