class Samovar::Flags
Represents a collection of flag alternatives for an option.
Flags parse text like ‘-f/–flag <value>` into individual flag parsers.
Public Class Methods
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/flags.rb, line 14 def initialize(text) @text = text @ordered = text.split(/\s+\|\s+/).map{|part| Flag.parse(part)} end
Initialize a new flags parser.
@parameter text [String] The flags specification string (e.g., ‘-f/–flag <value>`).
Public Instance Methods
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/flags.rb, line 37 def boolean? @ordered.count == 1 and @ordered.first.boolean? end
Whether this flag should have a true/false value if not specified otherwise.
@returns [Boolean] True if this is a boolean flag.
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/flags.rb, line 44 def count return @ordered.count end
The number of flag alternatives.
@returns [Integer] The count of flags.
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/flags.rb, line 23 def each(&block) @ordered.each(&block) end
Iterate over each flag.
@yields {|flag| …} Each flag in the collection.
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/flags.rb, line 30 def first @ordered.first end
Get the first flag.
@returns [Flag] The first flag.
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/flags.rb, line 59 def parse(input) @ordered.each do |flag| result = flag.parse(input) if result != nil return result end end return nil end
Parse a flag from the input.
@parameter input [Array(String)] The command-line arguments. @returns [Object | Nil] The parsed value, or nil if no match.
Source
# File vendor/bundle/ruby/3.4.0/gems/samovar-2.4.1/lib/samovar/flags.rb, line 51 def to_s "[#{@ordered.join(' | ')}]" end
Generate a string representation for usage output.
@returns [String] The usage string.