Class: Clamp::Option
- Inherits:
-
Object
show all
- Defined in:
- lib/clamp/option.rb,
lib/clamp/option/parsing.rb,
lib/clamp/option/declaration.rb
Defined Under Namespace
Modules: Declaration, Parsing
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(switches, type, description, options = {}) ⇒ Option
Returns a new instance of Option.
5
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/clamp/option.rb', line 5
def initialize(switches, type, description, options = {})
@switches = Array(switches)
@type = type
@description = description
if options.has_key?(:attribute_name)
@attribute_name = options[:attribute_name].to_s
end
if options.has_key?(:default)
@default_value = options[:default]
end
end
|
Instance Attribute Details
#default_value ⇒ Object
Returns the value of attribute default_value.
17
18
19
|
# File 'lib/clamp/option.rb', line 17
def default_value
@default_value
end
|
#description ⇒ Object
Returns the value of attribute description.
17
18
19
|
# File 'lib/clamp/option.rb', line 17
def description
@description
end
|
#switches ⇒ Object
Returns the value of attribute switches.
17
18
19
|
# File 'lib/clamp/option.rb', line 17
def switches
@switches
end
|
#type ⇒ Object
Returns the value of attribute type.
17
18
19
|
# File 'lib/clamp/option.rb', line 17
def type
@type
end
|
Instance Method Details
#attribute_name ⇒ Object
19
20
21
|
# File 'lib/clamp/option.rb', line 19
def attribute_name
@attribute_name ||= long_switch.sub(/^--(\[no-\])?/, '').tr('-', '_')
end
|
39
40
41
42
43
44
45
|
# File 'lib/clamp/option.rb', line 39
def (switch, arguments)
if flag?
flag_value(switch)
else
arguments.shift
end
end
|
#flag? ⇒ Boolean
31
32
33
|
# File 'lib/clamp/option.rb', line 31
def flag?
@type == :flag
end
|
#flag_value(switch) ⇒ Object
35
36
37
|
# File 'lib/clamp/option.rb', line 35
def flag_value(switch)
!(switch =~ /^--no-(.*)/ && switches.member?("--\[no-\]#{$1}"))
end
|
#handles?(switch) ⇒ Boolean
27
28
29
|
# File 'lib/clamp/option.rb', line 27
def handles?(switch)
recognised_switches.member?(switch)
end
|
#help ⇒ Object
47
48
49
50
51
|
# File 'lib/clamp/option.rb', line 47
def help
lhs = switches.join(", ")
lhs += " " + type unless flag?
[lhs, description]
end
|
#long_switch ⇒ Object
23
24
25
|
# File 'lib/clamp/option.rb', line 23
def long_switch
switches.find { |switch| switch =~ /^--/ }
end
|