class Samovar::Option
Represents a single command-line option.
An option is a flag-based argument that can have various forms (short, long, with or without values).
Attributes
An optional block to transform the parsed value.
@attribute [Proc | Nil]
The default value if the option is not provided.
@attribute [Object]
A description of the option for help output.
@attribute [String]
The flags for this option.
@attribute [Flags]
The key to use for storing the value.
@attribute [Symbol]
Whether the option is required.
@attribute [Boolean]
The type to coerce the value to.
@attribute [Class | Proc | Nil]
A fixed value to use regardless of user input.
@attribute [Object | Nil]
Public Class Methods
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/option.rb, line 24 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
Initialize a new option.
@parameter flags [String] The flags specification (e.g., ‘-f/–flag <value>`). @parameter description [String] A description of the option for help output. @parameter key [Symbol | Nil] The key to use for storing the value (defaults to derived from flag). @parameter default [Object] The default value if the option is not provided. @parameter value [Object | Nil] A fixed value to use regardless of user input. @parameter type [Class | Proc | Nil] The type to coerce the value to. @parameter required [Boolean] Whether the option is required. @yields {|value| …} An optional block to transform the parsed value.
Public Instance Methods
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/option.rb, line 107 def coerce(result) if @type result = coerce_type(result) end if @block result = @block.call(result) end return result end
Coerce and transform the result.
@parameter result [Object] The value to coerce and transform. @returns [Object] The coerced and transformed value.
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/option.rb, line 89 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
Coerce the result to the specified type.
@parameter result [Object] The value to coerce. @returns [Object] The coerced value.
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/option.rb, line 125 def parse(input, parent = nil, default = nil) result = @flags.parse(input) if result != nil @value.nil? ? coerce(result) : @value end end
Parse this option from the input.
@parameter input [Array(String)] The command-line arguments. @parameter parent [Command | Nil] The parent command (unused, kept for compatibility). @parameter default [Object | Nil] An override for the default value (unused, kept for compatibility). @returns [Object | Nil] The parsed value.
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/option.rb, line 143 def to_a if @default [@flags, @description, "(default: #{@default})"] elsif @required [@flags, @description, "(required)"] else [@flags, @description] end end
Generate an array representation for usage output.
@returns [Array] The usage array.
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/option.rb, line 136 def to_s @flags end
Generate a string representation for usage output.
@returns [String] The usage string.