Class: Clamp::Option::Definition
Overview
Represents an option of a Clamp::Command class.
Instance Attribute Summary collapse
#description, #environment_variable
Instance Method Summary
collapse
#append_method, #attribute_name, #default_method, #default_value, #help, #help_rhs, #hidden?, #ivar_name, #multivalued?, #of, #option_missing_message, #required?, #write_method
Constructor Details
#initialize(switches, type, description, options = {}) ⇒ Definition
Returns a new instance of Definition.
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/clamp/option/definition.rb', line 13
def initialize(switches, type, description, options = {})
@switches = Array(switches)
@type = type
@description = description
super(options)
@multivalued = options[:multivalued]
return unless options.key?(:required)
@required = options[:required]
raise ArgumentError, "Specifying a :default value with :required doesn't make sense" if options.key?(:default)
raise ArgumentError, "A required flag (boolean) doesn't make sense." if type == :flag
end
|
Instance Attribute Details
#switches ⇒ Object
Returns the value of attribute switches.
26
27
28
|
# File 'lib/clamp/option/definition.rb', line 26
def switches
@switches
end
|
#type ⇒ Object
Returns the value of attribute type.
26
27
28
|
# File 'lib/clamp/option/definition.rb', line 26
def type
@type
end
|
Instance Method Details
#default_conversion_block ⇒ Object
60
61
62
|
# File 'lib/clamp/option/definition.rb', line 60
def default_conversion_block
Clamp.method(:truthy?) if flag?
end
|
52
53
54
55
56
57
58
|
# File 'lib/clamp/option/definition.rb', line 52
def (switch, arguments)
if flag?
flag_value(switch)
else
arguments.shift
end
end
|
#flag? ⇒ Boolean
36
37
38
|
# File 'lib/clamp/option/definition.rb', line 36
def flag?
@type == :flag
end
|
#flag_value(switch) ⇒ Object
40
41
42
|
# File 'lib/clamp/option/definition.rb', line 40
def flag_value(switch)
!(switch =~ /^--no-(.*)/ && switches.member?("--\[no-\]#{Regexp.last_match(1)}"))
end
|
#handles?(switch) ⇒ Boolean
32
33
34
|
# File 'lib/clamp/option/definition.rb', line 32
def handles?(switch)
recognised_switches.member?(switch)
end
|
#help_lhs ⇒ Object
64
65
66
67
68
|
# File 'lib/clamp/option/definition.rb', line 64
def help_lhs
lhs = switches.join(", ")
lhs += " " + type unless flag?
lhs
end
|
#long_switch ⇒ Object
28
29
30
|
# File 'lib/clamp/option/definition.rb', line 28
def long_switch
switches.find { |switch| switch =~ /^--/ }
end
|
#read_method ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/clamp/option/definition.rb', line 44
def read_method
if flag?
super + "?"
else
super
end
end
|