Class: Slop::Option
- Inherits:
-
Object
- Object
- Slop::Option
- Defined in:
- lib/slop/option.rb
Instance Attribute Summary collapse
-
#argument_value ⇒ Object
The argument value after it's been cast according to the
:as
option. -
#count ⇒ Integer
The amount of times this option has been invoked.
-
#description ⇒ String
readonly
This options description.
-
#forced ⇒ Boolean
readonly
True if this options argument value has been forced.
-
#help ⇒ Object
readonly
True/false, or an optional help string to append.
-
#long_flag ⇒ String, #to_s
readonly
The long flag used for this option.
-
#match ⇒ Regexp
readonly
If provided, an options argument must match this regexp, otherwise Slop will raise an InvalidArgumentError.
-
#short_flag ⇒ String, #to_s
readonly
The short flag used for this option.
-
#tail ⇒ Boolean
readonly
True if the option should be grouped at the tail of the help list.
-
#unless ⇒ Object
Omit execution of this Options block or callback if this object exists in the Array of items passed to
Slop.new
.
Instance Method Summary collapse
-
#accepts_optional_argument? ⇒ Boolean
True if this option accepts an optional argument.
-
#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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/slop/option.rb', line 55 def initialize(slop, short, long, description, argument, ={}, &blk) @slop = slop @short_flag = short @long_flag = long @description = description @options = @expects_argument = argument @expects_argument = true if [:optional] == false @tail = [:tail] @match = [:match] @delimiter = [:delimiter] || ',' @limit = [:limit] || 0 @unless = [:unless] @help = [:help] @help = true if @help.nil? @forced = false @argument_value = nil @count = 0 @callback = blk if block_given? @callback ||= [:callback] build_longest_flag end |
Instance Attribute Details
#argument_value ⇒ Object
Returns the argument value after it's been cast
according to the :as
option.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/slop/option.rb', line 100 def argument_value return @argument_value if @forced value = @argument_value || @options[:default] return if value.nil? case @options[:as].to_s.downcase when 'array' value.split @delimiter, @limit when 'range' value_to_range value when 'string', 'str'; value.to_s when 'symbol', 'sym'; value.to_s.to_sym when 'integer', 'int'; value.to_s.to_i when 'float'; value.to_s.to_f else value end end |
#count ⇒ Integer
Returns The amount of times this option has been invoked.
33 34 35 |
# File 'lib/slop/option.rb', line 33 def count @count end |
#description ⇒ String (readonly)
Returns This options description.
11 12 13 |
# File 'lib/slop/option.rb', line 11 def description @description end |
#forced ⇒ Boolean (readonly)
Returns true if this options argument value has been forced.
25 26 27 |
# File 'lib/slop/option.rb', line 25 def forced @forced end |
#help ⇒ Object (readonly)
Returns true/false, or an optional help string to append.
22 23 24 |
# File 'lib/slop/option.rb', line 22 def help @help end |
#long_flag ⇒ String, #to_s (readonly)
Returns The long flag used for this option.
8 9 10 |
# File 'lib/slop/option.rb', line 8 def long_flag @long_flag end |
#match ⇒ Regexp (readonly)
Returns If provided, an options argument must match this regexp, otherwise Slop will raise an InvalidArgumentError.
19 20 21 |
# File 'lib/slop/option.rb', line 19 def match @match end |
#short_flag ⇒ String, #to_s (readonly)
Returns The short flag used for this option.
5 6 7 |
# File 'lib/slop/option.rb', line 5 def short_flag @short_flag end |
#tail ⇒ Boolean (readonly)
Returns True if the option should be grouped at the tail of the help list.
15 16 17 |
# File 'lib/slop/option.rb', line 15 def tail @tail end |
#unless ⇒ Object
Returns Omit execution of this Options block or callback if
this object exists in the Array of items passed to Slop.new
.
37 38 39 |
# File 'lib/slop/option.rb', line 37 def unless @unless end |
Instance Method Details
#accepts_optional_argument? ⇒ Boolean
Returns true if this option accepts an optional argument.
89 90 91 |
# File 'lib/slop/option.rb', line 89 def accepts_optional_argument? @options[:optional] end |
#call(obj = nil) ⇒ Object
Execute the block or callback object associated with this Option
131 132 133 |
# File 'lib/slop/option.rb', line 131 def call(obj=nil) @callback.call(obj) if @callback.respond_to?(:call) end |
#expects_argument? ⇒ Boolean
Returns true if this option expects an argument.
84 85 86 |
# File 'lib/slop/option.rb', line 84 def expects_argument? @expects_argument || @options[:argument] end |
#force_argument_value(value) ⇒ Object
Force an argument value, used when the desired argument value is negative (false or nil)
123 124 125 126 |
# File 'lib/slop/option.rb', line 123 def force_argument_value(value) @argument_value = value @forced = true end |
#inspect ⇒ String
172 173 174 175 176 |
# File 'lib/slop/option.rb', line 172 def inspect "#<Slop::Option short_flag=#{@short_flag.inspect} " + "long_flag=#{@long_flag.inspect} " + "description=#{@description.inspect}>" end |
#key ⇒ String
Returns either the long or short flag for this option.
94 95 96 |
# File 'lib/slop/option.rb', line 94 def key @long_flag || @short_flag end |
#omit_exec?(items) ⇒ Boolean
Returns true if this options :unless
argument exists
inside items.
138 139 140 141 |
# File 'lib/slop/option.rb', line 138 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).
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/slop/option.rb', line 148 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 spaces = " " * (diff + 6) out += spaces else spaces = " " * (@slop.longest_flag + 8) out += spaces end "#{out}#{@description}" end |