Class: Slop::Option

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

Constant Summary collapse

DEFAULT_CONFIG =
{
  help: true,
  tail: false,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags, desc, **config, &block) ⇒ Option

Returns a new instance of Option.



28
29
30
31
32
33
34
# File 'lib/slop/option.rb', line 28

def initialize(flags, desc, **config, &block)
  @flags  = flags
  @desc   = desc
  @config = DEFAULT_CONFIG.merge(config)
  @block  = block
  reset
end

Instance Attribute Details

#blockObject (readonly)

A custom proc that yields the option value when it’s executed.



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

def block
  @block
end

#configObject (readonly)

A Hash of configuration options.



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

def config
  @config
end

#countObject (readonly)

An Integer count for the total times this option has been executed.



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

def count
  @count
end

#descObject (readonly)

A custom description used for the help text.



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

def desc
  @desc
end

#flagsObject (readonly)

An Array of flags this option matches.



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

def flags
  @flags
end

#valueObject

Returns the value for this option. Falls back to the default (or nil).



84
85
86
# File 'lib/slop/option.rb', line 84

def value
  @value || default_value
end

Instance Method Details

#call(_value) ⇒ Object

This method is called immediately when an option is found. Override it in sub-classes.



60
61
62
63
# File 'lib/slop/option.rb', line 60

def call(_value)
  raise NotImplementedError,
    "you must override the `call' method for option #{self.class}"
end

#default_valueObject

Returns the default value for this option (default is nil).



89
90
91
# File 'lib/slop/option.rb', line 89

def default_value
  config[:default]
end

#ensure_call(value) ⇒ Object

Since ‘call()` can be used/overriden in subclasses, this method is used to do general tasks like increment count. This ensures you don’t have to call ‘super` when overriding `call()`. It’s used in the Parser.



47
48
49
50
51
52
53
54
55
56
# File 'lib/slop/option.rb', line 47

def ensure_call(value)
  @count += 1

  if value.nil? && expects_argument? && !suppress_errors?
    raise Slop::MissingArgument.new("missing argument for #{flag}", flags)
  end

  @value = call(value)
  block.call(@value) if block.respond_to?(:call)
end

#expects_argument?Boolean

Override this if this option type does not expect an argument (i.e a boolean option type).

Returns:

  • (Boolean)


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

def expects_argument?
  true
end

#finish(_result) ⇒ Object

By default this method does nothing. It’s called when all options have been parsed and allows you to mutate the ‘@value` attribute according to other options.



68
69
# File 'lib/slop/option.rb', line 68

def finish(_result)
end

#flagObject

Returns all flags joined by a comma. Used by the help string.



99
100
101
# File 'lib/slop/option.rb', line 99

def flag
  flags.join(", ")
end

#help?Boolean

Returns true if this option should be displayed in help text.

Returns:

  • (Boolean)


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

def help?
  config[:help]
end

#keyObject

Returns the last key as a symbol. Used in Options.to_hash.



104
105
106
# File 'lib/slop/option.rb', line 104

def key
  (config[:key] || flags.last.sub(/\A--?/, '')).to_sym
end

#null?Boolean

Override this if you want to ignore the return value for an option (i.e so Result#to_hash does not include it).

Returns:

  • (Boolean)


79
80
81
# File 'lib/slop/option.rb', line 79

def null?
  false
end

#resetObject

Reset the option count and value. Used when calling .reset on the Parser.



38
39
40
41
# File 'lib/slop/option.rb', line 38

def reset
  @value = nil
  @count = 0
end

#suppress_errors?Boolean

Returns true if we should ignore errors that cause exceptions to be raised.

Returns:

  • (Boolean)


94
95
96
# File 'lib/slop/option.rb', line 94

def suppress_errors?
  config[:suppress_errors]
end

#tailObject

Returns 1 if this option should be added to the tail of the help text. Used for sorting.



120
121
122
# File 'lib/slop/option.rb', line 120

def tail
  tail? ? 1 : -1
end

#tail?Boolean

Returns true if this option should be added to the tail of the help text.

Returns:

  • (Boolean)


114
115
116
# File 'lib/slop/option.rb', line 114

def tail?
  config[:tail]
end

#to_s(offset: 0) ⇒ Object

Returns the help text for this option (flags and description).



125
126
127
# File 'lib/slop/option.rb', line 125

def to_s(offset: 0)
  "%-#{offset}s  %s" % [flag, desc]
end