class Samovar::Option
Attributes
Public Class Methods
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.3.0/lib/samovar/option.rb, line 10 def initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block) @flags = Flags.new(flags) @description = description if key @key = key else @key = @flags.first.key end @default = default # If the value is given, it overrides the user specified input. @value = value @value ||= true if @flags.boolean? @type = type @required = required @block = block end
Public Instance Methods
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.3.0/lib/samovar/option.rb, line 56 def coerce(result) if @type result = coerce_type(result) end if @block result = @block.call(result) end return result end
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.3.0/lib/samovar/option.rb, line 42 def coerce_type(result) if @type == Integer Integer(result) elsif @type == Float Float(result) elsif @type == Symbol result.to_sym elsif @type.respond_to? :call @type.call(result) elsif @type.respond_to? :new @type.new(result) end end
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.3.0/lib/samovar/option.rb, line 68 def parse(input, parent = nil, default = nil) result = @flags.parse(input) if result != nil @value.nil? ? coerce(result) : @value elsif default ||= @default return default elsif @required raise MissingValueError.new(parent, self) end end
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.3.0/lib/samovar/option.rb, line 83 def to_a if @default [@flags, @description, "(default: #{@default})"] elsif @required [@flags, @description, "(required)"] else [@flags, @description] end end
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.3.0/lib/samovar/option.rb, line 79 def to_s @flags end