Class: LibCLImate::Climate::ParseResults

Inherits:
Object
  • Object
show all
Defined in:
lib/libclimate/climate.rb

Overview

Represents the results obtained from Climate#parse()

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(climate, arguments, options) ⇒ ParseResults

Returns a new instance of ParseResults.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/libclimate/climate.rb', line 173

def initialize(climate, arguments, options)

	@climate				=	climate

	@arguments				=	arguments

	@argv					=	arguments.argv
	@argv_original_copy		=	arguments.argv_original_copy
	@specifications			=	arguments.specifications
	@program_name			=	climate.program_name
	@flags					=	arguments.flags
	@options				=	arguments.options
	@values					=	arguments.values
end

Instance Attribute Details

#argvObject (readonly)

([String]) The original arguments passed into the Climate#parse() method



192
193
194
# File 'lib/libclimate/climate.rb', line 192

def argv
  @argv
end

#argv_original_copyObject (readonly)

(Array) unchanged copy of the original array of arguments passed to parse



195
196
197
# File 'lib/libclimate/climate.rb', line 195

def argv_original_copy
  @argv_original_copy
end

#climateObject (readonly)

(Climate) The Climate instance from which this instance was obtained



189
190
191
# File 'lib/libclimate/climate.rb', line 189

def climate
  @climate
end

#flagsObject (readonly)

(String) A (frozen) array of flags



204
205
206
# File 'lib/libclimate/climate.rb', line 204

def flags
  @flags
end

#optionsObject (readonly)

(String) A (frozen) array of options



207
208
209
# File 'lib/libclimate/climate.rb', line 207

def options
  @options
end

#program_nameObject (readonly)

(String) The program name



201
202
203
# File 'lib/libclimate/climate.rb', line 201

def program_name
  @program_name
end

#specificationsObject (readonly)

(Array) a frozen array of specifications



198
199
200
# File 'lib/libclimate/climate.rb', line 198

def specifications
  @specifications
end

#valuesObject (readonly)

(String) A (frozen) array of values



210
211
212
# File 'lib/libclimate/climate.rb', line 210

def values
  @values
end

Instance Method Details

#flag_is_specified(id) ⇒ Object



346
347
348
349
# File 'lib/libclimate/climate.rb', line 346

def flag_is_specified(id)

	!@arguments.find_flag(id).nil?
end

#lookup_flag(id) ⇒ Object



351
352
353
354
# File 'lib/libclimate/climate.rb', line 351

def lookup_flag(id)

	@arguments.find_flag(id)
end

#lookup_option(id) ⇒ Object



356
357
358
359
# File 'lib/libclimate/climate.rb', line 356

def lookup_option(id)

	@arguments.find_option(id)
end

#verify(**options) ⇒ Object

Verifies the initiating command-line against the specifications, raising an exception if any missing, unused, or unrecognised flags, options, or values are found



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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/libclimate/climate.rb', line 215

def verify(**options)

	if v = options[:raise]

		hm = {}

		hm[:raise_on_required] = v unless options.has_key?(:raise_on_required)
		hm[:raise_on_unrecognised] = v unless options.has_key?(:raise_on_unrecognised)
		hm[:raise_on_unused] = v unless options.has_key?(:raise_on_unused)

		options = options.merge hm
	end

	raise_on_required		=	options[:raise_on_required]
	raise_on_unrecognised	=	options[:raise_on_unrecognised]
	raise_on_unused			=	options[:raise_on_unused]


	# Verification:
	#
	# 1. Check arguments recognised
	# 1.a Flags
	# 1.b Options
	# 2. police any required options
	# 3. Check values

	# 1.a Flags

	self.flags.each do |flag|

		spec = specifications.detect do |sp|

			sp.kind_of?(::CLASP::FlagSpecification) && flag.name == sp.name
		end

		if spec

			if spec.respond_to?(:action) && !spec.action.nil?

				spec.action.call(flag, spec)
			end
		else

			message = make_abort_message_("unrecognised flag '#{f}'")

			if false

			elsif climate.ignore_unknown

				;
			elsif raise_on_unrecognised

				if raise_on_unrecognised.is_a?(Class)

					raise raise_on_unrecognised, message
				else

					raise RuntimeError, message
				end
			elsif climate.exit_on_unknown

				climate.abort message
			else

				if program_name && !program_name.empty?

					message = "#{program_name}: #{message}"
				end

				climate.stderr.puts message
			end
		end
	end

	# 1.b Options

	self.options.each do |option|

		spec = specifications.detect do |sp|

			sp.kind_of?(::CLASP::OptionSpecification) && option.name == sp.name
		end

		if spec

			if spec.respond_to?(:action) && !spec.action.nil?

				spec.action.call(option, spec)
			end
		else

			message = make_abort_message_("unrecognised option '#{f}'")

			if false

			elsif climate.ignore_unknown

				;
			elsif raise_on_unrecognised

				if raise_on_unrecognised.is_a?(Class)

					raise raise_on_unrecognised, message
				else

					raise RuntimeError, message
				end
			elsif climate.exit_on_unknown

				climate.abort message
			else

				if program_name && !program_name.empty?

					message = "#{program_name}: #{message}"
				end

				climate.stderr.puts message
			end
		end
	end

	# 2. police any required options

	climate.check_required_options_(specifications, self.options, [], raise_on_required)

	# 3. Check values

	climate.check_value_constraints_(values)
end