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) (defaults to: {})

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)


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

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

  @expects_argument = argument
  @expects_argument = true if options[:optional] == false

  @tail = options[:tail]
  @match = options[:match]

  @forced = false
  @argument_value = nil

  @delimiter = options[:delimiter] || ','
  @limit = options[:limit] || 0

  if @long_flag && @long_flag.size > @slop.longest_flag
    @slop.longest_flag = @long_flag.size
  end

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

Instance Attribute Details

#argument_valueObject

Returns the argument value after it's been case according to the :as option.

Returns:

  • (Object)

    the argument value after it's been case according to the :as option



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/slop/option.rb', line 87

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';  value.to_s
  when 'symbol';  value.to_s.to_sym
  when 'integer'; value.to_s.to_i
  when 'float';   value.to_s.to_f
  else
    value
  end
end

#callbackProc, #call (readonly)

Returns The object to execute when this option is used.

Returns:

  • (Proc, #call)

    The object to execute when this option is used



14
15
16
# File 'lib/slop/option.rb', line 14

def callback
  @callback
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

#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



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

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



18
19
20
# File 'lib/slop/option.rb', line 18

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



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

def accepts_optional_argument?
  @options[:optional]
end

#expects_argument?Boolean

Returns true if this option expects an argument.

Returns:

  • (Boolean)

    true if this option expects an argument



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

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)

Parameters:

  • value (Object)


110
111
112
113
# File 'lib/slop/option.rb', line 110

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

#forced?Boolean

Returns true if this argument value has been forced.

Returns:

  • (Boolean)

    true if this argument value has been forced



116
117
118
# File 'lib/slop/option.rb', line 116

def forced?
  @forced
end

#inspectString

Returns:

  • (String)


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

def inspect
  "#<Slop::Option short_flag=#{@short_flag.inspect} " +
  "long_flag=#{@long_flag.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



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

def key
  @long_flag || @short_flag
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:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/slop/option.rb', line 124

def to_s
  out = "    "
  out += @short_flag ? "-#{@short_flag}, " : ' ' * 4

  if @long_flag
    out += "--#{@long_flag}"
    diff = @slop.longest_flag - @long_flag.size
    spaces = " " * (diff + 6)
    out += spaces
  else
    spaces = " " * (@slop.longest_flag + 8)
    out += spaces
  end

  "#{out}#{@description}"
end