Class: Slop::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/slop/option.rb

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)
  • short (String, #to_s)
  • long (String, #to_s)
  • description (String)
  • argument (Boolean)
  • options (Hash)

Options Hash (options):

  • :optional (Boolean)
  • :argument (Boolean)
  • :default (Object)
  • :callback (Proc, #call)
  • :delimiter (String, #to_s) — default: ','
  • :limit (Integer) — default: 0
  • :tail (Boolean) — default: false
  • :match (Regexp)
  • :unless (String, #to_s)
  • :help (Boolean, String) — default: true


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/slop/option.rb', line 46

def initialize(slop, short, long, description, argument, options, &blk)
  @slop = slop
  @short_flag = short
  @long_flag = long
  @description = description
  @argument = argument
  @options = options

  @tail = options[:tail]
  @match = options[:match]
  @delimiter = options.fetch(:delimiter, ',')
  @limit = options.fetch(:limit, 0)
  @help = options.fetch(:help, true)
  @argument_type = options[:as].to_s.downcase

  @forced = false
  @argument_value = nil
  @count = 0

  @callback = blk if block_given?
  @callback ||= options[:callback]

  build_longest_flag
end

Instance Attribute Details

#countInteger

Returns The amount of times this option has been invoked.

Returns:

  • (Integer)

    The amount of times this option has been invoked



28
29
30
# File 'lib/slop/option.rb', line 28

def count
  @count
end

#descriptionString (readonly)

Returns This options description.

Returns:

  • (String)

    This options description



11
12
13
# File 'lib/slop/option.rb', line 11

def description
  @description
end

#forcedBoolean

Returns true if this options argument value has been forced.

Returns:

  • (Boolean)

    true if this options argument value has been forced



25
26
27
# File 'lib/slop/option.rb', line 25

def forced
  @forced
end

#helpObject (readonly)

Returns true/false, or an optional help string to append.

Returns:

  • (Object)

    true/false, or an optional help string to append



22
23
24
# File 'lib/slop/option.rb', line 22

def help
  @help
end

#long_flagString, #to_s (readonly)

Returns The long flag used for this option.

Returns:

  • (String, #to_s)

    The long flag used for this option



8
9
10
# File 'lib/slop/option.rb', line 8

def long_flag
  @long_flag
end

#matchRegexp (readonly)

Returns If provided, an options argument must match this regexp, otherwise Slop will raise an InvalidArgumentError.

Returns:

  • (Regexp)

    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_flagString, #to_s (readonly)

Returns The short flag used for this option.

Returns:

  • (String, #to_s)

    The short flag used for this option



5
6
7
# File 'lib/slop/option.rb', line 5

def short_flag
  @short_flag
end

#tailBoolean (readonly)

Returns True if the option should be grouped at the tail of the help list.

Returns:

  • (Boolean)

    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

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



77
78
79
# File 'lib/slop/option.rb', line 77

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/slop/option.rb', line 106

def argument_value
  return @argument_value if @forced
  value = @argument_value || @options[:default]
  return if value.nil?

  case @argument_type
  when 'array'; @argument_value
  when 'range'; value_to_range value
  when 'float'; value.to_s.to_f
  when 'string', 'str';  value.to_s
  when 'symbol', 'sym';  value.to_s.to_sym
  when 'integer', 'int'; value.to_s.to_i
  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



93
94
95
96
97
98
99
100
101
102
# File 'lib/slop/option.rb', line 93

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



135
136
137
# File 'lib/slop/option.rb', line 135

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



72
73
74
# File 'lib/slop/option.rb', line 72

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)


127
128
129
130
# File 'lib/slop/option.rb', line 127

def force_argument_value(value)
  @argument_value = value
  @forced = true
end

#inspectString

Returns:

  • (String)


174
175
176
177
178
# File 'lib/slop/option.rb', line 174

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



82
83
84
# File 'lib/slop/option.rb', line 82

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



142
143
144
145
# File 'lib/slop/option.rb', line 142

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:

  • (String)

See Also:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/slop/option.rb', line 152

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