Class: CLOptions::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/cloptions/switch.rb

Overview

VALID: –foo => [ 0, 0, 0] –foo [PARAM] => [ 0, 1, 0] –foo PARAM => [ 1, 0, 0] –foo PARAM, … => [-2, 0, 0] –foo [PARAM], … => [ 0, -1, 0] –foo P1, P2, P3 => [ 3, 0, 0] –foo P1, P2, [P3] => [ 2, 1, 0] –foo P1, P2, … => [-3, 0, 0]

POSSIBLE: –foo P1, [P2], P3 => [ 1, 1, 1] –foo P1, …, P3 => [-2, 0, 1] –foo P1, [P2], …, P3 => [ 1, -1, 1]

IMPOSSIBLE: –foo P1, [P2], P3, [P4] => [ 0, 0, 0]

Constant Summary collapse

Match =
/\A(?:-\w|--[\w-]+)(?:=.*)?\z/
Short =
/\A(-\w)(?:=([^,]+(?:(?:,[^,]+)*)|,))?\z/
Long =
/\A(--[\w-]+)(?:=([^,]+(?:(?:,[^,]+)*)|,))?\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(short, *rest, &adapter) ⇒ Switch

Returns a new instance of Switch.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cloptions/switch.rb', line 55

def initialize(short, *rest, &adapter)
	@help           = (::String === rest.last ? rest.pop : "").freeze
	@short          = short
	@long           = rest.shift
	@str_params     = rest.shift
	@params, @arity = *analyze_param(@str_params)
	@hash_key       = rest.shift
	@hash_key       = @long ? @long[2..-1].to_sym : @short[1,1].to_sym if @hash_key == true
	@mappings       = [@short, @long, @hash_key].compact
	@adapter        = adapter
	@to_s           = ("#{short}#{', ' if short and long}#{long}#{' '+@str_params if @str_params}")
end

Instance Attribute Details

#arityObject (readonly)

Returns the value of attribute arity.



53
54
55
# File 'lib/cloptions/switch.rb', line 53

def arity
  @arity
end

#hash_keyObject (readonly)

Returns the value of attribute hash_key.



47
48
49
# File 'lib/cloptions/switch.rb', line 47

def hash_key
  @hash_key
end

#helpObject (readonly)

Returns the value of attribute help.



49
50
51
# File 'lib/cloptions/switch.rb', line 49

def help
  @help
end

#longObject (readonly)

Returns the value of attribute long.



46
47
48
# File 'lib/cloptions/switch.rb', line 46

def long
  @long
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



48
49
50
# File 'lib/cloptions/switch.rb', line 48

def mappings
  @mappings
end

#paramsObject (readonly)

Returns the value of attribute params.



52
53
54
# File 'lib/cloptions/switch.rb', line 52

def params
  @params
end

#shortObject (readonly)

Returns the value of attribute short.



45
46
47
# File 'lib/cloptions/switch.rb', line 45

def short
  @short
end

#str_paramsObject (readonly)

Returns the value of attribute str_params.



50
51
52
# File 'lib/cloptions/switch.rb', line 50

def str_params
  @str_params
end

#to_sObject (readonly)

Returns the value of attribute to_s.



51
52
53
# File 'lib/cloptions/switch.rb', line 51

def to_s
  @to_s
end

Instance Method Details

#process(data, args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cloptions/switch.rb', line 95

def process(data, args)
	flag    = Flag.new(self, true, nil)
	min     = (@arity[0] < 0 ? -1-@arity[0] : @arity[0])+(@arity[2] < 0 ? -1-@arity[2] : @arity[2])
	max     = (@arity[0] < 0 || @arity[1] < 0) ? nil : min+@arity[1]
	return flag if max == 0

	flag[1] = []
	params  = []
	args.unshift(*data.split(/,/)) if data
	while args.first !~ Match
		params << (@adapter ? @adapter.call(args.shift) : args.shift)
		break unless args.first and args.first[-1] == ?,
	end
	raise "Invalid parameter count for flag #{self}" if ((max && !params.length.between?(min, max)) || params.length < min)
	if max == 0 then
		flag[1] = nil
	elsif min == 1 && max == 1 then
		flag[1] = params.first
	else
		3.times { |i| process_params(flag, i, params) }
	end
	flag
end