Class: OptSimple::Flag

Inherits:
Parameter show all
Defined in:
lib/opt_simple.rb

Overview

An optional parameter, usually with nothing following it. If not set on the CL, it will be false in the Result. ‘switches’ can be a String or an Array of Strings, and specifies the switches expected on the CL. ‘help’ provides a description of the parameter and an optional block can do parameter validation/transformation. If no block is given, then the strings specified (after the leading ‘-’s removed) will be used as keys in the Result and the values set to true when seen on the CL

Instance Attribute Summary

Attributes inherited from Parameter

#block, #metavar, #param_options, #switches

Instance Method Summary collapse

Methods inherited from Parameter

#error, #help_str, #mandatory?, #names, #set_opt, #switch_len, #switch_str

Constructor Details

#initialize(switches, help = "", &block) ⇒ Flag

Returns a new instance of Flag.



408
409
410
411
412
413
414
415
416
# File 'lib/opt_simple.rb', line 408

def initialize(switches,help="",&block)
  super(switches,help,&block)
  if block_given?
	@param_options[names.first] = false
  else
	#@block = Proc.new { @param_options[names.first] = true}
	@block = Proc.new { set_opt true }
  end
end

Instance Method Details

#accumulate_optObject

increment the parameter by one every time it is seen on the CL



419
420
421
422
423
424
425
426
# File 'lib/opt_simple.rb', line 419

def accumulate_opt
  # if this is the first time seen, set it to 1. 
  if @param_options[names.first] == false
	@param_options[names.first] = 1
  else
	@param_options[names.first] += 1
  end
end