Class: Samovar::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/samovar/option.rb

Overview

Represents a single command-line option.

An option is a flag-based argument that can have various forms (short, long, with or without values).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block) ⇒ Option

Initialize a new option.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/samovar/option.rb', line 24

def initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block)
	@flags = Flags.new(flags)
	@description = description
	
	if key
		@key = key
	else
		@key = @flags.first.key
	end
	
	@default = default
	
	# If the value is given, it overrides the user specified input.
	@value = value
	@value ||= true if @flags.boolean?
	
	@type = type
	@required = required
	@block = block
end

Instance Attribute Details

#blockObject (readonly)

An optional block to transform the parsed value.



83
84
85
# File 'lib/samovar/option.rb', line 83

def block
  @block
end

#defaultObject

The default value if the option is not provided.



63
64
65
# File 'lib/samovar/option.rb', line 63

def default
  @default
end

#descriptionObject (readonly)

A description of the option for help output.



53
54
55
# File 'lib/samovar/option.rb', line 53

def description
  @description
end

#flagsObject

The flags for this option.



48
49
50
# File 'lib/samovar/option.rb', line 48

def flags
  @flags
end

#keyObject

The key to use for storing the value.



58
59
60
# File 'lib/samovar/option.rb', line 58

def key
  @key
end

#requiredObject

Whether the option is required.



78
79
80
# File 'lib/samovar/option.rb', line 78

def required
  @required
end

#typeObject (readonly)

The type to coerce the value to.



73
74
75
# File 'lib/samovar/option.rb', line 73

def type
  @type
end

#valueObject

A fixed value to use regardless of user input.



68
69
70
# File 'lib/samovar/option.rb', line 68

def value
  @value
end

Instance Method Details

#coerce(result) ⇒ Object

Coerce and transform the result.



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/samovar/option.rb', line 107

def coerce(result)
	if @type
		result = coerce_type(result)
	end
	
	if @block
		result = @block.call(result)
	end
	
	return result
end

#coerce_type(result) ⇒ Object

Coerce the result to the specified type.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/samovar/option.rb', line 89

def coerce_type(result)
	if @type == Integer
		Integer(result)
	elsif @type == Float
		Float(result)
	elsif @type == Symbol
		result.to_sym
	elsif @type.respond_to? :call
		@type.call(result)
	elsif @type.respond_to? :new
		@type.new(result)
	end
end

#parse(input, parent = nil, default = nil) ⇒ Object

Parse this option from the input.



125
126
127
128
129
130
131
# File 'lib/samovar/option.rb', line 125

def parse(input, parent = nil, default = nil)
	result = @flags.parse(input)
	
	if result != nil
		@value.nil? ? coerce(result) : @value
	end
end

#to_aObject

Generate an array representation for usage output.



143
144
145
146
147
148
149
150
151
# File 'lib/samovar/option.rb', line 143

def to_a
	if @default
		[@flags, @description, "(default: #{@default})"]
	elsif @required
		[@flags, @description, "(required)"]
	else
		[@flags, @description]
	end
end

#to_sObject

Generate a string representation for usage output.



136
137
138
# File 'lib/samovar/option.rb', line 136

def to_s
	@flags
end