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/4.0.0/gems/samovar-2.5.1/lib/samovar/flags.rb, line 16 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/4.0.0/gems/samovar-2.5.1/lib/samovar/flags.rb, line 54 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/4.0.0/gems/samovar-2.5.1/lib/samovar/flags.rb, line 40 def completions @ordered.flat_map(&:completions) end
The possible flag prefixes for completion.
@returns [Array(String)] The flag prefixes and alternatives.
Source
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/lib/samovar/flags.rb, line 61 def count return @ordered.count end
The number of flag alternatives.
@returns [Integer] The count of flags.
Source
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/lib/samovar/flags.rb, line 25 def each(&block) @ordered.each(&block) end
Iterate over each flag.
@yields {|flag| β¦} Each flag in the collection.
Source
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/lib/samovar/flags.rb, line 47 def first @ordered.first end
Get the first flag.
@returns [Flag] The first flag.
Source
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/lib/samovar/flags.rb, line 33 def flag_for(token) @ordered.find{|flag| flag.prefix?(token)} end
Find the flag that matches the given token.
@parameter token [String] The token to match. @returns [Flag | Nil] The matching flag.
Source
# File vendor/bundle/ruby/4.0.0/gems/samovar-2.5.1/lib/samovar/flags.rb, line 76 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/4.0.0/gems/samovar-2.5.1/lib/samovar/flags.rb, line 68 def to_s "[#{@ordered.join(' | ')}]" end
Generate a string representation for usage output.
@returns [String] The usage string.