class Samovar::Options

Attributes

defaults[R]
key[R]
ordered[R]
title[R]

Public Class Methods

new(title = "Options", key: :options) click to toggle source
# File lib/samovar/options.rb, line 18
def initialize(title = "Options", key: :options)
        @title = title
        @ordered = []
        
        # We use this flag to option cache to improve parsing performance:
        @keyed = {}
        
        @key = key
        
        @defaults = {}
end
parse(*arguments, **options, &block) click to toggle source
# File lib/samovar/options.rb, line 10
def self.parse(*arguments, **options, &block)
        options = self.new(*arguments, **options)
        
        options.instance_eval(&block) if block_given?
        
        return options.freeze
end

Public Instance Methods

<<(option) click to toggle source
# File lib/samovar/options.rb, line 74
def << option
        @ordered << option
        option.flags.each do |flag|
                @keyed[flag.prefix] = option
                
                flag.alternatives.each do |alternative|
                        @keyed[alternative] = option
                end
        end
        
        if default = option.default
                @defaults[option.key] = option.default
        end
end
each(&block) click to toggle source
# File lib/samovar/options.rb, line 56
def each(&block)
        @ordered.each(&block)
end
empty?() click to toggle source
# File lib/samovar/options.rb, line 60
def empty?
        @ordered.empty?
end
freeze() click to toggle source
Calls superclass method
# File lib/samovar/options.rb, line 44
def freeze
        return self if frozen?
        
        @ordered.freeze
        @keyed.freeze
        @defaults.freeze
        
        @ordered.each(&:freeze)
        
        super
end
initialize_dup(source) click to toggle source
Calls superclass method
# File lib/samovar/options.rb, line 30
def initialize_dup(source)
        super
        
        @ordered = @ordered.dup
        @keyed = @keyed.dup
        @defaults = @defaults.dup
end
merge!(options) click to toggle source
# File lib/samovar/options.rb, line 68
def merge!(options)
        options.each do |option|
                self << option
        end
end
option(*arguments, **options, &block) click to toggle source
# File lib/samovar/options.rb, line 64
def option(*arguments, **options, &block)
        self << Option.new(*arguments, **options, &block)
end
parse(input, parent = nil, default = nil) click to toggle source
# File lib/samovar/options.rb, line 89
def parse(input, parent = nil, default = nil)
        values = (default || @defaults).dup
        
        while option = @keyed[input.first]
                prefix = input.first
                result = option.parse(input)
                if result != nil
                        values[option.key] = result
                end
        end
        
        return values
end
to_s() click to toggle source
# File lib/samovar/options.rb, line 103
def to_s
        @ordered.collect(&:to_s).join(' ')
end
usage(rows) click to toggle source
# File lib/samovar/options.rb, line 107
def usage(rows)
        @ordered.each do |option|
                rows << option
        end
end