Class: Slop::Options

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/slop/options.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  suppress_errors: false,
  type:            "null",
  banner:          true,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**config) {|_self| ... } ⇒ Options

Returns a new instance of Options.

Yields:

  • (_self)

Yield Parameters:

  • _self (Slop::Options)

    the object that the method was called on



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

def initialize(**config)
  @options    = []
  @separators = []
  @banner     = "usage: #{$0} [options]"
  @config     = DEFAULT_CONFIG.merge(config)
  @parser     = Parser.new(self, @config)

  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **config, &block) ⇒ Object

Handle custom option types. Will fall back to raising an exception if an option is not defined.



81
82
83
84
85
86
87
88
# File 'lib/slop/options.rb', line 81

def method_missing(name, *args, **config, &block)
  if respond_to_missing?(name)
    config[:type] = name
    on(*args, config, &block)
  else
    super
  end
end

Instance Attribute Details

The String banner prefixed to the help string.



24
25
26
# File 'lib/slop/options.rb', line 24

def banner
  @banner
end

#configObject (readonly)

A Hash of configuration options.



21
22
23
# File 'lib/slop/options.rb', line 21

def config
  @config
end

#optionsObject (readonly)

The Array of Option instances we’ve created.



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

def options
  @options
end

#parserObject (readonly)

Our Parser instance.



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

def parser
  @parser
end

#separatorsObject (readonly)

An Array of separators used for the help text.



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

def separators
  @separators
end

Instance Method Details

#each(&block) ⇒ Object

Implements the Enumerable interface.



75
76
77
# File 'lib/slop/options.rb', line 75

def each(&block)
  options.each(&block)
end

#on(*flags, **config, &block) ⇒ Object

Add a new option. This method is an alias for adding a NullOption (i.e an option with an ignored return value).

Example:

opts = Slop.parse do |o|
  o.on '--version' do
    puts Slop::VERSION
  end
end

opts.to_hash #=> {}

Returns the newly created Option subclass.



50
51
52
53
54
55
56
57
# File 'lib/slop/options.rb', line 50

def on(*flags, **config, &block)
  desc   = flags.pop unless flags.last.start_with?('-')
  config = self.config.merge(config)
  klass  = Slop.string_to_option_class(config[:type].to_s)
  option = klass.new(flags, desc, config, &block)

  add_option option
end

#parse(strings) ⇒ Object

Sugar to avoid ‘options.parser.parse(x)`.



70
71
72
# File 'lib/slop/options.rb', line 70

def parse(strings)
  parser.parse(strings)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/slop/options.rb', line 90

def respond_to_missing?(name, include_private = false)
  Slop.option_defined?(name) || super
end

#separator(string) ⇒ Object

Add a separator between options. Used when displaying the help text.



61
62
63
64
65
66
67
# File 'lib/slop/options.rb', line 61

def separator(string)
  if separators[options.size]
    separators.last << "\n#{string}"
  else
    separators[options.size] = string
  end
end

#to_aObject

Return a copy of our options Array.



95
96
97
# File 'lib/slop/options.rb', line 95

def to_a
  options.dup
end

#to_s(prefix: " " * 4) ⇒ Object

Returns the help text for this options. Used by Result#to_s.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/slop/options.rb', line 100

def to_s(prefix: " " * 4)
  str = config[:banner] ? "#{banner}\n" : ""
  len = longest_flag_length

  options.select(&:help?).sort_by(&:tail).each_with_index do |opt, i|
    # use the index to fetch an associated separator
    if sep = separators[i]
      str << "#{sep}\n"
    end

    str << "#{prefix}#{opt.to_s(offset: len)}\n"
  end

  str
end