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, Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(*args) ⇒ Object



22
23
24
# File 'lib/calrom/option_parser.rb', line 22

def self.call(*args)
  self.new.call(*args)
end

Instance Method Details

#call(argv, config = nil) ⇒ Object



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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/calrom/option_parser.rb', line 26

def call(argv, config = nil)
  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      calrom -3          - display the previous, current and next month surrounding today\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('-3', 'display the previous, current and next month surrounding today') do |value|\n      range_type = :three_months\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 = today - 1\n      range_type = :day\n    end\n\n    opts.on('--today', 'display current day') do |value|\n      day = today\n      range_type = :day\n    end\n\n    opts.on('--tomorrow', 'display following day') do |value|\n      day = 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    opts.on('--[no-]load-parents', 'explicitly enable/disable parent calendar loading') do |value|\n      config.load_parents = value\n    end\n\n    transferable = CR::Temporale::SUNDAY_TRANSFERABLE_SOLEMNITIES\n    opts.on('--to-sunday=SOLEMNITY', transferable, 'transfer solemnity to Sunday' + supported_values(transferable)) do |value|\n      config.transfer_to_sunday << value.to_sym\n    end\n\n    extensions = CR::Temporale::Extensions.all.collect {|cls| cls.name.split('::').last.to_sym }\n    opts.on('--temporale-extension=EXTENSION', extensions, 'use temporale extension' + supported_values(extensions)) do |value|\n      config.temporale_extensions << CR::Temporale::Extensions.const_get(value)\n    end\n\n    opts.on('--locale=LOCALE', 'override language in which temporale celebration titles are rendered' + supported_values(I18n.available_locales)) 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 =\n      Calrom::Formatter.constants\n        .collect(&:downcase)\n        .delete_if {|i| i == :formatter }\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    opts.on('--day-filter=EXPR', 'display only days for which the expression (Ruby snippet executed in context of each CalendariumRomanum::Day instance) evaluates as truthy') do |expr|\n      config.filter_days << expr\n    end\n\n    opts.on('--celebration-filter=EXPR', 'display only celebrations for which the expression (Ruby snippet executed in context of each CalendariumRomanum::Celebration instance) evaluates as truthy') do |expr|\n      config.filter_celebrations << expr\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.on('-v', '--verbose', 'enable verbose output') do\n      config.verbose = true\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      puts 'using calendarium-romanum v' + CR::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  begin\n    arguments = opt_parser.parse argv\n  rescue ::OptionParser::ParseError => e\n    raise Error.new(e.message)\n  end\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"