Class: Slop::Option

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(slop, short, long, description, argument, options, &blk) ⇒ Option

Returns a new instance of Option.

Parameters:

  • slop (Slop)

    The Slop object this Option belongs to

  • short (String, #to_s)

    The short flag representing this Option without prefix (ie: ‘a`)

  • long (String, #to_s)

    The long flag representing this Option without the prefix (ie: ‘foo`)

  • description (String)

    This options description

  • argument (Boolean)

    True if this option takes an argument

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :optional (Boolean)
    • When true, this option takes an optional argument, ie an argument does not have to be supplied.

  • :argument (Boolean)
    • True if this option takes an argument.

  • :default (Object)
    • The default value for this option when no argument is given

  • :callback (Proc, #call)
    • The callback object, used instead of passing a block to this option

  • :delimiter (String, #to_s) — default: ','
    • A delimiter string when processing this option as a list

  • :limit (Integer) — default: 0
    • A limit, used when processing this option as a list

  • :tail (Boolean) — default: false
    • When true, this option will be grouped at the bottom of the help text instead of in order of processing

  • :match (Regexp)
    • A regular expression this option should match

  • :unless (String, #to_s)
    • Used by ‘omit_exec` for omitting execution of this options callback if another option exists

  • :help (Boolean, String) — default: true
    • If this option is a string, it’ll be appended to the long flag help text (before the description). When false, no help information will be displayed for this option

  • :required (Boolean) — default: false
    • When true, this option is considered mandatory. That is, when not supplied, Slop will raise a ‘MissingOptionError`



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, options, &blk)
  @slop = slop

  self.short_flag = short
  self.long_flag = long
  self.description = description

  @argument = argument
  @options = options

  self.tail = @options[:tail]
  self.match = @options[:match]
  self.help = @options.fetch(:help, true)
  self.required = @options[:required]

  @delimiter = @options.fetch(:delimiter, ',')
  @limit = @options.fetch(:limit, 0)
  @argument_type = @options[:as].to_s.downcase
  @argument_value = nil

  self.forced = false
  self.count = 0

  @callback = blk if block_given?
  @callback ||= @options[: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

#countObject

Returns the value of attribute count

Returns:

  • (Object)

    the current value of count



26
27
28
# File 'lib/slop-2.3.1/lib/slop.rb', line 26

def count
  @count
end

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



26
27
28
# File 'lib/slop-2.3.1/lib/slop.rb', line 26

def description
  @description
end

#forcedObject

Returns the value of attribute forced

Returns:

  • (Object)

    the current value of forced



26
27
28
# File 'lib/slop-2.3.1/lib/slop.rb', line 26

def forced
  @forced
end

#helpObject

Returns the value of attribute help

Returns:

  • (Object)

    the current value of help



26
27
28
# File 'lib/slop-2.3.1/lib/slop.rb', line 26

def help
  @help
end

#long_flagObject

Returns the value of attribute long_flag

Returns:

  • (Object)

    the current value of long_flag



26
27
28
# File 'lib/slop-2.3.1/lib/slop.rb', line 26

def long_flag
  @long_flag
end

#matchObject

Returns the value of attribute match

Returns:

  • (Object)

    the current value of match



26
27
28
# File 'lib/slop-2.3.1/lib/slop.rb', line 26

def match
  @match
end

#requiredObject

Returns the value of attribute required

Returns:

  • (Object)

    the current value of required



26
27
28
# File 'lib/slop-2.3.1/lib/slop.rb', line 26

def required
  @required
end

#short_flagObject

Returns the value of attribute short_flag

Returns:

  • (Object)

    the current value of short_flag



26
27
28
# File 'lib/slop-2.3.1/lib/slop.rb', line 26

def short_flag
  @short_flag
end

#tailObject

Returns the value of attribute tail

Returns:

  • (Object)

    the current value of 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.

Returns:

  • (Boolean)

    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?
  @options[:optional]
end

#argument_valueObject

Returns the argument value after it’s been cast according to the ‘:as` option.

Returns:

  • (Object)

    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 || @options[: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

Parameters:

  • value (Object)

    The value to set this options argument to



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

Parameters:

  • The (Object)

    object to be sent to ‘:call`



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.

Returns:

  • (Boolean)

    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 || @options[:argument] || @options[:optional] == false
end

#force_argument_value(value) ⇒ Object

Force an argument value, used when the desired argument value is negative (false or nil)

Parameters:

  • value (Object)


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

#inspectString

Returns:



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

#keyString

Returns either the long or short flag for this option.

Returns:

  • (String)

    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.

Parameters:

  • items (Array)

    The original array of objects passed to ‘Slop.new`

Returns:

  • (Boolean)

    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 = @options[:unless].to_s.sub(/\A--?/, '')
  items.any? { |i| i.to_s.sub(/\A--?/, '') == string }
end

#to_sString

This option in a nice pretty string, including a short flag, long flag, and description (if they exist).

Returns:

See Also:



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