Class: Slop::Option
- Inherits:
-
Struct
- Object
- Struct
- Slop::Option
- Defined in:
- lib/slop-2.3.1/lib/slop.rb
Overview
Each option specified in ‘Slop#opt` creates an instance of this class
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#description ⇒ Object
Returns the value of attribute description.
-
#forced ⇒ Object
Returns the value of attribute forced.
-
#help ⇒ Object
Returns the value of attribute help.
-
#long_flag ⇒ Object
Returns the value of attribute long_flag.
-
#match ⇒ Object
Returns the value of attribute match.
-
#required ⇒ Object
Returns the value of attribute required.
-
#short_flag ⇒ Object
Returns the value of attribute short_flag.
-
#tail ⇒ Object
Returns the value of attribute tail.
Instance Method Summary collapse
-
#accepts_optional_argument? ⇒ Boolean
True if this option accepts an optional argument.
-
#argument_value ⇒ Object
The argument value after it’s been cast according to the ‘:as` option.
-
#argument_value=(value) ⇒ Object
Set this options argument value.
-
#call(obj = nil) ⇒ Object
Execute the block or callback object associated with this Option.
-
#expects_argument? ⇒ Boolean
True if this option expects an argument.
-
#force_argument_value(value) ⇒ Object
Force an argument value, used when the desired argument value is negative (false or nil).
-
#initialize(slop, short, long, description, argument, options, &blk) ⇒ Option
constructor
A new instance of Option.
- #inspect ⇒ String
-
#key ⇒ String
Either the long or short flag for this option.
-
#omit_exec?(items) ⇒ Boolean
True if this options ‘:unless` argument exists inside items.
-
#to_s ⇒ String
This option in a nice pretty string, including a short flag, long flag, and description (if they exist).
Constructor Details
#initialize(slop, short, long, description, argument, options, &blk) ⇒ Option
Returns a new instance of Option.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 78 def initialize(slop, short, long, description, argument, , &blk) @slop = slop self.short_flag = short self.long_flag = long self.description = description @argument = argument = self.tail = [:tail] self.match = [:match] self.help = .fetch(:help, true) self.required = [:required] @delimiter = .fetch(:delimiter, ',') @limit = .fetch(:limit, 0) @argument_type = [:as].to_s.downcase @argument_value = nil self.forced = false self.count = 0 @callback = blk if block_given? @callback ||= [:callback] if long_flag && long_flag.size > @slop.longest_flag @slop.longest_flag = long_flag.size @slop.longest_flag += help.size if help.respond_to?(:to_str) end end |
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count
26 27 28 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 26 def count @count end |
#description ⇒ Object
Returns the value of attribute description
26 27 28 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 26 def description @description end |
#forced ⇒ Object
Returns the value of attribute forced
26 27 28 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 26 def forced @forced end |
#help ⇒ Object
Returns the value of attribute help
26 27 28 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 26 def help @help end |
#long_flag ⇒ Object
Returns the value of attribute long_flag
26 27 28 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 26 def long_flag @long_flag end |
#match ⇒ Object
Returns the value of attribute match
26 27 28 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 26 def match @match end |
#required ⇒ Object
Returns the value of attribute required
26 27 28 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 26 def required @required end |
#short_flag ⇒ Object
Returns the value of attribute short_flag
26 27 28 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 26 def short_flag @short_flag end |
#tail ⇒ Object
Returns the value of attribute tail
26 27 28 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 26 def tail @tail end |
Instance Method Details
#accepts_optional_argument? ⇒ Boolean
Returns true if this option accepts an optional argument.
116 117 118 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 116 def accepts_optional_argument? [:optional] end |
#argument_value ⇒ Object
Returns the argument value after it’s been cast according to the ‘:as` option.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 146 def argument_value return @argument_value if forced # Check for count first to prefer 0 over nil return count if @argument_type == 'count' value = @argument_value || [:default] return if value.nil? case @argument_type when 'array'; @argument_value unless !expects_argument? when 'range'; value_to_range value unless !expects_argument? when 'float'; value.to_s.to_f unless !expects_argument? when 'string', 'str'; value.to_s unless !expects_argument? when 'symbol', 'sym'; value.to_s.to_sym unless !expects_argument? when 'integer', 'int'; value.to_s.to_i unless !expects_argument? else value end end |
#argument_value=(value) ⇒ Object
Set this options argument value.
If this options argument type is expected to be an Array, this method will split the value and concat elements into the original argument value
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 132 def argument_value=(value) if @argument_type == 'array' @argument_value ||= [] if value.respond_to?(:to_str) @argument_value.concat value.split(@delimiter, @limit) end else @argument_value = value end end |
#call(obj = nil) ⇒ Object
Execute the block or callback object associated with this Option
178 179 180 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 178 def call(obj=nil) @callback.call(obj) if @callback.respond_to?(:call) end |
#expects_argument? ⇒ Boolean
Returns true if this option expects an argument.
111 112 113 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 111 def expects_argument? @argument || [:argument] || [:optional] == false end |
#force_argument_value(value) ⇒ Object
Force an argument value, used when the desired argument value is negative (false or nil)
170 171 172 173 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 170 def force_argument_value(value) @argument_value = value self.forced = true end |
#inspect ⇒ String
217 218 219 220 221 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 217 def inspect "#<Slop::Option short_flag=#{short_flag.inspect} " + "long_flag=#{long_flag.inspect} argument=#{@argument.inspect} " + "description=#{description.inspect}>" end |
#key ⇒ String
Returns either the long or short flag for this option.
121 122 123 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 121 def key long_flag || short_flag end |
#omit_exec?(items) ⇒ Boolean
Returns true if this options ‘:unless` argument exists inside items.
185 186 187 188 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 185 def omit_exec?(items) string = [:unless].to_s.sub(/\A--?/, '') items.any? { |i| i.to_s.sub(/\A--?/, '') == string } end |
#to_s ⇒ String
This option in a nice pretty string, including a short flag, long flag, and description (if they exist).
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 195 def to_s out = " " out += short_flag ? "-#{short_flag}, " : ' ' * 4 if long_flag out += "--#{long_flag}" if help.respond_to? :to_str out += " #{help}" size = long_flag.size + help.size + 1 else size = long_flag.size end diff = @slop.longest_flag - size out += " " * (diff + 6) else out += " " * (@slop.longest_flag + 8) end "#{out}#{description}" end |